Skip to content

Instantly share code, notes, and snippets.

@abisuq
Last active December 28, 2017 06:54
Show Gist options
  • Save abisuq/126bbb51d2c4aa9978c337053360be71 to your computer and use it in GitHub Desktop.
Save abisuq/126bbb51d2c4aa9978c337053360be71 to your computer and use it in GitHub Desktop.
NODE_KOA_PAYJS_DEMO
const uuid = require('uuid')
const md5 = require('md5')
const qs = require('query-string')
const Router = require('koa-router')
const apiRoute = new Router()
const NOTIFY_URL = ''
const CALLBACK_URL = ''
const MCHID = '🆔'
const KEY = '🗝'
const API = 'https://payjs.cn/api/cashier'
apiRoute.get('/pay', (ctx, next) => {
const { title: body, fee: total_fee } = ctx.query
const params = {
mchid: MCHID,
out_trade_no: uuid.v4().replace(/-/gi, ''),
body,
total_fee
}
if (NOTIFY_URL) {
params.notify_url = NOTIFY_URL
}
if (CALLBACK_URL) {
params.callback_url = CALLBACK_URL
}
params.sign = sign(params)
const redirectURL = API + '?' + qs.stringify(params)
console.log('✨ REDIRECT URL 🔗 :', redirectURL)
ctx.redirect(redirectURL)
})
module.exports = apiRoute
function sign(_) {
return md5(qs.stringify(_) + `&key=${KEY}`).toUpperCase()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment