Skip to content

Commit 59f7152

Browse files
Merge pull request #2659 from appwrite/fix-captcha-payment
Fix: setup intent might return payment method in different format
2 parents 707019a + e6f8578 commit 59f7152

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/lib/stores/stripe.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,32 @@ export async function submitStripeCard(name: string, organizationId?: string) {
102102
}
103103

104104
if (setupIntent && setupIntent.status === 'succeeded') {
105-
if ((setupIntent.payment_method as PaymentMethod).card?.country === 'US') {
105+
const pm = setupIntent.payment_method as PaymentMethod | string | undefined;
106+
// If Stripe returned an expanded PaymentMethod object, check the card country.
107+
// If it returned a string id (common), `typeof pm === 'string'` and we skip this.
108+
if (typeof pm !== 'string' && pm?.card?.country === 'US') {
106109
// need to get state
107-
return setupIntent.payment_method as PaymentMethod;
110+
return pm as PaymentMethod;
108111
}
112+
113+
// The backend expects a provider method ID (string). Extract the id
114+
// whether Stripe returned the id string or an expanded object.
115+
let providerId: string | undefined;
116+
if (typeof pm === 'string') {
117+
providerId = pm;
118+
} else {
119+
providerId = (pm as PaymentMethod)?.id;
120+
}
121+
122+
if (!providerId) {
123+
const e = new Error('Unable to verify payment method.');
124+
trackError(e, Submit.PaymentMethodCreate);
125+
throw e;
126+
}
127+
109128
const method = await sdk.forConsole.billing.setPaymentMethod(
110129
paymentMethod.$id,
111-
(setupIntent.payment_method as PaymentMethod).id,
130+
providerId,
112131
name
113132
);
114133
paymentElement.destroy();

0 commit comments

Comments
 (0)