com.google.android.gms.wallet.PaymentDataRequest Java Examples

The following examples show how to use com.google.android.gms.wallet.PaymentDataRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PaymentTransparentActivity.java    From android-quickstart with Apache License 2.0 6 votes vote down vote up
private void showPaymentsSheet() {

    // Fetch the price based on the user selection
    long priceCents = getIntent().getLongExtra(Notifications.OPTION_PRICE_EXTRA, 2500L);

    // TransactionInfo transaction = PaymentsUtil.createTransaction(price);
    Optional<JSONObject> paymentDataRequestJson = PaymentsUtil.getPaymentDataRequest(priceCents);
    if (!paymentDataRequestJson.isPresent()) {
      return;
    }

    PaymentDataRequest request =
        PaymentDataRequest.fromJson(paymentDataRequestJson.get().toString());

    if (request != null) {
      final PaymentsClient paymentsClient = PaymentsUtil.createPaymentsClient(this);
      AutoResolveHelper.resolveTask(
          paymentsClient.loadPaymentData(request),
          this, LOAD_PAYMENT_DATA_REQUEST_CODE);
    }
  }
 
Example #2
Source File: CheckoutActivity.java    From in-app-payments-android-quickstart with Apache License 2.0 5 votes vote down vote up
private void startGooglePayActivity() {
  TransactionInfo transactionInfo = TransactionInfo.newBuilder()
      .setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_FINAL)
      .setTotalPrice("1.00")
      .setCurrencyCode("USD")
      .build();

  PaymentDataRequest paymentDataRequest =
      GooglePay.createPaymentDataRequest(ConfigHelper.GOOGLE_PAY_MERCHANT_ID,
          transactionInfo);

  Task<PaymentData> googlePayActivityTask = paymentsClient.loadPaymentData(paymentDataRequest);

  AutoResolveHelper.resolveTask(googlePayActivityTask, this, LOAD_PAYMENT_DATA_REQUEST_CODE);
}
 
Example #3
Source File: GPay.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Display the Google Pay payment sheet
 */
@ReactMethod
public void show(int environment, ReadableMap requestData, final Promise promise) {

    Activity activity = getCurrentActivity();

    if (activity == null) {
        promise.reject(E_NO_ACTIVITY, "activity is null");
        return;
    }

    this.mRequestPaymentPromise = promise;

    JSONObject paymentDataRequestJson = GPay.getPaymentDataRequest(requestData);
    if (paymentDataRequestJson == null) {
        promise.reject(E_NO_PAYMENT_REQUEST_JSON, "payment data request json is null");
        return;
    }
    PaymentDataRequest request =
            PaymentDataRequest.fromJson(paymentDataRequestJson.toString());
    if (request != null) {
        AutoResolveHelper.resolveTask(
                getPaymentsClient(environment, activity).loadPaymentData(request), activity, LOAD_PAYMENT_DATA_REQUEST_CODE);
    } else {
        promise.reject(E_NO_PAYMENT_REQUEST, "payment data request is null");
    }
}
 
Example #4
Source File: GPay.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Display the Google Pay payment sheet
 */
@ReactMethod
public void show(int environment, ReadableMap requestData, final Promise promise) {

    Activity activity = getCurrentActivity();

    if (activity == null) {
        promise.reject(E_NO_ACTIVITY, "activity is null");
        return;
    }

    this.mRequestPaymentPromise = promise;

    JSONObject paymentDataRequestJson = GPay.getPaymentDataRequest(requestData);
    if (paymentDataRequestJson == null) {
        promise.reject(E_NO_PAYMENT_REQUEST_JSON, "payment data request json is null");
        return;
    }
    PaymentDataRequest request =
            PaymentDataRequest.fromJson(paymentDataRequestJson.toString());
    if (request != null) {
        AutoResolveHelper.resolveTask(
                getPaymentsClient(environment, activity).loadPaymentData(request), activity, LOAD_PAYMENT_DATA_REQUEST_CODE);
    } else {
        promise.reject(E_NO_PAYMENT_REQUEST, "payment data request is null");
    }
}
 
Example #5
Source File: CollectCardInfoActivity.java    From gateway-android-sdk with Apache License 2.0 5 votes vote down vote up
void googlePayButtonClicked() {
    try {
        PaymentDataRequest request = PaymentDataRequest.fromJson(getPaymentDataRequest().toString());
        if (request != null) {
            // use the Gateway convenience handler for launching the Google Pay flow
            Gateway.requestGooglePayData(paymentsClient, request, CollectCardInfoActivity.this);
        }
    } catch (JSONException e) {
        Toast.makeText(this, "Could not request payment data", Toast.LENGTH_SHORT).show();
    }
}
 
Example #6
Source File: CheckoutActivity.java    From android-quickstart with Apache License 2.0 5 votes vote down vote up
public void requestPayment(View view) {

    // Disables the button to prevent multiple clicks.
    googlePayButton.setClickable(false);

    // The price provided to the API should include taxes and shipping.
    // This price is not displayed to the user.
    try {
      double garmentPrice = selectedGarment.getDouble("price");
      long garmentPriceCents = Math.round(garmentPrice * PaymentsUtil.CENTS_IN_A_UNIT.longValue());
      long priceCents = garmentPriceCents + SHIPPING_COST_CENTS;

      Optional<JSONObject> paymentDataRequestJson = PaymentsUtil.getPaymentDataRequest(priceCents);
      if (!paymentDataRequestJson.isPresent()) {
        return;
      }

      PaymentDataRequest request =
          PaymentDataRequest.fromJson(paymentDataRequestJson.get().toString());

      // Since loadPaymentData may show the UI asking the user to select a payment method, we use
      // AutoResolveHelper to wait for the user interacting with it. Once completed,
      // onActivityResult will be called with the result.
      if (request != null) {
        AutoResolveHelper.resolveTask(
            paymentsClient.loadPaymentData(request),
            this, LOAD_PAYMENT_DATA_REQUEST_CODE);
      }

    } catch (JSONException e) {
      throw new RuntimeException("The price cannot be deserialized from the JSON object.");
    }
  }
 
Example #7
Source File: GooglePaymentTest.java    From braintree_android with MIT License 4 votes vote down vote up
@Test
public void requestPayment_startsActivityWithOptionalValues() throws JSONException {
    BraintreeFragment fragment = getSetupFragment();
    String googlePaymentModuleVersion = com.braintreepayments.api.googlepayment.BuildConfig.VERSION_NAME;
    GooglePaymentRequest googlePaymentRequest = new GooglePaymentRequest()
            .allowPrepaidCards(true)
            .billingAddressFormat(1)
            .billingAddressRequired(true)
            .emailRequired(true)
            .phoneNumberRequired(true)
            .shippingAddressRequired(true)
            .shippingAddressRequirements(ShippingAddressRequirements.newBuilder().addAllowedCountryCode("USA").build())
            .transactionInfo(TransactionInfo.newBuilder()
                    .setTotalPrice("1.00")
                    .setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_FINAL)
                    .setCurrencyCode("USD")
                    .build());

    GooglePayment.requestPayment(fragment, googlePaymentRequest);

    ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
    verify(fragment).startActivityForResult(captor.capture(), eq(BraintreeRequestCodes.GOOGLE_PAYMENT));
    Intent intent = captor.getValue();

    assertEquals(GooglePaymentActivity.class.getName(), intent.getComponent().getClassName());
    assertEquals(WalletConstants.ENVIRONMENT_TEST, intent.getIntExtra(EXTRA_ENVIRONMENT, -1));
    PaymentDataRequest paymentDataRequest = intent.getParcelableExtra(EXTRA_PAYMENT_DATA_REQUEST);

    JSONObject paymentDataRequestJson = new JSONObject(paymentDataRequest.toJson());

    assertEquals(2, paymentDataRequestJson.get("apiVersion"));
    assertEquals(0, paymentDataRequestJson.get("apiVersionMinor"));

    assertEquals(true, paymentDataRequestJson.get("emailRequired"));
    assertEquals(true, paymentDataRequestJson.get("shippingAddressRequired"));

    JSONObject transactionInfoJson = paymentDataRequestJson.getJSONObject("transactionInfo");
    assertEquals("FINAL", transactionInfoJson.getString("totalPriceStatus"));
    assertEquals("1.00", transactionInfoJson.getString("totalPrice"));
    assertEquals("USD", transactionInfoJson.getString("currencyCode"));

    JSONArray allowedPaymentMethods = paymentDataRequestJson.getJSONArray("allowedPaymentMethods");
    JSONObject paypal = allowedPaymentMethods.getJSONObject(0);
    assertEquals("PAYPAL", paypal.getString("type"));

    JSONArray purchaseUnits = paypal.getJSONObject("parameters")
            .getJSONObject("purchase_context")
            .getJSONArray("purchase_units");
    assertEquals(1, purchaseUnits.length());

    JSONObject purchaseUnit = purchaseUnits.getJSONObject(0);
    assertEquals("paypal-client-id-for-google-payment", purchaseUnit.getJSONObject("payee")
            .getString("client_id"));
    assertEquals("true", purchaseUnit.getString("recurring_payment"));

    JSONObject paypalTokenizationSpecification = paypal.getJSONObject("tokenizationSpecification");
    assertEquals("PAYMENT_GATEWAY", paypalTokenizationSpecification.getString("type"));

    JSONObject paypalTokenizationSpecificationParams = paypalTokenizationSpecification.getJSONObject("parameters");
    assertEquals("braintree", paypalTokenizationSpecificationParams.getString("gateway"));
    assertEquals("v1", paypalTokenizationSpecificationParams.getString("braintree:apiVersion"));
    assertEquals(googlePaymentModuleVersion, paypalTokenizationSpecificationParams.getString("braintree:sdkVersion"));
    assertEquals("android-pay-merchant-id", paypalTokenizationSpecificationParams.getString("braintree:merchantId"));
    assertEquals("{\"source\":\"client\",\"version\":\"" + googlePaymentModuleVersion + "\",\"platform\":\"android\"}", paypalTokenizationSpecificationParams.getString("braintree:metadata"));
    assertFalse(paypalTokenizationSpecificationParams.has("braintree:clientKey"));
    assertEquals("paypal-client-id-for-google-payment", paypalTokenizationSpecificationParams.getString("braintree:paypalClientId"));

    JSONObject card = allowedPaymentMethods.getJSONObject(1);
    assertEquals("CARD", card.getString("type"));

    JSONObject cardParams = card.getJSONObject("parameters");
    assertTrue(cardParams.getBoolean("billingAddressRequired"));
    assertTrue(cardParams.getBoolean("allowPrepaidCards"));

    assertEquals("PAN_ONLY", cardParams.getJSONArray("allowedAuthMethods").getString(0));
    assertEquals("CRYPTOGRAM_3DS", cardParams.getJSONArray("allowedAuthMethods").getString(1));

    assertEquals("VISA", cardParams.getJSONArray("allowedCardNetworks").getString(0));
    assertEquals("MASTERCARD", cardParams.getJSONArray("allowedCardNetworks").getString(1));
    assertEquals("AMEX", cardParams.getJSONArray("allowedCardNetworks").getString(2));
    assertEquals("DISCOVER", cardParams.getJSONArray("allowedCardNetworks").getString(3));

    JSONObject tokenizationSpecification = card.getJSONObject("tokenizationSpecification");
    assertEquals("PAYMENT_GATEWAY", tokenizationSpecification.getString("type"));

    JSONObject cardTokenizationSpecificationParams = tokenizationSpecification.getJSONObject("parameters");
    assertEquals("braintree", cardTokenizationSpecificationParams.getString("gateway"));
    assertEquals("v1", cardTokenizationSpecificationParams.getString("braintree:apiVersion"));
    assertEquals(googlePaymentModuleVersion, cardTokenizationSpecificationParams.getString("braintree:sdkVersion"));
    assertEquals("android-pay-merchant-id", cardTokenizationSpecificationParams.getString("braintree:merchantId"));
    assertEquals("{\"source\":\"client\",\"version\":\"" + googlePaymentModuleVersion + "\",\"platform\":\"android\"}", cardTokenizationSpecificationParams.getString("braintree:metadata"));
    assertEquals("sandbox_tokenization_key", cardTokenizationSpecificationParams.getString("braintree:clientKey"));
}
 
Example #8
Source File: Gateway.java    From gateway-android-sdk with Apache License 2.0 2 votes vote down vote up
/**
 * A convenience method for initializing the request to get Google Pay card info
 *
 * @param paymentsClient An instance of the PaymentClient
 * @param request A properly formatted PaymentDataRequest
 * @param activity The calling activity
 * @see <a href="https://developers.google.com/pay/api/android/guides/tutorial#paymentsclient">Payments Client</a>
 * @see <a href="https://developers.google.com/pay/api/android/guides/tutorial#paymentdatarequest">Payment Data Request</a>
 */
public static void requestGooglePayData(PaymentsClient paymentsClient, PaymentDataRequest request, Activity activity) {
    AutoResolveHelper.resolveTask(paymentsClient.loadPaymentData(request), activity, REQUEST_GOOGLE_PAY_LOAD_PAYMENT_DATA);
}