Google Pay enables user to pay with card information securely stored by Google without cumbersome process of filling card and payment information on web site.
This example shows how you can process Google Pay payments using our shift4.js
.
To support Google Pay for the widest audience, we advise using the Google Pay API JavaScript library.
The entire process is detailed in the code snippets below and can be organized into the following main sections:
shift4.js
and Google Pay API JavaScript library [1]In steps 4, 5, and 6, code uses details that are utilized by Google's SDK to display payment sheet and generate final token. Those details are configured in following functions:
getAllowedPaymentMethod
configures supported card networks and tokenization details that will allow our API to decode payment token successfully.
createGooglePaymentDataRequest
uses getAllowedPaymentMethod
, but also defines transaction and payment process specific information like its currency, amount and merchant display name.
This page shows a basic integration example of Google Pay and our API. If you want to get better understanding of Google Pay API or want to better understand more complex possibilities, please refer to official Google Pay Web developer tutorial
There are two types of Google Pay tokens: PAN_ONLY
and CRYPTOGRAM_3DS
.
In case of CRYPTOGRAM_3DS
3D Secure data are embedded into the token and there are no additional requirements before making payment with it.
In case of PAN_ONLY
there is an additional requirement to perform 3D Secure before creating a charge. See next section to see how it is achieved using our API
Thanks to shift4.js
and our API you don't have to distinguish between the two token types and the 3D Secure will be triggered only when necessary.
Before using Google Pay payments on your website be sure to read through official guidelines wrote by Google:
@AjaxController
@RequestMapping("/ajax/examples/charge-with-google-pay")
class ExamplesAjaxChargeWithGooglePayController {
@PostMapping("/payment")
Map tokenPayment(@RequestBody GooglePayTokenPaymentExampleRequest exampleRequest) throws IOException {
try (Shift4Gateway shift4Gateway = createShift4Gateway()) {
// create charge using received token ID
ChargeRequest chargeRequest = new ChargeRequest(100, "USD")
.paymentMethod(new PaymentMethodRequest(exampleRequest.token));
Charge charge = shift4Gateway.createCharge(chargeRequest);
// pass clientObjectId of charge to frontend
return singletonMap("clientObjectId", charge.getClientObjectId());
} catch (Shift4Exception e) {
throw new BadRequestException(e.getMessage());
}
}
static class GooglePayTokenPaymentExampleRequest {
String token;
}
}