Search code examples
c#xamarin.androidgoogle-pay

Error using Google wallet in Xamarin Android


I'm trying to use Google Pay in my Xamarin app. First I added the GooglePlayServices package from nuget then I followed the documentation from here

here is my JSON

{
     "apiVersion": 2,
     "apiVersionMinor": 0,
     "merchantInfo": { "merchantName": "testName" },
      "allowedPaymentMethods": [
    {
       "type": "CARD",
       "parameters": {
       "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
       "allowedCardNetworks": ["AMEX", "DISCOVER", "MASTERCARD", "VISA"]
      }
    },
    {
      "type": "PAYMENT_GATEWAY",
     "parameters": { "gateway": "firstdata", "gatewayMerchantId": "12365" }
     }
    ],
      "transactionInfo": {
      "totalPriceStatus": "FINAL",
      "totalPrice": "4.10",
      "currencyCode": "USD",
      "checkoutOption": "COMPLETE_IMMEDIATE_PURCHASE"
  }
}

code:

    paymentsClient = WalletClass.GetPaymentsClient(
        Xamarin.Essentials.Platform.CurrentActivity,
        new WalletClass.WalletOptions.Builder()
           .SetEnvironment(WalletConstants.EnvironmentTest)
           .Build());
     var request =  PaymentDataRequest.FromJson(json);
     AutoResolveHelper.ResolveTask(paymentsClient.LoadPaymentData(request), 
                            Xamarin.Essentials.Platform.CurrentActivity, 999);

but I get an error Code 10: Developer Error

and if I do it like this

     var result = await paymentsClient.LoadPaymentDataAsync(request);

I get the following error 6: BuyFlow UI needs to be shown.


Solution

  • after rereviewing the documentation I realized that my json object was wrong instead of

    {
       "type": "CARD",
       "parameters": {
       "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
       "allowedCardNetworks": ["AMEX", "DISCOVER", "MASTERCARD", "VISA"]
      }
    },
    {
      "type": "PAYMENT_GATEWAY",
     "parameters": { "gateway": "firstdata", "gatewayMerchantId": "12365" }
     }
    

    it needs to look like this: as tokenizationSpecification part of the payment method object

     {
       "type": "CARD",
          "parameters": {
              "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
               "allowedCardNetworks": ["AMEX", "DISCOVER", "MASTERCARD", "VISA"]
             },  
        "tokenizationSpecification": {  
               "type": "PAYMENT_GATEWAY",
                "parameters": { "gateway": "firstdata", "gatewayMerchantId": "12365" }
     }
    

    what I'm wondering is why didnt it throw an error when I called PaymentDataRequest.FromJson(json)