支付分api:https://pay.weixin.qq.com/wiki/doc/apiv3/payscore.php?chapter=14_0&index=1
小程序授权(envVersion目前只能是release(正式版),所以调试也只能正式版调试)
-----res数据来自后台----
wx.navigateToMiniProgram({
appId: 'wxd8f3793ea3b935b8', //授权程序appid(固定)
path: 'pages/use/enable', //授权方地址(固定)
extraData: {
mch_id: res.data.mch_Id,//商户号
service_id: res.data.service_Id,//服务ID,商户后台配置
out_request_no: res.data.out_request_no,//随机数
timestamp: res.data.timeStamp,//时间戳
nonce_str: res.data.nonceStr,//随机数32位
sign_type: "HMAC-SHA256",
sign: res.data.sign//签名串
},
envVersion: 'release',
success(r) {
console.log(r)
//dosomething
util.showToast("授权成功", 1)
},
fail() {
//dosomething
util.showToast("授权失败", 0)
},
后台(.net):
string SERVICE_ID = "";//商户后台配置参数
string MerchantId = "";//商户后台配置参数
string KEY = "";//商户后台配置参数
try
{
Dictionary<string, string> Para_dic = new Dictionary<string, string>();
Para_dic.Add("mch_id", MerchantId);
Para_dic.Add("service_id", SERVICE_ID);
Para_dic.Add("out_request_no", out_request_no);//参数自己生成
Para_dic.Add("timestamp", timeStamp);//时间戳自己生成
Para_dic.Add("nonce_str", nonceStr);//参数自己生成
Para_dic.Add("sign_type", "HMAC-SHA256");
string[] keyArr = { "mch_id", "service_id", "out_request_no", "timestamp", "nonce_str", "sign_type" };
sign = signArithmetic_SHA(keyArr, Para_dic, KEY);
}
catch (Exception) { }
result = JsonConvert.SerializeObject(new { out_request_no = out_request_no, timeStamp = timeStamp, nonceStr = nonceStr, sign = sign, service_Id = SERVICE_ID, mch_Id = MerchantId });
签名串加密方法:
/// <summary>
/// 签名生成算法(SHA256)
/// 返回签名
/// </summary>
public static string signArithmetic_SHA(string[] keyArray, Dictionary<string, string> dataDir, string key)
{
if (keyArray.Length != dataDir.Count)
{
return "";
}
string stringA = "";
//ASCII码排序
Array.Sort(keyArray, string.CompareOrdinal);
for (int i = 0; i < dataDir.Count; i++)
{
stringA += keyArray[i];
foreach (var item in dataDir)
{
if (item.Key == keyArray[i])
{
stringA += "=" + item.Value + "&";
}
}
}
//拼接API密钥
string signtemp = stringA + "key=" + key;
return SHA256(signtemp, key).ToUpper();
}
private static string SHA256(string plaintext, string salt)
{
string result = "";
var enc = Encoding.Default;
byte[]
baText2BeHashed = enc.GetBytes(plaintext),
baSalt = enc.GetBytes(salt);
HMACSHA256 hasher = new HMACSHA256(baSalt);
byte[] baHashedText = hasher.ComputeHash(baText2BeHashed);
result = string.Join("", baHashedText.ToList().Select(b => b.ToString("x2")).ToArray());
return result;
}