Search in sources :

Example 11 with PaymentMethodNonceCallback

use of com.braintreepayments.api.interfaces.PaymentMethodNonceCallback in project braintree_android by braintree.

the class PayPalUnitTest method requestOneTimePayment_containsPaymentIntent.

@Test
public void requestOneTimePayment_containsPaymentIntent() throws JSONException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    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 {
            PayPalAccountBuilder payPalAccountBuilder = (PayPalAccountBuilder) invocation.getArguments()[1];
            JSONObject payload = new JSONObject(payPalAccountBuilder.build());
            assertEquals(PayPalRequest.INTENT_SALE, payload.getJSONObject("paypalAccount").getString("intent"));
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(new PayPalAccountNonce());
            latch.countDown();
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
    PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").intent(PayPalRequest.INTENT_SALE));
    SharedPreferences prefs = BraintreeSharedPreferences.getSharedPreferences(RuntimeEnvironment.application);
    assertNull(prefs.getString("com.braintreepayments.api.PayPal.PAYPAL_REQUEST_KEY", null));
    latch.await();
}
Also used : PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) SharedPreferences(android.content.SharedPreferences) BraintreeSharedPreferences(com.braintreepayments.api.internal.BraintreeSharedPreferences) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) Intent(android.content.Intent) CountDownLatch(java.util.concurrent.CountDownLatch) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) JSONObject(org.json.JSONObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PayPalAccountBuilder(com.braintreepayments.api.models.PayPalAccountBuilder) JSONObject(org.json.JSONObject) PayPalAccountNonce(com.braintreepayments.api.models.PayPalAccountNonce) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with PaymentMethodNonceCallback

use of com.braintreepayments.api.interfaces.PaymentMethodNonceCallback in project braintree_android by braintree.

the class PayPalUnitTest method requestBillingAgreement_paypalCreditReturnedInResponse.

@Test
public void requestBillingAgreement_paypalCreditReturnedInResponse() throws InterruptedException {
    BraintreeFragment fragment = mMockFragmentBuilder.successResponse(stringFromFixture("paypal_hermes_billing_agreement_response.json")).build();
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(PayPalAccountNonce.fromJson(stringFromFixture("payment_methods/paypal_account_response.json")));
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
    PayPal.requestBillingAgreement(fragment, new PayPalRequest().offerCredit(true), new PayPalApprovalHandler() {

        @Override
        public void handleApproval(Request request, PayPalApprovalCallback paypalApprovalCallback) {
            paypalApprovalCallback.onComplete(new Intent().setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/success?PayerID=HERMES-SANDBOX-PAYER-ID&paymentId=HERMES-SANDBOX-PAYMENT-ID&ba_token=EC-HERMES-SANDBOX-EC-TOKEN")));
        }
    });
    ArgumentCaptor<PaymentMethodNonce> nonceCaptor = ArgumentCaptor.forClass(PaymentMethodNonce.class);
    verify(fragment).postCallback(nonceCaptor.capture());
    verify(fragment).sendAnalyticsEvent("paypal.credit.accepted");
    assertTrue(nonceCaptor.getValue() instanceof PayPalAccountNonce);
    PayPalAccountNonce payPalAccountNonce = (PayPalAccountNonce) nonceCaptor.getValue();
    assertNotNull(payPalAccountNonce.getCreditFinancing());
    assertEquals(18, payPalAccountNonce.getCreditFinancing().getTerm());
}
Also used : PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) Request(com.paypal.android.sdk.onetouch.core.Request) Intent(android.content.Intent) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) PayPalApprovalHandler(com.braintreepayments.api.interfaces.PayPalApprovalHandler) PayPalApprovalCallback(com.braintreepayments.api.interfaces.PayPalApprovalCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PayPalAccountNonce(com.braintreepayments.api.models.PayPalAccountNonce) PaymentMethodNonce(com.braintreepayments.api.models.PaymentMethodNonce) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with PaymentMethodNonceCallback

use of com.braintreepayments.api.interfaces.PaymentMethodNonceCallback in project braintree_android by braintree.

the class PayPalUnitTest method authorizeAccount_whenSuccessful_sendsAnalyticsEvents.

@Test
public void authorizeAccount_whenSuccessful_sendsAnalyticsEvents() throws Exception {
    final AuthorizationRequest request = new AuthorizationRequest(RuntimeEnvironment.application);
    request.environment("test");
    request.successUrl("com.braintreepayments.api.test.braintree", "success");
    setField("mMsgGuid", request, "c862cf00-f878-4e38-bb83-65bcc4b51831");
    setField("mEncryptionKey", request, EncryptionUtils.hexStringToByteArray("0481806100DE4EBB5581163579990EE825737255A81A883B791A1BB6F5A7E81C"));
    doAnswer(new Answer<AuthorizationRequest>() {

        @Override
        public AuthorizationRequest answer(InvocationOnMock invocation) throws Throwable {
            return request;
        }
    }).when(PayPal.class, "getAuthorizationRequest", any(BraintreeFragment.class));
    final BraintreeFragment fragment = mMockFragmentBuilder.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?payloadEnc=k7mNFgzs404Wy8VOReO2E%2FTpfDoC44E1iwjptDooIkjjh1TcAupUCM8812g3zmBruc%2BFeIIwZlEhu6ugAXvLs5u6aHG4KU7FuPPLDS9OO87WAw0v3n7QIPp%2Bd5o%2Bk4VZ047w%2FXiijFuFKb4SRe9fg8kYGAYCtUR1IrK%2BhuvB3VCg7rkLk9V0n2YF3WcvmaLUt8SIYok1dbG8Ou4zDIXaZp7%2ByGalcyjN3MW3OLstaehD2jpuxlP6WDG6dkB6LZ2LEnHDV0X7j2vOtmSrrCtYZuFhlB%2FkKNkgsVhBrbHdqsfsBKyc7sHlsgT0Dz0TXc3BHqjJIWLrOuglt78QOM92%2B7GFM6JL5%2BARzJ4Tp9iI%2BU4QyQLTSkOGTA0LgSBUhr2srF41lWTXw65F4A%3D%3D&payload=eyJ2ZXJzaW9uIjozLCJtc2dfR1VJRCI6ImM4NjJjZjAwLWY4NzgtNGUzOC1iYjgzLTY1YmNjNGI1MTgzMSIsInJlc3BvbnNlX3R5cGUiOiJjb2RlIiwiZW52aXJvbm1lbnQiOiJtb2NrIiwiZXJyb3IiOm51bGx9&x-source=com.braintree.browserswitch"));
            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.authorizeAccount(fragment);
    verify(fragment).sendAnalyticsEvent("paypal.future-payments.selected");
    verify(fragment).sendAnalyticsEvent("paypal-future-payments.webswitch.started");
    verify(fragment).sendAnalyticsEvent("paypal-future-payments.webswitch.succeeded");
}
Also used : PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with PaymentMethodNonceCallback

use of com.braintreepayments.api.interfaces.PaymentMethodNonceCallback in project braintree_android by braintree.

the class PayPalUnitTest method requestOneTimePayment_paypalCreditReturnedInResponse.

@Test
public void requestOneTimePayment_paypalCreditReturnedInResponse() throws InterruptedException {
    BraintreeFragment fragment = mMockFragmentBuilder.successResponse(stringFromFixture("paypal_hermes_response.json")).build();
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(PayPalAccountNonce.fromJson(stringFromFixture("payment_methods/paypal_account_response.json")));
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
    PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").offerCredit(true), new PayPalApprovalHandler() {

        @Override
        public void handleApproval(Request request, PayPalApprovalCallback paypalApprovalCallback) {
            paypalApprovalCallback.onComplete(new Intent().setData(Uri.parse("com.braintreepayments.demo.braintree://onetouch/v1/success?PayerID=HERMES-SANDBOX-PAYER-ID&paymentId=HERMES-SANDBOX-PAYMENT-ID&token=EC-HERMES-SANDBOX-EC-TOKEN")));
        }
    });
    ArgumentCaptor<PaymentMethodNonce> nonceCaptor = ArgumentCaptor.forClass(PaymentMethodNonce.class);
    verify(fragment).postCallback(nonceCaptor.capture());
    verify(fragment).sendAnalyticsEvent("paypal.credit.accepted");
    assertTrue(nonceCaptor.getValue() instanceof PayPalAccountNonce);
    PayPalAccountNonce payPalAccountNonce = (PayPalAccountNonce) nonceCaptor.getValue();
    assertNotNull(payPalAccountNonce.getCreditFinancing());
    assertEquals(18, payPalAccountNonce.getCreditFinancing().getTerm());
}
Also used : PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) Request(com.paypal.android.sdk.onetouch.core.Request) Intent(android.content.Intent) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) PayPalApprovalHandler(com.braintreepayments.api.interfaces.PayPalApprovalHandler) PayPalApprovalCallback(com.braintreepayments.api.interfaces.PayPalApprovalCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PayPalAccountNonce(com.braintreepayments.api.models.PayPalAccountNonce) PaymentMethodNonce(com.braintreepayments.api.models.PaymentMethodNonce) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with PaymentMethodNonceCallback

use of com.braintreepayments.api.interfaces.PaymentMethodNonceCallback in project braintree_android by braintree.

the class PayPalUnitTest method authorizeAccount_doesNotCallCancelListenerWhenSuccessful.

@Test
public void authorizeAccount_doesNotCallCancelListenerWhenSuccessful() throws Exception {
    final AuthorizationRequest request = new AuthorizationRequest(RuntimeEnvironment.application);
    request.environment("test");
    request.successUrl("com.braintreepayments.api.test.braintree", "success");
    setField("mMsgGuid", request, "c862cf00-f878-4e38-bb83-65bcc4b51831");
    setField("mEncryptionKey", request, EncryptionUtils.hexStringToByteArray("0481806100DE4EBB5581163579990EE825737255A81A883B791A1BB6F5A7E81C"));
    doAnswer(new Answer<AuthorizationRequest>() {

        @Override
        public AuthorizationRequest answer(InvocationOnMock invocation) throws Throwable {
            return request;
        }
    }).when(PayPal.class, "getAuthorizationRequest", any(BraintreeFragment.class));
    final BraintreeFragment fragment = mMockFragmentBuilder.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?payloadEnc=k7mNFgzs404Wy8VOReO2E%2FTpfDoC44E1iwjptDooIkjjh1TcAupUCM8812g3zmBruc%2BFeIIwZlEhu6ugAXvLs5u6aHG4KU7FuPPLDS9OO87WAw0v3n7QIPp%2Bd5o%2Bk4VZ047w%2FXiijFuFKb4SRe9fg8kYGAYCtUR1IrK%2BhuvB3VCg7rkLk9V0n2YF3WcvmaLUt8SIYok1dbG8Ou4zDIXaZp7%2ByGalcyjN3MW3OLstaehD2jpuxlP6WDG6dkB6LZ2LEnHDV0X7j2vOtmSrrCtYZuFhlB%2FkKNkgsVhBrbHdqsfsBKyc7sHlsgT0Dz0TXc3BHqjJIWLrOuglt78QOM92%2B7GFM6JL5%2BARzJ4Tp9iI%2BU4QyQLTSkOGTA0LgSBUhr2srF41lWTXw65F4A%3D%3D&payload=eyJ2ZXJzaW9uIjozLCJtc2dfR1VJRCI6ImM4NjJjZjAwLWY4NzgtNGUzOC1iYjgzLTY1YmNjNGI1MTgzMSIsInJlc3BvbnNlX3R5cGUiOiJjb2RlIiwiZW52aXJvbm1lbnQiOiJtb2NrIiwiZXJyb3IiOm51bGx9&x-source=com.braintree.browserswitch"));
            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.authorizeAccount(fragment);
    verify(fragment, never()).postCancelCallback(anyInt());
}
Also used : PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

PaymentMethodNonceCallback (com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)22 InvocationOnMock (org.mockito.invocation.InvocationOnMock)17 Answer (org.mockito.stubbing.Answer)17 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)17 Test (org.junit.Test)16 PaymentMethodBuilder (com.braintreepayments.api.models.PaymentMethodBuilder)15 PayPalAccountNonce (com.braintreepayments.api.models.PayPalAccountNonce)14 Intent (android.content.Intent)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 JSONObject (org.json.JSONObject)12 PayPalRequest (com.braintreepayments.api.models.PayPalRequest)11 PaymentMethodNonce (com.braintreepayments.api.models.PaymentMethodNonce)8 AuthorizationRequest (com.paypal.android.sdk.onetouch.core.AuthorizationRequest)6 JSONException (org.json.JSONException)6 UnionPayCardBuilder (com.braintreepayments.api.models.UnionPayCardBuilder)4 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)3 PayPalApprovalCallback (com.braintreepayments.api.interfaces.PayPalApprovalCallback)3 PayPalApprovalHandler (com.braintreepayments.api.interfaces.PayPalApprovalHandler)3 Request (com.paypal.android.sdk.onetouch.core.Request)3 SharedPreferences (android.content.SharedPreferences)2