微信小程序支付签名生成(客户端) |
您所在的位置:网站首页 › 如何看微信签名 › 微信小程序支付签名生成(客户端) |
支付流程
微信支付主要有两步,第一步调用下单接口,第二步根据下单接口返回的预支付ID调用微信小程序的支付接口。这里下单接口是服务器调用的,支付功能是客户端做的,所以根据服务端返回的预支付ID加上其它参数加密后然后调用微信支付的wx.requestPayment()接口。中间用到的加密文件下文会有给出 二、签名生成方式 const MD5 = require('../../utils/md5.js') // 引入md5.js文件,md5文件在下面给出了 //调用微信支付 // payInfo 是后台下单后返回的,格式可能不一样, //只要取出里面的随机字符串(nonce_str)和预支付ID(prepay_id)就好了, //这两个字段,微信下单接口在请求时候要么会用,要么会返回,肯定会有的。 wxPay: function(payInfo){ let times = Date.parse(new Date())/1000;//这里传入的是秒,注意处于1000, let paySign = this.paysignjs( payInfo.nonce_str, `prepay_id=${payInfo.prepay_id}`,//注意拼接的内容 times ); console.log('YM---->签名结果:',paySign); wx.requestPayment({ timeStamp: `${times}`, nonceStr: payInfo.nonce_str, package: `prepay_id=${payInfo.prepay_id}`, signType: 'MD5', paySign: paySign, success (res) { console.log('YM',"--success-->支付结果:",res) }, fail (res) { console.log('YM',"--fail-->支付结果:",res) } }) } // 支付md5加密获取sign // nonceStr : 后台返回的随机字符串 //packagePay:后台返回的预支付id,一般为 prepay_id,传入参数的时候注意加前缀 “prepay_id=” paysignjs: function(nonceStr, packagePay, timeStamp) { //字符串拼接的顺序不要乱,必须按照这个格式 var string = "appId=xxxxxxx"//appId +`&nonceStr=${nonceStr}`//随机字符串 +`&package=${packagePay}`//预支付ID +"&signType=MD5"//签名类型 +`&timeStamp=${timeStamp}`//时间戳 +"&key=xxxxxxxxxxxxx";//32位的密钥,商户密钥或者是小程序的密钥(我设置的是一样的,所以不大知道是哪个) console.log('YM---->签名的原始字符串',string); let miyao = MD5.hexMD5(string); // 使用加密 return miyao; }, 三、依赖代码由于涉及到加密操作,这里主要使用的是MD5加密,这个加密方式要跟后台调用下单接口生成的sign方式保持一致,MD5加密貌似微信没有,所以这里使用的是网上的MD5加密方式,代码如下: /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. * Code also contributed by Greg Holt * See http://pajhome.org.uk/site/legal.html for details. */ /* * Add integers, wrapping at 2^32. This uses 16-bit operations internally * to work around bugs in some JS interpreters. */ function safe_add(x, y) { var lsw = (x & 0xFFFF) + (y & 0xFFFF) var msw = (x >> 16) + (y >> 16) + (lsw >> 16) return (msw return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b) } function ff(a, b, c, d, x, s, t) { return cmn((b & c) | ((~b) & d), a, b, x, s, t) } function gg(a, b, c, d, x, s, t) { return cmn((b & d) | (c & (~d)), a, b, x, s, t) } function hh(a, b, c, d, x, s, t) { return cmn(b ^ c ^ d, a, b, x, s, t) } function ii(a, b, c, d, x, s, t) { return cmn(c ^ (b | (~d)), a, b, x, s, t) } /* * Calculate the MD5 of an array of little-endian words, producing an array * of little-endian words. */ function coreMD5(x) { var a = 1732584193 var b = -271733879 var c = -1732584194 var d = 271733878 for (var i = 0; i var hex_tab = "0123456789abcdef" var str = "" for (var i = 0; i var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" var str = "" for (var i = 0; i var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks var blks = new Array(nblk * 16) for (var i = 0; i 2] |= (str.charCodeAt(i) & 0xFF) 2] |= 0x80 return binl2hex(coreMD5(str2binl(str))) } function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) } function b64MD5(str) { return binl2b64(coreMD5(str2binl(str))) } function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) } /* Backward compatibility */ function calcMD5(str) { return binl2hex(coreMD5(str2binl(str))) } module.exports = { hexMD5: hexMD5 } 四、参考链接https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_12.shtml https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=5 https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_2.shtml |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |