@@ -35,17 +35,18 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
3535import okhttp3.RequestBody.Companion.toRequestBody
3636import org.apache.commons.io.IOUtils
3737import java.io.IOException
38+ import java.lang.IllegalStateException
3839import java.math.BigDecimal
3940import java.nio.charset.Charset
4041import java.text.NumberFormat
4142import java.util.*
43+ import java.util.concurrent.CancellationException
4244
4345class PayoneInputView @JvmOverloads constructor(context : Context , attrs : AttributeSet ? = null , defStyleAttr : Int = 0 ) : FrameLayout(context, attrs, defStyleAttr), LifecycleObserver {
44- private var acceptedKeyguard = false
4546 private lateinit var webView: WebView
4647 private lateinit var progressBar: ProgressBar
4748 private var isActivityResumed = false
48- private lateinit var creditCardInfo: CreditCardInfo
49+ private var creditCardInfo: CreditCardInfo ? = null
4950 private lateinit var paymentType: PaymentMethod
5051 private lateinit var project: Project
5152 private lateinit var tokenizationData: PayoneTokenizationData
@@ -64,17 +65,21 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
6465 when (it.status) {
6566 Payone .AuthStatus .pending -> doLater(1000 )
6667 Payone .AuthStatus .successful -> {
67- creditCardInfo.userId = it.userID
68- authenticateAndSave()
68+ creditCardInfo?.let { cardInfo ->
69+ cardInfo.userId = it.userID
70+ authenticateAndSave(cardInfo)
71+ } ? : finishWithError()
6972 }
7073 Payone .AuthStatus .failed -> finishWithError()
7174 }
7275 } ? : error(null )
7376 }
7477
7578 override fun error (t : Throwable ? ) {
76- t?.printStackTrace()
77- doLater(1000 )
79+ t?.let {
80+ IllegalStateException (" Error while saving card" , it).printStackTrace()
81+ }
82+ if (t !is CancellationException ) doLater(1000 )
7883 }
7984 })
8085 }
@@ -235,7 +240,7 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
235240 fun Any.toJsonRequest () : RequestBody =
236241 GsonHolder .get().toJson(this ).toRequestBody(" application/json" .toMediaTypeOrNull())
237242
238- private fun authenticate () {
243+ private fun authenticate (creditCardInfo : CreditCardInfo ) {
239244 val req = Payone .PreAuthRequest (creditCardInfo.pseudocardpan, creditCardInfo.lastname)
240245 val request = Request .Builder ()
241246 .url(Snabble .getInstance().absoluteUrl(Snabble .getInstance().absoluteUrl(tokenizationData.links[" preAuth" ]?.href)))
@@ -263,23 +268,21 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
263268 }
264269
265270
266- private fun authenticateAndSave () {
271+ private fun authenticateAndSave (creditCardInfo : CreditCardInfo ) {
267272 Keyguard .unlock(UIUtils .getHostFragmentActivity(context), object : Keyguard .Callback {
268273 override fun success () {
269274 save(creditCardInfo)
270275 }
271276
272277 override fun error () {
273- if (isShown) {
274- finish()
275- } else {
276- acceptedKeyguard = true
277- }
278+ this @PayoneInputView.creditCardInfo = null
279+ finish()
278280 }
279281 })
280282 }
281283
282284 private fun save (info : CreditCardInfo ) {
285+ creditCardInfo = null
283286 val ccBrand = when (info.cardtype) {
284287 " V" -> PaymentCredentials .Brand .VISA
285288 " M" -> PaymentCredentials .Brand .MASTERCARD
@@ -302,11 +305,7 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
302305 Snabble .getInstance().paymentCredentialsStore.add(pc)
303306 Telemetry .event(Telemetry .Event .PaymentMethodAdded , pc.type.name)
304307 }
305- if (isShown) {
306- finish()
307- } else {
308- acceptedKeyguard = true
309- }
308+ finish()
310309 }
311310
312311 private fun finish () {
@@ -334,7 +333,7 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
334333 @OnLifecycleEvent(Lifecycle .Event .ON_RESUME )
335334 fun onResume () {
336335 isActivityResumed = true
337- if (this @PayoneInputView:: creditCardInfo.isInitialized ) polling.doNow()
336+ if (creditCardInfo != null ) polling.doNow()
338337 }
339338
340339 @OnLifecycleEvent(Lifecycle .Event .ON_PAUSE )
@@ -366,15 +365,17 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
366365 cardexpiredate : String? ,
367366 lastname : String?
368367 ) {
369- creditCardInfo = CreditCardInfo (
368+ CreditCardInfo (
370369 pseudocardpan = requireNotNull(pseudocardpan),
371370 truncatedcardpan = requireNotNull(truncatedcardpan),
372371 cardtype = requireNotNull(cardtype),
373372 cardexpiredate = requireNotNull(cardexpiredate),
374373 lastname = requireNotNull(lastname),
375- )
376- if (isActivityResumed) {
377- Dispatch .mainThread { authenticate() }
374+ ).let { cardInfo ->
375+ creditCardInfo = cardInfo
376+ if (isActivityResumed) {
377+ Dispatch .mainThread { authenticate(cardInfo) }
378+ }
378379 }
379380 }
380381
0 commit comments