1.前台頁面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>微信掃碼支付</title>
<style>
html { font-size:20px; }
.logo_g { width:8.5rem; }
.form-control { width:96%;height:2rem;font-size: 1.5rem;border-radius: 0.25rem;border: 1px solid #ccc; }
#btnSubmit { width: 96%; height: 2rem; border-radius: 0.25rem; background-color: #00CD00; border: 0px #FE6714 solid; cursor: pointer; color: white; font-size: 1.5rem;margin-top: 1rem; }
</style>
<script type="text/javascript">
window.onload=function(){
document.documentElement.style.fontSize=document.documentElement.clientWidth*20/320+'px';
window.onsize=function(){
document.documentElement.style.fontSize=document.documentElement.clientWidth*20/320+'px';
};
};
//調用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<%=wxJsApiParam%>,//josn串
function (res)
{
if (res.err_msg == "get_brand_wcpay_request:ok") {
alert("微信支付成功!");
} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
alert("用戶取消支付!");
} else {
alert(res.err_msg);
alert("支付失敗!");
}
}
);
}
function callpay()
{
//WeixinJSBridge.invoke()
if (typeof WeixinJSBridge == "undefined")
{
if (document.addEventListener)
{
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}
else if (document.attachEvent)
{
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}
else
{
jsApiCall();
}
}
</script>
</head>
<body>
<form id="form1" runat="server" style="text-align:center;">
<img class="logo_g" src="../Templates/vshop/t7/images/logo.png" />
<asp:TextBox ID="txtMoney" CssClass="form-control" runat="server" onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;" placeholder="請輸入金額" />
<asp:Button ID="btnSubmit" runat="server" Text="立即支付" OnClientClick="callpay()" />
</form>
</body>
</html>
2.后台代碼
public class wx_ScanCodePay : Page
{
protected Button btnSubmit;
protected TextBox txtMoney;
public static string wxEditAddrParam { get; set; }
public static string wxJsApiParam { get; set; } //H5調起JS API參數
protected void Page_Load(object sender, EventArgs e)
{
this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
if (!IsPostBack)
{
//JsApiPay jsApiPay = new JsApiPay(this);
//try
//{
// //調用【網頁授權獲取用戶信息】接口獲取用戶的openid和access_token
// jsApiPay.GetOpenidAndAccessToken();
// //獲取收貨地址js函數入口參數
// wxEditAddrParam = jsApiPay.GetEditAddressParameters();
// ViewState["openid"] = jsApiPay.openid;
//}
//catch (Exception ex)
//{
// Response.Write("<span style='color:#FF0000;font-size:20px'>" + "頁面加載出錯,請重試" + "</span>");
//}
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string amount = this.txtMoney.Text;
int money;
if (ViewState["openid"] != null)
{
string openid = ViewState["openid"].ToString();
//檢測是否給當前頁面傳遞了相關參數
if (string.IsNullOrEmpty(amount))
{
Response.Write("<span style='color:#FF0000;font-size:20px'>" + "請輸入正確的金額,請返回重試" + "</span>");
return;
}
if (!int.TryParse(amount, out money))
{
Response.Write("<span style='color:#FF0000;font-size:20px'>" + "請輸入正確的金額,請返回重試" + "</span>");
return;
}
//若傳遞了相關參數,則調統一下單接口,獲得后續相關接口的入口參數
JsApiPay jsApiPay = new JsApiPay(this);
jsApiPay.openid = openid;
jsApiPay.total_fee = (int)(money * 100M);
//JSAPI支付預處理
try
{
WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
wxJsApiParam = jsApiPay.GetJsApiParameters();//獲取H5調起JS API參數
}
catch (Exception ex)
{
Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下單失敗,請返回重試" + "</span>");
}
}
else
{
Response.Write("<span style='color:#FF0000;font-size:20px'>" + "頁面缺少參數,請返回重試" + "</span>");
}
}
}
3.JsApiPay.cs
namespace WxPayAPI { public class JsApiPay { /// <summary> /// 保存頁面對象,因為要在類的方法中使用Page的Request對象 /// </summary> private Page page {get;set;} /// <summary> /// openid用於調用統一下單接口 /// </summary> public string openid { get; set; } /// <summary> /// access_token用於獲取收貨地址js函數入口參數 /// </summary> public string access_token { get; set; } /// <summary> /// 商品金額,用於統一下單 /// </summary> public int total_fee { get; set; } /// <summary> /// 統一下單接口返回結果 /// </summary> public WxPayData unifiedOrderResult { get; set; } public JsApiPay(Page page) { this.page = page; } /** * * 網頁授權獲取用戶基本信息的全部過程 * 詳情請參看網頁授權獲取用戶基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html * 第一步:利用url跳轉獲取code * 第二步:利用code去獲取openid和access_token * */ public SiteSettings masterSettings = SettingsManager.GetMasterSettings(false); public void GetOpenidAndAccessToken() { if (!string.IsNullOrEmpty(page.Request.QueryString["code"])) { //獲取code碼,以獲取openid和access_token string code = page.Request.QueryString["code"]; // Log.Debug(this.GetType().ToString(), "Get code : " + code); GetOpenidAndAccessTokenFromCode(code); } else { //構造網頁授權獲取code的URL string host = page.Request.Url.Host; string path = page.Request.Path; string redirect_uri = HttpUtility.UrlEncode("http://" + host + path); WxPayData data = new WxPayData(); data.SetValue("appid", masterSettings.WeixinAppId); data.SetValue("redirect_uri", redirect_uri); data.SetValue("response_type", "code"); data.SetValue("scope", "snsapi_base"); data.SetValue("state", "STATE" + "#wechat_redirect"); string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl(); // Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url); try { //觸發微信返回code碼 page.Response.Redirect(url);//Redirect函數會拋出ThreadAbortException異常,不用處理這個異常 } catch(System.Threading.ThreadAbortException ex) { } } } /** * * 通過code換取網頁授權access_token和openid的返回數據,正確時返回的JSON數據包如下: * { * "access_token":"ACCESS_TOKEN", * "expires_in":7200, * "refresh_token":"REFRESH_TOKEN", * "openid":"OPENID", * "scope":"SCOPE", * "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" * } * 其中access_token可用於獲取共享收貨地址 * openid是微信支付jsapi支付接口統一下單時必須的參數 * 更詳細的說明請參考網頁授權獲取用戶基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html * @失敗時拋異常WxPayException */ public void GetOpenidAndAccessTokenFromCode(string code) { try { //構造獲取openid及access_token的url WxPayData data = new WxPayData(); data.SetValue("appid", masterSettings.WeixinAppId); data.SetValue("secret", masterSettings.WeixinAppSecret); data.SetValue("code", code); data.SetValue("grant_type", "authorization_code"); string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl(); //請求url以獲取數據 string result = HttpService.Get(url); // Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result); //保存access_token,用於收貨地址獲取 JsonData jd = JsonMapper.ToObject(result); access_token = (string)jd["access_token"]; //獲取用戶openid openid = (string)jd["openid"]; // Log.Debug(this.GetType().ToString(), "Get openid : " + openid); // Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token); } catch (Exception ex) { // Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxPayException(ex.ToString()); } } /** * 調用統一下單,獲得下單結果 * @return 統一下單結果 * @失敗時拋異常WxPayException */ public WxPayData GetUnifiedOrderResult() { //統一下單 WxPayData data = new WxPayData(); data.SetValue("body", "test");//商品描述 data.SetValue("attach", "test");//附加數據 data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); data.SetValue("total_fee", total_fee); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); data.SetValue("goods_tag", "test");//商品標記(優惠券可能用到) data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", openid); WxPayData result = WxPayApi.UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { // Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxPayException("UnifiedOrder response error!"); } unifiedOrderResult = result; return result; } /** * * 從統一下單成功返回的數據中獲取微信瀏覽器調起jsapi支付所需的參數, * 微信瀏覽器調起JSAPI時的輸入參數格式如下: * { * "appId" : "wx2421b1c4370ec43b", //公眾號名稱,由商戶傳入 * "timeStamp":" 1395712654", //時間戳,自1970年以來的秒數 * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機串 * "package" : "prepay_id=u802345jgfjsdfgsdg888", * "signType" : "MD5", //微信簽名方式: * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名 * } * @return string 微信瀏覽器調起JSAPI時的輸入參數,json格式可以直接做參數用 * 更詳細的說明請參考網頁端調起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 * */ public string GetJsApiParameters() { // Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing..."); WxPayData jsApiParam = new WxPayData(); jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); string parameters = jsApiParam.ToJson(); // Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters); return parameters; } /** * * 獲取收貨地址js函數入口參數,詳情請參考收貨地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9 * @return string 共享收貨地址js函數需要的參數,json格式可以直接做參數使用 */ public string GetEditAddressParameters() { string parameter = ""; try { string host = page.Request.Url.Host; string path = page.Request.Path; string queryString = page.Request.Url.Query; //這個地方要注意,參與簽名的是網頁授權獲取用戶信息時微信后台回傳的完整url string url = "http://" + host + path + queryString; //構造需要用SHA1算法加密的數據 WxPayData signData = new WxPayData(); signData.SetValue("appid", masterSettings.WeixinAppId); signData.SetValue("url", url); signData.SetValue("timestamp",WxPayApi.GenerateTimeStamp()); signData.SetValue("noncestr",WxPayApi.GenerateNonceStr()); signData.SetValue("accesstoken",access_token); string param = signData.ToUrl(); // Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param); //SHA1加密 string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); // Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign); //獲取收貨地址js函數入口參數 WxPayData afterData = new WxPayData(); afterData.SetValue("appId", masterSettings.WeixinAppId); afterData.SetValue("scope","jsapi_address"); afterData.SetValue("signType","sha1"); afterData.SetValue("addrSign",addrSign); afterData.SetValue("timeStamp",signData.GetValue("timestamp")); afterData.SetValue("nonceStr",signData.GetValue("noncestr")); //轉為json格式 parameter = afterData.ToJson(); // Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter); } catch (Exception ex) { // Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxPayException(ex.ToString()); } return parameter; } } }