|
@@ -0,0 +1,67 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const _ = require('lodash');
|
|
|
+const Controller = require('egg').Controller;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 微信支付
|
|
|
+ */
|
|
|
+class WxPayController extends Controller {
|
|
|
+ /**
|
|
|
+ * 微信支付取得sign等参数
|
|
|
+ */
|
|
|
+ async wxpaysign() {
|
|
|
+ const { orderno, money, content, openid } = this.ctx.request.body;
|
|
|
+ assert(orderno, 'orderno不能为空');
|
|
|
+ assert(openid, 'openid不能为空');
|
|
|
+ assert(money, 'money不能为空');
|
|
|
+ const { wxapi } = this.app.config;
|
|
|
+ const mch_id = wxapi.mchid;
|
|
|
+ const nonce_str = await this.service.wxpay.createNonceStr();
|
|
|
+ const timestamp = await this.service.wxpay.createTimeStamp();
|
|
|
+ const body = content;
|
|
|
+ const out_trade_no = orderno;
|
|
|
+ const total_fee = await this.service.wxpay.getmoney(money);
|
|
|
+ const spbill_create_ip = this.ctx.ip;
|
|
|
+ const notify_url = wxapi.wxurl;
|
|
|
+ const trade_type = 'JSAPI';
|
|
|
+ const sign = await this.service.wxpay.paysignjsapi(wxapi.appid, body, mch_id, nonce_str, notify_url, out_trade_no, spbill_create_ip, total_fee, trade_type, wxapi.mchkey, openid);
|
|
|
+ console.log('sign==', sign);
|
|
|
+ let formData = '<xml>';
|
|
|
+ formData += '<appid>' + wxapi.appid + '</appid>'; // appid
|
|
|
+ formData += '<body><![CDATA[' + body + ']]></body>';
|
|
|
+ formData += '<mch_id>' + mch_id + '</mch_id>'; // 商户号
|
|
|
+ formData += '<nonce_str>' + nonce_str + '</nonce_str>'; // 随机字符串,不长于32位。
|
|
|
+ formData += '<notify_url>' + notify_url + '</notify_url>';
|
|
|
+ formData += '<out_trade_no>' + out_trade_no + '</out_trade_no>';
|
|
|
+ formData += '<spbill_create_ip>' + spbill_create_ip + '</spbill_create_ip>';
|
|
|
+ formData += '<total_fee>' + total_fee + '</total_fee>';
|
|
|
+ formData += '<trade_type>' + trade_type + '</trade_type>';
|
|
|
+ formData += '<sign>' + sign + '</sign>';
|
|
|
+ formData += '<openid>' + openid + '</openid>';
|
|
|
+ formData += '</xml>';
|
|
|
+ console.log('formData===', formData);
|
|
|
+
|
|
|
+ const payurl = wxapi.payurl;
|
|
|
+ const paydata = await this.ctx.curl(payurl, {
|
|
|
+ method: 'post',
|
|
|
+ data: formData,
|
|
|
+ });
|
|
|
+ const prepay_id = await this.service.wxpay.getXMLNodeValue(paydata.data.toString('UTF-8'));
|
|
|
+ console.log('解析后的prepay_id==', prepay_id);
|
|
|
+ // 将预支付订单和其他信息一起签名后返回给前端
|
|
|
+ const finalsign = await this.service.wxpay.paysignjsapifinal(wxapi.appid, mch_id, prepay_id, nonce_str, timestamp, wxapi.mchkey);
|
|
|
+ const res = { appId: wxapi.appid, partnerId: wxapi.mchid, prepayId: prepay_id, nonceStr: nonce_str, timeStamp: timestamp, package: 'prepay_id=' + prepay_id, sign: finalsign };
|
|
|
+ this.ctx.ok({ data: res });
|
|
|
+ }
|
|
|
+
|
|
|
+ async wxpaysignback() {
|
|
|
+ const { ctx } = this;
|
|
|
+ console.log();
|
|
|
+ ctx.body = 'hi, egg';
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = WxPayController;
|