Search in sources :

Example 16 with PayPalRequest

use of com.braintreepayments.api.models.PayPalRequest 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"));
}
Also used : PostalAddress(com.braintreepayments.api.models.PostalAddress) JSONObject(org.json.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with PayPalRequest

use of com.braintreepayments.api.models.PayPalRequest in project braintree_android by braintree.

the class PayPalUnitTest method requestOneTimePayment_whenSuccessful_sendsAnalyticsEvents.

@Test
public void requestOneTimePayment_whenSuccessful_sendsAnalyticsEvents() {
    final BraintreeFragment fragment = mMockFragmentBuilder.successResponse(stringFromFixture("paypal_hermes_response.json")).build();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Intent intent = new Intent().setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/success?PayerID=HERMES-SANDBOX-PAYER-ID&paymentId=HERMES-SANDBOX-PAYMENT-ID&token=EC-HERMES-SANDBOX-EC-TOKEN"));
            PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent);
            return null;
        }
    }).when(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), any(Intent.class));
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(new PayPalAccountNonce());
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
    PayPal.requestOneTimePayment(fragment, new PayPalRequest("1"));
    verify(fragment).sendAnalyticsEvent("paypal.one-time-payment.selected");
    verify(fragment).sendAnalyticsEvent("paypal-single-payment.webswitch.started");
    verify(fragment).sendAnalyticsEvent("paypal-single-payment.webswitch.succeeded");
}
Also used : PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) JSONObject(org.json.JSONObject) Intent(android.content.Intent) PayPalAccountNonce(com.braintreepayments.api.models.PayPalAccountNonce) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with PayPalRequest

use of com.braintreepayments.api.models.PayPalRequest in project braintree_android by braintree.

the class PayPalUnitTest method requestOneTimePayment_doesNotCallCancelListenerWhenSuccessful.

@Test
public void requestOneTimePayment_doesNotCallCancelListenerWhenSuccessful() {
    final BraintreeFragment fragment = mMockFragmentBuilder.successResponse(stringFromFixture("paypal_hermes_response.json")).build();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Intent intent = new Intent().setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/success?PayerID=HERMES-SANDBOX-PAYER-ID&paymentId=HERMES-SANDBOX-PAYMENT-ID&token=EC-HERMES-SANDBOX-EC-TOKEN"));
            PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent);
            return null;
        }
    }).when(fragment).startActivity(any(Intent.class));
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(new PayPalAccountNonce());
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
    PayPal.requestOneTimePayment(fragment, new PayPalRequest("1"));
    verify(fragment, never()).postCancelCallback(anyInt());
}
Also used : PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) JSONObject(org.json.JSONObject) Intent(android.content.Intent) PayPalAccountNonce(com.braintreepayments.api.models.PayPalAccountNonce) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with PayPalRequest

use of com.braintreepayments.api.models.PayPalRequest in project braintree_android by braintree.

the class PayPalUnitTest method requestOneTimePayment_postParamsIncludeNoShipping.

@Test
public void requestOneTimePayment_postParamsIncludeNoShipping() throws JSONException {
    BraintreeFragment fragment = mMockFragmentBuilder.build();
    PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").shippingAddressRequired(false));
    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("1", json.get("amount"));
    assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping"));
    assertEquals(false, json.getJSONObject("experience_profile").get("address_override"));
}
Also used : JSONObject(org.json.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with PayPalRequest

use of com.braintreepayments.api.models.PayPalRequest in project braintree_android by braintree.

the class PayPalUnitTest method requestOneTimePayment_intent_canBeSetToSale.

@Test
public void requestOneTimePayment_intent_canBeSetToSale() throws JSONException {
    BraintreeFragment fragment = mMockFragmentBuilder.build();
    PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").intent(PayPalRequest.INTENT_SALE));
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(fragment.getHttpClient()).post(contains("/paypal_hermes/create_payment_resource"), dataCaptor.capture(), any(HttpResponseCallback.class));
    JSONObject json = new JSONObject(dataCaptor.getValue());
    assertEquals("1", json.get("amount"));
    assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping"));
    assertEquals(false, json.getJSONObject("experience_profile").get("address_override"));
    assertEquals(PayPalRequest.INTENT_SALE, json.get("intent"));
}
Also used : JSONObject(org.json.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

PayPalRequest (com.braintreepayments.api.models.PayPalRequest)39 Test (org.junit.Test)36 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)36 JSONObject (org.json.JSONObject)21 Intent (android.content.Intent)18 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)13 Matchers.anyString (org.mockito.Matchers.anyString)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 Answer (org.mockito.stubbing.Answer)12 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)12 PaymentMethodNonceCallback (com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)11 PayPalAccountNonce (com.braintreepayments.api.models.PayPalAccountNonce)11 PaymentMethodBuilder (com.braintreepayments.api.models.PaymentMethodBuilder)10 AuthorizationRequest (com.paypal.android.sdk.onetouch.core.AuthorizationRequest)6 Request (com.paypal.android.sdk.onetouch.core.Request)6 PayPalApprovalCallback (com.braintreepayments.api.interfaces.PayPalApprovalCallback)5 PayPalApprovalHandler (com.braintreepayments.api.interfaces.PayPalApprovalHandler)5 SharedPreferences (android.content.SharedPreferences)4 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)4 BraintreeSharedPreferences (com.braintreepayments.api.internal.BraintreeSharedPreferences)4