WeChat payment for node.js
npm install weixin-pay
Create a unified payment order
var WXPay = require ( 'weixin-pay' ) ;
var wxpay = WXPay ( {
appid : 'xxxxxxxx' ,
mch_id : '1234567890' ,
partner_key : 'xxxxxxxxxxxxxxxxx' , //微信商户平台API密钥
pfx : fs . readFileSync ( './wxpay_cert.p12' ) , //微信商户平台证书
} ) ;
wxpay . createUnifiedOrder ( {
body : '扫码支付测试' ,
out_trade_no : '20140703' + Math . random ( ) . toString ( ) . substr ( 2 , 10 ) ,
total_fee : 1 ,
spbill_create_ip : '192.168.2.210' ,
notify_url : 'http://wxpay_notify_url' ,
trade_type : 'NATIVE' ,
product_id : '1234567890'
} , function ( err , result ) {
console . log ( result ) ;
} ) ;
Query order
// 通过微信订单号查
wxpay . queryOrder ( { transaction_id : "xxxxxx" } , function ( err , order ) {
console . log ( order ) ;
} ) ;
// 通过商户订单号查
wxpay . queryOrder ( { out_trade_no : "xxxxxx" } , function ( err , order ) {
console . log ( order ) ;
} ) ;
Close order
wxpay . closeOrder ( { out_trade_no : "xxxxxx" } , function ( err , result ) {
console . log ( result ) ;
} ) ;
Refund interface
var 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 ) ;
} ) ;
Provide a function to generate a payment QR code link, and generate a QR code from the URL for the user to scan.
var url = wxpay . createMerchantPrepayUrl ( { product_id : '123456' } ) ;
After receiving the callback from WeChat, the merchant backend calls createUnifiedOrder() to generate a prepayment transaction order and returns the resulting XML data to WeChat.
What is mode one?
Directly call the createUnifiedOrder() function to generate a prepayment transaction order, and generate a QR code from the code_url in the result for the user to scan.
What is Mode 2?
Generate JS API payment parameters and send them to the page
wxpay . getBrandWCPayRequestParams ( {
openid : '微信用户 openid' ,
body : '公众号支付测试' ,
detail : '公众号支付测试' ,
out_trade_no : '20150331' + Math . random ( ) . toString ( ) . substr ( 2 , 10 ) ,
total_fee : 1 ,
spbill_create_ip : '192.168.2.210' ,
notify_url : 'http://wxpay_notify_url'
} , function ( err , result ) {
// in express
res . render ( 'wxpay/jsapi' , { payargs : result } )
} ) ;
Web page call parameters (take ejs as an example)
WeixinJSBridge . invoke (
"getBrandWCPayRequest" , < % - JSON . stringify ( payargs ) % > , function ( res ) {
if ( res . err_msg == "get_brand_wcpay_request:ok" ) {
// success
}
} ) ;
The merchant server handles WeChat callbacks (express is an example)
// 原生支付回调
router . use ( '/wxpay/native/callback' , wxpay . useWXCallback ( function ( msg , req , res , next ) {
// msg: 微信回调发送的数据
} ) ) ;
// 支付结果异步通知
router . use ( '/wxpay/notify' , wxpay . useWXCallback ( function ( msg , req , res , next ) {
// 处理商户业务逻辑
// res.success() 向微信返回处理成功信息,res.fail()返回失败信息。
res . success ( ) ;
} ) ) ;