Skip to content

Commit 819b287

Browse files
committed
fix(comment): fix comments
1 parent 54bc617 commit 819b287

File tree

1 file changed

+30
-51
lines changed

1 file changed

+30
-51
lines changed

src/pay/weixin.service.ts

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -144,64 +144,43 @@ export class WeixinService {
144144
}
145145

146146
callback(body: WeixinCallbackBody) {
147-
throw new Error(JSON.stringify(body));
148-
try {
149-
// Decrypt the callback data
150-
// Note: wechatpay-node-v3 library handles signature verification internally
151-
const data = this.pay.decipher_gcm(
152-
body.resource.ciphertext,
153-
body.resource.associated_data,
154-
body.resource.nonce,
155-
);
156-
157-
console.log(body, data);
158-
159-
const { out_trade_no, transaction_id, trade_state, amount } = data as {
160-
out_trade_no: string;
161-
transaction_id: string;
162-
trade_state: string;
163-
amount: { total: number };
164-
};
147+
const data = this.pay.decipher_gcm(
148+
body.resource.ciphertext,
149+
body.resource.associated_data,
150+
body.resource.nonce,
151+
);
165152

166-
// Process business logic asynchronously to avoid timeout
167-
// According to WeChat Pay docs, we should respond immediately and process business logic asynchronously
168-
setImmediate(() => {
169-
void (async () => {
170-
try {
171-
const order = await this.ordersService.findByOrderNo(out_trade_no);
153+
const { out_trade_no, transaction_id, trade_state, amount } = data as {
154+
out_trade_no: string;
155+
transaction_id: string;
156+
trade_state: string;
157+
amount: { total: number };
158+
};
172159

173-
if (amount.total !== order.amount) {
174-
console.error(
175-
`[WeChat Pay Callback] Order amount mismatch: ${out_trade_no}`,
176-
);
177-
return;
178-
}
160+
setImmediate(() => {
161+
void (async () => {
162+
try {
163+
const order = await this.ordersService.findByOrderNo(out_trade_no);
179164

180-
if (trade_state === 'SUCCESS') {
181-
await this.ordersService.markAsPaid(out_trade_no, transaction_id);
182-
} else if (trade_state === 'CLOSED') {
183-
await this.ordersService.close(out_trade_no);
184-
}
185-
} catch (error) {
165+
if (amount.total !== order.amount) {
186166
console.error(
187-
`[WeChat Pay Callback] Error processing callback: ${error}`,
167+
`[WeChat Pay Callback] Order amount mismatch: ${out_trade_no}`,
188168
);
169+
return;
189170
}
190-
})();
191-
});
192-
193-
// According to WeChat Pay V3 API docs:
194-
// For successful verification, return HTTP 200 or 204 with no response body
195-
return;
196-
} catch (error) {
197-
console.log(body, error);
198171

199-
throw new AppException(
200-
error.message,
201-
'SIGNATURE_VERIFICATION_FAILED',
202-
HttpStatus.BAD_REQUEST,
203-
);
204-
}
172+
if (trade_state === 'SUCCESS') {
173+
await this.ordersService.markAsPaid(out_trade_no, transaction_id);
174+
} else if (trade_state === 'CLOSED') {
175+
await this.ordersService.close(out_trade_no);
176+
}
177+
} catch (error) {
178+
console.error(
179+
`[WeChat Pay Callback] Error processing callback: ${error}`,
180+
);
181+
}
182+
})();
183+
});
205184
}
206185

207186
async query(userId: string, orderId: string) {

0 commit comments

Comments
 (0)