Skip to content

Commit

Permalink
fix: assign googlePayFragment on instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecruzan-stripe committed Mar 16, 2022
1 parent 5d36911 commit 2098a9e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ class GooglePayFragment : Fragment() {
readyCallback = ::onGooglePayLauncherReady,
resultCallback = ::onGooglePayResult
)

val intent = Intent(ON_GOOGLE_PAY_FRAGMENT_CREATED)
localBroadcastManager.sendBroadcast(intent)
}

fun presentForPaymentIntent(clientSecret: String) {
Expand Down
18 changes: 8 additions & 10 deletions android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,17 @@ class StripeSdkModule(private val reactContext: ReactApplicationContext) : React

private val googlePayReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
if (intent.action == ON_GOOGLE_PAY_FRAGMENT_CREATED) {
currentActivity?.let {
googlePayFragment = it.supportFragmentManager.findFragmentByTag("google_pay_launch_fragment") as GooglePayFragment
}
}
if (intent.action == ON_INIT_GOOGLE_PAY) {
val isReady = intent.extras?.getBoolean("isReady") ?: false

if (isReady) {
initGooglePayPromise?.resolve(WritableNativeMap())
} else {
initGooglePayPromise?.resolve(createError(GooglePayErrorType.Failed.toString(), "Google Pay is not available on this device"))
initGooglePayPromise?.resolve(
createError(
GooglePayErrorType.Failed.toString(),
"Google Pay is not available on this device. You can use isGooglePaySupported to preemptively check for Google Pay support."
)
)
}
}
if (intent.action == ON_GOOGLE_PAYMENT_METHOD_RESULT) {
Expand Down Expand Up @@ -305,7 +304,6 @@ class StripeSdkModule(private val reactContext: ReactApplicationContext) : React
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_CONFIGURE_FLOW_CONTROLLER));
localBroadcastManager.registerReceiver(mPaymentSheetReceiver, IntentFilter(ON_INIT_PAYMENT_SHEET));

localBroadcastManager.registerReceiver(googlePayReceiver, IntentFilter(ON_GOOGLE_PAY_FRAGMENT_CREATED))
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))
Expand Down Expand Up @@ -641,7 +639,7 @@ class StripeSdkModule(private val reactContext: ReactApplicationContext) : React

@ReactMethod
fun initGooglePay(params: ReadableMap, promise: Promise) {
val fragment = GooglePayFragment().also {
googlePayFragment = GooglePayFragment().also {
val bundle = toBundleObject(params)
it.arguments = bundle
}
Expand All @@ -650,7 +648,7 @@ class StripeSdkModule(private val reactContext: ReactApplicationContext) : React
initGooglePayPromise = promise

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

0 comments on commit 2098a9e

Please sign in to comment.