/** * @deprecated Use {@link #requestBillingAgreement(BraintreeFragment, PayPalRequest)}. */ @Deprecated public static void authorizeAccount(BraintreeFragment fragment) { authorizeAccount(fragment, null); }
/** * Starts the Billing Agreement flow for PayPal. This will launch the PayPal app if installed or * fall back to a browser switch. * * @param fragment A {@link BraintreeFragment} used to process the request. * @param request A {@link PayPalRequest} used to customize the request. */ public static void requestBillingAgreement(BraintreeFragment fragment, PayPalRequest request) { requestBillingAgreement(fragment, request, null); }
/** * Starts the Single Payment flow for PayPal. This will launch the PayPal app if installed or * fall back to a browser switch. * * @param fragment A {@link BraintreeFragment} used to process the request. * @param request A {@link PayPalRequest} used to customize the request. An amount MUST be specified. */ public static void requestOneTimePayment(BraintreeFragment fragment, PayPalRequest request) { requestOneTimePayment(fragment, request, null); }
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); } }); }
private static void startPayPal(final BraintreeFragment fragment, Request request, PayPalApprovalHandler handler) { persistRequest(fragment.getApplicationContext(), request); PayPalApprovalCallback callback = null; if (handler == null) { handler = getDefaultApprovalHandler(fragment); } else { callback = new PayPalApprovalCallback() { @Override public void onComplete(Intent data) { PayPal.onActivityResult(fragment, Activity.RESULT_OK, data); } @Override public void onCancel() { fragment.postCancelCallback(BraintreeRequestCodes.PAYPAL); } }; } handler.handleApproval(request, callback); }
@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 onComplete(Intent data) { PayPal.onActivityResult(fragment, Activity.RESULT_OK, data); }
@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; } if (configuration.getPayPal().shouldUseBillingAgreement() && !sFuturePaymentsOverride) { requestBillingAgreement(fragment, new PayPalRequest()); return; } fragment.sendAnalyticsEvent("paypal.future-payments.selected"); AuthorizationRequest request = getAuthorizationRequest(fragment); if (additionalScopes != null) { for (String scope : additionalScopes) { request.withScopeValue(scope); } } startPayPal(fragment, request, null); } });
CheckoutRequest checkoutRequest = getCheckoutRequest(fragment, null); JSONObject parameters = new JSONObject() .put(RETURN_URL_KEY, checkoutRequest.getSuccessUrl())
@Test public void getBillingAgreement_containsCorrectValues() throws JSONException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_offline_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder.configuration(configuration).build(); BillingAgreementRequest request = PayPal.getBillingAgreementRequest(fragment, "https://paypal.com/?ba_token=pairingId"); assertEquals(EnvironmentManager.MOCK, request.getEnvironment()); assertEquals("com.braintreepayments.api.braintree://onetouch/v1/cancel", request.getCancelUrl()); assertEquals("com.braintreepayments.api.braintree://onetouch/v1/success", request.getSuccessUrl()); assertEquals("paypal_client_id", request.getClientId()); assertEquals("pairingId", request.getPairingId()); }
@Test public void getAuthorizationRequest_containsCorrectValues() throws JSONException, InvalidArgumentException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_offline_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder .authorization(Authorization.fromString(TOKENIZATION_KEY)) .configuration(configuration) .build(); AuthorizationRequest request = PayPal.getAuthorizationRequest(fragment); assertEquals(EnvironmentManager.MOCK, request.getEnvironment()); assertEquals("com.braintreepayments.api.braintree://onetouch/v1/cancel", request.getCancelUrl()); assertEquals("com.braintreepayments.api.braintree://onetouch/v1/success", request.getSuccessUrl()); assertEquals("paypal_client_id", request.getClientId()); assertEquals(configuration.getPayPal().getPrivacyUrl(), request.getPrivacyUrl()); assertEquals(configuration.getPayPal().getUserAgreementUrl(), request.getUserAgreementUrl()); String[] scopes = request.getScopeString().split(" "); Arrays.sort(scopes); assertEquals(2, scopes.length); assertEquals("email", scopes[0]); assertEquals("https://uri.paypal.com/services/payments/futurepayments", scopes[1]); }
@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));
@Test public void getCheckoutRequest_containsCorrectValues() throws JSONException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_offline_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder.configuration(configuration).build(); CheckoutRequest request = PayPal.getCheckoutRequest(fragment, "https://paypal.com/?token=pairingId"); assertEquals(EnvironmentManager.MOCK, request.getEnvironment()); assertEquals("com.braintreepayments.api.braintree://onetouch/v1/cancel", request.getCancelUrl()); assertEquals("com.braintreepayments.api.braintree://onetouch/v1/success", request.getSuccessUrl()); assertEquals("paypal_client_id", request.getClientId()); assertEquals("pairingId", request.getPairingId()); }
@Test public void getBillingAgreementRequest_buildsWithLiveStageUrl() throws JSONException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_live_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder.configuration(configuration).build(); Request request = PayPal.getBillingAgreementRequest(fragment, null); assertEquals(EnvironmentManager.LIVE, request.getEnvironment()); }
@Test public void getAuthorizationRequest_buildsWithLiveStageUrl() throws JSONException, InvalidArgumentException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_live_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder .authorization(Authorization.fromString(TOKENIZATION_KEY)) .configuration(configuration) .build(); Request request = PayPal.getAuthorizationRequest(fragment); assertEquals(EnvironmentManager.LIVE, request.getEnvironment()); }
public void launchSinglePayment(View v) { setProgressBarIndeterminateVisibility(true); PayPal.requestOneTimePayment(mBraintreeFragment, getPayPalRequest("1.00")); }
public void launchBillingAgreement(View v) { setProgressBarIndeterminateVisibility(true); PayPal.requestBillingAgreement(mBraintreeFragment, getPayPalRequest(null)); }
public void launchFuturePaymentAddressScope(View v) { setProgressBarIndeterminateVisibility(true); PayPalOverrides.setFuturePaymentsOverride(true); PayPal.authorizeAccount(mBraintreeFragment, Collections.singletonList(PayPal.SCOPE_ADDRESS)); }
@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&ba_token=EC-HERMES-SANDBOX-EC-TOKEN")); PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent); return null; } }).when(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), any(Intent.class));
@Test public void getCheckoutRequest_buildsWithOfflineStageUrl() throws JSONException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_offline_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder.configuration(configuration).build(); Request request = PayPal.getCheckoutRequest(fragment, null); assertEquals(EnvironmentManager.MOCK, request.getEnvironment()); }