Skip to content

PM-1213 - handle insufficient funds errors #48

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 2 commits into from
May 13, 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
15 changes: 1 addition & 14 deletions src/api/withdrawal/withdrawal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,7 @@ export class WithdrawalService {
},
});

try {
// generate quote
await this.trolleyService.client.batch.generateQuote(paymentBatch.id);

// trigger trolley payment (batch) process
await this.trolleyService.client.batch.startProcessing(
paymentBatch.id,
);
} catch (error) {
this.logger.error(
`Failed to process trolley payment batch: ${error.message}`,
);
throw new Error('Failed to process trolley payment batch!');
}
await this.trolleyService.startProcessingPayment(paymentBatch.id);

this.logger.log(
`Payment release created successfully. ID: ${paymentRelease.payment_release_id}`,
Expand Down
74 changes: 62 additions & 12 deletions src/shared/global/trolley.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ const client = trolley({
secret: TROLLEY_SECRET_KEY,
});

/**
* Determines if the provided validation errors indicate an "insufficient funds" error.
*/
const isInsufficientFundsError = ({
validationErrors,
}: {
validationErrors: { code: string }[];
}) =>
validationErrors.length === 1 &&
validationErrors[0].code === 'non_sufficient_funds';

@Injectable()
export class TrolleyService {
private readonly logger = new Logger(`global/TrolleyService`);
Expand Down Expand Up @@ -78,24 +89,63 @@ export class TrolleyService {
return;
}

const paymentPayload = {
recipient: {
id: recipientId,
},
sourceAmount: totalAmount.toString(),
sourceCurrency: 'USD',
memo: 'Topcoder payment',
// TODO: remove `,${Date.now()}`
// if externalId is present, it must be unique
externalId: `${winningsIds.join(',')},${Date.now()}`,
};

try {
const payment = await this.client.payment.create(paymentBatch.id, {
recipient: {
id: recipientId,
},
sourceAmount: totalAmount.toString(),
sourceCurrency: 'USD',
memo: 'Topcoder payment',
// TODO: remove `,${Date.now()}`
// if externalId is present, it must be unique
externalId: `${winningsIds.join(',')},${Date.now()}`,
});
const payment = await this.client.payment.create(
paymentBatch.id,
paymentPayload,
);

this.logger.debug(`Created payment with id ${payment.id}`);

return paymentBatch;
} catch (e) {
this.logger.error(`Failed to create payment, error '${e.message}'!`);
this.logger.error(
`Failed to create payment, error '${e.message}'!`,
paymentPayload,
e.validationErrors
? { validationErrors: e.validationErrors }
: undefined,
);
}
}

async startProcessingPayment(paymentBatchId: string) {
try {
// generate quote
await this.client.batch.generateQuote(paymentBatchId);

// trigger trolley payment (batch) process
await this.client.batch.startProcessing(paymentBatchId);
} catch (error) {
// payments with insufficient funds error are still created in trolley,
// and they are storred as "pending".
// no need to do anything. just log a warning, and move on
if (isInsufficientFundsError(error)) {
this.logger.warn(
`Insufficient funds while processing payment: ${error.validationErrors}`,
);
return;
}

this.logger.error(
`Failed to process trolley payment batch: ${error.message}`,
error.validationErrors
? { validationErrors: error.validationErrors }
: undefined,
);
throw new Error('Failed to process trolley payment batch!');
}
}
}