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

The following examples show how to use com.google.android.gms.wallet.PaymentMethodTokenizationParameters. 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
/**
 * Creates a MaskedWalletRequest for direct merchant integration (no payment processor)
 *
 * @param itemInfo {@link com.google.android.gms.samples.wallet.ItemInfo} containing details
 *                 of an item.
 * @param publicKey base64-encoded public encryption key. See instructions for more details.
 * @return {@link MaskedWalletRequest} instance
 */
public static MaskedWalletRequest createMaskedWalletRequest(ItemInfo itemInfo,
                                                            String publicKey) {
    // Validate the public key
    if (publicKey == null || publicKey.contains("REPLACE_ME")) {
        throw new IllegalArgumentException("Invalid public key, see README for instructions.");
    }

    // Create direct integration parameters
    // [START direct_integration_parameters]
    PaymentMethodTokenizationParameters parameters =
            PaymentMethodTokenizationParameters.newBuilder()
                .setPaymentMethodTokenizationType(PaymentMethodTokenizationType.NETWORK_TOKEN)
                .addParameter("publicKey", publicKey)
                .build();
    // [END direct_integration_parameters]

    return createMaskedWalletRequest(itemInfo, parameters);
}
 
Example #2
Source File: WalletUtil.java    From androidpay-quickstart with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a MaskedWalletRequest for processing payments with Stripe
 *
 * @param itemInfo {@link com.google.android.gms.samples.wallet.ItemInfo} containing details
 *                 of an item.
 * @param publishableKey Stripe publishable key.
 * @param version Stripe API version.
 * @return {@link MaskedWalletRequest} instance
 */
public static MaskedWalletRequest createStripeMaskedWalletRequest(ItemInfo itemInfo,
                                                                  String publishableKey,
                                                                  String version) {
    // Validate Stripe configuration
    if ("REPLACE_ME".equals(publishableKey) || "REPLACE_ME".equals(version)) {
        throw new IllegalArgumentException("Invalid Stripe configuration, see README for instructions.");
    }

    // [START stripe_integration_parameters]
    PaymentMethodTokenizationParameters parameters = PaymentMethodTokenizationParameters.newBuilder()
            .setPaymentMethodTokenizationType(PaymentMethodTokenizationType.PAYMENT_GATEWAY)
            .addParameter("gateway", "stripe")
            .addParameter("stripe:publishableKey", publishableKey)
            .addParameter("stripe:version", version)
            .build();
    // [END stripe_integration_parameters]

  return createMaskedWalletRequest(itemInfo, parameters);
}
 
Example #3
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 #4
Source File: PaymentFormActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void showAndroidPay() {
    if (getParentActivity() == null || androidPayContainer == null) {
        return;
    }

    WalletFragmentOptions.Builder optionsBuilder = WalletFragmentOptions.newBuilder();
    optionsBuilder.setEnvironment(paymentForm.invoice.test ? WalletConstants.ENVIRONMENT_TEST : WalletConstants.ENVIRONMENT_PRODUCTION);
    optionsBuilder.setMode(WalletFragmentMode.BUY_BUTTON);

    WalletFragmentStyle walletFragmentStyle;
    if (androidPayPublicKey != null) {
        androidPayContainer.setBackgroundColor(androidPayBackgroundColor);
        walletFragmentStyle = new WalletFragmentStyle()
                .setBuyButtonText(WalletFragmentStyle.BuyButtonText.BUY_WITH)
                .setBuyButtonAppearance(androidPayBlackTheme ? WalletFragmentStyle.BuyButtonAppearance.ANDROID_PAY_LIGHT_WITH_BORDER : WalletFragmentStyle.BuyButtonAppearance.ANDROID_PAY_DARK)
                .setBuyButtonWidth(WalletFragmentStyle.Dimension.MATCH_PARENT);
    } else {
        walletFragmentStyle = new WalletFragmentStyle()
                .setBuyButtonText(WalletFragmentStyle.BuyButtonText.LOGO_ONLY)
                .setBuyButtonAppearance(WalletFragmentStyle.BuyButtonAppearance.ANDROID_PAY_LIGHT_WITH_BORDER)
                .setBuyButtonWidth(WalletFragmentStyle.Dimension.WRAP_CONTENT);
    }

    optionsBuilder.setFragmentStyle(walletFragmentStyle);
    WalletFragment walletFragment = WalletFragment.newInstance(optionsBuilder.build());
    FragmentManager fragmentManager = getParentActivity().getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(fragment_container_id, walletFragment);
    fragmentTransaction.commit();

    ArrayList<TLRPC.TL_labeledPrice> arrayList = new ArrayList<>(paymentForm.invoice.prices);
    if (shippingOption != null) {
        arrayList.addAll(shippingOption.prices);
    }
    totalPriceDecimal = getTotalPriceDecimalString(arrayList);

    PaymentMethodTokenizationParameters parameters;
    if (androidPayPublicKey != null) {
        parameters = PaymentMethodTokenizationParameters.newBuilder()
                .setPaymentMethodTokenizationType(PaymentMethodTokenizationType.NETWORK_TOKEN)
                .addParameter("publicKey", androidPayPublicKey)
                .build();
    } else {
        parameters = PaymentMethodTokenizationParameters.newBuilder()
                .setPaymentMethodTokenizationType(PaymentMethodTokenizationType.PAYMENT_GATEWAY)
                .addParameter("gateway", "stripe")
                .addParameter("stripe:publishableKey", stripeApiKey)
                .addParameter("stripe:version", StripeApiHandler.VERSION)
                .build();
    }

    MaskedWalletRequest maskedWalletRequest = MaskedWalletRequest.newBuilder()
            .setPaymentMethodTokenizationParameters(parameters)
            .setEstimatedTotalPrice(totalPriceDecimal)
            .setCurrencyCode(paymentForm.invoice.currency)
            .build();

    WalletFragmentInitParams initParams = WalletFragmentInitParams.newBuilder()
            .setMaskedWalletRequest(maskedWalletRequest)
            .setMaskedWalletRequestCode(LOAD_MASKED_WALLET_REQUEST_CODE)
            .build();

    walletFragment.initialize(initParams);
    androidPayContainer.setVisibility(View.VISIBLE);
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(ObjectAnimator.ofFloat(androidPayContainer, "alpha", 0.0f, 1.0f));
    animatorSet.setInterpolator(new DecelerateInterpolator());
    animatorSet.setDuration(180);
    animatorSet.start();
}
 
Example #5
Source File: PaymentFormActivity.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void showAndroidPay() {
    if (getParentActivity() == null || androidPayContainer == null) {
        return;
    }

    WalletFragmentOptions.Builder optionsBuilder = WalletFragmentOptions.newBuilder();
    optionsBuilder.setEnvironment(paymentForm.invoice.test ? WalletConstants.ENVIRONMENT_TEST : WalletConstants.ENVIRONMENT_PRODUCTION);
    optionsBuilder.setMode(WalletFragmentMode.BUY_BUTTON);

    WalletFragmentStyle walletFragmentStyle;
    if (androidPayPublicKey != null) {
        androidPayContainer.setBackgroundColor(androidPayBackgroundColor);
        walletFragmentStyle = new WalletFragmentStyle()
                .setBuyButtonText(WalletFragmentStyle.BuyButtonText.BUY_WITH)
                .setBuyButtonAppearance(androidPayBlackTheme ? WalletFragmentStyle.BuyButtonAppearance.ANDROID_PAY_LIGHT_WITH_BORDER : WalletFragmentStyle.BuyButtonAppearance.ANDROID_PAY_DARK)
                .setBuyButtonWidth(WalletFragmentStyle.Dimension.MATCH_PARENT);
    } else {
        walletFragmentStyle = new WalletFragmentStyle()
                .setBuyButtonText(WalletFragmentStyle.BuyButtonText.LOGO_ONLY)
                .setBuyButtonAppearance(WalletFragmentStyle.BuyButtonAppearance.ANDROID_PAY_LIGHT_WITH_BORDER)
                .setBuyButtonWidth(WalletFragmentStyle.Dimension.WRAP_CONTENT);
    }

    optionsBuilder.setFragmentStyle(walletFragmentStyle);
    WalletFragment walletFragment = WalletFragment.newInstance(optionsBuilder.build());
    FragmentManager fragmentManager = getParentActivity().getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(fragment_container_id, walletFragment);
    fragmentTransaction.commit();

    ArrayList<TLRPC.TL_labeledPrice> arrayList = new ArrayList<>(paymentForm.invoice.prices);
    if (shippingOption != null) {
        arrayList.addAll(shippingOption.prices);
    }
    totalPriceDecimal = getTotalPriceDecimalString(arrayList);

    PaymentMethodTokenizationParameters parameters;
    if (androidPayPublicKey != null) {
        parameters = PaymentMethodTokenizationParameters.newBuilder()
                .setPaymentMethodTokenizationType(PaymentMethodTokenizationType.NETWORK_TOKEN)
                .addParameter("publicKey", androidPayPublicKey)
                .build();
    } else {
        parameters = PaymentMethodTokenizationParameters.newBuilder()
                .setPaymentMethodTokenizationType(PaymentMethodTokenizationType.PAYMENT_GATEWAY)
                .addParameter("gateway", "stripe")
                .addParameter("stripe:publishableKey", stripeApiKey)
                .addParameter("stripe:version", StripeApiHandler.VERSION)
                .build();
    }

    MaskedWalletRequest maskedWalletRequest = MaskedWalletRequest.newBuilder()
            .setPaymentMethodTokenizationParameters(parameters)
            .setEstimatedTotalPrice(totalPriceDecimal)
            .setCurrencyCode(paymentForm.invoice.currency)
            .build();

    WalletFragmentInitParams initParams = WalletFragmentInitParams.newBuilder()
            .setMaskedWalletRequest(maskedWalletRequest)
            .setMaskedWalletRequestCode(LOAD_MASKED_WALLET_REQUEST_CODE)
            .build();

    walletFragment.initialize(initParams);
    androidPayContainer.setVisibility(View.VISIBLE);
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(ObjectAnimator.ofFloat(androidPayContainer, "alpha", 0.0f, 1.0f));
    animatorSet.setInterpolator(new DecelerateInterpolator());
    animatorSet.setDuration(180);
    animatorSet.start();
}
 
Example #6
Source File: GooglePaymentTest.java    From braintree_android with MIT License 4 votes vote down vote up
@Test(timeout = 5000)
public void getTokenizationParameters_returnsCorrectParametersInCallback() throws Exception {
    String config = mBaseConfiguration.googlePayment(mBaseConfiguration.googlePayment()
            .supportedNetworks(new String[]{"visa", "mastercard", "amex", "discover"}))
            .build();
    final Configuration configuration = Configuration.fromJson(config);

    BraintreeFragment fragment = getFragment(mActivityTestRule.getActivity(), TOKENIZATION_KEY, config);

    GooglePayment.getTokenizationParameters(fragment, new TokenizationParametersListener() {
        @Override
        public void onResult(PaymentMethodTokenizationParameters parameters,
                Collection<Integer> allowedCardNetworks) {
            assertEquals("braintree", parameters.getParameters().getString("gateway"));
            assertEquals(configuration.getMerchantId(),
                    parameters.getParameters().getString("braintree:merchantId"));
            assertEquals(configuration.getGooglePayment().getGoogleAuthorizationFingerprint(),
                    parameters.getParameters().getString("braintree:authorizationFingerprint"));
            assertEquals("v1",
                    parameters.getParameters().getString("braintree:apiVersion"));
            assertEquals(BuildConfig.VERSION_NAME,
                    parameters.getParameters().getString("braintree:sdkVersion"));

            try {
                JSONObject metadata = new JSONObject(parameters.getParameters().getString("braintree:metadata"));
                assertNotNull(metadata);
                assertEquals(BuildConfig.VERSION_NAME, metadata.getString("version"));
                assertNotNull(metadata.getString("sessionId"));
                assertEquals("custom", metadata.getString("integration"));
                assertEquals("android", metadata.get("platform"));
            } catch (JSONException e) {
                fail("Failed to unpack json from tokenization parameters: " + e.getMessage());
            }

            assertEquals(4, allowedCardNetworks.size());
            assertTrue(allowedCardNetworks.contains(CardNetwork.VISA));
            assertTrue(allowedCardNetworks.contains(CardNetwork.MASTERCARD));
            assertTrue(allowedCardNetworks.contains(CardNetwork.AMEX));
            assertTrue(allowedCardNetworks.contains(CardNetwork.DISCOVER));

            mLatch.countDown();
        }
    });

    mLatch.await();
}
 
Example #7
Source File: TokenizationParametersListener.java    From braintree_android with MIT License 2 votes vote down vote up
/**
 * Called when tokenization parameters for Android Pay are available.Useful for existing Google
 * Wallet or Android Pay integrations, or when full control over the
 * {@link com.google.android.gms.wallet.MaskedWalletRequest} and
 * {@link com.google.android.gms.wallet.FullWalletRequest} is required.
 *
 * {@link PaymentMethodTokenizationParameters} should be supplied to the
 * {@link com.google.android.gms.wallet.MaskedWalletRequest} via
 * {@link com.google.android.gms.wallet.MaskedWalletRequest.Builder#setPaymentMethodTokenizationParameters(PaymentMethodTokenizationParameters)}
 * and {@link Collection<Integer>} allowedCardNetworks should be supplied to the
 * {@link com.google.android.gms.wallet.MaskedWalletRequest} via
 * {@link com.google.android.gms.wallet.MaskedWalletRequest.Builder#addAllowedCardNetworks(Collection)}.
 *
 * @param parameters {@link PaymentMethodTokenizationParameters}
 * @param allowedCardNetworks {@link Collection<Integer>} of card networks supported by the current
 *        Braintree merchant account.
 */
void onResult(PaymentMethodTokenizationParameters parameters, Collection<Integer> allowedCardNetworks);