Skip to content

Instantly share code, notes, and snippets.

@abisuq
Last active December 28, 2017 06:54

Revisions

  1. abisuq renamed this gist Dec 27, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. abisuq created this gist Dec 27, 2017.
    38 changes: 38 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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()
    }