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

feat: ⚡ Update payment details after calling initPaymentSheet #1653

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ class PaymentSheetFragment(
flowController?.confirm()
}

fun updateIntentConfiguration(bundle: Bundle, promise: Promise){
val onFlowControllerConfigure = PaymentSheet.FlowController.ConfigCallback { _, _ ->
val result = flowController?.getPaymentOption()?.let {
val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
val imageString = getBase64FromBitmap(bitmap)
val option: WritableMap = WritableNativeMap()
option.putString("label", it.label)
option.putString("image", imageString)
createResult("paymentOption", option)
} ?: run {
WritableNativeMap()
}
promise.resolve(result)
}
Comment on lines +296 to +308
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this is copied from above, maybe we can extract this out to a helper function?


this.intentConfiguration = buildIntentConfiguration(bundle);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.intentConfiguration = buildIntentConfiguration(bundle);
this.intentConfiguration = buildIntentConfiguration(bundle);

this.flowController?.configureWithIntentConfiguration(
intentConfiguration = this.intentConfiguration!!,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildIntentConfiguration returns a nullable value so this !! seems dangerous, maybe we can add some null-handling before this

configuration = paymentSheetConfiguration,
callback = onFlowControllerConfigure
);
}

private fun configureFlowController() {
val onFlowControllerConfigure = PaymentSheet.FlowController.ConfigCallback { _, _ ->
val result = flowController?.getPaymentOption()?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ class StripeSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
}
}

@ReactMethod
fun updatePaymentSheet(params: ReadableMap, promise: Promise){
if (paymentSheetFragment == null) {
promise.resolve(PaymentSheetFragment.createMissingInitError())
return
}
val bundle = toBundleObject(params);
paymentSheetFragment?.updateIntentConfiguration(bundle,promise);
}

@ReactMethod
fun presentPaymentSheet(options: ReadableMap, promise: Promise) {
if (paymentSheetFragment == null) {
Expand Down
5 changes: 3 additions & 2 deletions example/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,15 @@ app.post('/payment-intent-for-payment-sheet', async (req, res) => {

try {
const paymentIntent = await stripe.paymentIntents.create({
amount: 5099,
currency: 'usd',
amount: req.body.amount || 5099,
currency: req.body.currency || 'usd',
payment_method: req.body.paymentMethodId,
customer: req.body.customerId,
});

return res.send({ clientSecret: paymentIntent.client_secret });
} catch (e) {
console.log(e);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove console.log here and in the other introduced locations

return res.send({ error: e });
}
});
Expand Down
6 changes: 6 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import CollectBankAccountScreen from './screens/CollectBankAccountScreen';
import CashAppScreen from './screens/CashAppScreen';
import PaymentSheetDeferredIntentScreen from './screens/PaymentSheetDeferredIntentScreen';
import PaymentSheetDeferredIntentMultiStepScreen from './screens/PaymentSheetDeferredIntentMultiStepScreen';
import PaymentSheetDeferredIntentMultiStepScreenWithUpdates from './screens/PaymentSheetDeferredIntentMultiStepScreenWithUpdates';
import CustomerSheetScreen from './screens/CustomerSheetScreen';
import RevolutPayScreen from './screens/RevolutPayScreen';

Expand Down Expand Up @@ -89,6 +90,7 @@ export type RootStackParamList = {
CollectBankAccountScreen: undefined;
PaymentSheetDeferredIntentScreen: undefined;
PaymentSheetDeferredIntentMultiStepScreen: undefined;
PaymentSheetDeferredIntentMultiStepScreenWithUpdates: undefined;
CustomerSheetScreen: undefined;
RevolutPayScreen: undefined;
};
Expand Down Expand Up @@ -165,6 +167,10 @@ export default function App() {
name="PaymentSheetDeferredIntentMultiStepScreen"
component={PaymentSheetDeferredIntentMultiStepScreen}
/>
<Stack.Screen
name="PaymentSheetDeferredIntentMultiStepScreenWithUpdates"
component={PaymentSheetDeferredIntentMultiStepScreenWithUpdates}
/>
<Stack.Screen
name="PaymentsUICustomScreen"
component={PaymentsUICustomScreen}
Expand Down
10 changes: 10 additions & 0 deletions example/src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ export default function HomeScreen() {
}}
/>
</View>
<View style={styles.buttonContainer}>
<Button
title="Prebuilt UI (multi-step) (deferred intent) with updates"
onPress={() => {
navigation.navigate(
'PaymentSheetDeferredIntentMultiStepScreenWithUpdates'
);
}}
/>
</View>
<View style={styles.buttonContainer}>
<Button
title="Customer Sheet"
Expand Down
Loading