Search in sources :

Example 1 with PayPalAccountBuilder

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

the class TokenizationClientUnitTest method tokenize_tokenizesNonCardPaymentMethodsWithRestWhenGraphQLIsEnabled.

@Test
public void tokenize_tokenizesNonCardPaymentMethodsWithRestWhenGraphQLIsEnabled() {
    BraintreeFragment fragment = new MockFragmentBuilder().configuration(new TestConfigurationBuilder().graphQL().build()).build();
    TokenizationClient.tokenize(fragment, new PayPalAccountBuilder(), null);
    TokenizationClient.tokenize(fragment, new UnionPayCardBuilder(), null);
    TokenizationClient.tokenize(fragment, new VenmoAccountBuilder(), null);
    verifyZeroInteractions(fragment.getGraphQLHttpClient());
}
Also used : VenmoAccountBuilder(com.braintreepayments.api.models.VenmoAccountBuilder) PayPalAccountBuilder(com.braintreepayments.api.models.PayPalAccountBuilder) UnionPayCardBuilder(com.braintreepayments.api.models.UnionPayCardBuilder) TestConfigurationBuilder(com.braintreepayments.testutils.TestConfigurationBuilder) Test(org.junit.Test)

Example 2 with PayPalAccountBuilder

use of com.braintreepayments.api.models.PayPalAccountBuilder 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 3 with PayPalAccountBuilder

use of com.braintreepayments.api.models.PayPalAccountBuilder 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 4 with PayPalAccountBuilder

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

the class PayPal method parseResponse.

/**
 * Parse the PayPal response URL using OneTouchCore.
 *
 * @param paypalRequest Original {@link PayPalRequest} that started this flow.
 * @param result Context that received the result.
 * @param intent The {@link Intent} returned in result.
 * @return A {@link PayPalAccountBuilder} or null if the intent is invalid.
 */
private static PayPalAccountBuilder parseResponse(PayPalRequest paypalRequest, Request request, Result result, Intent intent) {
    PayPalAccountBuilder paypalAccountBuilder = new PayPalAccountBuilder().clientMetadataId(request.getClientMetadataId());
    if (request instanceof CheckoutRequest && paypalRequest != null) {
        paypalAccountBuilder.intent(paypalRequest.getIntent());
    }
    if (isAppSwitch(intent)) {
        paypalAccountBuilder.source("paypal-app");
    } else {
        paypalAccountBuilder.source("paypal-browser");
    }
    JSONObject payload = result.getResponse();
    // Modify payload in 'mock' mode to scope the response
    try {
        JSONObject clientJson = payload.getJSONObject("client");
        JSONObject response = payload.getJSONObject("response");
        if (EnvironmentManager.MOCK.equalsIgnoreCase(clientJson.getString("client")) && response.getString("code") != null && !(request instanceof CheckoutRequest)) {
            payload.put("response", new JSONObject().put("code", "fake-code:" + ((AuthorizationRequest) request).getScopeString()));
        }
    } catch (JSONException ignored) {
    }
    paypalAccountBuilder.oneTouchCoreData(payload);
    return paypalAccountBuilder;
}
Also used : JSONObject(org.json.JSONObject) PayPalAccountBuilder(com.braintreepayments.api.models.PayPalAccountBuilder) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) JSONException(org.json.JSONException)

Aggregations

PayPalAccountBuilder (com.braintreepayments.api.models.PayPalAccountBuilder)4 JSONObject (org.json.JSONObject)3 Test (org.junit.Test)3 PaymentMethodNonceCallback (com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JSONException (org.json.JSONException)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 BraintreeSharedPreferences (com.braintreepayments.api.internal.BraintreeSharedPreferences)1 PayPalAccountNonce (com.braintreepayments.api.models.PayPalAccountNonce)1 PayPalRequest (com.braintreepayments.api.models.PayPalRequest)1 PaymentMethodBuilder (com.braintreepayments.api.models.PaymentMethodBuilder)1 PaymentMethodNonce (com.braintreepayments.api.models.PaymentMethodNonce)1 UnionPayCardBuilder (com.braintreepayments.api.models.UnionPayCardBuilder)1 VenmoAccountBuilder (com.braintreepayments.api.models.VenmoAccountBuilder)1 TestConfigurationBuilder (com.braintreepayments.testutils.TestConfigurationBuilder)1 CheckoutRequest (com.paypal.android.sdk.onetouch.core.CheckoutRequest)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)1