-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
CollectBankAccountLauncherFragment.kt
96 lines (88 loc) · 3.74 KB
/
CollectBankAccountLauncherFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package versioned.host.exp.exponent.modules.api.components.reactnativestripesdk
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import versioned.host.exp.exponent.modules.api.components.reactnativestripesdk.utils.*
import versioned.host.exp.exponent.modules.api.components.reactnativestripesdk.utils.createError
import versioned.host.exp.exponent.modules.api.components.reactnativestripesdk.utils.createResult
import versioned.host.exp.exponent.modules.api.components.reactnativestripesdk.utils.mapFromPaymentIntentResult
import versioned.host.exp.exponent.modules.api.components.reactnativestripesdk.utils.mapFromSetupIntentResult
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.StripeIntent
import com.stripe.android.payments.bankaccount.CollectBankAccountConfiguration
import com.stripe.android.payments.bankaccount.CollectBankAccountLauncher
import com.stripe.android.payments.bankaccount.navigation.CollectBankAccountResult
class CollectBankAccountLauncherFragment(
private val context: ReactApplicationContext,
private val publishableKey: String,
private val stripeAccountId: String?,
private val clientSecret: String,
private val isPaymentIntent: Boolean,
private val collectParams: CollectBankAccountConfiguration.USBankAccount,
private val promise: Promise
) : Fragment() {
private lateinit var collectBankAccountLauncher: CollectBankAccountLauncher
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
collectBankAccountLauncher = createBankAccountLauncher()
return FrameLayout(requireActivity()).also {
it.visibility = View.GONE
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(
view,
savedInstanceState)
if (isPaymentIntent) {
collectBankAccountLauncher.presentWithPaymentIntent(
publishableKey,
stripeAccountId,
clientSecret,
collectParams
)
} else {
collectBankAccountLauncher.presentWithSetupIntent(
publishableKey,
stripeAccountId,
clientSecret,
collectParams
)
}
}
private fun createBankAccountLauncher(): CollectBankAccountLauncher {
return CollectBankAccountLauncher.create(this) { result ->
when (result) {
is CollectBankAccountResult.Completed -> {
val intent = result.response.intent
if (intent.status === StripeIntent.Status.RequiresPaymentMethod) {
promise.resolve(createError(ErrorType.Canceled.toString(), "Bank account collection was canceled."))
} else if (intent.status === StripeIntent.Status.RequiresConfirmation) {
promise.resolve(
if (isPaymentIntent)
createResult("paymentIntent", mapFromPaymentIntentResult(intent as PaymentIntent))
else
createResult("setupIntent", mapFromSetupIntentResult(intent as SetupIntent))
)
}
}
is CollectBankAccountResult.Cancelled -> {
promise.resolve(createError(ErrorType.Canceled.toString(), "Bank account collection was canceled."))
}
is CollectBankAccountResult.Failed -> {
promise.resolve(createError(ErrorType.Failed.toString(), result.error))
}
}
removeFragment(context)
}
}
companion object {
const val TAG = "collect_bank_account_launcher_fragment"
}
}