Java Code Examples for com.google.android.gms.wallet.PaymentDataRequest#fromJson()

The following examples show how to use com.google.android.gms.wallet.PaymentDataRequest#fromJson() . 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: 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 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: 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 5
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.");
    }
  }