Search in sources :

Example 6 with PaymentMethodNonceCallback

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

the class UnionPayUnitTest method mockSuccessCallback.

/* helpers */
private void mockSuccessCallback() {
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            UnionPayCardBuilder cardBuilder = (UnionPayCardBuilder) invocation.getArguments()[1];
            CardNonce cardNonce = mock(CardNonce.class);
            boolean hasSmsCode = false;
            try {
                hasSmsCode = new JSONObject(cardBuilder.build()).getJSONObject("options").has("smsCode");
            } catch (JSONException ignored) {
            }
            if (hasSmsCode) {
                when(cardNonce.getNonce()).thenReturn("nonce");
            }
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(cardNonce);
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(UnionPayCardBuilder.class), any(PaymentMethodNonceCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) JSONObject(org.json.JSONObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UnionPayCardBuilder(com.braintreepayments.api.models.UnionPayCardBuilder) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) JSONException(org.json.JSONException) CardNonce(com.braintreepayments.api.models.CardNonce)

Example 7 with PaymentMethodNonceCallback

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

the class TokenizationClientTest method tokenize_tokenizesAPayPalAccountWithATokenizationKey.

@Test(timeout = 10000)
public void tokenize_tokenizesAPayPalAccountWithATokenizationKey() throws InterruptedException, JSONException {
    final CountDownLatch latch = new CountDownLatch(1);
    BraintreeFragment fragment = getFragmentWithAuthorization(mActivity, TOKENIZATION_KEY);
    JSONObject otcJson = new JSONObject(FixturesHelper.stringFromFixture("paypal_otc_response.json"));
    PayPalAccountBuilder paypalAccountBuilder = new PayPalAccountBuilder().oneTouchCoreData(otcJson);
    TokenizationClient.tokenize(fragment, paypalAccountBuilder, new PaymentMethodNonceCallback() {

        @Override
        public void success(PaymentMethodNonce paymentMethodNonce) {
            assertIsANonce(paymentMethodNonce.getNonce());
            assertEquals("PayPal", paymentMethodNonce.getTypeLabel());
            latch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            fail(exception.getMessage());
        }
    });
    latch.await();
}
Also used : JSONObject(org.json.JSONObject) PayPalAccountBuilder(com.braintreepayments.api.models.PayPalAccountBuilder) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) CountDownLatch(java.util.concurrent.CountDownLatch) PaymentMethodNonce(com.braintreepayments.api.models.PaymentMethodNonce) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 8 with PaymentMethodNonceCallback

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

the class CardUnitTest method mockFailureCallback.

private void mockFailureCallback() {
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).failure(new ErrorWithResponse(422, ""));
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)

Example 9 with PaymentMethodNonceCallback

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

the class CardUnitTest method mockSuccessCallback.

/* helpers */
private void mockSuccessCallback() {
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).success(new CardNonce());
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) CardNonce(com.braintreepayments.api.models.CardNonce)

Example 10 with PaymentMethodNonceCallback

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

the class PayPalUnitTest method requestOneTimePayment_isSuccessful.

@Test
public void requestOneTimePayment_isSuccessful() {
    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).postCallback(any(PayPalAccountNonce.class));
    SharedPreferences prefs = BraintreeSharedPreferences.getSharedPreferences(RuntimeEnvironment.application);
    assertNull(prefs.getString("com.braintreepayments.api.PayPal.REQUEST_KEY", null));
    assertNull(prefs.getString("com.braintreepayments.api.PayPal.REQUEST_TYPE_KEY", null));
}
Also used : PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) SharedPreferences(android.content.SharedPreferences) BraintreeSharedPreferences(com.braintreepayments.api.internal.BraintreeSharedPreferences) 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)

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