mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
feat: wx pay
This commit is contained in:
@@ -48,6 +48,7 @@
|
|||||||
"sass": "^1.58.3",
|
"sass": "^1.58.3",
|
||||||
"sharp": "^0.31.3",
|
"sharp": "^0.31.3",
|
||||||
"tunnel": "^0.0.6",
|
"tunnel": "^0.0.6",
|
||||||
|
"wxpay-v3": "^3.0.2",
|
||||||
"zustand": "^4.3.5"
|
"zustand": "^4.3.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
867
pnpm-lock.yaml
generated
867
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,12 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import axios from 'axios';
|
|
||||||
import { connectToDatabase, User, Pay } from '@/service/mongo';
|
import { connectToDatabase, User, Pay } from '@/service/mongo';
|
||||||
import { authToken } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import { PaySchema } from '@/types/mongoSchema';
|
import { PaySchema } from '@/types/mongoSchema';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
|
import { getPayResult } from '@/service/utils/wxpay';
|
||||||
|
|
||||||
|
/* 校验支付结果 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
@@ -25,18 +26,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
throw new Error('订单已结算');
|
throw new Error('订单已结算');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data } = await axios.get(
|
const payRes = await getPayResult(payOrder.orderId);
|
||||||
`https://sif268.laf.dev/wechat-order-query?order_number=${payOrder.orderId}&api_key=${process.env.WXPAYCODE}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// 校验下是否超过一天
|
// 校验下是否超过一天
|
||||||
const orderTime = dayjs(payOrder.createTime);
|
const orderTime = dayjs(payOrder.createTime);
|
||||||
const diffInHours = dayjs().diff(orderTime, 'hours');
|
const diffInHours = dayjs().diff(orderTime, 'hours');
|
||||||
|
|
||||||
if (data.trade_state === 'SUCCESS') {
|
if (payRes.trade_state === 'SUCCESS') {
|
||||||
// 订单已支付
|
// 订单已支付
|
||||||
try {
|
try {
|
||||||
// 更新订单状态
|
// 更新订单状态. 如果没有合适的订单,说明订单重复了
|
||||||
const updateRes = await Pay.updateOne(
|
const updateRes = await Pay.updateOne(
|
||||||
{
|
{
|
||||||
_id: payId,
|
_id: payId,
|
||||||
@@ -61,7 +60,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
});
|
});
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
} else if (data.trade_state === 'CLOSED' || diffInHours > 24) {
|
} else if (payRes.trade_state === 'CLOSED' || diffInHours > 24) {
|
||||||
// 订单已关闭
|
// 订单已关闭
|
||||||
await Pay.findByIdAndUpdate(payId, {
|
await Pay.findByIdAndUpdate(payId, {
|
||||||
status: 'CLOSED'
|
status: 'CLOSED'
|
||||||
@@ -70,7 +69,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
data: '订单已过期'
|
data: '订单已过期'
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
throw new Error(data.trade_state_desc);
|
throw new Error(payRes.trade_state_desc);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.log(err);
|
// console.log(err);
|
||||||
|
@@ -1,14 +1,14 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { jsonRes } from '@/service/response';
|
import { jsonRes } from '@/service/response';
|
||||||
import axios from 'axios';
|
|
||||||
import { authToken } from '@/service/utils/tools';
|
import { authToken } from '@/service/utils/tools';
|
||||||
import { customAlphabet } from 'nanoid';
|
import { customAlphabet } from 'nanoid';
|
||||||
import { connectToDatabase, Pay } from '@/service/mongo';
|
import { connectToDatabase, Pay } from '@/service/mongo';
|
||||||
import { PRICE_SCALE } from '@/constants/common';
|
import { PRICE_SCALE } from '@/constants/common';
|
||||||
|
import { nativePay } from '@/service/utils/wxpay';
|
||||||
|
|
||||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 20);
|
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 20);
|
||||||
|
|
||||||
|
/* 获取支付二维码 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
try {
|
try {
|
||||||
const { authorization } = req.headers;
|
const { authorization } = req.headers;
|
||||||
@@ -23,15 +23,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
const id = nanoid();
|
const id = nanoid();
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
const response = await axios({
|
const code_url = await nativePay(amount * 100, id);
|
||||||
url: 'https://sif268.laf.dev/wechat-pay',
|
|
||||||
method: 'POST',
|
|
||||||
data: {
|
|
||||||
trade_order_number: id,
|
|
||||||
amount: amount * 100,
|
|
||||||
api_key: process.env.WXPAYCODE
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 充值记录 + 1
|
// 充值记录 + 1
|
||||||
const payOrder = await Pay.create({
|
const payOrder = await Pay.create({
|
||||||
@@ -43,11 +35,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
data: {
|
data: {
|
||||||
payId: payOrder._id,
|
payId: payOrder._id,
|
||||||
codeUrl: response.data?.code_url
|
codeUrl: code_url
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err, '==');
|
||||||
jsonRes(res, {
|
jsonRes(res, {
|
||||||
code: 500,
|
code: 500,
|
||||||
error: err
|
error: err
|
||||||
|
18
src/pages/api/user/payRes.ts
Normal file
18
src/pages/api/user/payRes.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { connectToDatabase, User, Pay } from '@/service/mongo';
|
||||||
|
import { authToken } from '@/service/utils/tools';
|
||||||
|
import { PaySchema } from '@/types/mongoSchema';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
res.send('');
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
31
src/service/utils/wxpay.ts
Normal file
31
src/service/utils/wxpay.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// @ts-ignore
|
||||||
|
import Payment from 'wxpay-v3';
|
||||||
|
|
||||||
|
export const getPayment = () => {
|
||||||
|
return new Payment({
|
||||||
|
appid: process.env.WX_APPID,
|
||||||
|
mchid: process.env.WX_MCHID,
|
||||||
|
private_key: process.env.WX_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
||||||
|
serial_no: process.env.WX_SERIAL_NO,
|
||||||
|
apiv3_private_key: process.env.WX_V3_CODE,
|
||||||
|
notify_url: process.env.WX_NOTIFY_URL
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const nativePay = (amount: number, payId: string): Promise<string> =>
|
||||||
|
getPayment()
|
||||||
|
.native({
|
||||||
|
description: 'Fast GPT 余额充值',
|
||||||
|
out_trade_no: payId,
|
||||||
|
amount: {
|
||||||
|
total: amount
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((res: any) => JSON.parse(res.data).code_url);
|
||||||
|
|
||||||
|
export const getPayResult = (payId: string) =>
|
||||||
|
getPayment()
|
||||||
|
.getTransactionsByOutTradeNo({
|
||||||
|
out_trade_no: payId
|
||||||
|
})
|
||||||
|
.then((res: any) => JSON.parse(res.data));
|
Reference in New Issue
Block a user