Braintree/src/main/java/com/braintreepayments/api/PayPal.java
private static void createPaymentResource(BraintreeFragment fragment, PayPalRequest request,
            boolean isBillingAgreement, HttpResponseCallback callback)
            throws JSONException, ErrorWithResponse, BraintreeException {
        String currencyCode = request.getCurrencyCode();
        if (currencyCode == null) {
            currencyCode = fragment.getConfiguration().getPayPal().getCurrencyIsoCode();
        }

        CheckoutRequest checkoutRequest = getCheckoutRequest(fragment, null);
        JSONObject parameters = new JSONObject()
                .put(RETURN_URL_KEY, checkoutRequest.getSuccessUrl())
                .put(CANCEL_URL_KEY, checkoutRequest.getCancelUrl())
                .put(OFFER_CREDIT_KEY, request.shouldOfferCredit());

        if (fragment.getAuthorization() instanceof ClientToken) {
            parameters.put(AUTHORIZATION_FINGERPRINT_KEY, fragment.getAuthorization().getBearer());
        } else {
            parameters.put(TOKENIZATION_KEY, fragment.getAuthorization().getBearer());
        }

        if (!isBillingAgreement) {
            parameters.put(AMOUNT_KEY, request.getAmount())
                    .put(CURRENCY_ISO_CODE_KEY, currencyCode)
                    .put(INTENT_KEY, request.getIntent());
        } else {
            if (!TextUtils.isEmpty(request.getBillingAgreementDescription())) {
                parameters.put(DESCRIPTION_KEY, request.getBillingAgreementDescription());
            }
        }

        JSONObject experienceProfile = new JSONObject();
        experienceProfile.put(NO_SHIPPING_KEY, !request.isShippingAddressRequired());
        experienceProfile.put(LANDING_PAGE_TYPE_KEY, request.getLandingPageType());
        String displayName = request.getDisplayName();
        if (TextUtils.isEmpty(displayName)) {
            displayName = fragment.getConfiguration().getPayPal().getDisplayName();
        }
        experienceProfile.put(DISPLAY_NAME_KEY, displayName);

        if (request.getLocaleCode() != null) {
            experienceProfile.put(LOCALE_CODE_KEY, request.getLocaleCode());
        }

        if (request.getShippingAddressOverride() != null) {
            experienceProfile.put(ADDRESS_OVERRIDE_KEY, !request.isShippingAddressEditable());

            JSONObject shippingAddressJson;
            if (isBillingAgreement) {
                shippingAddressJson = new JSONObject();
                parameters.put(SHIPPING_ADDRESS_KEY, shippingAddressJson);
            } else {
                shippingAddressJson = parameters;
            }

            PostalAddress shippingAddress = request.getShippingAddressOverride();
            shippingAddressJson.put(PostalAddressParser.LINE_1_KEY, shippingAddress.getStreetAddress());
            shippingAddressJson.put(PostalAddressParser.LINE_2_KEY, shippingAddress.getExtendedAddress());
            shippingAddressJson.put(PostalAddressParser.LOCALITY_KEY, shippingAddress.getLocality());
            shippingAddressJson.put(PostalAddressParser.REGION_KEY, shippingAddress.getRegion());
            shippingAddressJson.put(PostalAddressParser.POSTAL_CODE_UNDERSCORE_KEY, shippingAddress.getPostalCode());
            shippingAddressJson.put(PostalAddressParser.COUNTRY_CODE_UNDERSCORE_KEY, shippingAddress.getCountryCodeAlpha2());
            shippingAddressJson.put(PostalAddressParser.RECIPIENT_NAME_UNDERSCORE_KEY, shippingAddress.getRecipientName());
        } else {
            experienceProfile.put(ADDRESS_OVERRIDE_KEY, false);
        }

        if(request.getMerchantAccountId() != null) {
            parameters.put(MERCHANT_ACCOUNT_ID, request.getMerchantAccountId());
        }

        parameters.put(EXPERIENCE_PROFILE_KEY, experienceProfile);

        String apiUrl = isBillingAgreement ? SETUP_BILLING_AGREEMENT_ENDPOINT : CREATE_SINGLE_PAYMENT_ENDPOINT;
        String versionedPath = "/v1/" + apiUrl;
        fragment.getHttpClient().post(versionedPath, parameters.toString(), callback);
    }
private static void createPaymentResource(BraintreeFragment fragment, PayPalRequest request,
            boolean isBillingAgreement, HttpResponseCallback callback)
            throws JSONException, ErrorWithResponse, BraintreeException {
        String currencyCode = request.getCurrencyCode();
        if (currencyCode == null) {
            currencyCode = fragment.getConfiguration().getPayPal().getCurrencyIsoCode();
        }

        CheckoutRequest checkoutRequest = getCheckoutRequest(fragment, null);
        JSONObject parameters = new JSONObject()
                .put(RETURN_URL_KEY, checkoutRequest.getSuccessUrl())
                .put(CANCEL_URL_KEY, checkoutRequest.getCancelUrl())
                .put(OFFER_CREDIT_KEY, request.shouldOfferCredit());

        if (fragment.getAuthorization() instanceof ClientToken) {
            parameters.put(AUTHORIZATION_FINGERPRINT_KEY, fragment.getAuthorization().getBearer());
        } else {
            parameters.put(TOKENIZATION_KEY, fragment.getAuthorization().getBearer());
        }

        if (!isBillingAgreement) {
            parameters.put(AMOUNT_KEY, request.getAmount())
                    .put(CURRENCY_ISO_CODE_KEY, currencyCode)
                    .put(INTENT_KEY, request.getIntent());
        } else {
            if (!TextUtils.isEmpty(request.getBillingAgreementDescription())) {
                parameters.put(DESCRIPTION_KEY, request.getBillingAgreementDescription());
            }
        }

        JSONObject experienceProfile = new JSONObject();
        experienceProfile.put(NO_SHIPPING_KEY, !request.isShippingAddressRequired());
        experienceProfile.put(LANDING_PAGE_TYPE_KEY, request.getLandingPageType());
        String displayName = request.getDisplayName();
        if (TextUtils.isEmpty(displayName)) {
            displayName = fragment.getConfiguration().getPayPal().getDisplayName();
        }
        experienceProfile.put(DISPLAY_NAME_KEY, displayName);

        if (request.getLocaleCode() != null) {
            experienceProfile.put(LOCALE_CODE_KEY, request.getLocaleCode());
        }

        if (request.getShippingAddressOverride() != null) {
            experienceProfile.put(ADDRESS_OVERRIDE_KEY, !request.isShippingAddressEditable());

            JSONObject shippingAddressJson;
            if (isBillingAgreement) {
                shippingAddressJson = new JSONObject();
                parameters.put(SHIPPING_ADDRESS_KEY, shippingAddressJson);
            } else {
                shippingAddressJson = parameters;
            }

            PostalAddress shippingAddress = request.getShippingAddressOverride();
            shippingAddressJson.put(PostalAddressParser.LINE_1_KEY, shippingAddress.getStreetAddress());
            shippingAddressJson.put(PostalAddressParser.LINE_2_KEY, shippingAddress.getExtendedAddress());
            shippingAddressJson.put(PostalAddressParser.LOCALITY_KEY, shippingAddress.getLocality());
            shippingAddressJson.put(PostalAddressParser.REGION_KEY, shippingAddress.getRegion());
            shippingAddressJson.put(PostalAddressParser.POSTAL_CODE_UNDERSCORE_KEY, shippingAddress.getPostalCode());
            shippingAddressJson.put(PostalAddressParser.COUNTRY_CODE_UNDERSCORE_KEY, shippingAddress.getCountryCodeAlpha2());
            shippingAddressJson.put(PostalAddressParser.RECIPIENT_NAME_UNDERSCORE_KEY, shippingAddress.getRecipientName());
        } else {
            experienceProfile.put(ADDRESS_OVERRIDE_KEY, false);
        }

        if(request.getMerchantAccountId() != null) {
            parameters.put(MERCHANT_ACCOUNT_ID, request.getMerchantAccountId());
        }

        parameters.put(EXPERIENCE_PROFILE_KEY, experienceProfile);

        String apiUrl = isBillingAgreement ? SETUP_BILLING_AGREEMENT_ENDPOINT : CREATE_SINGLE_PAYMENT_ENDPOINT;
        String versionedPath = "/v1/" + apiUrl;
        fragment.getHttpClient().post(versionedPath, parameters.toString(), callback);
    }
private static void createPaymentResource ( BraintreeFragment fragment , PayPalRequest request , boolean isBillingAgreement , HttpResponseCallback callback ) throws JSONException , ErrorWithResponse , BraintreeException { String currencyCode = request . getCurrencyCode ( ) ; if ( currencyCode == null ) { currencyCode = fragment . getConfiguration ( ) . getPayPal ( ) . getCurrencyIsoCode ( ) ; } CheckoutRequest checkoutRequest = getCheckoutRequest ( fragment , null ) ; JSONObject parameters = new JSONObject ( ) . put ( RETURN_URL_KEY , checkoutRequest . getSuccessUrl ( ) ) . put ( CANCEL_URL_KEY , checkoutRequest . getCancelUrl ( ) ) . put ( OFFER_CREDIT_KEY , request . shouldOfferCredit ( ) ) ; if ( fragment . getAuthorization ( ) instanceof ClientToken ) { parameters . put ( AUTHORIZATION_FINGERPRINT_KEY , fragment . getAuthorization ( ) . getBearer ( ) ) ; } else { parameters . put ( TOKENIZATION_KEY , fragment . getAuthorization ( ) . getBearer ( ) ) ; } if ( ! isBillingAgreement ) { parameters . put ( AMOUNT_KEY , request . getAmount ( ) ) . put ( CURRENCY_ISO_CODE_KEY , currencyCode ) . put ( INTENT_KEY , request . getIntent ( ) ) ; } else { if ( ! TextUtils . isEmpty ( request . getBillingAgreementDescription ( ) ) ) { parameters . put ( DESCRIPTION_KEY , request . getBillingAgreementDescription ( ) ) ; } } JSONObject experienceProfile = new JSONObject ( ) ; experienceProfile . put ( NO_SHIPPING_KEY , ! request . isShippingAddressRequired ( ) ) ; experienceProfile . put ( LANDING_PAGE_TYPE_KEY , request . getLandingPageType ( ) ) ; String displayName = request . getDisplayName ( ) ; if ( TextUtils . isEmpty ( displayName ) ) { displayName = fragment . getConfiguration ( ) . getPayPal ( ) . getDisplayName ( ) ; } experienceProfile . put ( DISPLAY_NAME_KEY , displayName ) ; if ( request . getLocaleCode ( ) != null ) { experienceProfile . put ( LOCALE_CODE_KEY , request . getLocaleCode ( ) ) ; } if ( request . getShippingAddressOverride ( ) != null ) { experienceProfile . put ( ADDRESS_OVERRIDE_KEY , ! request . isShippingAddressEditable ( ) ) ; JSONObject shippingAddressJson ; if ( isBillingAgreement ) { shippingAddressJson = new JSONObject ( ) ; parameters . put ( SHIPPING_ADDRESS_KEY , shippingAddressJson ) ; } else { shippingAddressJson = parameters ; } PostalAddress shippingAddress = request . getShippingAddressOverride ( ) ; shippingAddressJson . put ( PostalAddressParser . LINE_1_KEY , shippingAddress . getStreetAddress ( ) ) ; shippingAddressJson . put ( PostalAddressParser . LINE_2_KEY , shippingAddress . getExtendedAddress ( ) ) ; shippingAddressJson . put ( PostalAddressParser . LOCALITY_KEY , shippingAddress . getLocality ( ) ) ; shippingAddressJson . put ( PostalAddressParser . REGION_KEY , shippingAddress . getRegion ( ) ) ; shippingAddressJson . put ( PostalAddressParser . POSTAL_CODE_UNDERSCORE_KEY , shippingAddress . getPostalCode ( ) ) ; shippingAddressJson . put ( PostalAddressParser . COUNTRY_CODE_UNDERSCORE_KEY , shippingAddress . getCountryCodeAlpha2 ( ) ) ; shippingAddressJson . put ( PostalAddressParser . RECIPIENT_NAME_UNDERSCORE_KEY , shippingAddress . getRecipientName ( ) ) ; } else { experienceProfile . put ( ADDRESS_OVERRIDE_KEY , false ) ; } if ( request . getMerchantAccountId ( ) != null ) { parameters . put ( MERCHANT_ACCOUNT_ID , request . getMerchantAccountId ( ) ) ; } parameters . put ( EXPERIENCE_PROFILE_KEY , experienceProfile ) ; String apiUrl = isBillingAgreement ? SETUP_BILLING_AGREEMENT_ENDPOINT : CREATE_SINGLE_PAYMENT_ENDPOINT ; String versionedPath = "/v1/" + apiUrl ; fragment . getHttpClient ( ) . post ( versionedPath , parameters . toString ( ) , callback ) ; }
Create a PayPalPaymentResource on behalf of the merchant. To be used in the PayPal Checkout
flows for Single Payment and Billing Agreement.

@param fragment A {@link BraintreeFragment} used to process the request.
@param request A {@link PayPalRequest} used to customize the request.
@param isBillingAgreement A boolean. If true, this will use the Billing Agreement. Otherwise,
PayPal will perform a Single Payment.
@param callback A callback on the http request.
Create a PayPalPaymentResource on behalf of the merchant . To be used in the PayPal Checkout flows for Single Payment and Billing Agreement .