|
|
@@ -0,0 +1,201 @@
|
|
|
+const axios = require('axios'),
|
|
|
+ crypto = require('crypto'),
|
|
|
+ path = require('path'),
|
|
|
+ fs = require('fs'),
|
|
|
+ qs = require('querystring'),
|
|
|
+ urlencode = require('urlencode'),
|
|
|
+ iconv = require('iconv-lite'),
|
|
|
+ qr = require('qr-image'),
|
|
|
+ express = require('express'),
|
|
|
+ app = express()
|
|
|
+
|
|
|
+const sandbox = fs.readFileSync(path.resolve(__dirname, './sandbox_iobox_private.pem')).toString()
|
|
|
+
|
|
|
+const format = (fmt = 'yyyy-MM-dd hh:mm:ss') => {
|
|
|
+ let date = new Date()
|
|
|
+ var o = {
|
|
|
+ "M+": date.getMonth() + 1, //月份
|
|
|
+ "d+": date.getDate(), //日
|
|
|
+ "h+": date.getHours(), //小时
|
|
|
+ "m+": date.getMinutes(), //分
|
|
|
+ "s+": date.getSeconds(), //秒
|
|
|
+ "q+": Math.floor((date.getMonth() + 3) / 3), //季度
|
|
|
+ "S": date.getMilliseconds() //毫秒
|
|
|
+ };
|
|
|
+ if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
|
|
+ for (var k in o)
|
|
|
+ if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
|
|
+ return fmt;
|
|
|
+}
|
|
|
+
|
|
|
+const time = format()
|
|
|
+
|
|
|
+const userconfig = {
|
|
|
+ app_id: "2016032301234394",
|
|
|
+ version: "1.0",
|
|
|
+ format: "json",
|
|
|
+ sign_type: "RSA2",
|
|
|
+ method: "alipay.system.oauth.token",
|
|
|
+ timestamp: time,
|
|
|
+ auth_token: null,
|
|
|
+ alipay_sdk: "alipay-sdk-php-20161101",
|
|
|
+ terminal_type: null,
|
|
|
+ terminal_info: null,
|
|
|
+ prod_code: null,
|
|
|
+ notify_url: null,
|
|
|
+ charset: "utf-8",
|
|
|
+ app_auth_token: null,
|
|
|
+ grant_type: 'authorization_code',
|
|
|
+ code: '',
|
|
|
+}
|
|
|
+
|
|
|
+const test = {
|
|
|
+ alipay_sdk: 'alipay-sdk-php-20161101',
|
|
|
+ app_id: '2016032301234394',
|
|
|
+ charset: 'utf-8',
|
|
|
+ code: '',
|
|
|
+ format: 'json',
|
|
|
+ grant_type: 'authorization_code',
|
|
|
+ method: 'alipay.system.oauth.token',
|
|
|
+ sign_type: 'RSA2',
|
|
|
+ timestamp: time,
|
|
|
+ version: '1.0'
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 生成签名
|
|
|
+ * @param {String} content 签名内容
|
|
|
+ * @param {String} privateKey 私钥
|
|
|
+ * @returns {String}
|
|
|
+ */
|
|
|
+
|
|
|
+const getSign = (content, privateKey = sandbox) => {
|
|
|
+ let data = crypto.createSign('RSA-SHA256').update(content).sign(privateKey,'base64')
|
|
|
+ return urlencode(data)
|
|
|
+}
|
|
|
+
|
|
|
+const splice = (msg) => {
|
|
|
+ let data = ''
|
|
|
+ for (var prop in msg) {
|
|
|
+ data += prop + '=' + msg[prop] + '&'
|
|
|
+ }
|
|
|
+ return data.substr(0,data.length-1)
|
|
|
+}
|
|
|
+
|
|
|
+const requester = async (url) => {
|
|
|
+ try {
|
|
|
+ let {data, status} = await axios({
|
|
|
+ url: url,
|
|
|
+ method: 'get',
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'application/x-www-form-urlencoded'
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if (status === 200) {
|
|
|
+ return data
|
|
|
+ }
|
|
|
+ } catch(e) {
|
|
|
+ console.log(e)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const out = {
|
|
|
+ alipay_sdk: 'alipay-sdk-php-20161101',
|
|
|
+ app_id: '2016032301234394',
|
|
|
+ biz_content: '',
|
|
|
+ charset: 'utf-8',
|
|
|
+ format: 'json',
|
|
|
+ method: 'alipay.trade.create',
|
|
|
+ sign_type: 'RSA2',
|
|
|
+ timestamp: time,
|
|
|
+ version: '1.0',
|
|
|
+}
|
|
|
+
|
|
|
+const outreq = {
|
|
|
+ app_id: '2016032301234394',
|
|
|
+ version: '1.0',
|
|
|
+ format: 'json',
|
|
|
+ sign_type: 'RSA2',
|
|
|
+ method: 'alipay.trade.create',
|
|
|
+ timestamp: time,
|
|
|
+ auth_token: null,
|
|
|
+ alipay_sdk: 'alipay-sdk-php-20161101',
|
|
|
+ terminal_type: null,
|
|
|
+ terminal_info: null,
|
|
|
+ prod_code: null,
|
|
|
+ notify_url: null,
|
|
|
+ charset: 'utf-8',
|
|
|
+ app_auth_token: null,
|
|
|
+}
|
|
|
+
|
|
|
+const requesters = async (userid) => {
|
|
|
+ let tradeno = Date.now().toString()
|
|
|
+ out.biz_content = '{"out_trade_no":"' + tradeno + '","subject":"ceshi","buyer_id":"'+ userid +'","total_amount":"0.01"}'
|
|
|
+ let url = 'https://openapi.alipay.com/gateway.do?' + qs.stringify(outreq) + '&sign=' + getSign(splice(out)) + '&biz_content=' + urlencode(out.biz_content)
|
|
|
+ let data = await requester(url)
|
|
|
+ return data
|
|
|
+}
|
|
|
+
|
|
|
+app.use('/', express.static(__dirname + '/'))
|
|
|
+
|
|
|
+app.get('/qr', (req, res) => {
|
|
|
+ let text = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2016032301234394&scope=auth_base&redirect_uri=http://10.3.16.61:8000/'
|
|
|
+ let img = qr.image(text ,{size :10});
|
|
|
+ res.writeHead(200, {'Content-Type': 'image/png'});
|
|
|
+ img.pipe(res);
|
|
|
+})
|
|
|
+
|
|
|
+app.get('/token', async (req, res) => {
|
|
|
+ userconfig.code = req.query.auth_code
|
|
|
+ test.code = req.query.auth_code
|
|
|
+ let url = 'https://openapi.alipay.com/gateway.do?' + qs.stringify(userconfig) + '&sign=' + getSign(splice(test))
|
|
|
+ let data = await requester(url)
|
|
|
+ let trade = await requesters(data.alipay_system_oauth_token_response.user_id)
|
|
|
+ res.send(trade.alipay_trade_create_response.trade_no)
|
|
|
+})
|
|
|
+
|
|
|
+const sigtrader = {
|
|
|
+ alipay_sdk: 'alipay-sdk-php-20161101',
|
|
|
+ app_id: '2016032301234394',
|
|
|
+ biz_content: '{"trade_no":"2018062021001004450535911615"}',
|
|
|
+ charset: 'utf-8',
|
|
|
+ format: 'json',
|
|
|
+ method: 'alipay.trade.query',
|
|
|
+ sign_type: 'RSA2',
|
|
|
+ timestamp: time,
|
|
|
+ version: '1.0',
|
|
|
+}
|
|
|
+
|
|
|
+const reqtrader = {
|
|
|
+ app_id: '2016032301234394',
|
|
|
+ version: '1.0',
|
|
|
+ format: 'json',
|
|
|
+ sign_type: 'RSA2',
|
|
|
+ method: 'alipay.trade.query',
|
|
|
+ timestamp: time,
|
|
|
+ auth_token: null,
|
|
|
+ alipay_sdk: 'alipay-sdk-php-20161101',
|
|
|
+ terminal_type: null,
|
|
|
+ terminal_info: null,
|
|
|
+ prod_code: null,
|
|
|
+ notify_url: null,
|
|
|
+ charset: 'utf-8',
|
|
|
+ app_auth_token: null,
|
|
|
+}
|
|
|
+
|
|
|
+const trade = async (msg) => {
|
|
|
+ sigtrader.biz_content = '{"trade_no":"' + msg + '"}'
|
|
|
+ let url = 'https://openapi.alipay.com/gateway.do?' + qs.stringify(reqtrader) + '&sign=' + getSign(splice(sigtrader)) + '&biz_content=' + urlencode(sigtrader.biz_content)
|
|
|
+ let data = await requester(url)
|
|
|
+ return data
|
|
|
+}
|
|
|
+
|
|
|
+app.get('/trade', async (req, res) => {
|
|
|
+ let older = req.query.older
|
|
|
+ let data = await trade(older)
|
|
|
+ res.send(data.alipay_trade_query_response)
|
|
|
+})
|
|
|
+
|
|
|
+app.listen('8000', () => {
|
|
|
+ console.log('server start')
|
|
|
+})
|