main.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. const axios = require('axios'),
  2. crypto = require('crypto'),
  3. path = require('path'),
  4. fs = require('fs'),
  5. qs = require('querystring'),
  6. urlencode = require('urlencode'),
  7. iconv = require('iconv-lite'),
  8. qr = require('qr-image'),
  9. express = require('express'),
  10. app = express()
  11. const sandbox = fs.readFileSync(path.resolve(__dirname, './sandbox_iobox_private.pem')).toString()
  12. const format = (fmt = 'yyyy-MM-dd hh:mm:ss') => {
  13. let date = new Date()
  14. var o = {
  15. "M+": date.getMonth() + 1, //月份
  16. "d+": date.getDate(), //日
  17. "h+": date.getHours(), //小时
  18. "m+": date.getMinutes(), //分
  19. "s+": date.getSeconds(), //秒
  20. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  21. "S": date.getMilliseconds() //毫秒
  22. };
  23. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  24. for (var k in o)
  25. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  26. return fmt;
  27. }
  28. const time = format()
  29. const userconfig = {
  30. app_id: "2016032301234394",
  31. version: "1.0",
  32. format: "json",
  33. sign_type: "RSA2",
  34. method: "alipay.system.oauth.token",
  35. timestamp: time,
  36. auth_token: null,
  37. alipay_sdk: "alipay-sdk-php-20161101",
  38. terminal_type: null,
  39. terminal_info: null,
  40. prod_code: null,
  41. notify_url: null,
  42. charset: "utf-8",
  43. app_auth_token: null,
  44. grant_type: 'authorization_code',
  45. code: '',
  46. }
  47. const test = {
  48. alipay_sdk: 'alipay-sdk-php-20161101',
  49. app_id: '2016032301234394',
  50. charset: 'utf-8',
  51. code: '',
  52. format: 'json',
  53. grant_type: 'authorization_code',
  54. method: 'alipay.system.oauth.token',
  55. sign_type: 'RSA2',
  56. timestamp: time,
  57. version: '1.0'
  58. }
  59. /**
  60. * 生成签名
  61. * @param {String} content 签名内容
  62. * @param {String} privateKey 私钥
  63. * @returns {String}
  64. */
  65. const getSign = (content, privateKey = sandbox) => {
  66. let data = crypto.createSign('RSA-SHA256').update(content).sign(privateKey,'base64')
  67. return urlencode(data)
  68. }
  69. const splice = (msg) => {
  70. let data = ''
  71. for (var prop in msg) {
  72. data += prop + '=' + msg[prop] + '&'
  73. }
  74. return data.substr(0,data.length-1)
  75. }
  76. const requester = async (url) => {
  77. try {
  78. let {data, status} = await axios({
  79. url: url,
  80. method: 'get',
  81. headers: {
  82. 'content-type': 'application/x-www-form-urlencoded'
  83. }
  84. })
  85. if (status === 200) {
  86. return data
  87. }
  88. } catch(e) {
  89. console.log(e)
  90. }
  91. }
  92. const out = {
  93. alipay_sdk: 'alipay-sdk-php-20161101',
  94. app_id: '2016032301234394',
  95. biz_content: '',
  96. charset: 'utf-8',
  97. format: 'json',
  98. method: 'alipay.trade.create',
  99. sign_type: 'RSA2',
  100. timestamp: time,
  101. version: '1.0',
  102. }
  103. const outreq = {
  104. app_id: '2016032301234394',
  105. version: '1.0',
  106. format: 'json',
  107. sign_type: 'RSA2',
  108. method: 'alipay.trade.create',
  109. timestamp: time,
  110. auth_token: null,
  111. alipay_sdk: 'alipay-sdk-php-20161101',
  112. terminal_type: null,
  113. terminal_info: null,
  114. prod_code: null,
  115. notify_url: null,
  116. charset: 'utf-8',
  117. app_auth_token: null,
  118. }
  119. const requesters = async (userid) => {
  120. let tradeno = Date.now().toString()
  121. out.biz_content = '{"out_trade_no":"' + tradeno + '","subject":"ceshi","buyer_id":"'+ userid +'","total_amount":"0.01"}'
  122. let url = 'https://openapi.alipay.com/gateway.do?' + qs.stringify(outreq) + '&sign=' + getSign(splice(out)) + '&biz_content=' + urlencode(out.biz_content)
  123. let data = await requester(url)
  124. return data
  125. }
  126. app.use('/', express.static(__dirname + '/'))
  127. app.get('/qr', (req, res) => {
  128. let text = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2016032301234394&scope=auth_base&redirect_uri=http://10.3.16.61:8000/'
  129. let img = qr.image(text ,{size :10});
  130. res.writeHead(200, {'Content-Type': 'image/png'});
  131. img.pipe(res);
  132. })
  133. app.get('/token', async (req, res) => {
  134. userconfig.code = req.query.auth_code
  135. test.code = req.query.auth_code
  136. let url = 'https://openapi.alipay.com/gateway.do?' + qs.stringify(userconfig) + '&sign=' + getSign(splice(test))
  137. let data = await requester(url)
  138. let trade = await requesters(data.alipay_system_oauth_token_response.user_id)
  139. res.send(trade.alipay_trade_create_response.trade_no)
  140. })
  141. const sigtrader = {
  142. alipay_sdk: 'alipay-sdk-php-20161101',
  143. app_id: '2016032301234394',
  144. biz_content: '{"trade_no":"2018062021001004450535911615"}',
  145. charset: 'utf-8',
  146. format: 'json',
  147. method: 'alipay.trade.query',
  148. sign_type: 'RSA2',
  149. timestamp: time,
  150. version: '1.0',
  151. }
  152. const reqtrader = {
  153. app_id: '2016032301234394',
  154. version: '1.0',
  155. format: 'json',
  156. sign_type: 'RSA2',
  157. method: 'alipay.trade.query',
  158. timestamp: time,
  159. auth_token: null,
  160. alipay_sdk: 'alipay-sdk-php-20161101',
  161. terminal_type: null,
  162. terminal_info: null,
  163. prod_code: null,
  164. notify_url: null,
  165. charset: 'utf-8',
  166. app_auth_token: null,
  167. }
  168. const trade = async (msg) => {
  169. sigtrader.biz_content = '{"trade_no":"' + msg + '"}'
  170. let url = 'https://openapi.alipay.com/gateway.do?' + qs.stringify(reqtrader) + '&sign=' + getSign(splice(sigtrader)) + '&biz_content=' + urlencode(sigtrader.biz_content)
  171. let data = await requester(url)
  172. return data
  173. }
  174. app.get('/trade', async (req, res) => {
  175. let older = req.query.older
  176. let data = await trade(older)
  177. res.send(data.alipay_trade_query_response)
  178. })
  179. app.listen('8000', () => {
  180. console.log('server start')
  181. })