Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I setup the shipping callbacks with Google Pay

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);
like image 411
luhahaha Avatar asked Nov 18 '25 14:11

luhahaha


2 Answers

Unfortunately, this isn't currently available for Android. I know support for this is planned, however, I can't say when it will be available.

like image 143
Soc Avatar answered Nov 21 '25 04:11

Soc


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

like image 24
Joshua Mckinsey Avatar answered Nov 21 '25 04:11

Joshua Mckinsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!