fix: pay error catch
This commit is contained in:
parent
f382b2194d
commit
deb9be4160
@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import { connectToDatabase, User, Pay, TrainingData } from '@/service/mongo';
|
import { connectToDatabase, User, Pay, TrainingData } from '@/service/mongo';
|
||||||
import { authUser } from '@/service/utils/auth';
|
import { authUser } from '@/service/utils/auth';
|
||||||
import { PaySchema, UserModelSchema } from '@/types/mongoSchema';
|
import { PaySchema } from '@/types/mongoSchema';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { getPayResult } from '@/service/utils/wxpay';
|
import { getPayResult } from '@/service/utils/wxpay';
|
||||||
import { pushPromotionRecord } from '@/service/utils/promotion';
|
import { pushPromotionRecord } from '@/service/utils/promotion';
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Payment from 'wxpay-v3';
|
import Payment from 'wxpay-v3';
|
||||||
|
|
||||||
export const getPayment = () => {
|
export const getPayment = () =>
|
||||||
return new Payment({
|
new Payment({
|
||||||
appid: process.env.WX_APPID,
|
appid: process.env.WX_APPID,
|
||||||
mchid: process.env.WX_MCHID,
|
mchid: process.env.WX_MCHID,
|
||||||
private_key: process.env.WX_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
private_key: process.env.WX_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
||||||
@ -10,22 +10,30 @@ export const getPayment = () => {
|
|||||||
apiv3_private_key: process.env.WX_V3_CODE,
|
apiv3_private_key: process.env.WX_V3_CODE,
|
||||||
notify_url: process.env.WX_NOTIFY_URL
|
notify_url: process.env.WX_NOTIFY_URL
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
export const nativePay = (amount: number, payId: string): Promise<string> =>
|
export const nativePay = async (amount: number, payId: string) => {
|
||||||
getPayment()
|
try {
|
||||||
.native({
|
const res = await getPayment().native({
|
||||||
description: 'Fast GPT 余额充值',
|
description: 'Fast GPT 余额充值',
|
||||||
out_trade_no: payId,
|
out_trade_no: payId,
|
||||||
amount: {
|
amount: {
|
||||||
total: amount
|
total: amount
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
.then((res: any) => JSON.parse(res.data).code_url);
|
return JSON.parse(res.data).code_url as string;
|
||||||
|
} catch (error) {
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const getPayResult = (payId: string) =>
|
export const getPayResult = async (payId: string) => {
|
||||||
getPayment()
|
try {
|
||||||
.getTransactionsByOutTradeNo({
|
const res = await getPayment().getTransactionsByOutTradeNo({
|
||||||
out_trade_no: payId
|
out_trade_no: payId
|
||||||
})
|
});
|
||||||
.then((res: any) => JSON.parse(res.data));
|
|
||||||
|
return JSON.parse(res.data);
|
||||||
|
} catch (error) {
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user