Skip to content
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!');
}
}
}