Skip to content

Commit

Permalink
add lowdb and handlePay
Browse files Browse the repository at this point in the history
  • Loading branch information
fritx committed Oct 26, 2017
1 parent e1f8bf2 commit 0b5aea2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
*.log
yarn.lock
gx.conf.js
db.json
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "gh-wxpay",
"main": "src/server",
"scripts": {
"start": "node."
"dev": "nodemon . --ignore db.json --delay 5",
"start": "node ."
},
"dependencies": {
"form-data": "^2.3.1",
Expand All @@ -12,6 +13,7 @@
"koa-router": "^7.2.1",
"koa-session": "^5.5.0",
"lodash": "^4.17.4",
"lowdb": "^1.0.0",
"node-fetch": "^1.7.3",
"xml2json": "^0.11.0"
}
Expand Down
29 changes: 19 additions & 10 deletions src/custom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let _ = require('lodash')
let db = require('./db')

exports.handleOrder = handleOrder
exports.handlePay = handlePay
Expand All @@ -13,11 +14,11 @@ async function handleOrder (ctx, conf) {
}

// todo: 根据product_id 下单
let dateStr = new Date().toJSON().replace(/[\-:]|T.*/g, '')
let date_str = new Date().toJSON().replace(/[\-:]|T.*/g, '')
var params = {
body: '吮指原味鸡 * 1',
attach: '{"部位":"三角"}',
out_trade_no: `${dateStr}-${user_id}-${product_id}`,
out_trade_no: `${date_str}-${user_id}-${product_id}`,
total_fee: 1,
spbill_create_ip: conf.host_ip,
product_id: 'test-kfc',
Expand All @@ -27,18 +28,26 @@ async function handleOrder (ctx, conf) {
}

async function handlePay (res) {
// todo lowdb 写入支付结果数据
res.out_trade_no
res.total_fee
res.open_id
res.is_subscribe

if (res.result_code !== 'SUCCESS') {
let exists = db.get('orders').find(r => {
return r.pay_res.out_trade_no === res.out_trade_no
}).value()
if (exists) return

let record = {}
record.pay_res = res

let [date_str, user_id, product_id] = res.out_trade_no.spllit('-')
_.assign(record, { date_str, user_id, product_id })

if (res.result_code !== 'SUCCESS') {
// noop
} else {
res.err_code
res.err_code_des
// todo: 需要预插入订单 并校验返回的订单金额是否一致
// https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_7
// if (res.total_fee) {}
}
db.get('orders').push(record).write()
}

async function getSession (ctx) {
Expand Down
15 changes: 15 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let FileSync = require('lowdb/adapters/FileSync')
let low = require('lowdb')

let dbFile = __dirname + '/../db.json'
let adapter = new FileSync(dbFile)
let db = low(adapter)

db.defaults({
products: [],
users: [],
orders: []
})
.write()

exports.db = db
9 changes: 5 additions & 4 deletions src/wxpay/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ let { createOrder } = require('./order')
let xmlJs = require('./xml-js')
let wxSign = require('./wxsign')
let KoaBody = require('koa-body')
let koaBody = KoaBody()
let _ = require('lodash')

module.exports = (router, conf) => {
Expand All @@ -16,11 +15,12 @@ module.exports = (router, conf) => {
})

// 支付结果 异步通知
router.post('/wxpay/notify', koaBody, async ctx => {
// res.text: https://github.com/dlau/koa-body/blob/master/test/index.js
router.post('/wxpay/notify', KoaBody({ multipart: true }), async ctx => {
let data
try {
let xml = ctx.request.body
let res = xmlJs.toJson(xml)
let xml = ctx.request.text
let res = xmlJs.toJs(xml)

let expected = wxSign(res, conf.mch_key)
if (res.sign !== expected) {
Expand All @@ -35,6 +35,7 @@ module.exports = (router, conf) => {
throw err
}

res = _.omit(res, ['nonce_str', 'sign', 'sign_type'])
await handlePay(res)
data = {
return_code: 'SUCCESS'
Expand Down

0 comments on commit 0b5aea2

Please sign in to comment.