Skip to content

Commit

Permalink
update after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecruzan-stripe committed Apr 18, 2022
1 parent ae046a1 commit 369587b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 com.stripe.android.model.PaymentIntent
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.StripeIntent
Expand All @@ -16,7 +17,7 @@ import com.stripe.android.payments.bankaccount.CollectBankAccountLauncher
import com.stripe.android.payments.bankaccount.navigation.CollectBankAccountResult

class CollectBankAccountLauncherFragment(
private val activity: AppCompatActivity,
private val context: ReactApplicationContext,
private val publishableKey: String,
private val clientSecret: String,
private val isPaymentIntent: Boolean,
Expand Down Expand Up @@ -77,7 +78,7 @@ class CollectBankAccountLauncherFragment(
promise.resolve(createError(ErrorType.Failed.toString(), result.error))
}
}
activity.supportFragmentManager.beginTransaction().remove(this).commit()
(context.currentActivity as? AppCompatActivity)?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ 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 com.stripe.android.googlepaylauncher.GooglePayEnvironment
import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncher

class GooglePayPaymentMethodLauncherFragment(
private val activity: AppCompatActivity,
private val context: ReactApplicationContext,
private val isTestEnv: Boolean,
private val paymentMethodRequired: Boolean,
private val promise: Promise
Expand All @@ -36,7 +37,7 @@ class GooglePayPaymentMethodLauncherFragment(
),
readyCallback = {
promise.resolve(it)
activity.supportFragmentManager.beginTransaction().remove(this).commit()
(context.currentActivity as? AppCompatActivity)?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
},
resultCallback = {}
)
Expand Down
65 changes: 40 additions & 25 deletions android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@ReactModule(name = StripeSdkModule.NAME)
class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
class StripeSdkModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
var cardFieldView: CardFieldView? = null
var cardFormView: CardFormView? = null

Expand Down Expand Up @@ -216,21 +216,23 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
PaymentConfiguration.init(reactApplicationContext, publishableKey, stripeAccountId)

paymentLauncherFragment = PaymentLauncherFragment(stripe, publishableKey, stripeAccountId)
(currentActivity as AppCompatActivity).supportFragmentManager.beginTransaction()
.add(paymentLauncherFragment, "payment_launcher_fragment")
.commit()
getCurrentActivityOrResolveWithError(promise)?.let {
it.supportFragmentManager.beginTransaction()
.add(paymentLauncherFragment, "payment_launcher_fragment")
.commit()

val localBroadcastManager = LocalBroadcastManager.getInstance(reactApplicationContext)
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_PAYMENT_RESULT_ACTION))
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_PAYMENT_OPTION_ACTION))
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_CONFIGURE_FLOW_CONTROLLER))
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_INIT_PAYMENT_SHEET))
val localBroadcastManager = LocalBroadcastManager.getInstance(reactApplicationContext)
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_PAYMENT_RESULT_ACTION))
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_PAYMENT_OPTION_ACTION))
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_CONFIGURE_FLOW_CONTROLLER))
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_INIT_PAYMENT_SHEET))

localBroadcastManager.registerReceiver(googlePayReceiver, IntentFilter(ON_INIT_GOOGLE_PAY))
localBroadcastManager.registerReceiver(googlePayReceiver, IntentFilter(ON_GOOGLE_PAY_RESULT))
localBroadcastManager.registerReceiver(googlePayReceiver, IntentFilter(ON_GOOGLE_PAYMENT_METHOD_RESULT))
localBroadcastManager.registerReceiver(googlePayReceiver, IntentFilter(ON_INIT_GOOGLE_PAY))
localBroadcastManager.registerReceiver(googlePayReceiver, IntentFilter(ON_GOOGLE_PAY_RESULT))
localBroadcastManager.registerReceiver(googlePayReceiver, IntentFilter(ON_GOOGLE_PAYMENT_METHOD_RESULT))

promise.resolve(null)
promise.resolve(null)
}
}

@ReactMethod
Expand Down Expand Up @@ -555,14 +557,14 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
@ReactMethod
@SuppressWarnings("unused")
fun isGooglePaySupported(params: ReadableMap?, promise: Promise) {
getCurrentActivityOrResolveWithError(promise)?.let {
val fragment = GooglePayPaymentMethodLauncherFragment(
it,
getBooleanOrFalse(params, "testEnv"),
getBooleanOrFalse(params, "existingPaymentMethodRequired"),
promise
)
val fragment = GooglePayPaymentMethodLauncherFragment(
reactContext,
getBooleanOrFalse(params, "testEnv"),
getBooleanOrFalse(params, "existingPaymentMethodRequired"),
promise
)

getCurrentActivityOrResolveWithError(promise)?.let {
it.supportFragmentManager.beginTransaction()
.add(fragment, "google_pay_support_fragment")
.commit()
Expand Down Expand Up @@ -643,17 +645,18 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
)

val fragment = CollectBankAccountLauncherFragment(
currentActivity as AppCompatActivity,
reactContext,
publishableKey,
clientSecret,
isPaymentIntent,
collectParams,
promise
)

(currentActivity as AppCompatActivity).supportFragmentManager.beginTransaction()
.add(fragment, "collect_bank_account_launcher_fragment")
.commit()
getCurrentActivityOrResolveWithError(promise)?.let {
it.supportFragmentManager.beginTransaction()
.add(fragment, "collect_bank_account_launcher_fragment")
.commit()
}
}

@ReactMethod
Expand Down Expand Up @@ -724,6 +727,18 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
}
}

/**
* Safely get and cast the current activity as an AppCompatActivity. If that fails, the promise
* provided will be resolved with an error message instructing the user to retry the method.
*/
private fun getCurrentActivityOrResolveWithError(promise: Promise?): AppCompatActivity? {
(currentActivity as? AppCompatActivity)?.let {
return it
}
promise?.resolve(createMissingActivityError())
return null
}

companion object {
const val NAME = "StripeSdk"
}
Expand Down

0 comments on commit 369587b

Please sign in to comment.