Skip to content

Commit deb9be4

Browse files
committed
fix: pay error catch
1 parent f382b21 commit deb9be4

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

client/src/pages/api/user/checkPayResult.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
22
import { jsonRes } from '@/service/response';
33
import { connectToDatabase, User, Pay, TrainingData } from '@/service/mongo';
44
import { authUser } from '@/service/utils/auth';
5-
import { PaySchema, UserModelSchema } from '@/types/mongoSchema';
5+
import { PaySchema } from '@/types/mongoSchema';
66
import dayjs from 'dayjs';
77
import { getPayResult } from '@/service/utils/wxpay';
88
import { pushPromotionRecord } from '@/service/utils/promotion';

client/src/service/utils/wxpay.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
// @ts-ignore
22
import Payment from 'wxpay-v3';
33

4-
export const getPayment = () => {
5-
return new Payment({
4+
export const getPayment = () =>
5+
new Payment({
66
appid: process.env.WX_APPID,
77
mchid: process.env.WX_MCHID,
88
private_key: process.env.WX_PRIVATE_KEY?.replace(/\\n/g, '\n'),
99
serial_no: process.env.WX_SERIAL_NO,
1010
apiv3_private_key: process.env.WX_V3_CODE,
1111
notify_url: process.env.WX_NOTIFY_URL
1212
});
13-
};
1413

15-
export const nativePay = (amount: number, payId: string): Promise<string> =>
16-
getPayment()
17-
.native({
14+
export const nativePay = async (amount: number, payId: string) => {
15+
try {
16+
const res = await getPayment().native({
1817
description: 'Fast GPT 余额充值',
1918
out_trade_no: payId,
2019
amount: {
2120
total: amount
2221
}
23-
})
24-
.then((res: any) => JSON.parse(res.data).code_url);
22+
});
23+
return JSON.parse(res.data).code_url as string;
24+
} catch (error) {
25+
return Promise.reject(error);
26+
}
27+
};
2528

26-
export const getPayResult = (payId: string) =>
27-
getPayment()
28-
.getTransactionsByOutTradeNo({
29+
export const getPayResult = async (payId: string) => {
30+
try {
31+
const res = await getPayment().getTransactionsByOutTradeNo({
2932
out_trade_no: payId
30-
})
31-
.then((res: any) => JSON.parse(res.data));
33+
});
34+
35+
return JSON.parse(res.data);
36+
} catch (error) {
37+
return Promise.reject(error);
38+
}
39+
};

0 commit comments

Comments
 (0)