use of com.braintreepayments.api.models.PostalAddress in project braintree_android by braintree.
the class PayPalUnitTest method requestOneTimePayment_postParamsIncludeNoShippingAndAddressAndAddressOverride.
@Test
public void requestOneTimePayment_postParamsIncludeNoShippingAndAddressAndAddressOverride() throws JSONException {
BraintreeFragment fragment = mMockFragmentBuilder.build();
PostalAddress address = new PostalAddress().streetAddress("123 Fake St.").extendedAddress("Apt. v.0").locality("Oakland").region("CA").postalCode("12345").countryCodeAlpha2("US");
PayPalRequest request = new PayPalRequest("3.43").shippingAddressRequired(false).shippingAddressOverride(address);
PayPal.requestOneTimePayment(fragment, request);
ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
verify(fragment.getHttpClient()).post(pathCaptor.capture(), dataCaptor.capture(), any(HttpResponseCallback.class));
assertTrue(pathCaptor.getValue().contains("/paypal_hermes/create_payment_resource"));
JSONObject json = new JSONObject(dataCaptor.getValue());
assertEquals("3.43", json.get("amount"));
assertEquals("123 Fake St.", json.get("line1"));
assertEquals("Apt. v.0", json.get("line2"));
assertEquals("Oakland", json.get("city"));
assertEquals("CA", json.get("state"));
assertEquals("12345", json.get("postal_code"));
assertEquals("US", json.get("country_code"));
assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping"));
assertEquals(true, json.getJSONObject("experience_profile").get("address_override"));
}
use of com.braintreepayments.api.models.PostalAddress in project braintree_android by braintree.
the class PayPalUnitTest method requestOneTimePayment_postParamsIncludeAddressAndAddressOverride.
@Test
public void requestOneTimePayment_postParamsIncludeAddressAndAddressOverride() throws JSONException {
BraintreeFragment fragment = mMockFragmentBuilder.build();
PostalAddress address = new PostalAddress().streetAddress("123 Fake St.").extendedAddress("Apt. v.0").locality("Oakland").region("CA").postalCode("12345").countryCodeAlpha2("US");
PayPalRequest request = new PayPalRequest("3.43").shippingAddressRequired(true).shippingAddressOverride(address);
PayPal.requestOneTimePayment(fragment, request);
ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
verify(fragment.getHttpClient()).post(pathCaptor.capture(), dataCaptor.capture(), any(HttpResponseCallback.class));
assertTrue(pathCaptor.getValue().contains("/paypal_hermes/create_payment_resource"));
JSONObject json = new JSONObject(dataCaptor.getValue());
assertEquals("3.43", json.get("amount"));
assertEquals("123 Fake St.", json.get("line1"));
assertEquals("Apt. v.0", json.get("line2"));
assertEquals("Oakland", json.get("city"));
assertEquals("CA", json.get("state"));
assertEquals("12345", json.get("postal_code"));
assertEquals("US", json.get("country_code"));
assertEquals(false, json.getJSONObject("experience_profile").get("no_shipping"));
assertEquals(true, json.getJSONObject("experience_profile").get("address_override"));
}
use of com.braintreepayments.api.models.PostalAddress in project braintree_android by braintree.
the class PayPal method createPaymentResource.
/**
* 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.
*/
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, true);
PostalAddress shippingAddress = request.getShippingAddressOverride();
parameters.put(PostalAddress.LINE_1_KEY, shippingAddress.getStreetAddress());
parameters.put(PostalAddress.LINE_2_KEY, shippingAddress.getExtendedAddress());
parameters.put(PostalAddress.LOCALITY_KEY, shippingAddress.getLocality());
parameters.put(PostalAddress.REGION_KEY, shippingAddress.getRegion());
parameters.put(PostalAddress.POSTAL_CODE_UNDERSCORE_KEY, shippingAddress.getPostalCode());
parameters.put(PostalAddress.COUNTRY_CODE_UNDERSCORE_KEY, shippingAddress.getCountryCodeAlpha2());
parameters.put(PostalAddress.RECIPIENT_NAME_UNDERSCORE_KEY, shippingAddress.getRecipientName());
} else {
experienceProfile.put(ADDRESS_OVERRIDE_KEY, false);
}
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);
}
Aggregations