123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 'use strict';
- const xmlreader = require('xmlreader');
- const fs = require('fs');
- const Service = require('egg').Service;
- class WxPayService extends Service {
- constructor(ctx) {
- super(ctx);
- this.abc = 'wxa9997e40dea04213';
- }
- // 把金额转为分
- async getmoney(money) {
- return parseFloat(money) * 100;
- }
- // 随机字符串产生函数
- async createNonceStr() {
- return Math.random().toString(36).substr(2, 15);
- }
- // 时间戳产生函数
- async createTimeStamp() {
- return parseInt(new Date().getTime() / 1000) + '';
- }
- // 签名加密算法
- async paysignjsapi(appid, body, mch_id, nonce_str, notify_url, out_trade_no, spbill_create_ip, total_fee, trade_type, mchkey, openid) {
- const ret = {
- appid,
- mch_id,
- nonce_str,
- body,
- notify_url,
- out_trade_no,
- spbill_create_ip,
- total_fee,
- trade_type,
- openid,
- };
- let string = await this.raw(ret);
- const key = mchkey;
- string = string + '&key=' + key;
- const crypto = require('crypto');
- return crypto.createHash('md5').update(string, 'utf8').digest('hex')
- .toUpperCase();
- }
- // 签名加密算法,第二次的签名
- async paysignjsapifinal(appid, mch_id, prepayid, noncestr, timestamp, mchkey) {
- const ret = {
- appId: appid,
- timeStamp: timestamp,
- nonceStr: noncestr,
- package: 'prepay_id=' + prepayid,
- signType: 'MD5',
- };
- let string = await this.raw(ret);
- const key = mchkey;
- string = string + '&key=' + key;
- const crypto = require('crypto');
- return crypto.createHash('md5').update(string, 'utf8').digest('hex')
- .toUpperCase();
- }
- async getXMLNodeValue(xml) {
- // var tmp = xml.split("<"+node_name+">");
- // console.log('tmp',tmp);
- // var _tmp = tmp[1].split("</"+node_name+">");
- // console.log('_tmp',_tmp);
- // return _tmp[0];
- const parseObj = await new Promise(function(resolve) {
- xmlreader.read(xml, function(errors, response) {
- if (errors !== null) {
- console.log(errors);
- return;
- }
- const prepay_id = response.xml.prepay_id.text();
- resolve(prepay_id);
- });
- });
- return parseObj;
- }
- async raw(args) {
- let keys = Object.keys(args);
- keys = keys.sort();
- const newArgs = {};
- keys.forEach(function(key) {
- newArgs[key] = args[key];
- });
- let string = '';
- for (const k in newArgs) {
- string += '&' + k + '=' + newArgs[k];
- }
- string = string.substr(1);
- return string;
- }
- }
- module.exports = WxPayService;
|