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

The following examples show how to use com.google.android.gms.wallet.LineItem. 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: WalletUtil.java    From androidpay-quickstart with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param lineItems List of {@link com.google.android.gms.wallet.LineItem} used for calculating
 *                  the cart total.
 * @return cart total.
 */
private static String calculateCartTotal(List<LineItem> lineItems) {
    BigDecimal cartTotal = BigDecimal.ZERO;

    // Calculate the total price by adding up each of the line items
    for (LineItem lineItem: lineItems) {
        BigDecimal lineItemTotal = lineItem.getTotalPrice() == null ?
                new BigDecimal(lineItem.getUnitPrice())
                        .multiply(new BigDecimal(lineItem.getQuantity())) :
                new BigDecimal(lineItem.getTotalPrice());

        cartTotal = cartTotal.add(lineItemTotal);
    }

    return cartTotal.setScale(2, RoundingMode.HALF_EVEN).toString();
}
 
Example #2
Source File: WalletUtil.java    From androidpay-quickstart with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param itemInfo {@link com.google.android.gms.samples.wallet.ItemInfo} to use for creating
 *                 the {@link com.google.android.gms.wallet.FullWalletRequest}
 * @param googleTransactionId
 * @return {@link FullWalletRequest} instance
 */
public static FullWalletRequest createFullWalletRequest(ItemInfo itemInfo,
        String googleTransactionId) {

    List<LineItem> lineItems = buildLineItems(itemInfo, false);

    String cartTotal = calculateCartTotal(lineItems);

    // [START full_wallet_request]
    FullWalletRequest request = FullWalletRequest.newBuilder()
            .setGoogleTransactionId(googleTransactionId)
            .setCart(Cart.newBuilder()
                    .setCurrencyCode(Constants.CURRENCY_CODE_USD)
                    .setTotalPrice(cartTotal)
                    .setLineItems(lineItems)
                    .build())
            .build();
    // [END full_wallet_request]

    return request;
}
 
Example #3
Source File: MainActivity.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
private MaskedWalletRequest generateMaskedWalletRequest() {
  MaskedWalletRequest maskedWalletRequest =
      MaskedWalletRequest.newBuilder()
          .setMerchantName("Google I/O Codelab")
          .setPhoneNumberRequired(true)
          .setShippingAddressRequired(true)
          .setCurrencyCode("USD")
          .setShouldRetrieveWalletObjects(true)
          .setEstimatedTotalPrice("10.00")
          .setCart(Cart.newBuilder()
              .setCurrencyCode("USD")
              .setTotalPrice("10.00")
              .addLineItem(LineItem.newBuilder()
                  .setCurrencyCode("USD")
                  .setDescription("Google I/O Sticker")
                  .setQuantity("1")
                  .setUnitPrice("10.00")
                  .setTotalPrice("10.00")
                  .build())
              .build())
          .build();

  return maskedWalletRequest;
}
 
Example #4
Source File: MainActivity.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
private FullWalletRequest generateFullWalletRequest(String googleTransactionId) {
  FullWalletRequest fullWalletRequest = FullWalletRequest.newBuilder()
      .setGoogleTransactionId(googleTransactionId)
      .setCart(Cart.newBuilder()
          .setCurrencyCode("USD")
          .setTotalPrice("10.10")
          .addLineItem(LineItem.newBuilder()
              .setCurrencyCode("USD")
              .setDescription("Google I/O Sticker")
              .setQuantity("1")
              .setUnitPrice("10.00")
              .setTotalPrice("10.00")
              .build())
          .addLineItem(LineItem.newBuilder()
              .setCurrencyCode("USD")
              .setDescription("Tax")
              .setRole(LineItem.Role.TAX)
              .setTotalPrice(".10")
              .build())
          .build())
      .build();
  return fullWalletRequest;
}
 
Example #5
Source File: WalletUtil.java    From androidpay-quickstart with Apache License 2.0 5 votes vote down vote up
private static MaskedWalletRequest createMaskedWalletRequest(ItemInfo itemInfo,
        PaymentMethodTokenizationParameters parameters) {
    // Build a List of all line items
    List<LineItem> lineItems = buildLineItems(itemInfo, true);

    // Calculate the cart total by iterating over the line items.
    String cartTotal = calculateCartTotal(lineItems);

    // [START masked_wallet_request]
    MaskedWalletRequest request = MaskedWalletRequest.newBuilder()
            .setMerchantName(Constants.MERCHANT_NAME)
            .setPhoneNumberRequired(true)
            .setShippingAddressRequired(true)
            .setCurrencyCode(Constants.CURRENCY_CODE_USD)
            .setEstimatedTotalPrice(cartTotal)
                    // Create a Cart with the current line items. Provide all the information
                    // available up to this point with estimates for shipping and tax included.
            .setCart(Cart.newBuilder()
                    .setCurrencyCode(Constants.CURRENCY_CODE_USD)
                    .setTotalPrice(cartTotal)
                    .setLineItems(lineItems)
                    .build())
            .setPaymentMethodTokenizationParameters(parameters)
            .build();

    return request;
    // [END masked_wallet_request]
}
 
Example #6
Source File: WalletUtil.java    From androidpay-quickstart with Apache License 2.0 5 votes vote down vote up
/**
 * Build a list of line items based on the {@link ItemInfo} and a boolean that indicates
 * whether to use estimated values of tax and shipping for setting up the
 * {@link MaskedWalletRequest} or actual values in the case of a {@link FullWalletRequest}
 *
 * @param itemInfo {@link com.google.android.gms.samples.wallet.ItemInfo} used for building the
 *                 {@link com.google.android.gms.wallet.LineItem} list.
 * @param isEstimate {@code boolean} that indicates whether to use estimated values for
 *                   shipping and tax values.
 * @return list of line items
 */
private static List<LineItem> buildLineItems(ItemInfo itemInfo, boolean isEstimate) {
    List<LineItem> list = new ArrayList<LineItem>();
    String itemPrice = toDollars(itemInfo.priceMicros);

    list.add(LineItem.newBuilder()
            .setCurrencyCode(Constants.CURRENCY_CODE_USD)
            .setDescription(itemInfo.name)
            .setQuantity("1")
            .setUnitPrice(itemPrice)
            .setTotalPrice(itemPrice)
            .build());

    String shippingPrice = toDollars(
            isEstimate ? itemInfo.estimatedShippingPriceMicros : itemInfo.shippingPriceMicros);

    list.add(LineItem.newBuilder()
            .setCurrencyCode(Constants.CURRENCY_CODE_USD)
            .setDescription(Constants.DESCRIPTION_LINE_ITEM_SHIPPING)
            .setRole(LineItem.Role.SHIPPING)
            .setTotalPrice(shippingPrice)
            .build());

    String tax = toDollars(
            isEstimate ? itemInfo.estimatedTaxMicros : itemInfo.taxMicros);

    list.add(LineItem.newBuilder()
            .setCurrencyCode(Constants.CURRENCY_CODE_USD)
            .setDescription(Constants.DESCRIPTION_LINE_ITEM_TAX)
            .setRole(LineItem.Role.TAX)
            .setTotalPrice(tax)
            .build());

    return list;
}
 
Example #7
Source File: PaymentFormActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onActivityResultFragment(int requestCode, int resultCode, Intent data) {
    if (requestCode == LOAD_MASKED_WALLET_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            showEditDoneProgress(true, true);
            setDonePressed(true);

            MaskedWallet maskedWallet = data.getParcelableExtra(WalletConstants.EXTRA_MASKED_WALLET);

            Cart.Builder cardBuilder = Cart.newBuilder()
                    .setCurrencyCode(paymentForm.invoice.currency)
                    .setTotalPrice(totalPriceDecimal);

            ArrayList<TLRPC.TL_labeledPrice> arrayList = new ArrayList<>(paymentForm.invoice.prices);
            if (shippingOption != null) {
                arrayList.addAll(shippingOption.prices);
            }
            for (int a = 0; a < arrayList.size(); a++) {
                TLRPC.TL_labeledPrice price = arrayList.get(a);
                String amount = LocaleController.getInstance().formatCurrencyDecimalString(price.amount, paymentForm.invoice.currency, false);
                cardBuilder.addLineItem(LineItem.newBuilder()
                        .setCurrencyCode(paymentForm.invoice.currency)
                        .setQuantity("1")
                        .setDescription(price.label)
                        .setTotalPrice(amount)
                        .setUnitPrice(amount).build());
            }
            FullWalletRequest fullWalletRequest = FullWalletRequest.newBuilder()
                    .setCart(cardBuilder.build())
                    .setGoogleTransactionId(maskedWallet.getGoogleTransactionId())
                    .build();
            Wallet.Payments.loadFullWallet(googleApiClient, fullWalletRequest, LOAD_FULL_WALLET_REQUEST_CODE);
        } else {
            showEditDoneProgress(true, false);
            setDonePressed(false);
        }
    } else if (requestCode == LOAD_FULL_WALLET_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            FullWallet fullWallet = data.getParcelableExtra(WalletConstants.EXTRA_FULL_WALLET);
            String tokenJSON = fullWallet.getPaymentMethodToken().getToken();
            try {
                if (androidPayPublicKey != null) {
                    androidPayCredentials = new TLRPC.TL_inputPaymentCredentialsAndroidPay();
                    androidPayCredentials.payment_token = new TLRPC.TL_dataJSON();
                    androidPayCredentials.payment_token.data = tokenJSON;
                    androidPayCredentials.google_transaction_id = fullWallet.getGoogleTransactionId();
                    String[] descriptions = fullWallet.getPaymentDescriptions();
                    if (descriptions.length > 0) {
                        cardName = descriptions[0];
                    } else {
                        cardName = "Android Pay";
                    }
                } else {
                    Token token = TokenParser.parseToken(tokenJSON);
                    paymentJson = String.format(Locale.US, "{\"type\":\"%1$s\", \"id\":\"%2$s\"}", token.getType(), token.getId());
                    Card card = token.getCard();
                    cardName = card.getType() + " *" + card.getLast4();
                }
                goToNextStep();
                showEditDoneProgress(true, false);
                setDonePressed(false);
            } catch (JSONException ignore) {
                showEditDoneProgress(true, false);
                setDonePressed(false);
            }
        } else {
            showEditDoneProgress(true, false);
            setDonePressed(false);
        }
    }
}
 
Example #8
Source File: PaymentFormActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onActivityResultFragment(int requestCode, int resultCode, Intent data) {
    if (requestCode == LOAD_MASKED_WALLET_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            showEditDoneProgress(true, true);
            setDonePressed(true);

            MaskedWallet maskedWallet = data.getParcelableExtra(WalletConstants.EXTRA_MASKED_WALLET);

            Cart.Builder cardBuilder = Cart.newBuilder()
                    .setCurrencyCode(paymentForm.invoice.currency)
                    .setTotalPrice(totalPriceDecimal);

            ArrayList<TLRPC.TL_labeledPrice> arrayList = new ArrayList<>(paymentForm.invoice.prices);
            if (shippingOption != null) {
                arrayList.addAll(shippingOption.prices);
            }
            for (int a = 0; a < arrayList.size(); a++) {
                TLRPC.TL_labeledPrice price = arrayList.get(a);
                String amount = LocaleController.getInstance().formatCurrencyDecimalString(price.amount, paymentForm.invoice.currency, false);
                cardBuilder.addLineItem(LineItem.newBuilder()
                        .setCurrencyCode(paymentForm.invoice.currency)
                        .setQuantity("1")
                        .setDescription(price.label)
                        .setTotalPrice(amount)
                        .setUnitPrice(amount).build());
            }
            FullWalletRequest fullWalletRequest = FullWalletRequest.newBuilder()
                    .setCart(cardBuilder.build())
                    .setGoogleTransactionId(maskedWallet.getGoogleTransactionId())
                    .build();
            Wallet.Payments.loadFullWallet(googleApiClient, fullWalletRequest, LOAD_FULL_WALLET_REQUEST_CODE);
        } else {
            showEditDoneProgress(true, false);
            setDonePressed(false);
        }
    } else if (requestCode == LOAD_FULL_WALLET_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            FullWallet fullWallet = data.getParcelableExtra(WalletConstants.EXTRA_FULL_WALLET);
            String tokenJSON = fullWallet.getPaymentMethodToken().getToken();
            try {
                if (androidPayPublicKey != null) {
                    androidPayCredentials = new TLRPC.TL_inputPaymentCredentialsAndroidPay();
                    androidPayCredentials.payment_token = new TLRPC.TL_dataJSON();
                    androidPayCredentials.payment_token.data = tokenJSON;
                    androidPayCredentials.google_transaction_id = fullWallet.getGoogleTransactionId();
                    String[] descriptions = fullWallet.getPaymentDescriptions();
                    if (descriptions.length > 0) {
                        cardName = descriptions[0];
                    } else {
                        cardName = "Android Pay";
                    }
                } else {
                    Token token = TokenParser.parseToken(tokenJSON);
                    paymentJson = String.format(Locale.US, "{\"type\":\"%1$s\", \"id\":\"%2$s\"}", token.getType(), token.getId());
                    Card card = token.getCard();
                    cardName = card.getType() + " *" + card.getLast4();
                }
                goToNextStep();
                showEditDoneProgress(true, false);
                setDonePressed(false);
            } catch (JSONException ignore) {
                showEditDoneProgress(true, false);
                setDonePressed(false);
            }
        } else {
            showEditDoneProgress(true, false);
            setDonePressed(false);
        }
    }
}