123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 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')
- })
|