Skip to content

Commit

Permalink
Merge pull request #1252 from braintree/shipping-callback-feature
Browse files Browse the repository at this point in the history
[DO NOT REVIEW] Merge Shipping Callback Feature Branch
  • Loading branch information
jaxdesmarais authored Jan 21, 2025
2 parents 402d25a + 7a3b5c9 commit b192b70
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* PayPal
* Fix bug to ensure that `PayPalVaultRequest.userAuthenticationEmail` is not sent as an empty string
* Add `shippingCallbackUrl` to `PayPalCheckoutRequest`
* ThreeDSecure
* Return error if no `dfReferenceId` is returned in the 3D Secure flow

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.braintreepayments.api.paypal

import android.net.Uri
import android.text.TextUtils
import com.braintreepayments.api.core.Authorization
import com.braintreepayments.api.core.ClientToken
Expand Down Expand Up @@ -54,6 +55,9 @@ import org.json.JSONObject
*
* @property shouldOfferPayLater Offers PayPal Pay Later if the customer qualifies. Defaults to
* false.
* @property shippingCallbackUrl Server side shipping callback URL to be notified when a customer
* updates their shipping address or options. A callback request will be sent to the merchant server
* at this URL.
*/
@Parcelize
class PayPalCheckoutRequest @JvmOverloads constructor(
Expand All @@ -64,6 +68,7 @@ class PayPalCheckoutRequest @JvmOverloads constructor(
var currencyCode: String? = null,
var shouldRequestBillingAgreement: Boolean = false,
var shouldOfferPayLater: Boolean = false,
var shippingCallbackUrl: Uri? = null,
override var localeCode: String? = null,
override var billingAgreementDescription: String? = null,
override var isShippingAddressRequired: Boolean = false,
Expand All @@ -75,7 +80,7 @@ class PayPalCheckoutRequest @JvmOverloads constructor(
override var riskCorrelationId: String? = null,
override var userAuthenticationEmail: String? = null,
override var userPhoneNumber: PayPalPhoneNumber? = null,
override var lineItems: List<PayPalLineItem> = emptyList(),
override var lineItems: List<PayPalLineItem> = emptyList()
) : PayPalRequest(
hasUserLocationConsent = hasUserLocationConsent,
localeCode = localeCode,
Expand Down Expand Up @@ -105,6 +110,10 @@ class PayPalCheckoutRequest @JvmOverloads constructor(
.put(CANCEL_URL_KEY, cancelUrl)
.put(OFFER_PAY_LATER_KEY, shouldOfferPayLater)

shippingCallbackUrl?.let {
if (it.toString().isNotEmpty()) parameters.put(SHIPPING_CALLBACK_URL_KEY, it)
}

if (authorization is ClientToken) {
parameters.put(AUTHORIZATION_FINGERPRINT_KEY, authorization.bearer)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,6 @@ abstract class PayPalRequest internal constructor(
internal const val PLAN_TYPE_KEY: String = "plan_type"
internal const val PLAN_METADATA_KEY: String = "plan_metadata"
internal const val PHONE_NUMBER_KEY: String = "phone_number"
internal const val SHIPPING_CALLBACK_URL_KEY: String = "shipping_callback_url"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PayPalVaultRequest
override var riskCorrelationId: String? = null,
override var userAuthenticationEmail: String? = null,
override var userPhoneNumber: PayPalPhoneNumber? = null,
override var lineItems: List<PayPalLineItem> = emptyList(),
override var lineItems: List<PayPalLineItem> = emptyList()
) : PayPalRequest(
hasUserLocationConsent = hasUserLocationConsent,
localeCode = localeCode,
Expand All @@ -62,7 +62,7 @@ class PayPalVaultRequest
merchantAccountId = merchantAccountId,
riskCorrelationId = riskCorrelationId,
userAuthenticationEmail = userAuthenticationEmail,
lineItems = lineItems
lineItems = lineItems,
) {

@Throws(JSONException::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.braintreepayments.api.paypal;

import android.net.Uri;
import android.os.Parcel;

import com.braintreepayments.api.core.Authorization;
import com.braintreepayments.api.core.Configuration;
import com.braintreepayments.api.core.PostalAddress;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand Down Expand Up @@ -157,6 +159,58 @@ public void createRequestBody_does_not_set_userAuthenticationEmail_when_email_is
}

@Test
public void createRequestBody_sets_shippingCallbackUri_when_not_null() throws JSONException {
String urlString = "https://www.example.com/path";
Uri uri = Uri.parse(urlString);

PayPalCheckoutRequest request = new PayPalCheckoutRequest("1.00", true);
request.setShippingCallbackUrl(uri);

String requestBody = request.createRequestBody(
mock(Configuration.class),
mock(Authorization.class),
"success_url",
"cancel_url",
null
);

JSONObject jsonObject = new JSONObject(requestBody);
assertEquals(urlString, jsonObject.getString("shipping_callback_url"));
}

@Test
public void createRequestBody_does_not_set_shippingCallbackUri_when_null() throws JSONException {
PayPalCheckoutRequest request = new PayPalCheckoutRequest("1.00", true);

String requestBody = request.createRequestBody(
mock(Configuration.class),
mock(Authorization.class),
"success_url",
"cancel_url",
null
);

JSONObject jsonObject = new JSONObject(requestBody);
assertFalse(jsonObject.has("shipping_callback_url"));
}

@Test
public void createRequestBody_does_not_set_shippingCallbackUri_when_empty() throws JSONException {
PayPalCheckoutRequest request = new PayPalCheckoutRequest("1.00", true);
request.setShippingCallbackUrl(Uri.parse(""));

String requestBody = request.createRequestBody(
mock(Configuration.class),
mock(Authorization.class),
"success_url",
"cancel_url",
null
);

JSONObject jsonObject = new JSONObject(requestBody);
assertFalse(jsonObject.has("shipping_callback_url"));
}

public void createRequestBody_sets_userPhoneNumber_when_not_null() throws JSONException {
PayPalCheckoutRequest request = new PayPalCheckoutRequest("1.00", true);

Expand All @@ -171,4 +225,5 @@ public void createRequestBody_sets_userPhoneNumber_when_not_null() throws JSONEx

assertTrue(requestBody.contains("\"phone_number\":{\"country_code\":\"1\",\"national_number\":\"1231231234\"}"));
}
}
}

0 comments on commit b192b70

Please sign in to comment.