Skip to content

Commit b116f75

Browse files
authored
fix(payment): Return and set the correct status when a session is created with stripe (medusajs#12769)
1 parent a263d88 commit b116f75

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

packages/core/types/src/payment/provider.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ export interface UpdateAccountHolderInput extends PaymentProviderInput {
190190
/**
191191
* The data to delete an account holder.
192192
*/
193-
export interface DeleteAccountHolderInput extends Omit<PaymentProviderInput, "context"> {
193+
export interface DeleteAccountHolderInput
194+
extends Omit<PaymentProviderInput, "context"> {
194195
/**
195196
* The context of deleting the account holder.
196197
*/
@@ -237,6 +238,10 @@ export interface InitiatePaymentOutput extends PaymentProviderOutput {
237238
* The ID of the payment session in the payment provider.
238239
*/
239240
id: string
241+
/**
242+
* The status of the payment session, which will be stored in the payment session's `status` field.
243+
*/
244+
status?: PaymentSessionStatus
240245
}
241246

242247
/**
@@ -305,12 +310,15 @@ export interface DeleteAccountHolderOutput extends PaymentProviderOutput {}
305310
/**
306311
* The result of listing payment methods for an account holder in the third-party payment provider.
307312
*/
308-
export interface ListPaymentMethodsOutput extends Array<PaymentProviderOutput & {
309-
/**
310-
* The ID of the payment method in the payment provider.
311-
*/
312-
id: string
313-
}> {}
313+
export interface ListPaymentMethodsOutput
314+
extends Array<
315+
PaymentProviderOutput & {
316+
/**
317+
* The ID of the payment method in the payment provider.
318+
*/
319+
id: string
320+
}
321+
> {}
314322

315323
/**
316324
* The result of saving a payment method.

packages/modules/payment/src/services/payment-module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ export default class PaymentModuleService
363363
{
364364
id: paymentSession!.id,
365365
data: { ...input.data, ...providerPaymentSession.data },
366+
status: providerPaymentSession.status ?? PaymentSessionStatus.PENDING,
366367
},
367368
sharedContext
368369
)

packages/modules/providers/payment-stripe/src/core/stripe-base.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
215215
}
216216
}
217217

218-
async getPaymentStatus({
219-
data,
220-
}: GetPaymentStatusInput): Promise<GetPaymentStatusOutput> {
221-
const id = data?.id as string
218+
async getPaymentStatus(
219+
input: GetPaymentStatusInput
220+
): Promise<GetPaymentStatusOutput> {
221+
const id = input?.data?.id as string
222222
if (!id) {
223223
throw this.buildError(
224224
"No payment intent ID provided while getting payment status",
@@ -227,30 +227,11 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
227227
}
228228

229229
const paymentIntent = await this.stripe_.paymentIntents.retrieve(id)
230-
const dataResponse = paymentIntent as unknown as Record<string, unknown>
230+
const statusResponse = this.getStatus(paymentIntent)
231231

232-
switch (paymentIntent.status) {
233-
case "requires_payment_method":
234-
if (paymentIntent.last_payment_error) {
235-
return { status: PaymentSessionStatus.ERROR, data: dataResponse }
236-
}
237-
return { status: PaymentSessionStatus.PENDING, data: dataResponse }
238-
case "requires_confirmation":
239-
case "processing":
240-
return { status: PaymentSessionStatus.PENDING, data: dataResponse }
241-
case "requires_action":
242-
return {
243-
status: PaymentSessionStatus.REQUIRES_MORE,
244-
data: dataResponse,
245-
}
246-
case "canceled":
247-
return { status: PaymentSessionStatus.CANCELED, data: dataResponse }
248-
case "requires_capture":
249-
return { status: PaymentSessionStatus.AUTHORIZED, data: dataResponse }
250-
case "succeeded":
251-
return { status: PaymentSessionStatus.CAPTURED, data: dataResponse }
252-
default:
253-
return { status: PaymentSessionStatus.PENDING, data: dataResponse }
232+
return {
233+
status: statusResponse.status,
234+
data: statusResponse.data as unknown as Record<string, unknown>,
254235
}
255236
}
256237

@@ -281,15 +262,15 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
281262
const isPaymentIntent = "id" in sessionData
282263
return {
283264
id: isPaymentIntent ? sessionData.id : (data?.session_id as string),
265+
status: isPaymentIntent ? this.getStatus(sessionData).status : undefined,
284266
data: sessionData as unknown as Record<string, unknown>,
285267
}
286268
}
287269

288270
async authorizePayment(
289271
input: AuthorizePaymentInput
290272
): Promise<AuthorizePaymentOutput> {
291-
const statusResponse = await this.getPaymentStatus(input)
292-
return statusResponse
273+
return this.getPaymentStatus(input)
293274
}
294275

295276
async cancelPayment({
@@ -605,6 +586,34 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
605586
return { id: resp.id, data: resp as unknown as Record<string, unknown> }
606587
}
607588

589+
private getStatus(
590+
paymentIntent: Stripe.PaymentIntent
591+
): Omit<GetPaymentStatusOutput, "data"> & { data: Stripe.PaymentIntent } {
592+
switch (paymentIntent.status) {
593+
case "requires_payment_method":
594+
if (paymentIntent.last_payment_error) {
595+
return { status: PaymentSessionStatus.ERROR, data: paymentIntent }
596+
}
597+
return { status: PaymentSessionStatus.PENDING, data: paymentIntent }
598+
case "requires_confirmation":
599+
case "processing":
600+
return { status: PaymentSessionStatus.PENDING, data: paymentIntent }
601+
case "requires_action":
602+
return {
603+
status: PaymentSessionStatus.REQUIRES_MORE,
604+
data: paymentIntent,
605+
}
606+
case "canceled":
607+
return { status: PaymentSessionStatus.CANCELED, data: paymentIntent }
608+
case "requires_capture":
609+
return { status: PaymentSessionStatus.AUTHORIZED, data: paymentIntent }
610+
case "succeeded":
611+
return { status: PaymentSessionStatus.CAPTURED, data: paymentIntent }
612+
default:
613+
return { status: PaymentSessionStatus.PENDING, data: paymentIntent }
614+
}
615+
}
616+
608617
async getWebhookActionAndData(
609618
webhookData: ProviderWebhookPayload["payload"]
610619
): Promise<WebhookActionResult> {

0 commit comments

Comments
 (0)