Skip to content

Commit d61eac9

Browse files
committed
PM-1099 - more logs & make sure transaction is updated
1 parent 8e6828d commit d61eac9

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

src/api/webhooks/trolley/handlers/payment.handler.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class PaymentHandler {
2727
): Promise<any> {
2828
// TODO: remove slice-1
2929
const winningIds = (payload.externalId ?? '').split(',').slice(0, -1);
30+
const externalTransactionId = payload.batch.id;
3031

3132
if (!winningIds.length) {
3233
this.logger.error(
@@ -37,17 +38,17 @@ export class PaymentHandler {
3738
if (payload.status !== 'processed') {
3839
await this.updatePaymentStates(
3940
winningIds,
40-
payload.id,
41-
payment_status.PROCESSING,
42-
'FAILED',
41+
externalTransactionId,
42+
payload.status.toUpperCase() as payment_status,
43+
payload.status.toUpperCase(),
4344
);
4445

4546
return;
4647
}
4748

4849
await this.updatePaymentStates(
4950
winningIds,
50-
payload.id,
51+
externalTransactionId,
5152
payment_status.PAID,
5253
'PROCESSED',
5354
);

src/api/webhooks/trolley/handlers/payment.types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ export interface PaymentProcessedEventData {
1818
targetAmount: string; // net amount
1919
failureMessage: string | null;
2020
memo: string | null;
21+
batch: {
22+
id: string;
23+
};
2124
}

src/api/webhooks/trolley/trolley.service.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import crypto from 'crypto';
2-
import { Inject, Injectable } from '@nestjs/common';
2+
import { Inject, Injectable, Logger } from '@nestjs/common';
33
import { trolley_webhook_log, webhook_status } from '@prisma/client';
44
import { PrismaService } from 'src/shared/global/prisma.service';
55
import { ENV_CONFIG } from 'src/config';
@@ -20,6 +20,8 @@ if (!trolleyWhHmac) {
2020
*/
2121
@Injectable()
2222
export class TrolleyService {
23+
private readonly logger = new Logger('Webhooks/TolleyService');
24+
2325
constructor(
2426
@Inject('trolleyHandlerFns')
2527
private readonly handlers: Map<
@@ -118,22 +120,36 @@ export class TrolleyService {
118120
*/
119121
async handleEvent(headers: Request['headers'], payload: any) {
120122
const requestId = headers[TrolleyHeaders.id];
123+
this.logger.debug(`Received webhook event with ID: ${requestId}`);
121124

122125
try {
123126
await this.setEventState(requestId, webhook_status.logged, payload, {
124127
event_time: headers[TrolleyHeaders.created],
125128
});
126129

127130
const { model, action, body } = payload;
131+
this.logger.debug(`Processing event - ${requestId} - ${model}.${action}`);
132+
128133
const handler = this.handlers.get(`${model}.${action}`);
129-
// do nothing if there's no handler for the event (event was logged in db)
130134
if (!handler) {
135+
this.logger.debug(
136+
`No handler found for event - ${requestId} - ${model}.${action}. Event logged but not processed.`,
137+
);
131138
return;
132139
}
133140

141+
this.logger.debug(
142+
`Invoking handler for event - ${requestId} - ${model}.${action}`,
143+
);
134144
await handler(body[model]);
145+
146+
this.logger.debug(`Successfully processed event with ID: ${requestId}`);
135147
await this.setEventState(requestId, webhook_status.processed);
136148
} catch (e) {
149+
this.logger.error(
150+
`Error processing event with ID: ${requestId} - ${e.message ?? e}`,
151+
e.stack,
152+
);
137153
await this.setEventState(requestId, webhook_status.error, void 0, {
138154
error_message: e.message ?? e,
139155
});

src/api/withdrawal/withdrawal.service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export class WithdrawalService {
155155
status: payment_status.PROCESSING,
156156
payment_method_id: hasVerifiedPaymentMethod.payment_method_id,
157157
payee_id: recipient.trolley_id,
158+
external_transaction_id: paymentBatch.id,
158159
payment_release_associations: {
159160
createMany: {
160161
data: winnings.map((w) => ({

src/shared/global/trolley.service.ts

-9
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@ export class TrolleyService {
7575
}
7676

7777
try {
78-
console.log(paymentBatch.id, {
79-
recipient: {
80-
id: recipientId,
81-
},
82-
sourceAmount: totalAmount.toString(),
83-
memo: 'Topcoder payment',
84-
externalId: `${winningsIds.join(',')}`,
85-
});
86-
8778
const payment = await this.client.payment.create(paymentBatch.id, {
8879
recipient: {
8980
id: recipientId,

src/shared/payments/payments.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class PaymentsService {
120120

121121
if (r.count === 0 || r.count !== winningsIds.length) {
122122
throw new Error(
123-
'Failed to set payment processing state! Not all rows were updated! Please check the provided winnings IDs and status.',
123+
'Not all rows were updated! Please check the provided winnings IDs and status.',
124124
);
125125
}
126126
} catch (error) {
@@ -148,7 +148,7 @@ export class PaymentsService {
148148

149149
if (r.count === 0) {
150150
throw new Error(
151-
'Failed to update payment release status! No rows were updated. Please check the provided externalTransaction ID and status.',
151+
'No rows were updated. Please check the provided externalTransaction ID and status.',
152152
);
153153
}
154154
} catch (error) {

0 commit comments

Comments
 (0)