diff --git a/CHANGELOG.md b/CHANGELOG.md index ebacc35970..1a00ad99f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ## 4.42.0 (2024-03-12) +* PayPal + * Add optional property `PayPalVaultRequest.setUserAuthenticationEmail()` * BraintreeCore * Send `paypal_context_id` in `analytics_event` to PayPal's analytics service (FPTI) when available * Venmo diff --git a/PayPal/src/main/java/com/braintreepayments/api/PayPalRequest.java b/PayPal/src/main/java/com/braintreepayments/api/PayPalRequest.java index e48b25164d..d2576a5a41 100644 --- a/PayPal/src/main/java/com/braintreepayments/api/PayPalRequest.java +++ b/PayPal/src/main/java/com/braintreepayments/api/PayPalRequest.java @@ -26,6 +26,7 @@ public abstract class PayPalRequest implements Parcelable { static final String REQUEST_BILLING_AGREEMENT_KEY = "request_billing_agreement"; static final String BILLING_AGREEMENT_DETAILS_KEY = "billing_agreement_details"; static final String DESCRIPTION_KEY = "description"; + static final String PAYER_EMAIL_KEY = "payer_email"; static final String AUTHORIZATION_FINGERPRINT_KEY = "authorization_fingerprint"; static final String TOKENIZATION_KEY = "client_key"; static final String RETURN_URL_KEY = "return_url"; diff --git a/PayPal/src/main/java/com/braintreepayments/api/PayPalVaultRequest.java b/PayPal/src/main/java/com/braintreepayments/api/PayPalVaultRequest.java index 83c4b373cf..643678d0c4 100644 --- a/PayPal/src/main/java/com/braintreepayments/api/PayPalVaultRequest.java +++ b/PayPal/src/main/java/com/braintreepayments/api/PayPalVaultRequest.java @@ -4,6 +4,8 @@ import android.os.Parcelable; import android.text.TextUtils; +import androidx.annotation.Nullable; + import org.json.JSONException; import org.json.JSONObject; @@ -14,6 +16,8 @@ public class PayPalVaultRequest extends PayPalRequest implements Parcelable { private boolean shouldOfferCredit; + private String userAuthenticationEmail; + public PayPalVaultRequest() { } @@ -30,6 +34,21 @@ public boolean getShouldOfferCredit() { return shouldOfferCredit; } + /** + * Optional: User email to initiate a quicker authentication flow in cases where the user has a + * PayPal Account with the same email. + * + * @param userAuthenticationEmail - email address of the payer + */ + public void setUserAuthenticationEmail(@Nullable String userAuthenticationEmail) { + this.userAuthenticationEmail = userAuthenticationEmail; + } + + @Nullable + public String getUserAuthenticationEmail() { + return this.userAuthenticationEmail; + } + String createRequestBody(Configuration configuration, Authorization authorization, String successUrl, String cancelUrl) throws JSONException { JSONObject parameters = new JSONObject() .put(RETURN_URL_KEY, successUrl) @@ -47,6 +66,8 @@ String createRequestBody(Configuration configuration, Authorization authorizatio parameters.put(DESCRIPTION_KEY, billingAgreementDescription); } + parameters.putOpt(PAYER_EMAIL_KEY, userAuthenticationEmail); + JSONObject experienceProfile = new JSONObject(); experienceProfile.put(NO_SHIPPING_KEY, !isShippingAddressRequired()); experienceProfile.put(LANDING_PAGE_TYPE_KEY, getLandingPageType()); diff --git a/PayPal/src/test/java/com/braintreepayments/api/PayPalVaultRequestUnitTest.java b/PayPal/src/test/java/com/braintreepayments/api/PayPalVaultRequestUnitTest.java index b4d07c7e71..af852b66cc 100644 --- a/PayPal/src/test/java/com/braintreepayments/api/PayPalVaultRequestUnitTest.java +++ b/PayPal/src/test/java/com/braintreepayments/api/PayPalVaultRequestUnitTest.java @@ -1,9 +1,12 @@ package com.braintreepayments.api; +import android.content.pm.Signature; import android.os.Parcel; +import org.json.JSONException; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.robolectric.RobolectricTestRunner; import java.util.ArrayList; @@ -12,6 +15,7 @@ import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; +import static org.mockito.Mockito.mock; @RunWith(RobolectricTestRunner.class) public class PayPalVaultRequestUnitTest { @@ -80,12 +84,12 @@ public void parcelsCorrectly() { assertEquals("en-US", result.getLocaleCode()); assertEquals("Billing Agreement Description", - result.getBillingAgreementDescription()); + result.getBillingAgreementDescription()); assertTrue(result.getShouldOfferCredit()); assertTrue(result.isShippingAddressRequired()); assertTrue(result.isShippingAddressEditable()); assertEquals("Postal Address", result.getShippingAddressOverride() - .getRecipientName()); + .getRecipientName()); assertEquals(PayPalRequest.LANDING_PAGE_TYPE_LOGIN, result.getLandingPageType()); assertEquals("Display Name", result.getDisplayName()); assertEquals("123-correlation", result.getRiskCorrelationId()); @@ -93,4 +97,20 @@ public void parcelsCorrectly() { assertEquals(1, result.getLineItems().size()); assertEquals("An Item", result.getLineItems().get(0).getName()); } + + @Test + public void createRequestBody_sets_userAuthenticationEmail_when_not_null() throws JSONException { + String payerEmail = "payer_email@example.com"; + PayPalVaultRequest request = new PayPalVaultRequest(); + + request.setUserAuthenticationEmail(payerEmail); + String requestBody = request.createRequestBody( + mock(Configuration.class), + mock(Authorization.class), + "success_url", + "cancel_url" + ); + + assertTrue(requestBody.contains("\"payer_email\":" + "\"" + payerEmail + "\"")); + } }