-
Notifications
You must be signed in to change notification settings - Fork 0
PM-1279 - paypal fee collection #66
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ export class WithdrawalService { | |
private readonly tcMembersService: TopcoderMembersService, | ||
) {} | ||
|
||
getTrolleyRecipientByUserId(userId: string) { | ||
getDbTrolleyRecipientByUserId(userId: string) { | ||
return this.prisma.trolley_recipient.findUnique({ | ||
where: { user_id: userId }, | ||
}); | ||
|
@@ -118,6 +118,7 @@ export class WithdrawalService { | |
paymentMethodId: number, | ||
recipientId: string, | ||
winnings: ReleasableWinningRow[], | ||
metadata: any, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
try { | ||
const paymentRelease = await tx.payment_releases.create({ | ||
|
@@ -127,6 +128,7 @@ export class WithdrawalService { | |
status: payment_status.PROCESSING, | ||
payment_method_id: paymentMethodId, | ||
payee_id: recipientId, | ||
metadata, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
payment_release_associations: { | ||
createMany: { | ||
data: winnings.map((w) => ({ | ||
|
@@ -176,7 +178,9 @@ export class WithdrawalService { | |
winningsIds: string[], | ||
paymentMemo?: string, | ||
) { | ||
this.logger.log('Processing withdrawal request'); | ||
this.logger.log( | ||
`Processing withdrawal request for user ${userHandle}(${userId}), winnings: ${winningsIds.join(', ')}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
const hasActiveTaxForm = await this.taxFormRepo.hasActiveTaxForm(userId); | ||
|
||
if (!hasActiveTaxForm) { | ||
|
@@ -206,6 +210,9 @@ export class WithdrawalService { | |
} | ||
|
||
if (userInfo.email.toLowerCase().indexOf('wipro.com') > -1) { | ||
this.logger.error( | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`User ${userHandle}(${userId}) attempted withdrawal but is restricted due to email domain '${userInfo.email}'.`, | ||
); | ||
throw new Error( | ||
'Please contact Topgear support to process your withdrawal.', | ||
); | ||
|
@@ -219,33 +226,84 @@ export class WithdrawalService { | |
tx, | ||
); | ||
|
||
this.logger.log( | ||
`Begin processing payments for user ${userHandle}(${userId})`, | ||
winnings, | ||
); | ||
|
||
const dbTrolleyRecipient = | ||
await this.getDbTrolleyRecipientByUserId(userId); | ||
|
||
if (!dbTrolleyRecipient) { | ||
throw new Error( | ||
`Trolley recipient not found for user ${userHandle}(${userId})!`, | ||
); | ||
} | ||
|
||
const totalAmount = this.checkTotalAmount(winnings); | ||
let paymentAmount = totalAmount; | ||
let feeAmount = 0; | ||
const trolleyRecipientPayoutDetails = | ||
await this.trolleyService.getRecipientPayoutDetails( | ||
dbTrolleyRecipient.trolley_id, | ||
); | ||
|
||
if (!trolleyRecipientPayoutDetails) { | ||
throw new Error( | ||
`Recipient payout details not found for Trolley Recipient ID '${dbTrolleyRecipient.trolley_id}', for user ${userHandle}(${userId}).`, | ||
); | ||
} | ||
|
||
this.logger.log('Begin processing payments', winnings); | ||
if ( | ||
trolleyRecipientPayoutDetails.payoutMethod === 'paypal' && | ||
ENV_CONFIG.TROLLEY_PAYPAL_FEE_PERCENT | ||
) { | ||
const feePercent = | ||
Number(ENV_CONFIG.TROLLEY_PAYPAL_FEE_PERCENT) / 100; | ||
|
||
const recipient = await this.getTrolleyRecipientByUserId(userId); | ||
feeAmount = +Math.min( | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ENV_CONFIG.TROLLEY_PAYPAL_FEE_MAX_AMOUNT, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. high There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vas3a I also think this needs correction and kind of struggle to understand it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, good catch. wanted to test in dev, as I had some issues locally. restarting PC resolved local so I'll run through it before merging. |
||
feePercent * paymentAmount, | ||
).toFixed(2); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!recipient) { | ||
throw new Error(`Trolley recipient not found for user '${userId}'!`); | ||
paymentAmount -= feeAmount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do round up paymentAmount to 2nd digit after the point, right? Or shold we do it here? |
||
} | ||
|
||
this.logger.log( | ||
` | ||
Total amount won: $${totalAmount.toFixed(2)} USD, to be paid: $${paymentAmount.toFixed(2)} USD. | ||
Fee applied: $${feeAmount.toFixed(2)} USD (${Number(ENV_CONFIG.TROLLEY_PAYPAL_FEE_PERCENT)}%, max ${ENV_CONFIG.TROLLEY_PAYPAL_FEE_MAX_AMOUNT}). | ||
Payout method type: ${trolleyRecipientPayoutDetails.payoutMethod}. | ||
`, | ||
); | ||
|
||
const paymentRelease = await this.createDbPaymentRelease( | ||
tx, | ||
userId, | ||
totalAmount, | ||
paymentAmount, | ||
connectedPaymentMethod.payment_method_id, | ||
recipient.trolley_id, | ||
dbTrolleyRecipient.trolley_id, | ||
winnings, | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also include the |
||
netAmount: paymentAmount, | ||
feeAmount, | ||
totalAmount: totalAmount, | ||
payoutMethod: trolleyRecipientPayoutDetails.payoutMethod, | ||
env_trolley_paypal_fee_percent: | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ENV_CONFIG.TROLLEY_PAYPAL_FEE_PERCENT, | ||
env_trolley_paypal_fee_max_amount: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. medium |
||
ENV_CONFIG.TROLLEY_PAYPAL_FEE_MAX_AMOUNT, | ||
}, | ||
); | ||
|
||
const paymentBatch = await this.trolleyService.startBatchPayment( | ||
`${userId}_${userHandle}`, | ||
); | ||
|
||
const trolleyPayment = await this.trolleyService.createPayment( | ||
recipient.trolley_id, | ||
dbTrolleyRecipient.trolley_id, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
paymentBatch.id, | ||
totalAmount, | ||
paymentAmount, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
paymentRelease.payment_release_id, | ||
paymentMemo, | ||
); | ||
|
Uh oh!
There was an error while loading. Please reload this page.