-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Extract hard-coded payment state & process
This commit makes it possible to completely configure the payment process by extracting all transition validation and allowing the developer to replace with custom logic.
- Loading branch information
1 parent
cdb2b75
commit 4c5c946
Showing
8 changed files
with
123 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/core/src/config/payment/default-payment-process.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { HistoryEntryType } from '@vendure/common/lib/generated-types'; | ||
|
||
import { awaitPromiseOrObservable, Transitions } from '../../common/index'; | ||
import { FulfillmentState, PaymentState } from '../../service/index'; | ||
|
||
import { PaymentProcess } from './payment-process'; | ||
|
||
declare module '../../service/helpers/payment-state-machine/payment-state' { | ||
interface PaymentStates { | ||
Authorized: never; | ||
Settled: never; | ||
Declined: never; | ||
} | ||
} | ||
|
||
let configService: import('../config.service').ConfigService; | ||
let historyService: import('../../service/index').HistoryService; | ||
|
||
/** | ||
* @description | ||
* The default {@link PaymentProcess} | ||
* | ||
* @docsCategory payment | ||
*/ | ||
export const defaultPaymentProcess: PaymentProcess<PaymentState> = { | ||
transitions: { | ||
Created: { | ||
to: ['Authorized', 'Settled', 'Declined', 'Error', 'Cancelled'], | ||
}, | ||
Authorized: { | ||
to: ['Settled', 'Error', 'Cancelled'], | ||
}, | ||
Settled: { | ||
to: ['Cancelled'], | ||
}, | ||
Declined: { | ||
to: ['Cancelled'], | ||
}, | ||
Error: { | ||
to: ['Cancelled'], | ||
}, | ||
Cancelled: { | ||
to: [], | ||
}, | ||
}, | ||
async init(injector) { | ||
// Lazily import these services to avoid a circular dependency error | ||
// due to this being used as part of the DefaultConfig | ||
const ConfigService = await import('../config.service').then(m => m.ConfigService); | ||
const HistoryService = await import('../../service/index').then(m => m.HistoryService); | ||
configService = injector.get(ConfigService); | ||
historyService = injector.get(HistoryService); | ||
}, | ||
async onTransitionStart(fromState, toState, data) { | ||
// nothing here by default | ||
}, | ||
async onTransitionEnd(fromState, toState, data) { | ||
await historyService.createHistoryEntryForOrder({ | ||
ctx: data.ctx, | ||
orderId: data.order.id, | ||
type: HistoryEntryType.ORDER_PAYMENT_TRANSITION, | ||
data: { | ||
paymentId: data.payment.id, | ||
from: fromState, | ||
to: toState, | ||
}, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters