Google Pay - Mastercard-Gateway/gateway-android-sdk GitHub Wiki
Google Pay
To enable Google Pay support within your app, refer to the Google Pay Documentation. These instructions should guide you in how to:
- Set up the Google Pay API
- Request payment information from the Google wallet
- Incorporate the button into your layout
- Handle the response from the Google Pay API
Since Google Pay integration is optional with the Gateway SDK, you provide the appropriate play services dependency.
implementation 'com.google.android.gms:play-services-wallet:X.X.X'
The Gateway is integrated with Google Pay as a PAYMENT_GATEWAY
type. In order to request encrypted card information from Google Pay, you must use the valid name mpgs
in your request. Be sure to replace YOUR_MERCHANT_ID
with the merchant ID you use on the Gateway.
JSONObject getTokenizationSpecification() throws JSONException {
return new JSONObject()
.put("type", "PAYMENT_GATEWAY")
.put("parameters", new JSONObject()
.put("gateway", "mpgs")
.put("gatewayMerchantId", "YOUR_MERCHANT_ID"));
}
The Gateway SDK offers a Google Pay lifecycle handler for added convenience. You may implement the provided Google Pay callback and use the result handler within your Activity. This alleviates the need to manually handle the Google Pay activity results, and will delegate the important transaction steps to the callback methods.
/**
* This is a shortened version of the CollectCardInfoActivity class in the sample app.
*/
public class YourActivity extends AppCompatActivity {
GooglePayCallback googlePayCallback = new GooglePayCallback();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
// init Google Pay client
paymentsClient = Wallet.getPaymentsClient(this, new Wallet.WalletOptions.Builder()
.setEnvironment(WalletConstants.ENVIRONMENT_TEST)
.build());
// init google pay button
((Button) findViewById(R.id.googlePayBtn)).setOnClickListener(v -> googlePayButtonClicked());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Delegate the Google Pay transaction lifecycle to the Gateway SDK
if (Gateway.handleGooglePayResult(requestCode, resultCode, data, googlePayCallback)) {
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
void googlePayButtonClicked() {
try {
// refer to sample app for full example
PaymentDataRequest request = PaymentDataRequest.fromJson(getPaymentDataRequest().toString());
if (request != null) {
// In order to use the Gateway result handler,
// you must use the Gateway convenience method for launching the Google Pay flow
Gateway.requestGooglePayData(paymentsClient, request, this);
}
} catch (JSONException e) {
Toast.makeText(this, "Could not request payment data", Toast.LENGTH_SHORT).show();
}
}
class GooglePayCallback implements GatewayGooglePayCallback {
@Override
public void onReceivedPaymentData(JSONObject paymentData) {
// ...
}
@Override
public void onGooglePayCancelled() {
// ...
}
@Override
public void onGooglePayError(Status status) {
// ...
}
}
}
Once you have received payment data from Google Pay, you may use it to update a session with the Gateway.
JSONObject paymentMethodData = paymentData.getJSONObject("paymentMethodData");
String token = paymentMethodData.getJSONObject("tokenizationData").getString("token");
GatewayMap request = new GatewayMap()
.set("sourceOfFunds.provided.card.devicePayment.paymentToken", paymentToken);
gateway.updateSession(sessionId, apiVersion, request, new GatewayCallback() {
@Override
public void onSuccess(GatewayMap response) {
// ...
}
@Override
public void onError(Throwable throwable) {
// ...
}
});