@@ -15,6 +15,11 @@ import com.braintreepayments.api.shopperinsights.ShopperInsightsAnalytics.BUTTON
1515import com.braintreepayments.api.shopperinsights.v2.internal.CreateCustomerSessionApi
1616import com.braintreepayments.api.shopperinsights.v2.internal.UpdateCustomerSessionApi
1717import com.braintreepayments.api.shopperinsights.v2.internal.GenerateCustomerRecommendationsApi
18+ import com.braintreepayments.api.shopperinsights.v2.internal.GenerateCustomerRecommendationsApi.GenerateCustomerRecommendationsResult
19+ import kotlinx.coroutines.CoroutineDispatcher
20+ import kotlinx.coroutines.CoroutineScope
21+ import kotlinx.coroutines.Dispatchers
22+ import kotlinx.coroutines.launch
1823
1924/* *
2025 * Use [ShopperInsightsClientV2] to optimize your checkout experience by prioritizing the customer’s preferred payment
@@ -36,7 +41,9 @@ class ShopperInsightsClientV2 internal constructor(
3641 private val generateCustomerRecommendationsApi : GenerateCustomerRecommendationsApi =
3742 GenerateCustomerRecommendationsApi (braintreeClient),
3843 private val deviceInspector : DeviceInspector = DeviceInspectorProvider ().deviceInspector,
39- lazyAnalyticsClient : Lazy <AnalyticsClient > = AnalyticsClient .lazyInstance
44+ lazyAnalyticsClient : Lazy <AnalyticsClient > = AnalyticsClient .lazyInstance,
45+ private val mainDispatcher : CoroutineDispatcher = Dispatchers .Main ,
46+ private val coroutineScope : CoroutineScope = CoroutineScope (mainDispatcher)
4047) {
4148
4249 /* *
@@ -64,18 +71,27 @@ class ShopperInsightsClientV2 internal constructor(
6471 customerSessionRequest : CustomerSessionRequest ,
6572 customerSessionCallback : (customerSessionResult: CustomerSessionResult ) -> Unit
6673 ) {
74+ coroutineScope.launch {
75+ val result = createCustomerSession(customerSessionRequest)
76+ customerSessionCallback(result)
77+ }
78+ }
79+
80+ private suspend fun createCustomerSession (
81+ customerSessionRequest : CustomerSessionRequest
82+ ): CustomerSessionResult {
6783 analyticsClient.sendEvent(ShopperInsightsAnalytics .CREATE_CUSTOMER_SESSION_STARTED )
68- createCustomerSessionApi.execute(customerSessionRequest) { createCustomerSessionResult ->
69- when ( createCustomerSessionResult) {
70- is CreateCustomerSessionApi . CreateCustomerSessionResult . Success -> {
71- analyticsClient.sendEvent( ShopperInsightsAnalytics . CREATE_CUSTOMER_SESSION_SUCCEEDED )
72- customerSessionCallback( CustomerSessionResult . Success (createCustomerSessionResult.sessionId) )
73- }
74-
75- is CreateCustomerSessionApi . CreateCustomerSessionResult . Error -> {
76- analyticsClient.sendEvent( ShopperInsightsAnalytics . CREATE_CUSTOMER_SESSION_FAILED )
77- customerSessionCallback( CustomerSessionResult . Failure (createCustomerSessionResult.error) )
78- }
84+ return when (
85+ val createCustomerSessionResult = createCustomerSessionApi.execute(customerSessionRequest)
86+ ) {
87+ is CreateCustomerSessionApi . CreateCustomerSessionResult . Success -> {
88+ analyticsClient.sendEvent( ShopperInsightsAnalytics . CREATE_CUSTOMER_SESSION_SUCCEEDED )
89+ CustomerSessionResult . Success (createCustomerSessionResult.sessionId)
90+ }
91+
92+ is CreateCustomerSessionApi . CreateCustomerSessionResult . Error -> {
93+ analyticsClient.sendEvent( ShopperInsightsAnalytics . CREATE_CUSTOMER_SESSION_FAILED )
94+ CustomerSessionResult . Failure (createCustomerSessionResult.error)
7995 }
8096 }
8197 }
@@ -94,18 +110,28 @@ class ShopperInsightsClientV2 internal constructor(
94110 sessionId : String ,
95111 customerSessionCallback : (customerSessionResult: CustomerSessionResult ) -> Unit
96112 ) {
113+ coroutineScope.launch {
114+ val result = updateCustomerSession(customerSessionRequest, sessionId)
115+ customerSessionCallback(result)
116+ }
117+ }
118+
119+ private suspend fun updateCustomerSession (
120+ customerSessionRequest : CustomerSessionRequest ,
121+ sessionId : String
122+ ): CustomerSessionResult {
97123 analyticsClient.sendEvent(ShopperInsightsAnalytics .UPDATE_CUSTOMER_SESSION_STARTED )
98- updateCustomerSessionApi.execute(customerSessionRequest, sessionId) { result ->
99- when (result) {
100- is UpdateCustomerSessionApi . UpdateCustomerSessionResult . Success -> {
101- analyticsClient.sendEvent( ShopperInsightsAnalytics . UPDATE_CUSTOMER_SESSION_SUCCEEDED )
102- customerSessionCallback( CustomerSessionResult . Success (result.sessionId) )
103- }
104-
105- is UpdateCustomerSessionApi . UpdateCustomerSessionResult . Error -> {
106- analyticsClient.sendEvent( ShopperInsightsAnalytics . UPDATE_CUSTOMER_SESSION_FAILED )
107- customerSessionCallback( CustomerSessionResult . Failure (result.error) )
108- }
124+ return when (
125+ val updateCustomerSessionResult = updateCustomerSessionApi.execute(customerSessionRequest, sessionId)
126+ ) {
127+ is UpdateCustomerSessionApi . UpdateCustomerSessionResult . Success -> {
128+ analyticsClient.sendEvent( ShopperInsightsAnalytics . UPDATE_CUSTOMER_SESSION_SUCCEEDED )
129+ CustomerSessionResult . Success (updateCustomerSessionResult.sessionId)
130+ }
131+
132+ is UpdateCustomerSessionApi . UpdateCustomerSessionResult . Error -> {
133+ analyticsClient.sendEvent( ShopperInsightsAnalytics . UPDATE_CUSTOMER_SESSION_FAILED )
134+ CustomerSessionResult . Failure (updateCustomerSessionResult.error)
109135 }
110136 }
111137 }
@@ -125,28 +151,30 @@ class ShopperInsightsClientV2 internal constructor(
125151 sessionId : String? = null,
126152 customerRecommendationsCallback : (customerRecommendationsResult: CustomerRecommendationsResult ) -> Unit
127153 ) {
154+ coroutineScope.launch {
155+ val result = generateCustomerRecommendations(customerSessionRequest, sessionId)
156+ customerRecommendationsCallback(result)
157+ }
158+ }
159+
160+ private suspend fun generateCustomerRecommendations (
161+ customerSessionRequest : CustomerSessionRequest ? = null,
162+ sessionId : String? = null,
163+ ): CustomerRecommendationsResult {
128164 analyticsClient.sendEvent(ShopperInsightsAnalytics .GET_CUSTOMER_RECOMMENDATIONS_STARTED )
129- generateCustomerRecommendationsApi.execute(customerSessionRequest, sessionId) {
130- generateCustomerRecommendationsResult ->
131- when (generateCustomerRecommendationsResult) {
132- is GenerateCustomerRecommendationsApi .GenerateCustomerRecommendationsResult .Success -> {
133- analyticsClient.sendEvent(ShopperInsightsAnalytics .GET_CUSTOMER_RECOMMENDATIONS_SUCCEEDED )
134- customerRecommendationsCallback(
135- CustomerRecommendationsResult .Success (
136- generateCustomerRecommendationsResult.customerRecommendations
137- )
138- )
139- }
140-
141- is GenerateCustomerRecommendationsApi .GenerateCustomerRecommendationsResult .Error -> {
142- analyticsClient.sendEvent(ShopperInsightsAnalytics .GET_CUSTOMER_RECOMMENDATIONS_FAILED )
143- customerRecommendationsCallback(
144- CustomerRecommendationsResult .Failure (
145- generateCustomerRecommendationsResult.error
146- )
147- )
148- }
149- }
165+ return when (
166+ val generateCustomerRecommendationsResult =
167+ generateCustomerRecommendationsApi.execute(customerSessionRequest, sessionId)
168+ ) {
169+ is GenerateCustomerRecommendationsResult .Success -> {
170+ analyticsClient.sendEvent(ShopperInsightsAnalytics .GET_CUSTOMER_RECOMMENDATIONS_SUCCEEDED )
171+ CustomerRecommendationsResult .Success (generateCustomerRecommendationsResult.customerRecommendations)
172+ }
173+
174+ is GenerateCustomerRecommendationsResult .Error -> {
175+ analyticsClient.sendEvent(ShopperInsightsAnalytics .GET_CUSTOMER_RECOMMENDATIONS_FAILED )
176+ CustomerRecommendationsResult .Failure (generateCustomerRecommendationsResult.error)
177+ }
150178 }
151179 }
152180
0 commit comments