Skip to content

Update returned payment handling #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/api/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,12 @@ export class AdminService {
currentVersion: number,
) {
let setDatePaidNull = false;
if (
(oldPaymentStatus === PaymentStatus.PAID ||
oldPaymentStatus === PaymentStatus.PROCESSING) &&
if ([
PaymentStatus.PAID,
PaymentStatus.PROCESSING,
PaymentStatus.RETURNED,
PaymentStatus.FAILED,
].includes(oldPaymentStatus as PaymentStatus) &&
newPaymentStatus === PaymentStatus.OWED
) {
setDatePaidNull = true;
Expand Down
7 changes: 6 additions & 1 deletion src/api/webhooks/trolley/handlers/payment.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export class PaymentHandler {
paymentId,
payload.status.toUpperCase() as payment_status,
payload.status.toUpperCase(),
{ failureMessage: payload.failureMessage },
{
failureMessage: payload.failureMessage,
returnedNote: payload.returnedNote,
errors: payload.errors?.join(', '),
},
);

return;
Expand All @@ -56,6 +60,7 @@ export class PaymentHandler {
INNER JOIN public.payment_release_associations pra
ON pra.payment_id = p.payment_id
WHERE pra.payment_release_id::text = ${paymentId}
FOR UPDATE
`
).map((w) => w.id);

Expand Down
2 changes: 2 additions & 0 deletions src/api/webhooks/trolley/handlers/payment.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface PaymentProcessedEventData {
fees: string;
targetAmount: string; // net amount
failureMessage: string | null;
errors?: string[];
returnedNote?: string;
memo: string | null;
batch: {
id: string;
Expand Down
14 changes: 14 additions & 0 deletions src/shared/payments/payments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ export class PaymentsService {
metadata?: JsonObject,
) {
const prismaClient = transaction || this.prisma;

const failedOrReturnedRelease = await prismaClient.payment_releases.findFirst({
where: {
payment_release_id: paymentId,
status: { in: [payment_status.RETURNED, payment_status.FAILED] },
}
});

if (failedOrReturnedRelease) {
throw new Error(
`Not processing payment release ${paymentId} because it was already marked as '${failedOrReturnedRelease.status}'.`,
);
}

try {
const r = await prismaClient.payment_releases.updateMany({
where: {
Expand Down