Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Expose `createCustomerSession` as a public suspend function
* Expose `updateCustomerSession` as a public suspend function
* Expose `generateCustomerRecommendations` as a public suspend function
* Add `CustomerSessionRequest.payPalCampaigns` to associate PayPal campaigns with a customer session
* ThreeDSecure
* Expose `createPaymentAuthRequest` as a public suspend function
* Expose `prepareLookup` as a public suspend function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.braintreepayments.api.core.ExperimentalBetaApi
* @property payPalAppInstalled If the PayPal app is installed on the device.
* @property venmoAppInstalled If the Venmo app is installed on the device.
* @property purchaseUnits List of purchase units containing the amount and currency code.
* @property payPalCampaigns List of PayPal co-marketing campaigns to associate with the session.
*
* Warning: This feature is in beta. It's public API may change or be removed in future releases.
*/
Expand All @@ -20,4 +21,5 @@ data class CustomerSessionRequest(
var payPalAppInstalled: Boolean? = null,
var venmoAppInstalled: Boolean? = null,
var purchaseUnits: List<PurchaseUnit>? = null,
var payPalCampaigns: List<PayPalCampaign>? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.braintreepayments.api.shopperinsights.v2

import com.braintreepayments.api.core.ExperimentalBetaApi

/**
* A PayPal co-marketing campaign to associate with a customer session.
*
* @property id The PayPal-assigned campaign identifier.
*/
@ExperimentalBetaApi
data class PayPalCampaign(val id: String)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CustomerSessionRequestBuilder {
internal data class JsonRequestObjects(
val customer: JSONObject,
val purchaseUnits: JSONArray?,
val payPalCampaigns: JSONArray? = null,
)

internal fun createRequestObjects(customerSessionRequest: CustomerSessionRequest): JsonRequestObjects {
Expand Down Expand Up @@ -42,7 +43,17 @@ class CustomerSessionRequestBuilder {
}
}

return JsonRequestObjects(customer, purchaseUnits)
val payPalCampaigns = customerSessionRequest.payPalCampaigns
?.takeIf { it.isNotEmpty() }
?.let { campaigns ->
JSONArray().apply {
campaigns.forEach { campaign ->
put(JSONObject().put(CAMPAIGN_ID, campaign.id))
}
}
}

return JsonRequestObjects(customer, purchaseUnits, payPalCampaigns)
}

private companion object {
Expand All @@ -53,5 +64,6 @@ class CustomerSessionRequestBuilder {
private const val AMOUNT = "amount"
private const val VALUE = "value"
private const val CURRENCY_CODE = "currencyCode"
private const val CAMPAIGN_ID = "id"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.braintreepayments.api.shopperinsights.v2.internal

import com.braintreepayments.api.core.ExperimentalBetaApi
import com.braintreepayments.api.shopperinsights.v2.CustomerSessionRequest
import com.braintreepayments.api.shopperinsights.v2.PayPalCampaign
import com.braintreepayments.api.shopperinsights.v2.PurchaseUnit
import org.json.JSONArray
import org.json.JSONObject
Expand Down Expand Up @@ -76,5 +77,34 @@ class CustomerSessionRequestBuilderUnitTest {

JSONAssert.assertEquals(expectedCustomer, result.customer, false)
assertNull(result.purchaseUnits)
assertNull(result.payPalCampaigns)
}

@Test
fun `createRequestObjects builds correct JSON array when payPalCampaigns are provided`() {
val customerSessionRequest = CustomerSessionRequest(
payPalCampaigns = listOf(
PayPalCampaign(id = "campaign-1"),
PayPalCampaign(id = "campaign-2")
)
)

val result = requestBuilder.createRequestObjects(customerSessionRequest)

val expectedCampaigns = JSONArray().apply {
put(JSONObject().put("id", "campaign-1"))
put(JSONObject().put("id", "campaign-2"))
}

JSONAssert.assertEquals(expectedCampaigns, result.payPalCampaigns, true)
}

@Test
fun `createRequestObjects returns null payPalCampaigns when list is empty`() {
Comment thread
santugowda marked this conversation as resolved.
Outdated
val customerSessionRequest = CustomerSessionRequest(payPalCampaigns = emptyList())

val result = requestBuilder.createRequestObjects(customerSessionRequest)

assertNull(result.payPalCampaigns)
}
}