首页 > 热点 > 微信 > 正文

微信JSAPI支付操作需要注意的细节

2024-07-22 01:16:42
字体:
来源:转载
供稿:网友

首先介绍一下我在调用微信支付接口使用的是 weixin.senparc SDK,非常方便好用开源的一个微信开发SDK。

weixin.senparc SDK 官网:http://weixin.senparc.com/

先去下载下来Senparc.Weixin SDK。

在调起支付接口之前,需要先要调用统一下单接口,商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易回话标识后再在APP里面调起支付。

 微信 JsApi支付 在这个目录下 Senparc.Weixin.MP.Sample.Controllers 找到JsApi支付。

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);   }  }            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表