Skip to content

Commit

Permalink
Merge pull request #4 from jipan25/master
Browse files Browse the repository at this point in the history
新增:退款接口
  • Loading branch information
tvrcgo committed Dec 16, 2015
2 parents 6c9565e + 7abd0e6 commit 9b564ae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ wxpay.closeOrder({ out_trade_no:"xxxxxx"}, function(err, result){
console.log(result);
});
```
退款接口
```js
let params = {
appid: 'xxxxxxxx',
mch_id: '1234567890',
op_user_id: '商户号即可',
out_refund_no: '20140703'+Math.random().toString().substr(2, 10),
total_fee: '1', //原支付金额
refund_fee: '1', //退款金额
transaction_id: '微信订单号' }

wxpay.refund(params, function(err, result){
console.log('refund', arguments);
});
```

### 原生支付 (NATIVE)

Expand Down Expand Up @@ -107,7 +122,14 @@ WeixinJSBridge.invoke(
商户服务端处理微信的回调(express为例)
```js
var router = express.Router();
var wxpay = require('weixin-pay');
var WXPay = require('weixin-pay');

var wxpay = WXPay({
appid: 'xxxxxxxx',
mch_id: '1234567890',
partner_key: 'xxxxxxxxxxxxxxxxx', //微信商户平台API密钥
pfx: fs.readFileSync('./wxpay_cert.p12'), //微信商户平台证书
});

// 原生支付回调
router.use('/wxpay/native/callback', wxpay.useWXCallback(function(msg, req, res, next){
Expand Down
26 changes: 25 additions & 1 deletion lib/wxpay.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,28 @@ WXPay.mix('closeOrder', function(order, fn){
}, function(err, res, body){
util.parseXML(body, fn);
});
});
});


WXPay.mix('refund',function(order, fn){
if (!(order.transaction_id || order.out_refund_no)) {
fn(null, { return_code: 'FAIL', return_msg:'缺少参数' });
}

order.nonce_str = order.nonce_str || util.generateNonceString();
util.mix(order, this.wxpayID);
order.sign = this.sign(order);

request({
url: "https://api.mch.weixin.qq.com/secapi/pay/refund",
method: "POST",
body: util.buildXML({xml: order}),
agentOptions: {
pfx: this.options.pfx,
passphrase: this.options.mch_id
}
}, function(err, response, body){
util.parseXML(body, fn);
});
});

3 comments on commit 9b564ae

@icmr
Copy link

@icmr icmr commented on 9b564ae Mar 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通过npm install安装的版本并没有退款接口的代码

@jipan25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner没有发布到npm上吧

@tvrcgo
Copy link
Owner Author

@tvrcgo tvrcgo commented on 9b564ae Mar 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已发布

Please sign in to comment.