Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
794a3698
authored
Apr 05, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: wx pay
parent
fbbc3236
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
62 additions
and
21 deletions
+62
-21
package.json
+1
-0
pnpm-lock.yaml
+0
-0
src/pages/api/user/checkPayResult.ts
+7
-8
src/pages/api/user/getPayCode.ts
+5
-13
src/pages/api/user/payRes.ts
+18
-0
src/service/utils/wxpay.ts
+31
-0
No files found.
package.json
View file @
794a3698
...
...
@@ -48,6 +48,7 @@
"
sass
"
:
"^1.58.3"
,
"
sharp
"
:
"^0.31.3"
,
"
tunnel
"
:
"^0.0.6"
,
"
wxpay-v3
"
:
"^3.0.2"
,
"
zustand
"
:
"^4.3.5"
},
"devDependencies"
:
{
...
...
pnpm-lock.yaml
View file @
794a3698
This diff is collapsed.
Click to expand it.
src/pages/api/user/checkPayResult.ts
View file @
794a3698
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'
;
import
{
getPayResult
}
from
'@/service/utils/wxpay'
;
/* 校验支付结果 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
const
{
authorization
}
=
req
.
headers
;
...
...
@@ -25,18 +26,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
throw
new
Error
(
'订单已结算'
);
}
const
{
data
}
=
await
axios
.
get
(
`https://sif268.laf.dev/wechat-order-query?order_number=
${
payOrder
.
orderId
}
&api_key=
${
process
.
env
.
WXPAYCODE
}
`
);
const
payRes
=
await
getPayResult
(
payOrder
.
orderId
);
// 校验下是否超过一天
const
orderTime
=
dayjs
(
payOrder
.
createTime
);
const
diffInHours
=
dayjs
().
diff
(
orderTime
,
'hours'
);
if
(
data
.
trade_state
===
'SUCCESS'
)
{
if
(
payRes
.
trade_state
===
'SUCCESS'
)
{
// 订单已支付
try
{
// 更新订单状态
// 更新订单状态
. 如果没有合适的订单,说明订单重复了
const
updateRes
=
await
Pay
.
updateOne
(
{
_id
:
payId
,
...
...
@@ -61,7 +60,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
console
.
log
(
error
);
}
}
else
if
(
data
.
trade_state
===
'CLOSED'
||
diffInHours
>
24
)
{
}
else
if
(
payRes
.
trade_state
===
'CLOSED'
||
diffInHours
>
24
)
{
// 订单已关闭
await
Pay
.
findByIdAndUpdate
(
payId
,
{
status
:
'CLOSED'
...
...
@@ -70,7 +69,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
data
:
'订单已过期'
});
}
else
{
throw
new
Error
(
data
.
trade_state_desc
);
throw
new
Error
(
payRes
.
trade_state_desc
);
}
}
catch
(
err
)
{
// console.log(err);
...
...
src/pages/api/user/getPayCode.ts
View file @
794a3698
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
jsonRes
}
from
'@/service/response'
;
import
axios
from
'axios'
;
import
{
authToken
}
from
'@/service/utils/tools'
;
import
{
customAlphabet
}
from
'nanoid'
;
import
{
connectToDatabase
,
Pay
}
from
'@/service/mongo'
;
import
{
PRICE_SCALE
}
from
'@/constants/common'
;
import
{
nativePay
}
from
'@/service/utils/wxpay'
;
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890'
,
20
);
/* 获取支付二维码 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
const
{
authorization
}
=
req
.
headers
;
...
...
@@ -23,15 +23,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const
id
=
nanoid
();
await
connectToDatabase
();
const
response
=
await
axios
({
url
:
'https://sif268.laf.dev/wechat-pay'
,
method
:
'POST'
,
data
:
{
trade_order_number
:
id
,
amount
:
amount
*
100
,
api_key
:
process
.
env
.
WXPAYCODE
}
});
const
code_url
=
await
nativePay
(
amount
*
100
,
id
);
// 充值记录 + 1
const
payOrder
=
await
Pay
.
create
({
...
...
@@ -43,11 +35,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
jsonRes
(
res
,
{
data
:
{
payId
:
payOrder
.
_id
,
codeUrl
:
response
.
data
?.
code_url
codeUrl
:
code_url
}
});
}
catch
(
err
)
{
console
.
log
(
err
);
console
.
log
(
err
,
'=='
);
jsonRes
(
res
,
{
code
:
500
,
error
:
err
...
...
src/pages/api/user/payRes.ts
0 → 100644
View file @
794a3698
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
});
}
}
src/service/utils/wxpay.ts
0 → 100644
View file @
794a3698
// @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
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment