Skip to main content

PaymentService

PaymentService

Contains methods relating to Payment entities.

Signature
class PaymentService {
constructor(connection: TransactionalConnection, paymentStateMachine: PaymentStateMachine, refundStateMachine: RefundStateMachine, paymentMethodService: PaymentMethodService, eventBus: EventBus)
create(ctx: RequestContext, input: DeepPartial<Payment>) => Promise<Payment>;
findOneOrThrow(ctx: RequestContext, id: ID, relations: string[] = ['order']) => Promise<Payment>;
transitionToState(ctx: RequestContext, paymentId: ID, state: PaymentState) => Promise<Payment | PaymentStateTransitionError>;
getNextStates(payment: Payment) => readonly PaymentState[];
createPayment(ctx: RequestContext, order: Order, amount: number, method: string, metadata: any) => Promise<Payment | IneligiblePaymentMethodError>;
settlePayment(ctx: RequestContext, paymentId: ID) => Promise<PaymentStateTransitionError | Payment>;
cancelPayment(ctx: RequestContext, paymentId: ID) => Promise<PaymentStateTransitionError | Payment>;
createManualPayment(ctx: RequestContext, order: Order, amount: number, input: ManualPaymentInput) => ;
createRefund(ctx: RequestContext, input: RefundOrderInput, order: Order, selectedPayment: Payment) => Promise<Refund | RefundStateTransitionError | RefundAmountError>;
}

constructor

method
(connection: TransactionalConnection, paymentStateMachine: PaymentStateMachine, refundStateMachine: RefundStateMachine, paymentMethodService: PaymentMethodService, eventBus: EventBus) => PaymentService

create

method
(ctx: RequestContext, input: DeepPartial<Payment>) => Promise<Payment>

findOneOrThrow

method
(ctx: RequestContext, id: ID, relations: string[] = ['order']) => Promise<Payment>

transitionToState

method
(ctx: RequestContext, paymentId: ID, state: PaymentState) => Promise<Payment | PaymentStateTransitionError>

Transitions a Payment to the given state.

When updating a Payment in the context of an Order, it is preferable to use the OrderService transitionPaymentToState() method, which will also handle updating the Order state too.

getNextStates

method
(payment: Payment) => readonly PaymentState[]

createPayment

method
(ctx: RequestContext, order: Order, amount: number, method: string, metadata: any) => Promise<Payment | IneligiblePaymentMethodError>

Creates a new Payment.

When creating a Payment in the context of an Order, it is preferable to use the OrderService addPaymentToOrder() method, which will also handle updating the Order state too.

settlePayment

method
(ctx: RequestContext, paymentId: ID) => Promise<PaymentStateTransitionError | Payment>

Settles a Payment.

When settling a Payment in the context of an Order, it is preferable to use the OrderService settlePayment() method, which will also handle updating the Order state too.

cancelPayment

method
(ctx: RequestContext, paymentId: ID) => Promise<PaymentStateTransitionError | Payment>

createManualPayment

method
(ctx: RequestContext, order: Order, amount: number, input: ManualPaymentInput) =>

Creates a Payment from the manual payment mutation in the Admin API

When creating a manual Payment in the context of an Order, it is preferable to use the OrderService addManualPaymentToOrder() method, which will also handle updating the Order state too.

createRefund

method
(ctx: RequestContext, input: RefundOrderInput, order: Order, selectedPayment: Payment) => Promise<Refund | RefundStateTransitionError | RefundAmountError>

Creates a Refund against the specified Payment. If the amount to be refunded exceeds the value of the specified Payment (in the case of multiple payments on a single Order), then the remaining outstanding refund amount will be refunded against the next available Payment from the Order.

When creating a Refund in the context of an Order, it is preferable to use the OrderService refundOrder() method, which performs additional validation.