Search in sources :

Example 36 with PayPalRequest

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

the class PayPalUnitTest method requestBillingAgreement_persistsPayPalRequest.

@Test
public void requestBillingAgreement_persistsPayPalRequest() {
    BraintreeFragment braintreeFragment = mMockFragmentBuilder.build();
    SharedPreferences prefs = BraintreeSharedPreferences.getSharedPreferences(RuntimeEnvironment.application);
    PayPal.requestOneTimePayment(braintreeFragment, new PayPalRequest("1").intent(PayPalRequest.INTENT_SALE));
    assertNotNull(prefs.getString("com.braintreepayments.api.PayPal.PAYPAL_REQUEST_KEY", null));
}
Also used : SharedPreferences(android.content.SharedPreferences) BraintreeSharedPreferences(com.braintreepayments.api.internal.BraintreeSharedPreferences) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with PayPalRequest

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

the class PayPalUnitTest method requestOneTimePayment_customHandlerSuccessCallbackIsInvoked.

@Test
public void requestOneTimePayment_customHandlerSuccessCallbackIsInvoked() 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(new PayPalAccountNonce());
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
    PayPal.requestOneTimePayment(fragment, new PayPalRequest("1"), 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());
    assertTrue(nonceCaptor.getValue() instanceof PayPalAccountNonce);
}
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 38 with PayPalRequest

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

the class PayPal method requestOneTimePayment.

private static void requestOneTimePayment(final BraintreeFragment fragment, final PayPalRequest paypalRequest, final boolean isBillingAgreement, final PayPalApprovalHandler handler) {
    final HttpResponseCallback callback = new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            final PayPalPaymentResource paypalPaymentResource;
            try {
                paypalPaymentResource = PayPalPaymentResource.fromJson(responseBody);
            } catch (JSONException e) {
                fragment.postCallback(e);
                return;
            }
            String redirectUrl = Uri.parse(paypalPaymentResource.getRedirectUrl()).buildUpon().appendQueryParameter(USER_ACTION_KEY, paypalRequest.getUserAction()).toString();
            Request request;
            if (isBillingAgreement) {
                request = getBillingAgreementRequest(fragment, redirectUrl);
            } else {
                request = getCheckoutRequest(fragment, redirectUrl);
            }
            startPayPal(fragment, request, handler);
        }

        @Override
        public void failure(Exception e) {
            fragment.postCallback(e);
        }
    };
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (!configuration.isPayPalEnabled()) {
                fragment.postCallback(new BraintreeException("PayPal is not enabled"));
                return;
            }
            if (!isManifestValid(fragment)) {
                fragment.sendAnalyticsEvent("paypal.invalid-manifest");
                fragment.postCallback(new BraintreeException("BraintreeBrowserSwitchActivity missing, " + "incorrectly configured in AndroidManifest.xml or another app defines the same browser " + "switch url as this app. See " + "https://developers.braintreepayments.com/guides/client-sdk/android/v2#browser-switch " + "for the correct configuration"));
                return;
            }
            try {
                persistPayPalRequest(fragment.getApplicationContext(), paypalRequest);
                createPaymentResource(fragment, paypalRequest, isBillingAgreement, callback);
            } catch (JSONException | ErrorWithResponse | BraintreeException ex) {
                fragment.postCallback(ex);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) PayPalPaymentResource(com.braintreepayments.api.models.PayPalPaymentResource) PayPalConfiguration(com.braintreepayments.api.models.PayPalConfiguration) Configuration(com.braintreepayments.api.models.Configuration) Request(com.paypal.android.sdk.onetouch.core.Request) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) BillingAgreementRequest(com.paypal.android.sdk.onetouch.core.BillingAgreementRequest) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) PendingRequest(com.paypal.android.sdk.onetouch.core.sdk.PendingRequest) JSONException(org.json.JSONException) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) BrowserSwitchException(com.braintreepayments.api.exceptions.BrowserSwitchException)

Example 39 with PayPalRequest

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

the class PayPal method onSuccess.

private static void onSuccess(final BraintreeFragment fragment, Intent data, Request request, Result result) {
    PayPalRequest paypalRequest = getPersistedPayPalRequest(fragment.getApplicationContext());
    TokenizationClient.tokenize(fragment, parseResponse(paypalRequest, request, result, data), new PaymentMethodNonceCallback() {

        @Override
        public void success(PaymentMethodNonce paymentMethodNonce) {
            if (paymentMethodNonce instanceof PayPalAccountNonce && ((PayPalAccountNonce) paymentMethodNonce).getCreditFinancing() != null) {
                fragment.sendAnalyticsEvent("paypal.credit.accepted");
            }
            fragment.postCallback(paymentMethodNonce);
        }

        @Override
        public void failure(Exception exception) {
            fragment.postCallback(exception);
        }
    });
}
Also used : PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) PayPalAccountNonce(com.braintreepayments.api.models.PayPalAccountNonce) PaymentMethodNonce(com.braintreepayments.api.models.PaymentMethodNonce) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) BrowserSwitchException(com.braintreepayments.api.exceptions.BrowserSwitchException)

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