Skip to content

Commit

Permalink
fix(core): Publish PaymentStateTransitionEvent when settlePayment fails
Browse files Browse the repository at this point in the history
Fixes #886
  • Loading branch information
michaelbromley committed May 25, 2021
1 parent fce00c4 commit c01106c
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions packages/core/src/service/services/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,28 @@ export class PaymentService {
payment,
paymentMethod.handler.args,
);
const fromState = payment.state;
let toState: PaymentState;
payment.metadata = this.mergePaymentMetadata(payment.metadata, settlePaymentResult.metadata);
if (settlePaymentResult.success) {
const fromState = payment.state;
const toState = 'Settled';
try {
await this.paymentStateMachine.transition(ctx, payment.order, payment, toState);
} catch (e) {
const transitionError = ctx.translate(e.message, { fromState, toState });
return new PaymentStateTransitionError(transitionError, fromState, toState);
}
payment.metadata = this.mergePaymentMetadata(payment.metadata, settlePaymentResult.metadata);
await this.connection.getRepository(ctx, Payment).save(payment, { reload: false });
this.eventBus.publish(
new PaymentStateTransitionEvent(fromState, toState, ctx, payment, payment.order),
);
toState = 'Settled';
} else {
toState = settlePaymentResult.state || 'Error';
payment.errorMessage = settlePaymentResult.errorMessage;
payment.metadata = this.mergePaymentMetadata(payment.metadata, settlePaymentResult.metadata);
await this.paymentStateMachine.transition(
ctx,
payment.order,
payment,
settlePaymentResult.state || 'Error',
);
await this.connection.getRepository(ctx, Payment).save(payment, { reload: false });
}
try {
await this.paymentStateMachine.transition(ctx, payment.order, payment, toState);
} catch (e) {
const transitionError = ctx.translate(e.message, { fromState, toState });
return new PaymentStateTransitionError(transitionError, fromState, toState);
}
await this.connection.getRepository(ctx, Payment).save(payment, { reload: false });
this.eventBus.publish(
new PaymentStateTransitionEvent(fromState, toState, ctx, payment, payment.order),
);
return payment;
}

/**
* Creates a Payment from the manual payment mutation in the Admin API
*/
Expand Down Expand Up @@ -202,6 +197,7 @@ export class PaymentService {
const nonFailedRefunds = payment.refunds?.filter(refund => refund.state !== 'Failed') ?? [];
return summate(nonFailedRefunds, 'total');
}

const existingNonFailedRefunds =
orderWithRefunds.payments
?.reduce((refunds, p) => [...refunds, ...p.refunds], [] as Refund[])
Expand Down

0 comments on commit c01106c

Please sign in to comment.