Skip to content

Commit 4e9dd1d

Browse files
committed
fix: Allow setting the status of a payment session when updating
1 parent 474e97c commit 4e9dd1d

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

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

Lines changed: 6 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,
@@ -238,6 +238,11 @@ export interface UpdatePaymentSessionDTO {
238238
*/
239239
amount: BigNumberInput
240240

241+
/**
242+
* The status of the payment session.
243+
*/
244+
status?: PaymentSessionStatus
245+
241246
/**
242247
* Necessary context data for the associated payment provider.
243248
*/

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export default class PaymentModuleService
415415
): Promise<PaymentSessionDTO> {
416416
const session = await this.paymentSessionService_.retrieve(
417417
data.id,
418-
{ select: ["id", "data", "provider_id"] },
418+
{ select: ["id", "status", "data", "provider_id"] },
419419
sharedContext
420420
)
421421

@@ -435,6 +435,8 @@ export default class PaymentModuleService
435435
amount: data.amount,
436436
currency_code: data.currency_code,
437437
data: providerData.data,
438+
// Allow the caller to explicitly set the status (eg. due to a webhook), fallback to the update response, and finally to the existing status.
439+
status: data.status ?? providerData.status ?? session.status,
438440
},
439441
sharedContext
440442
)

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)