基于nodejs后端⽀付接⼝
本⽂不谈框架,不谈代码组织结构,只谈怎么实现,
1,移动端⽀付流程如下:
这个流程可以app⽀付⽂档到,最重要的就是统⼀下单这个接⼝,可以简化⼀下这个接⼝,简化后的流程如下:
app给后端传:⾦额total_fee,32位的商户订单号out_trade_no,⽀付成功后的回调地址notify_url 这最基本的3个参数,后端通过的统⼀下单接
⼝h.weixin.qq/pay/unifiedorder ⽣成⼀些参数,在返回给app端,app根据这些参数再加上授权,调起⽀付,⽀付成功后返回到商家app,后台⾃动检测是否⽀出成功,成功后调⽤app传给后端的回调地址notify_url接⼝,app后端在回调地址的接⼝中处理什么数据库改写操作,发消息等等的动作。
nodejs需要的⼀些包:
"dependencies": {
"crypto": "^1.0.1",
"express": "^4.16.3",
"request": "^2.85.0",
"xmlreader": "^0.2.3"
}
xmlreader处理xml的⼯具包,crypto数据加密包
2,⼯具类util.js
var xmlreader = require("xmlreader");
诗经中的女孩灵动名字var fs = require("fs");
var wxpay = {
//把⾦额转为分
getmoney: function (money) {
return parseFloat(money) * 100;
},
/
晋江好文推荐/ 随机字符串产⽣函数
createNonceStr: function () {
return Math.random().toString(36).substr(2, 15);
},
// 时间戳产⽣函数
createTimeStamp: function () {
return parseInt(new Date().getTime() / 1000) + '';
},
//签名加密算法
paysignjsapi: function (appid, body, mch_id, nonce_str, notify_url, out_trade_no, spbill_create_ip, total_fee, trade_type, mchkey) { var ret = {
appid: appid,
mch_id: mch_id,
nonce_str: nonce_str,
body: body,
notify_url: notify_url,
out_trade_no: out_trade_no,
spbill_create_ip: spbill_create_ip,
total_fee: total_fee,
trade_type: trade_type
};
console.log('ret==', ret);
var string = raw(ret);
var key = mchkey;
string = string + '&key=' + key;
console.log('string=', string);
var crypto = require('crypto');
ateHash('md5').update(string, 'utf8').digest('hex').toUpperCase();
},
弥//签名加密算法,第⼆次的签名
paysignjsapifinal: function (appid,mch_id,prepayid,noncestr,timestamp,mchkey) {
优秀员工评选办法var ret = {
appid: appid,
partnerid: mch_id,
prepayid: prepayid,
package: 'Sign=WXPay',
noncestr: noncestr,
timestamp: timestamp,
};
console.log('retretret==', ret);
var string = raw(ret);
var key = mchkey;
string = string + '&key=' + key;
console.log('stringstringstring=', string);
var crypto = require('crypto');
ateHash('md5').update(string, 'utf8').digest('hex').toUpperCase();
},
getXMLNodeValue: function (xml) {
// var tmp = xml.split("<"+node_name+">");
// console.log('tmp',tmp);
// var _tmp = tmp[1].split("</"+node_name+">");
// console.log('_tmp',_tmp);
// return _tmp[0];迈腾轮毂
if (null !== errors) {
匮怎么读console.log(errors)
return;
}
console.log('长度===', l.().length);
var prepay_id = l.();
console.log('解析后的prepay_id==',prepay_id);
return prepay_id;
});
}
}
function raw(args) {
var keys = Object.keys(args);
keys = keys.sort()
var newArgs = {};
keys.forEach(function (key) {
newArgs[key] = args[key];
});
var string = '';
for (var k in newArgs) {
string += '&' + k + '=' + newArgs[k];
}
string = string.substr(1);
return string;
}
3,调⽤app.js
appid,appsecret是开放平台的数据,mchid是对应的商户平台id,mchkey是商户平台陪的安全秘钥。
var express = require('express');
var request = require('request');
var xmlreader = require("xmlreader");
var fs = require("fs");
var app = express();
var wxpay = require('./util');
var appid = 'wx28f86efd25cc3312';
var appsecret = 'b8687555bf947b1947b62767c448723';
var mchid = '1499403456'
var mchkey = '8r435jVd7yA0354nsvkxb4cN3x7Se4322';
var wxurl = 'XXXXXXXXX/weixinNotify.action';
<('/',(req,res)=>{
//⾸先拿到前端传过来的参数
let orderCode = derCode;
let money = ;
let orderID = derID;
console.log('APP传过来的参数是',orderCode+'----'+money+'------'+orderID+'----'+appid+'-----'+appsecret+'-----'+mchid+'-----'+mchkey);
//⾸先⽣成签名sign
appid
let mch_id = mchid;
let nonce_str = ateNonceStr();
let timestamp = ateTimeStamp();
let body = '测试⽀付';
let out_trade_no = orderCode;
let total_fee = ey(money);
let spbill_create_ip = Address;
let notify_url = wxurl;
let trade_type = 'APP';
let sign = wxpay.paysignjsapi(appid,body,mch_id,nonce_str,notify_url,out_trade_no,spbill_create_ip,total_fee,trade_type,mchkey);
console.log('sign==',sign);
//组装xml数据
var formData = "<xml>";
formData += "<appid>"+appid+"</appid>"; //appid
formData += "<body><![CDATA["+"测试⽀付"+"]]></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 += "</xml>";
console.log('formData===',formData);
var url = 'h.weixin.qq/pay/unifiedorder';
request({url:url,method:'POST',body: formData},function(err,response,body){
if(!err && response.statusCode == 200){
console.log(body);
if (null !== errors) {
console.log(errors)
return;
}
console.log('长度===', l.().length);
var prepay_id = l.();
console.log('解析后的prepay_id==',prepay_id);
//将预⽀付订单和其他信息⼀起签名后返回给前端
let finalsign = wxpay.paysignjsapifinal(appid,mch_id,prepay_id,nonce_str,timestamp,mchkey);
res.json({'appId':appid,'partnerId':mchid,'prepayId':prepay_id,'nonceStr':nonce_str,'timeStamp':timestamp,'package':'Sign=WXPay','sign':finalsign}); });
}
});
})
app.listen(3000,()=>{
console.log('服务器启动了....'); });
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论