-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package-lock.json | ||
yarn.lock | ||
pnpm.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# bytedance-mini-pay 示例代码 | ||
|
||
## 安装依赖 | ||
|
||
```bash | ||
pnpm install | ||
|
||
or | ||
|
||
yarn install | ||
|
||
or | ||
|
||
npm i | ||
``` | ||
|
||
## 跑起来 | ||
|
||
```bash | ||
node index.js | ||
``` | ||
|
||
看到如下输出即可使用 | ||
|
||
```bash | ||
$ node index.js | ||
Example app listening at http://localhost:8800 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const express = require('express'); | ||
const { TTPay } = require('bytedance-mini-pay'); | ||
|
||
const app = express(); | ||
const port = 8800; | ||
|
||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
const ttpay = new TTPay({ | ||
appId: '你的AppId', | ||
appSecret: '你的AppSecret', | ||
SALT: '你的SALT', | ||
// TOKEN: '你的TOKEN', // 可选 | ||
// mchId: '你的商户号', // 可选 | ||
// notifyURL: '你的支付回调URL', // 可选 | ||
}); | ||
|
||
app.post('/order', async (req, res) => { | ||
const { orderNo, amount, subject, body } = req.body; | ||
|
||
const options = { | ||
valid_time: 60 * 60 * 24, // 订单支付的有效时间 (单位: 秒) | ||
}; | ||
ttpay.createOrder(orderNo, amount, subject, body, options) | ||
.then(resp => res.send(resp)) | ||
.catch(err => res.send(err)) | ||
.finally(() => res.end()); | ||
}); | ||
|
||
app.get('/refund/:id', async (req, res) => { | ||
const { id: orderNo } = req.params; | ||
const refundNo = `${orderNo}-${Number((Math.random() % 1000) * 10000).toFixed(0)}`; | ||
console.log('refundNo:', refundNo); | ||
|
||
ttpay.createRefund(orderNo, refundNo, 1, 'RNM, 退钱!') | ||
.then(resp => res.send(resp)) | ||
.catch(err => res.send(err)) | ||
.finally(() => res.end()); | ||
}); | ||
|
||
app.get('/query/order/:id', async (req, res) => { | ||
ttpay.queryOrder(req.params.id) | ||
.then(resp => res.send(resp)) | ||
.catch(err => res.send(err)) | ||
.finally(() => res.end()); | ||
}); | ||
|
||
app.get('/query/refund/:id', async (req, res) => { | ||
ttpay.queryRefund(req.params.id) | ||
.then(resp => res.send(resp)) | ||
.catch(err => res.send(err)) | ||
.finally(() => res.end()); | ||
}); | ||
|
||
app.use('/callback', (req, res) => { | ||
// 下单回调的处理 | ||
if (req.body.type === 'payment') { | ||
const msg = JSON.parse(req.body.msg); | ||
console.log('msg.cp_orderno ------', msg.cp_orderno); | ||
} | ||
|
||
ttpay.ackNotify(body => res.json(body).end()); | ||
}); | ||
|
||
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"bytedance-mini-pay": "^0.0.3", | ||
"express": "^4.17.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters