Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sdk_uuid for payment and setup intents #551

Merged
merged 12 commits into from
Oct 16, 2023
Prev Previous commit
Next Next commit
android updates to use uuid for SetupIntent
  • Loading branch information
ianlin-bbpos committed Oct 7, 2023
commit ccb961154958aef2a8f1d57f45772adeb882d12f
Original file line number Diff line number Diff line change
@@ -157,7 +157,7 @@ internal fun mapFromPaymentIntent(paymentIntent: PaymentIntent, uuid: String): R
putString("sdk_uuid", uuid)
}

internal fun mapFromSetupIntent(setupIntent: SetupIntent): ReadableMap = nativeMapOf {
internal fun mapFromSetupIntent(setupIntent: SetupIntent, uuid: String): ReadableMap = nativeMapOf {
putString("created", convertToUnixTimestamp(setupIntent.created))
putString("id", setupIntent.id)
putString("status", mapFromSetupIntentStatus(setupIntent.status))
@@ -170,6 +170,7 @@ internal fun mapFromSetupIntent(setupIntent: SetupIntent): ReadableMap = nativeM
putString("onBehalfOfId", setupIntent.onBehalfOfId)
putString("paymentMethodId", setupIntent.paymentMethodId)
putString("singleUseMandateId", setupIntent.singleUseMandateId)
putString("sdk_uuid", uuid)
}

internal fun mapFromSetupAttempt(attempt: SetupAttempt?): ReadableMap? = attempt?.let {
Original file line number Diff line number Diff line change
@@ -491,16 +491,18 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
SetupIntentParameters.Builder().setCustomer(customerId).build()
} ?: SetupIntentParameters.NULL

terminal.createSetupIntent(intentParams, RNSetupIntentCallback(promise) {
setupIntents[it.id] = it
val uuid = UUID.randomUUID().toString()
terminal.createSetupIntent(intentParams, RNSetupIntentCallback(promise, uuid) {
setupIntents[uuid] = it
})
}

@ReactMethod
@Suppress("unused")
fun retrieveSetupIntent(clientSecret: String, promise: Promise) {
terminal.retrieveSetupIntent(clientSecret, RNSetupIntentCallback(promise) {
setupIntents[it.id] = it
val uuid = UUID.randomUUID().toString()
terminal.retrieveSetupIntent(clientSecret, RNSetupIntentCallback(promise, uuid) {
setupIntents[uuid] = it
})
}

@@ -531,19 +533,25 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
@Suppress("unused")
fun collectSetupIntentPaymentMethod(params: ReadableMap, promise: Promise) =
withExceptionResolver(promise) {
val setupIntentId = params.getString("setupIntentId")
val setupIntentJson = requireParam(params.getMap("setupIntent")) {
"You must provide a setupIntent"
}
val uuid = requireParam(setupIntentJson.getString("sdk_uuid")) {
"You must provide a sdk_uuid"
}
val setupIntent = requireParam(setupIntents[uuid]) {
"There is no associated setupIntent with id $uuid"
}

val customerConsentCollected = getBoolean(params, "customerConsentCollected")

val setupIntent = requireParam(setupIntents[setupIntentId]) {
"There is no created setupIntent with id $setupIntentId"
}
collectSetupIntentCancelable = terminal.collectSetupIntentPaymentMethod(
setupIntent,
customerConsentCollected,
SetupIntentConfiguration.Builder()
.setEnableCustomerCancellation(false)
.build(),
RNSetupIntentCallback(promise) { setupIntents[it.id] = it }
RNSetupIntentCallback(promise, uuid) { setupIntents[uuid] = it }
)
}

@@ -588,28 +596,34 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :

@ReactMethod
@Suppress("unused")
fun cancelSetupIntent(setupIntentId: String, promise: Promise) =
fun cancelSetupIntent(setupIntent: ReadableMap, promise: Promise) =
withExceptionResolver(promise) {
val setupIntent = requireParam(setupIntents[setupIntentId]) {
"There is no associated setupIntent with id $setupIntentId"
val uuid = requireParam(setupIntent.getString("sdk_uuid")) {
"You must provide a sdk_uuid"
}
val setupIntent = requireParam(setupIntents[uuid]) {
"There is no associated setupIntent with id $uuid"
}

val params = SetupIntentCancellationParameters.Builder().build()

terminal.cancelSetupIntent(setupIntent, params, RNSetupIntentCallback(promise) {
terminal.cancelSetupIntent(setupIntent, params, RNSetupIntentCallback(promise, uuid) {
setupIntents[setupIntent.id] = null
})
}

@ReactMethod
@Suppress("unused")
fun confirmSetupIntent(setupIntentId: String, promise: Promise) =
fun confirmSetupIntent(setupIntent: ReadableMap, promise: Promise) =
withExceptionResolver(promise) {
val setupIntent = requireParam(setupIntents[setupIntentId]) {
"There is no associated setupIntent with id $setupIntentId"
val uuid = requireParam(setupIntent.getString("sdk_uuid")) {
"You must provide a sdk_uuid"
}
val setupIntent = requireParam(setupIntents[uuid]) {
"There is no associated setupIntent with id $uuid"
}

terminal.confirmSetupIntent(setupIntent, RNSetupIntentCallback(promise) {
terminal.confirmSetupIntent(setupIntent, RNSetupIntentCallback(promise, uuid) {
setupIntents[it.id] = null
})
}
Original file line number Diff line number Diff line change
@@ -10,13 +10,14 @@ import com.stripeterminalreactnative.nativeMapOf

class RNSetupIntentCallback(
private val promise: Promise,
private val uuid: String,
private val onSetupIntentSuccess: (SetupIntent) -> Unit = {}
): SetupIntentCallback {

override fun onSuccess(setupIntent: SetupIntent) {
onSetupIntentSuccess(setupIntent)
promise.resolve(nativeMapOf {
putMap("setupIntent", mapFromSetupIntent(setupIntent))
putMap("setupIntent", mapFromSetupIntent(setupIntent, uuid))
})
}