Skip to content

Instantly share code, notes, and snippets.

@ibeeger
Last active May 25, 2018 09:28
Show Gist options
  • Save ibeeger/1c20c633994604acac58b8b5834613b1 to your computer and use it in GitHub Desktop.
Save ibeeger/1c20c633994604acac58b8b5834613b1 to your computer and use it in GitHub Desktop.
微信支付总结

微信公众号支付

准备工作

  • appid 公众号id
  • mch_id 商户号 公众号申请微信支付成功后 会有个商户号
  • key 密钥 申请成功后会收到邮件 会带着密钥 用于签名

配置环境:

  • 配置支付目录(开发,生产环境)
  • 配置支付成功回调地址

开发

let item = {}
	Object.keys(json).sort().map(function(key) {
		item[key] = json[key];
	});
 

3、拼接 (记得加上 key) 放在最后就行

querystring.stringify(json)

4、签名 md5 记得转换大写

let t = crypto.createHash("md5");
t.update(url, "utf8", "hex");
t.digest("hex").toUpperCase();

5、增加sign字段
6、请求的时候 记得转换xml

  • h5发起支付
    签名注意 1、时间戳记得转换成字符串 ios会提示有问题 2、记得加上key

退款

 const json = {
	appid: '****',
	mch_id: '****',
	nonce_str:Math.random().toString(36).substr(2),
	out_trade_no:process.argv[2], //支付的订单id
	out_refund_no:"HT*******", //退款id需要自己生成
	total_fee:"1",
	refund_fee:"1"
}
let _post = util.sign(json);
_post = js2xmlparser.parse("xml", _post);

注意 退款是需要证书的: https://api.mch.weixin.qq.com/secapi/pay/refund

const agent = new http.Agent({
	pfx: fs.readFileSync(__dirname + "/../../cert/apiclient_cert.p12"),
	passphrase: json.mch_id 
});

let _options = {
		protocol: protocol,
		hostname: host,
		port: port,
		path: url,
		method: method,
		agent:agent,
		headers: {
		}
	};
let _req = http.request(_options, (res)=>{});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment