I would like to create a payment method in my Android app, like the one in Google Photos: I want to ask the shipping address and update dynamically the shipping cost according to the country.
I follow the Google Pay documentation but it doesn't give any explanation of how to set the callbacks like, for example, is explained in the Google Pay web documentation. I also tried to look for some tutorials but seems that no one tried to do what I'm trying to do.
For example I would like to show an error like this picture
When I try to create the JSON to add the callback, Google pay crashes without giving any log, neither with adb -d logcat -s WalletMerchantError
JSONObject paymentDataRequestJSON = new JSONObject()
.put("apiVersion", 2)
.put("apiVersionMinor", 0)
.put("allowedPaymentMethods", new JSONArray()
.put(cardPaymentMethod))
.put("transactionInfo", transactionInfo)
//.put("merchantInfo", merchantInfo) // TODO
.put("callbackIntents", new JSONArray()
.put("SHIPPING_ADDRESS")
.put("SHIPPING_OPTION"))
.put("shippingAddressRequired", true);
The shipping info is now available on Google Pay. You can extract the info by referencing the code below.
enter code here
JSONObject paymentMethodData;
JSONObject shippingAddress;
try {
paymentMethodData = new JSONObject(paymentInformation).getJSONObject("paymentMethodData");
shippingAddress = new JSONObject(paymentInformation).getJSONObject("shippingAddress");
shippingname = shippingAddress.getString("name");
streetaddress1 = shippingAddress.getString("address1");
streetaddress2 = shippingAddress.getString("address2");
city = shippingAddress.getString("locality");
zip5 = shippingAddress.getString("postalCode");
state = shippingAddress.getString("administrativeArea");
In your paymentsutil.java file make sure shippingAddress requirements is set to true as seen below.
enter code here /* An optional shipping address requirement is a top-level property of the PaymentDataRequest
JSON object. */
paymentDataRequest.put("shippingAddressRequired", true);
You can reference the links below for Gpay sample code, but you will have to add the code to extract the shipping info. If you want to see the this data or other data in the payment information object, you can print it out using 'Log'
enter code here Log.i("PaymentInformation", new JSONObject(paymentInformation).toString());
To view the output, you can run your app and then click 'Run' at the bottom of the screen.
https://github.com/google-pay/android-quickstart https://developers.google.com/pay/api/android/guides/tutorial