@@ -2,13 +2,15 @@ package com.woocommerce.android.ui.blaze
2
2
3
3
import android.os.Parcelable
4
4
import com.woocommerce.android.OnChangedException
5
+ import com.woocommerce.android.model.CreditCardType
5
6
import com.woocommerce.android.tools.SelectedSite
6
7
import com.woocommerce.android.ui.products.ProductDetailRepository
7
8
import com.woocommerce.android.util.TimezoneProvider
8
9
import com.woocommerce.android.util.WooLog
9
10
import kotlinx.coroutines.flow.map
10
11
import kotlinx.parcelize.Parcelize
11
12
import org.wordpress.android.fluxc.model.blaze.BlazeAdSuggestion
13
+ import org.wordpress.android.fluxc.model.blaze.BlazePaymentMethod.PaymentMethodInfo
12
14
import org.wordpress.android.fluxc.store.blaze.BlazeCampaignsStore
13
15
import java.util.Date
14
16
import javax.inject.Inject
@@ -39,6 +41,7 @@ class BlazeRepository @Inject constructor(
39
41
WooLog .w(WooLog .T .BLAZE , " Failed to fetch languages: ${result.error} " )
40
42
Result .failure(OnChangedException (result.error))
41
43
}
44
+
42
45
else -> Result .success(Unit )
43
46
}
44
47
}
@@ -54,6 +57,7 @@ class BlazeRepository @Inject constructor(
54
57
WooLog .w(WooLog .T .BLAZE , " Failed to fetch devices: ${result.error} " )
55
58
Result .failure(OnChangedException (result.error))
56
59
}
60
+
57
61
else -> Result .success(Unit )
58
62
}
59
63
}
@@ -69,6 +73,7 @@ class BlazeRepository @Inject constructor(
69
73
WooLog .w(WooLog .T .BLAZE , " Failed to fetch interests: ${result.error} " )
70
74
Result .failure(OnChangedException (result.error))
71
75
}
76
+
72
77
else -> Result .success(Unit )
73
78
}
74
79
}
@@ -84,6 +89,7 @@ class BlazeRepository @Inject constructor(
84
89
WooLog .w(WooLog .T .BLAZE , " Failed to fetch locations: ${result.error} " )
85
90
Result .failure(OnChangedException (result.error))
86
91
}
92
+
87
93
else -> Result .success(
88
94
result.model?.map { location ->
89
95
Location (location.id, location.name, location.parent?.name, location.type)
@@ -106,6 +112,7 @@ class BlazeRepository @Inject constructor(
106
112
WooLog .w(WooLog .T .BLAZE , " Failed to fetch ad suggestions: ${result.error} " )
107
113
Result .failure(OnChangedException (result.error))
108
114
}
115
+
109
116
else -> Result .success(result.model?.mapToUiModel() ? : emptyList())
110
117
}
111
118
}
@@ -123,6 +130,48 @@ class BlazeRepository @Inject constructor(
123
130
)
124
131
}
125
132
133
+ suspend fun fetchPaymentMethods (): Result <PaymentMethodsData > {
134
+ val result = blazeCampaignsStore.fetchBlazePaymentMethods(selectedSite.get())
135
+
136
+ return when {
137
+ result.isError -> {
138
+ WooLog .w(WooLog .T .BLAZE , " Failed to fetch payment methods: ${result.error} " )
139
+ Result .failure(OnChangedException (result.error))
140
+ }
141
+
142
+ else -> result.model?.let { paymentMethods ->
143
+ Result .success(
144
+ PaymentMethodsData (
145
+ savedPaymentMethods = paymentMethods.savedPaymentMethods.map { paymentMethod ->
146
+ PaymentMethod (
147
+ id = paymentMethod.id,
148
+ name = paymentMethod.name,
149
+ info = when (paymentMethod.info) {
150
+ is PaymentMethodInfo .CreditCardInfo ->
151
+ (paymentMethod.info as PaymentMethodInfo .CreditCardInfo ).let {
152
+ PaymentMethod .PaymentMethodInfo .CreditCard (
153
+ creditCardType = CreditCardType .fromString(it.type),
154
+ cardHolderName = it.cardHolderName
155
+ )
156
+ }
157
+
158
+ PaymentMethodInfo .Unknown -> {
159
+ PaymentMethod .PaymentMethodInfo .Unknown
160
+ }
161
+ }
162
+ )
163
+ },
164
+ addPaymentMethodUrls = PaymentMethodUrls (
165
+ formUrl = paymentMethods.addPaymentMethodUrls.formUrl,
166
+ successUrl = paymentMethods.addPaymentMethodUrls.successUrl,
167
+ idUrlParameter = paymentMethods.addPaymentMethodUrls.idUrlParameter
168
+ )
169
+ )
170
+ )
171
+ } ? : Result .failure(NullPointerException (" API response is null" ))
172
+ }
173
+ }
174
+
126
175
@Parcelize
127
176
data class CampaignPreview (
128
177
val productId : Long ,
@@ -146,6 +195,37 @@ class BlazeRepository @Inject constructor(
146
195
val durationInDays : Int ,
147
196
val startDate : Date ,
148
197
) : Parcelable
198
+
199
+ @Parcelize
200
+ data class PaymentMethodsData (
201
+ val savedPaymentMethods : List <PaymentMethod >,
202
+ val addPaymentMethodUrls : PaymentMethodUrls
203
+ ) : Parcelable
204
+
205
+ @Parcelize
206
+ data class PaymentMethod (
207
+ val id : String ,
208
+ val name : String ,
209
+ val info : PaymentMethodInfo
210
+ ) : Parcelable {
211
+ sealed interface PaymentMethodInfo : Parcelable {
212
+ @Parcelize
213
+ data class CreditCard (
214
+ val creditCardType : CreditCardType ,
215
+ val cardHolderName : String
216
+ ) : PaymentMethodInfo
217
+
218
+ @Parcelize
219
+ data object Unknown : PaymentMethodInfo
220
+ }
221
+ }
222
+
223
+ @Parcelize
224
+ data class PaymentMethodUrls (
225
+ val formUrl : String ,
226
+ val successUrl : String ,
227
+ val idUrlParameter : String
228
+ ) : Parcelable
149
229
}
150
230
151
231
@Parcelize
0 commit comments