public ActionResult JsApi(string code, string state)
{
if (string.IsNullOrEmpty(code))
{
return Content("您拒绝了授权!");
}
if (!state.Contains("|"))
{
//这里的state其实是会暴露给客户端的,验证能力很弱,这里只是演示一下
//实际上可以存任何想传递的数据,比如用户ID,并且需要结合例如下面的Session["OAuthAccessToken"]进行验证
return Content("验证失败!请从正规途径进入!1001");
}
try
{
//获取产品信息
var stateData = state.Split('|');
int productId = 0;
ProductModel product = null;
if (int.TryParse(stateData[0], out productId))
{
int hc = 0;
if (int.TryParse(stateData[1], out hc))
{
var products = ProductModel.GetFakeProductList();
product = products.FirstOrDefault(z => z.Id == productId);
if (product == null || product.GetHashCode() != hc)
{
return Content("商品信息不存在,或非法进入!1002");
}
ViewData["product"] = product;
}
}
//通过,用code换取access_token
var openIdResult = OAuthApi.GetAccessToken(TenPayV3Info.AppId, TenPayV3Info.AppSecret, code);
if (openIdResult.errcode != ReturnCode.请求成功)
{
return Content("错误:" + openIdResult.errmsg);
}
string sp_billno = Request["order_no"];
if (string.IsNullOrEmpty(sp_billno))
{
//生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一
sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId, DateTime.Now.ToString("yyyyMMdd"),
TenPayV3Util.BuildRandomStr(10));
}
else
{
sp_billno = Request["order_no"];
}
var timeStamp = TenPayV3Util.GetTimestamp();
var nonceStr = TenPayV3Util.GetNoncestr();
var body = product == null ? "test" : product.Name;
var price = product == null ? 100 : product.Price * 100;
var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, body, sp_billno, price, Request.UserHostAddress, TenPayV3Info.TenPayV3Notify, TenPayV3Type.JSAPI, openIdResult.openid, TenPayV3Info.Key, nonceStr);
var result = TenPayV3.Unifiedorder(xmlDataInfo);//调用统一订单接口
//JsSdkUiPackage jsPackage = new JsSdkUiPackage(TenPayV3Info.AppId, timeStamp, nonceStr,);
var package = string.Format("prepay_id={0}", result.prepay_id);
ViewData["appId"] = TenPayV3Info.AppId;
ViewData["timeStamp"] = timeStamp;
ViewData["nonceStr"] = nonceStr;
ViewData["package"] = package;
ViewData["paySign"] = TenPayV3.GetJsPaySign(TenPayV3Info.AppId, timeStamp, nonceStr, package, TenPayV3Info.Key);
return View();
}
catch (Exception ex)
{
var msg = ex.Message;
msg += "<br>" + ex.StackTrace;
msg += "<br>==Source==<br>" + ex.Source;
if (ex.InnerException != null)
{
msg += "<br>===InnerException===<br>" + ex.InnerException.Message;
}
return Content(msg);
}
}
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[您没有JSAPI支付权限]]></return_msg> </xml>
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[商户号mch_id与appid不匹配]]></return_msg> </xml>
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[appid and openid not match]]></return_msg> </xml>
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[openid is invalid]]></return_msg> </xml>
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx2421b1c4370ec43b]]></appid> <mch_id><![CDATA[10000100]]></mch_id> <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str> <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id> <trade_type><![CDATA[APP]]></trade_type> </xml>
ViewData["appId"] = TenPayV3Info.AppId;
ViewData["timeStamp"] = timeStamp;
ViewData["nonceStr"] = nonceStr;
ViewData["package"] = package;
ViewData["paySign"] = TenPayV3.GetJsPaySign(TenPayV3Info.AppId, timeStamp, nonceStr, package, TenPayV3Info.Key);
wx.chooseWXPay({
timestamp: 0, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: '', // 支付签名随机串,不长于 32 位
package: '', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType: '', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: '', // 支付签名
success: function (res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {//支付成功
} else {
//支付失败
}
// 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有