最近在帮客户做小程序,使用的uni-app做,在做微信小程序的时候遇到一堆坑,后来发现也不算啥坑,主要是不够细心仔细,这里记录一下实现过程。其实整个过程遵循微信的文档,一步都不能错,所有传参区分大小写,这样才不会遇到各种问题。
第一步:获取用户openID
1.设置manifest.json文件
a.在【APP SDK配置】设置中,【登录鉴权】中勾选【微信登录】,填上微信公众号的Appid和AppSecret。
b.在【微信小程序配置】设置中,填上微信小程序的Appid
2.获取用户OpenID
前端处理部分:
新建一个页面,在脚本的onLoad()里面放如下代码,用来获取用户登录的code,再用这个code发送到后端获取用户的OpenID。
// 调用 微信 login 获取 code
uni.login({
success: (res) => {
uni.request({
url: _self.apiServer, //这里是你后端的地址
method: 'POST',
data: {
usercode: res.code
},
success: (sessions) => {
uni.setStorageSync('OPENID', sessions.data.data.openid + ''); //这里就拿到了用户OpenID了
return [sessions.data.data.session_key];
},
fail: (ex) => {
return false;
}
});
},
fail: (ex) => {
return false;
}
});
后端处理部分:
后端接收到code后,就按下面方法处理,获取用户Openid
string wxappid = "xxxxxxxxxxx";//小程序的APPid
string wxsecret = "xxxxxxxxxxx";//小程序的APPsecret
string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxappid + "&secret=" + wxsecret + "&js_code=" + usercode + "&grant_type=authorization_code";
string strJson = HttpRequestUtil.RequestUrl(url); //这里就获取到了用户的openID了,然后返回给前端
附上RequestUrl方法
public static string RequestUrl(string url, string method)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = method;
request.ContentType = "text/html";
request.Headers.Add("charset", "utf-8");
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
第二步:调用统一支付API,获取最终支付的一些必要参数
前端代码:
uni.request({
url: _self.apiServer, //这里是你后端的地址
method: 'POST',
data: {
user_openid: openid, //用户的OpeinID
filter01: price, //下单的价格
filter02: '预约服务' //下单的描述
},
success: (sessions) => {
//调用用户支付请求
},
fail: (ex) => {
uni.showToast({
title: "请求数据失败!",
icon: "none"
});
return false;
}
});
后端代码:
string wxappid = "xxxxxxxxxxxxxxxxxxx"; //小程序的Appid
Int64 payprice = Convert.ToInt64(Convert.ToDecimal(filter01) * 100); //腾讯默认的单位是分,所以这里转换一下
string payremark = filter02;
string openid = user_openid;
string MerchantID = "xxxxxxxxxxxx"; //商户号
string APIKey = "xxxxxxxxxxxxxxxxxxxxxxxx"; //APIkey
var payment = new Payment(MerchantID, wxappid, APIKey, "这个放你的回调网址");
var orderId = "TS" + DateTime.Now.ToString("yyyyMMddhhmmssffff");
var jsonStr = payment.Pay(payprice, orderId, payremark, "这个放你的服务器IP", openid);
public class Payment
{
private string WeiXinPayUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
private string packageStr = "Sign=WXPay";
///
/// 微信支付商户号(从微信发给你的邮件中获得的)
///
public string MchId
{
get;
internal set;
}
///
/// 应用的APPID(微信发给你的邮件中也有这项内容,一般以wx开头,微信开放平台-管理中心-应用详情也可以看到这项内容)
///
public string AppId
{
get;
internal set;
}
///
/// 这里是API密钥,不是Appsecret,这里最容易出错了!请务必注意!
/// 设置方法:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
///
public string ApiKey
{
get;
internal set;
}
///
/// 支付成功后,微信会请求这个路径,
///
public string NotifyUrl
{
get;
internal set;
}
///
/// 支付类构造函数,三个关键参数缺一不可,均不能为空
///
/// 微信支付商户号(从微信发给你的邮件中获得的)
/// 应用的APPID(微信发给你的邮件中也有这项内容,一般以wx开头,微信开放平台-管理中心-应用详情也可以看到这项内容)
///
/// 这里是API密钥,不是Appsecret,这里最容易出错了!请务必注意!
/// 设置方法:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
///
public Payment(string MchId,string AppId,string ApiKey,string NotifyUrl)
{
this.MchId = MchId;
this.AppId = AppId;
this.ApiKey = ApiKey;
this.NotifyUrl = NotifyUrl;
}
///
/// 开发发起支付
///
/// 总金额,单位:分,不能为空
/// 订单号,你自己定就好了,不要重复,不能为空
/// 订单描述,不能为空
/// 客户端的IP地址,不能为空
/// 用户的OpenID,不能为空
/// 货币类型,默认是CNY,人民币
///
public string Pay(Int64 TotalFee,string TradeNo,string Des,string ClientIp,string Openid, string FeeType = "CNY")
{
//为发送请求给微信服务器准备数据
var nstr = MakeNonceStr();
Hashtable packageParameter = new Hashtable();
packageParameter.Add("appid", this.AppId);
packageParameter.Add("body", Des);
packageParameter.Add("mch_id", this.MchId);
packageParameter.Add("nonce_str", nstr);
packageParameter.Add("notify_url", this.NotifyUrl);
packageParameter.Add("openid", Openid);//小程序支付必须参数
packageParameter.Add("out_trade_no", TradeNo);
packageParameter.Add("spbill_create_ip", ClientIp);
packageParameter.Add("total_fee", TotalFee.ToString());
packageParameter.Add("trade_type", "JSAPI");//小程序支付
packageParameter.Add("sign_type", "MD5");
packageParameter.Add("fee_type", FeeType);
var sign = CreateSign(packageParameter, "MD5"); //第一次签名
packageParameter.Add("sign", sign);
var xe = PostDataToWeiXin(packageParameter);
//为响应客户端的请求准备数据
var timeStamp = MakeTimestamp();
var prepayId = xe.Element("prepay_id").Value;
nstr = MakeNonceStr();
Hashtable paySignReqHandler = new Hashtable();
paySignReqHandler.Add("appId", this.AppId);
paySignReqHandler.Add("timeStamp", timeStamp.ToString());
paySignReqHandler.Add("nonceStr", nstr);
paySignReqHandler.Add("package", "prepay_id=" + prepayId);
paySignReqHandler.Add("signType", "MD5");
var paySign = CreateSign(paySignReqHandler, "MD5"); //第二次签名
var obj = new
{
appid = this.AppId,
partnerid = this.MchId,
prepayid = prepayId,
package = packageStr,
noncestr = nstr,
timestamp = timeStamp,
sign = paySign
};
var serializer = new JavaScriptSerializer();
var result = serializer.Serialize(obj);
return result;
}
private string MakeNonceStr()
{
var timestap = DateTime.Now.ToString("yyyyMMddhhmmssffff");
return GetMD5(timestap);
}
private string CreateSign(Hashtable parameters,string sign_type="MD5")
{
var sb = new StringBuilder();
var akeys = new ArrayList(parameters.Keys);
akeys.Sort();//排序,这是微信要求的
foreach (string k in akeys)
{
var v = (string)parameters[k];
sb.Append(k + "=" + v + "&");
}
sb.Append("key=" + ApiKey);
string sign = "";
if (sign_type== "MD5")
{
sign = GetMD5(sb.ToString());
}
else if (sign_type == "HMACSHA256")
{
sign = GetHMACSHA256(sb.ToString(), ApiKey);
}
return sign;
}
private string GetMD5(string src)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = Encoding.UTF8.GetBytes(src);
byte[] md5data = md5.ComputeHash(data);
md5.Clear();
var retStr = BitConverter.ToString(md5data);
retStr = retStr.Replace("-", "").ToUpper();
return retStr;
}
private string GetHMACSHA256(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < hashmessage.Length; i++)
{
builder.Append(hashmessage[i].ToString("x2"));
}
return builder.ToString().ToUpper();
}
}
private Int64 MakeTimestamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
private XElement PostDataToWeiXin(Hashtable parameters)
{
var xmlStr = getXmlStr(parameters);
var data = Encoding.UTF8.GetBytes(xmlStr);
Stream responseStream;
HttpWebRequest request = WebRequest.Create(WeiXinPayUrl) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
try
{
responseStream = request.GetResponse().GetResponseStream();
}
catch (Exception exception)
{
throw exception;
}
string str = string.Empty;
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
{
str = reader.ReadToEnd();
}
responseStream.Close();
var xe = XElement.Parse(str);
return xe;
}
private string getXmlStr(Hashtable parameters)
{
var sb = new StringBuilder();
sb.Append("
foreach (string k in parameters.Keys)
{
var v = (string)parameters[k];
if (Regex.IsMatch(v, @"^[0-9.]$"))
{
sb.Append("<" + k + ">" + v + "" + k + ">");
}
else
{
sb.Append("<" + k + ">" + k + ">");
}
}
sb.Append("
return sb.ToString();
}
}
第三步:调用最后的用户支付请求
前端代码:
// 调起支付
uni.requestPayment({
provider: 'wxpay', //付款商家
timeStamp: sessions.data.data.timestamp + '',
nonceStr: sessions.data.data.noncestr,
package: 'prepay_id=' + sessions.data.data.prepayid,
signType: 'MD5', //加密方式
paySign: sessions.data.data.sign,
success: function(res) {
uni.showToast({
title: "支付成功!",
icon: 'success'
});
//更新后台数据库
},
fail: function(err) {
uni.showToast({
title: err.errMsg,
icon: "none"
});
}
});