Skip to content

Commit c0807f5

Browse files
authored
fix: Allow setting the status of a payment session when updating (medusajs#12809)
1 parent 474e97c commit c0807f5

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

packages/core/core-flows/src/payment-collection/steps/create-payment-session.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export interface CreatePaymentSessionStepInput {
3838
* Learn more in [this documentation](https://docs.medusajs.com/resources/commerce-modules/payment/payment-session#data-property).
3939
*/
4040
data?: Record<string, unknown>
41+
42+
/**
43+
* Holds custom data in key-value pairs.
44+
*/
45+
metadata?: Record<string, unknown>
4146
}
4247

4348
export const createPaymentSessionStepId = "create-payment-session"
@@ -57,6 +62,7 @@ export const createPaymentSessionStep = createStep(
5762
amount: input.amount,
5863
data: input.data ?? {},
5964
context: input.context,
65+
metadata: input.metadata ?? {},
6066
}
6167
)
6268

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ export interface PaymentSessionDTO {
581581
* @expandable
582582
*/
583583
payment?: PaymentDTO
584+
585+
/**
586+
* Holds custom data in key-value pairs.
587+
*/
588+
metadata?: Record<string, unknown>
584589
}
585590

586591
/**

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BigNumberInput } from "../totals"
2-
import { PaymentCollectionStatus } from "./common"
2+
import { PaymentCollectionStatus, PaymentSessionStatus } from "./common"
33
import {
44
PaymentAccountHolderDTO,
55
PaymentCustomerDTO,
@@ -212,6 +212,11 @@ export interface CreatePaymentSessionDTO {
212212
* Necessary context data for the associated payment provider.
213213
*/
214214
context?: PaymentProviderContext
215+
216+
/**
217+
* Holds custom data in key-value pairs.
218+
*/
219+
metadata?: Record<string, unknown>
215220
}
216221

217222
/**
@@ -238,10 +243,20 @@ export interface UpdatePaymentSessionDTO {
238243
*/
239244
amount: BigNumberInput
240245

246+
/**
247+
* The status of the payment session.
248+
*/
249+
status?: PaymentSessionStatus
250+
241251
/**
242252
* Necessary context data for the associated payment provider.
243253
*/
244254
context?: PaymentProviderContext
255+
256+
/**
257+
* Holds custom data in key-value pairs.
258+
*/
259+
metadata?: Record<string, unknown>
245260
}
246261

247262
/**

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,12 @@ export interface AuthorizePaymentOutput extends PaymentProviderOutput {
257257
/**
258258
* The result of updating a payment.
259259
*/
260-
export interface UpdatePaymentOutput extends PaymentProviderOutput {}
260+
export interface UpdatePaymentOutput extends PaymentProviderOutput {
261+
/**
262+
* The status of the payment, which will be stored in the payment session's `status` field.
263+
*/
264+
status?: PaymentSessionStatus
265+
}
261266

262267
/**
263268
* The result of deleting a payment.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ export default class PaymentModuleService
401401
currency_code: data.currency_code,
402402
context: data.context,
403403
data: data.data,
404+
metadata: data.metadata,
404405
},
405406
sharedContext
406407
)
@@ -415,7 +416,7 @@ export default class PaymentModuleService
415416
): Promise<PaymentSessionDTO> {
416417
const session = await this.paymentSessionService_.retrieve(
417418
data.id,
418-
{ select: ["id", "data", "provider_id"] },
419+
{ select: ["id", "status", "data", "provider_id"] },
419420
sharedContext
420421
)
421422

@@ -435,6 +436,9 @@ export default class PaymentModuleService
435436
amount: data.amount,
436437
currency_code: data.currency_code,
437438
data: providerData.data,
439+
// Allow the caller to explicitly set the status (eg. due to a webhook), fallback to the update response, and finally to the existing status.
440+
status: data.status ?? providerData.status ?? session.status,
441+
metadata: data.metadata,
438442
},
439443
sharedContext
440444
)

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
229229
const paymentIntent = await this.stripe_.paymentIntents.retrieve(id)
230230
const statusResponse = this.getStatus(paymentIntent)
231231

232-
return {
233-
status: statusResponse.status,
234-
data: statusResponse.data as unknown as Record<string, unknown>,
235-
}
232+
return statusResponse as unknown as GetPaymentStatusOutput
236233
}
237234

238235
async initiatePayment({
@@ -262,8 +259,9 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
262259
const isPaymentIntent = "id" in sessionData
263260
return {
264261
id: isPaymentIntent ? sessionData.id : (data?.session_id as string),
265-
status: isPaymentIntent ? this.getStatus(sessionData).status : undefined,
266-
data: sessionData as unknown as Record<string, unknown>,
262+
...(this.getStatus(
263+
sessionData as unknown as Stripe.PaymentIntent
264+
) as unknown as Pick<InitiatePaymentOutput, "data" | "status">),
267265
}
268266
}
269267

@@ -377,7 +375,9 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
377375
}: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
378376
const amountNumeric = getSmallestUnit(amount, currency_code)
379377
if (isPresent(amount) && data?.amount === amountNumeric) {
380-
return { data }
378+
return this.getStatus(
379+
data as unknown as Stripe.PaymentIntent
380+
) as unknown as UpdatePaymentOutput
381381
}
382382

383383
try {
@@ -392,7 +392,9 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
392392
}
393393
)) as unknown as Record<string, unknown>
394394

395-
return { data: sessionData }
395+
return this.getStatus(
396+
sessionData as unknown as Stripe.PaymentIntent
397+
) as unknown as UpdatePaymentOutput
396398
} catch (e) {
397399
throw this.buildError("An error occurred in updatePayment", e)
398400
}
@@ -586,9 +588,10 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
586588
return { id: resp.id, data: resp as unknown as Record<string, unknown> }
587589
}
588590

589-
private getStatus(
590-
paymentIntent: Stripe.PaymentIntent
591-
): Omit<GetPaymentStatusOutput, "data"> & { data: Stripe.PaymentIntent } {
591+
private getStatus(paymentIntent: Stripe.PaymentIntent): {
592+
data: Stripe.PaymentIntent
593+
status: PaymentSessionStatus
594+
} {
592595
switch (paymentIntent.status) {
593596
case "requires_payment_method":
594597
if (paymentIntent.last_payment_error) {

0 commit comments

Comments
 (0)