-
-
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): Add settlePayment mutation
Relates to #117
- Loading branch information
1 parent
c90a2a4
commit f2b9a12
Showing
19 changed files
with
468 additions
and
120 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
24 changes: 24 additions & 0 deletions
24
packages/core/src/event-bus/events/payment-state-transition-event.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,24 @@ | ||
import { RequestContext } from '../../api/common/request-context'; | ||
import { Order } from '../../entity/order/order.entity'; | ||
import { Payment } from '../../entity/payment/payment.entity'; | ||
import { PaymentState } from '../../service/helpers/payment-state-machine/payment-state'; | ||
import { VendureEvent } from '../vendure-event'; | ||
|
||
/** | ||
* @description | ||
* This event is fired whenever a {@link Payment} transitions from one {@link PaymentState} to another, e.g. | ||
* a Payment is authorized by the payment provider. | ||
* | ||
* @docsCategory events | ||
*/ | ||
export class PaymentStateTransitionEvent extends VendureEvent { | ||
constructor( | ||
public fromState: PaymentState, | ||
public toState: PaymentState, | ||
public ctx: RequestContext, | ||
public payment: Payment, | ||
public order: Order, | ||
) { | ||
super(); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
packages/core/src/service/helpers/payment-state-machine/payment-state-machine.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,46 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { RequestContext } from '../../../api/common/request-context'; | ||
import { IllegalOperationError } from '../../../common/error/errors'; | ||
import { FSM, StateMachineConfig } from '../../../common/finite-state-machine'; | ||
import { ConfigService } from '../../../config/config.service'; | ||
import { Order } from '../../../entity/order/order.entity'; | ||
import { Payment } from '../../../entity/payment/payment.entity'; | ||
import { EventBus } from '../../../event-bus/event-bus'; | ||
import { PaymentStateTransitionEvent } from '../../../event-bus/events/payment-state-transition-event'; | ||
|
||
import { PaymentState, paymentStateTransitions, PaymentTransitionData } from './payment-state'; | ||
|
||
@Injectable() | ||
export class PaymentStateMachine { | ||
|
||
private readonly config: StateMachineConfig<PaymentState, PaymentTransitionData> = { | ||
transitions: paymentStateTransitions, | ||
onTransitionStart: async (fromState, toState, data) => { | ||
return true; | ||
}, | ||
onTransitionEnd: (fromState, toState, data) => { | ||
this.eventBus.publish(new PaymentStateTransitionEvent(fromState, toState, data.ctx, data.payment, data.order)); | ||
}, | ||
onError: (fromState, toState, message) => { | ||
throw new IllegalOperationError(message || 'error.cannot-transition-order-from-to', { | ||
fromState, | ||
toState, | ||
}); | ||
}, | ||
}; | ||
|
||
constructor(private configService: ConfigService, | ||
private eventBus: EventBus) {} | ||
|
||
getNextStates(payment: Payment): PaymentState[] { | ||
const fsm = new FSM(this.config, payment.state); | ||
return fsm.getNextStates(); | ||
} | ||
|
||
async transition(ctx: RequestContext, order: Order, payment: Payment, state: PaymentState) { | ||
const fsm = new FSM(this.config, payment.state); | ||
await fsm.transitionTo(state, { ctx, order, payment }); | ||
payment.state = state; | ||
} | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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