Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Better call Google Pay

Better call Google Pay

Александр Смирнов, PapaJobs – MOSDROID #16 Sulfur

Детально погрузимся в то, как устроен Google Pay под капотом. Узнаем, как просто встроить возможность оплаты в ваше приложение и когда его можно (и главное, нужно) использовать.

MOSDROID

March 30, 2019
Tweet

More Decks by MOSDROID

Other Decks in Programming

Transcript

  1. USERS READY Billions of Users
 across Google Hundreds of millions

    
 of forms of payment saved
 to their Accounts Users ready
 to pay everywhere
  2. DESIGN • Display Google Pay early and prominently as payment

    method 
 • Display all relevant purchase information including final price 
 and items/services purchased, before confirming an order
 • If you display payment information on confirmation screens
 or emails, you may display the payment card description 
 returned by Google Pay API
 • Use only the official button styles and logo assets in your app
  3. FAST DIVE 1. Add dependencies + Configure
 2.Initiate PaymentsClient
 3.isReadyToPay()

    
 4.loadPaymentData()
 5.Handle the PaymentData response object
  4. 1. ADD DEPENDENCIES + CONFIGURE dependencies { implementation 'com.google.android.gms:play-services-wallet:16.0.0' implementation

    'com.android.support:appcompat-v7:24.1.1' } <meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true" />
  5. 2. Create PaymentsClient Instance PaymentsClient mPaymentsClient = Wallet.getPaymentsClient( this, new

    Wallet.WalletOptions.Builder() .setEnvironment(WalletConstants.ENVIRONMENT_TEST) .build());
  6. 3. Call isReadyToPay() public static JSONObject getIsReadyToPayRequest() { JSONObject isReadyToPayRequest

    = getBaseRequest(); isReadyToPayRequest.put("allowedPaymentMethods", new JSONArray().put(getBaseCardPaymentMethod())); return isReadyToPayRequest; }
  7. 3. Call isReadyToPay() { "apiVersion": 2, "apiVersionMinor": 0, "allowedPaymentMethods": [

    { "type": "CARD", "parameters": { "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"], "allowedCardNetworks": ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"] } } ] }
  8. 3. Call isReadyToPay() IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(getIsReadyToPayRequest().toString()); Task<Boolean> task =

    mPaymentsClient.isReadyToPay(request); task.addOnCompleteListener( new OnCompleteListener<Boolean>() { @Override public void onComplete(@NonNull Task<Boolean> task) { try { boolean result = task.getResult(ApiException.class); if (result) { // show Google Pay as a payment option } } catch (ApiException e) { } } });
  9. 4. Create PaymentDataRequest object public static JSONObject getPaymentDataRequest() { JSONObject

    paymentDataRequest = getBaseRequest(); paymentDataRequest.put( "allowedPaymentMethods", new JSONArray() .put(getCardPaymentMethod())); paymentDataRequest.put("transactionInfo", getTransactionInfo()); paymentDataRequest.put("merchantInfo", getMerchantInfo()); return paymentDataRequest; }
  10. 4. Create PaymentDataRequest object private static JSONObject getTransactionInfo() { JSONObject

    transactionInfo = new JSONObject(); transactionInfo.put("totalPrice", "12.34"); transactionInfo.put("totalPriceStatus", "FINAL"); transactionInfo.put("currencyCode", "USD"); return transactionInfo; }
  11. 4. Create PaymentDataRequest object private static JSONObject getMerchantInfo() { return

    new JSONObject() .put("merchantName", "Example Merchant"); }
  12. 4. Create PaymentDataRequest object "apiVersion": 2, "apiVersionMinor": 0, "merchantInfo": {"merchantName":

    "Example Merchant"}, "allowedPaymentMethods": [ { "type": "CARD", "parameters": { "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"], "allowedCardNetworks": ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"] }, "tokenizationSpecification": { "type": "PAYMENT_GATEWAY", "parameters": { "gateway": "example", "gatewayMerchantId": "exampleGatewayMerchantId" } } } ], "transactionInfo": { "totalPriceStatus": "FINAL", "totalPrice": "12.34", "currencyCode": “USD”}
  13. 4. REGISTER EVENT HANDLER findViewById(R.id.googlepay) .setOnClickListener( new View.OnClickListener() { @Override

    public void onClick(View v) { PaymentDataRequest request = PaymentDataRequest.fromJson(paymentDataRequest.toString()); if (request != null) { AutoResolveHelper.resolveTask( mPaymentsClient.loadPaymentData(request), this, // LOAD_PAYMENT_DATA_REQUEST_CODE is a constant integer value you define LOAD_PAYMENT_DATA_REQUEST_CODE); } } });
  14. 5. HANDLE THE RESPONSE OBJECT protected void onActivityResult(int requestCode, int

    resultCode, Intent data) { switch (requestCode) { // value passed in AutoResolveHelper case LOAD_PAYMENT_DATA_REQUEST_CODE: switch (resultCode) { case Activity.RESULT_OK: PaymentData paymentData = PaymentData.getFromIntent(data); String json = paymentData.toJson(); // if using gateway tokenization, pass this token without modification String paymentMethodData = new JSONObject(json).getJSONObject(“paymentMethodData”); String paymentToken = paymentMethodData.getJSONObject("tokenizationData").getString("token"); break; case Activity.RESULT_CANCELED: break; case AutoResolveHelper.RESULT_ERROR: Status status = AutoResolveHelper.getStatusFromIntent(data); // Log the status for debugging. // Generally, there is no need to show an error to the user. // The Google Pay payment sheet will present any account errors. break; } break; } }
  15. TEST & DEPLOY •Testing ◦ Test environment does not return

    live chargeable tokens in the PaymentData response ◦ The environment value can be one of the following:
 WalletConstants.ENVIRONMENT_TEST
 WalletConstants.ENVIRONMENT_PRODUCTION ◦ Review branding requirements
 •Deploying to your production ◦ Get access to the production environment – https://services.google.com/fb/forms/googlepaymentAPI/ ◦ Sign your app with release keys ◦ Configure your app for production
  16. REFERENCES Google Pay API Android Sample App
 https://github.com/google-pay/paymentsapi-quickstart
 
 Google

    Pay API Developer Site – https://g.co/pay/developers 
 Demo – https://g.co/pay/demo 
 One more reference
 https://g.co/pay/processors https://g.co/pay/autofill https://g.co/pay/loyalty https://g.co/pay/passes