天天看点

sails 框架微信支付回调解析xml中间件配置

1、npm安装中间件

2、修改框架config/http.js

将middleware中的 bodyParser改为:

bodyParser: (function () {
  var skipper = require('skipper')();
  var xmlparser = require('express-xml-bodyparser')();
  return function (req, res, next) {
    if (req.headers && (req.headers['content-type'] == 'text/xml' ||
        req.headers['content-type'] == 'application/xml')) {
      return xmlparser(req, res, next);
    }
    return skipper(req, res, next);
  };
      

})()

3、支付回调中可以从body取xml值,

req.body.xml 是一个json对象,不过没个key的值是一个数组,例如: 

{ appid: [ 'xxxxxxxxxxxx' ], attach: [ '直播充值' ], bank_type: [ 'CFT' ], cash_fee: [ '1' ],  fee_type: [ 'CNY' ],  is_subscribe: [ 'Y' ], mch_id: [ '1482240392' ],  nonce_str: [ 'z1gtrk61cee' ],  openid: [ 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ], out_trade_no: [ 'xxxxxxxxxxxxxxxxxxxxxxxx' ],  result_code: [ 'SUCCESS' ], return_code: [ 'SUCCESS' ], sign: [ 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ],  time_end: [ '20180612153951' ], total_fee: [ '1' ],  trade_type: [ 'NATIVE' ], transaction_id: [ 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ] }

所以在取值判断的时候要注意

继续阅读