feat: Add support for listing saved payment methods in module and Stripe (#10994)

This commit is contained in:
Stevche Radevski
2025-01-16 16:16:04 +01:00
committed by GitHub
parent 114b2133aa
commit f99f720dd4
8 changed files with 171 additions and 0 deletions
@@ -2,6 +2,8 @@ import Stripe from "stripe"
import {
CreatePaymentProviderSession,
PaymentMethodResponse,
PaymentProviderContext,
PaymentProviderError,
PaymentProviderSessionResponse,
ProviderWebhookPayload,
@@ -86,6 +88,8 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
res.payment_method = extra?.payment_method as string | undefined
res.return_url = extra?.return_url as string | undefined
return res
}
@@ -294,6 +298,27 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
}
}
async listPaymentMethods(
context: PaymentProviderContext
): Promise<PaymentMethodResponse[]> {
const customerId = context.customer?.metadata?.stripe_id
if (!customerId) {
return []
}
const paymentMethods = await this.stripe_.customers.listPaymentMethods(
customerId as string,
// In order to keep the interface simple, we just list the maximum payment methods, which should be enough in almost all cases.
// We can always extend the interface to allow additional filtering, if necessary.
{ limit: 100 }
)
return paymentMethods.data.map((method) => ({
id: method.id,
data: method as unknown as Record<string, unknown>,
}))
}
async getWebhookActionAndData(
webhookData: ProviderWebhookPayload["payload"]
): Promise<WebhookActionResult> {