Skip to content

Commit

Permalink
fix(payments-plugin): Idempotent 'paid' Mollie webhooks (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvdbrug authored Oct 17, 2023
1 parent b7f3452 commit 2f7a8d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
18 changes: 10 additions & 8 deletions packages/payments-plugin/src/mollie/mollie.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ export function toMollieOrderLines(order: Order, alreadyPaid: number): CreatePar
vatAmount: toAmount(surcharge.priceWithTax - surcharge.price, order.currencyCode),
})));
// Deduct amount already paid
lines.push({
name: 'Already paid',
quantity: 1,
unitPrice: toAmount(-alreadyPaid, order.currencyCode),
totalAmount: toAmount(-alreadyPaid, order.currencyCode),
vatRate: String(0),
vatAmount: toAmount(0, order.currencyCode),
});
if (alreadyPaid) {
lines.push({
name: 'Already paid',
quantity: 1,
unitPrice: toAmount(-alreadyPaid, order.currencyCode),
totalAmount: toAmount(-alreadyPaid, order.currencyCode),
vatRate: String(0),
vatAmount: toAmount(0, order.currencyCode),
});
}
return lines;
}

Expand Down
14 changes: 7 additions & 7 deletions packages/payments-plugin/src/mollie/mollie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ export class MollieService {
`Unable to find order ${mollieOrder.orderNumber}, unable to process Mollie order ${mollieOrder.id}`,
);
}
if (order.state === 'PaymentSettled') {
Logger.info(
`Order ${order.code} is already 'PaymentSettled', no need for handling Mollie status '${mollieOrder.status}'`,
loggerCtx,
);
return;
}
if (mollieOrder.status === OrderStatus.expired) {
// Expired is fine, a customer can retry the payment later
return;
Expand All @@ -248,13 +255,6 @@ export class MollieService {
if (order.state === 'PaymentAuthorized' && mollieOrder.status === OrderStatus.completed) {
return this.settleExistingPayment(ctx, order, mollieOrder.id);
}
if (order.state === 'PaymentAuthorized' || order.state === 'PaymentSettled') {
Logger.info(
`Order ${order.code} is '${order.state}', no need for handling Mollie status '${mollieOrder.status}'`,
loggerCtx,
);
return;
}
// Any other combination of Mollie status and Vendure status indicates something is wrong.
throw Error(
`Unhandled incoming Mollie status '${mollieOrder.status}' for order ${order.code} with status '${order.state}'`,
Expand Down

0 comments on commit 2f7a8d5

Please sign in to comment.