nodejs WeChatで支払う
3306 ワード
WeChat支払いは、WeChat公衆プラットフォームにWeChatライセンスディレクトリとWeChat Resportディレクトリを設定し、あなたが支払う時に支払うデータを収集するディレクトリであり、ライセンスディレクトリのみが支払うことができます. WeChatで支払う前にユーザ認証を行います.
router.js
pay.
router.js
var express = require('express');
var router = express.Router();
var OAuth = require('wechat-oauth');
var client = new OAuth(config.wx_appid, config.wx_secret);
var middleware = require('wechat-pay').middleware;
var Payment = require('wechat-pay').Payment;
var initConfig = {
partnerKey: config.wechat.partnerKey,
appId: config.wechat.appId,
mchId: config.wechat.mchId,
notifyUrl: "http://xxxxxxxxxxxxxx/notify/",
pfx: fs.readFileSync("apiclient_cert.p12")
};
var payment = new Payment(initConfig);
/**
*
* state
**/
router.get('/auth', function(req, res){
var url = client.getAuthorizeURL('http://xxxxxxx/ ',state, 'snsapi_userinfo');
res.redirect(url);
});
/**
*
**/
router.get('/ ', function(req, res) {
client.getAccessToken(code, function (err, result) {
var openid = result.data.openid;
var order = {
body: ' ',
attach: ' ',
out_trade_no: ' ( , )',
total_fee: ' ( )',
spbill_create_ip: ' ip',
openid: ' openid',
trade_type: 'JSAPI'
};
payment.getBrandWCPayRequestParams(order, function(err, payargs){
if(err) {
log.error(err);
}
res.render(' ', {
appId: payargs.appId,
timeStamp: payargs.timeStamp,
nonceStr: payargs.nonceStr,
package: payargs.package,
signType: payargs.signType,
paySign: payargs.paySign,
body:body,
total:total,
num:num,
proname:project_name,
state:state
});
});
});
});
/**
*
*( )
*/
router.post('/notify', middleware(initConfig).getNotify().done(function(message, req, res, next) {
console.log(message);//
if(message.return_code == 'SUCCESS' && message.result_code == 'SUCCESS'){
//
}
}));
//
module.exports = router;
支払いページpay.
function goPay(appId, timeStamp, nonceStr, package, signType, paySign) {
WeixinJSBridge.invoke('getBrandWCPayRequest', {
"appId" : appId,
"timeStamp":timeStamp,
"nonceStr" : nonceStr,
"package" : package,
"signType" : signType,
"paySign" : paySign
}, function(res){
if(res.err_msg == "get_brand_wcpay_request:ok"){
alert(" ");
}else{
alert(" , ");
}
});
}