Skip to content

Commit

Permalink
(example): 添加示例代码
Browse files Browse the repository at this point in the history
  • Loading branch information
flxxyz committed Jul 15, 2021
1 parent 1c7a417 commit bbeeb41
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
3 changes: 3 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package-lock.json
yarn.lock
pnpm.lock
29 changes: 29 additions & 0 deletions example/README.md
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

```
66 changes: 66 additions & 0 deletions example/index.js
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}`));
6 changes: 6 additions & 0 deletions example/package.json
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"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bytedance-mini-pay",
"version": "0.0.2",
"version": "0.0.3",
"description": "字节跳动小程序支付SDK",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit bbeeb41

Please sign in to comment.