Skip to content

Commit

Permalink
create getCurrentActivityOrResolveWithError method
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecruzan-stripe committed Apr 18, 2022
1 parent 07843fe commit 18b226a
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
private var initGooglePayPromise: Promise? = null
private var presentGooglePayPromise: Promise? = null

private val currentActivity: AppCompatActivity?
get() = (super.getCurrentActivity() as? AppCompatActivity)

private val mActivityEventListener = object : BaseActivityEventListener() {
override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) {
if (::stripe.isInitialized) {
Expand Down Expand Up @@ -239,7 +236,7 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
@ReactMethod
@SuppressWarnings("unused")
fun initPaymentSheet(params: ReadableMap, promise: Promise) {
currentActivity?.let { activity ->
getCurrentActivityOrResolveWithError(promise)?.let { activity ->
this.initPaymentSheetPromise = promise

paymentSheetFragment = PaymentSheetFragment().also {
Expand All @@ -249,8 +246,6 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
activity.supportFragmentManager.beginTransaction()
.add(paymentSheetFragment!!, "payment_sheet_launch_fragment")
.commit()
} ?: run {
promise.resolve(createMissingActivityError())
}
}

Expand All @@ -269,14 +264,12 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
}

private fun payWithFpx() {
currentActivity?.let {
getCurrentActivityOrResolveWithError(confirmPromise)?.let {
AddPaymentMethodActivityStarter(it)
.startForResult(AddPaymentMethodActivityStarter.Args.Builder()
.setPaymentMethodType(PaymentMethod.Type.Fpx)
.build()
)
} ?: run {
confirmPromise?.resolve(createMissingActivityError())
}
}

Expand Down Expand Up @@ -562,7 +555,7 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
@ReactMethod
@SuppressWarnings("unused")
fun isGooglePaySupported(params: ReadableMap?, promise: Promise) {
currentActivity?.let {
getCurrentActivityOrResolveWithError(promise)?.let {
val fragment = GooglePayPaymentMethodLauncherFragment(
it,
getBooleanOrFalse(params, "testEnv"),
Expand All @@ -573,8 +566,6 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
it.supportFragmentManager.beginTransaction()
.add(fragment, "google_pay_support_fragment")
.commit()
} ?: run {
promise.resolve(createMissingActivityError())
}
}

Expand All @@ -586,14 +577,12 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
it.arguments = bundle
}

currentActivity?.let {
getCurrentActivityOrResolveWithError(promise)?.let {
initGooglePayPromise = promise

it.supportFragmentManager.beginTransaction()
.add(googlePayFragment!!, "google_pay_launch_fragment")
.commit()
} ?: run {
promise.resolve(createMissingActivityError())
}
}

Expand Down

0 comments on commit 18b226a

Please sign in to comment.