Commit 287514a0 authored by 任国军's avatar 任国军

add wechat notify

parent 5199994f
Pipeline #24333 passed with stage
in 4 seconds
......@@ -4,17 +4,8 @@ const xml2js = require('xml2js');
const Controller = require('egg').Controller;
class TestController extends Controller {
async test() {
this.ctx.set('content-type', 'text/xml');
const data = {
MsgType: 'transfer_customer_service',
ToUserName: 'to',
FromUserName: 'from',
CreateTime: parseInt(new Date() / 1000),
};
const builder = new xml2js.Builder({ rootName: 'xml' });
let xml = builder.buildObject(data);
xml = '<xml><ToUserName><![CDATA[$openid]]></ToUserName><FromUserName><![CDATA[$appId]]></FromUserName><CreateTime>$createTine</CreateTime><MsgType><![CDATA[transfer_customer_service]]></MsgType></xml>';
this.ctx.body = xml;
const ret = await this.service.course.v5.wechat.pay({});
this.ctx.success(ret);
}
}
module.exports = TestController;
......@@ -93,6 +93,7 @@ class WechatController extends Controller {
xmlData += '<return_msg><![CDATA[OK]]></return_msg>';
xmlData += '</xml>';
ctx.body = xmlData;
console.logger.info(ctx.request.body);
return ctx.body;
}
}
......
......@@ -314,17 +314,23 @@ class WechatService extends Service {
const appId = 'wx07a5f0ed5bdf4751';
const mchId = '1596633771';
const key = 'xuepenQuXuanKeweixinzhifu2020063';
const nonce_str = ctx.helper.md5(moment().millisecond()).splice(0, 16);
const nonce_str = ctx.helper.md5(moment().millisecond()).slice(0, 16);
const body = input.body || '趣选课-测试';
const out_trade_no = '2020060315300001';
const total_fee = '0.01';
const spbill_create_ip = ctx.helper.getClientIP();
const total_fee = '1';
const spbill_create_ip = ctx.helper.getClientIP() || '127.0.0.1';
const notify_url = 'https://uat-nginx.jianbing.com/51business/api/course/v5/wechat/pay/notify';
const trade_type = 'JSAPI';
const openid = 'ofaz74vASbOyxwS1hyxrmQjYatlU';
// 签名
const sign = ctx.helper.md5(`appid=${appId}&body=${body}&mch_id=${mchId}&nonce_str=${nonce_str}&notify_url=${notify_url}&out_trade_no=${out_trade_no}&spbill_create_ip=${spbill_create_ip}&total_fee=${total_fee}&trade_type=${trade_type}&key=${key}`);
sign.toUpperCase();
const arr = [ `appid=${appId}`, `body=${body}`, `mch_id=${mchId}`, `nonce_str=${nonce_str}`, `notify_url=${notify_url}`, `out_trade_no=${out_trade_no}`, `spbill_create_ip=${spbill_create_ip}`, `total_fee=${total_fee}`, `trade_type=${trade_type}`, `openid=${openid}` ];
arr.sort();
let sign = ctx.helper.md5(arr.join('&') + `&key=${key}`);
console.log(arr.join('&') + `&key=${key}`);
sign = sign.toUpperCase();
console.log(sign);
// 组装xml数据
let xmlData = '<xml>';
......@@ -337,11 +343,13 @@ class WechatService extends Service {
xmlData += '<spbill_create_ip>' + spbill_create_ip + '</spbill_create_ip>';
xmlData += '<total_fee>' + total_fee + '</total_fee>';
xmlData += '<trade_type>' + trade_type + '</trade_type>';
xmlData += '<openid>' + openid + '</openid>';
xmlData += '<sign>' + sign + '</sign>';
xmlData += '</xml>';
const ret = request({ url: 'https://api.mch.weixin.qq.com/pay/unifiedorder', method: 'POST', body: xmlData }, function(err, response, body) {
if (!err && response.statusCode === 200) {
console.log('result:');
console.log(body);
return body;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment