public void JsAPiAsyncTest() { var key = TenPayHelper.GetRegisterKey(Config.SenparcWeixinSetting); var TenPayV3Info = TenPayV3InfoCollection.Data[key]; var price = 100; var name = "单元测试-" + DateTime.Now.ToString(); var sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId /*10位*/, SystemTime.Now.ToString("yyyyMMddHHmmss"), TenPayV3Util.BuildRandomStr(6)); //TODO: JsApiRequestData修改构造函数参数顺序 jsApiRequestData = new(TenPayV3Info.AppId, TenPayV3Info.MchId, name, sp_billno, new TenpayDateTime(DateTime.Now.AddHours(1)), null, TenPayV3Info.TenPayV3Notify, null, new() { currency = "CNY", total = price }, new(openId), null, null, null); BasePayApis basePayApis = new BasePayApis(); var result = basePayApis.JsApiAsync(jsApiRequestData).GetAwaiter().GetResult(); Console.WriteLine("微信支付 V3 预支付结果:" + result.ToJson(true)); Assert.IsNotNull(result); Assert.IsTrue(result.ResultCode.Success); Assert.IsTrue(result.VerifySignSuccess == true);//通过验证 }
public async Task <IActionResult> JsApi(int productId, int hc) { try { //获取产品信息 var products = ProductModel.GetFakeProductList(); var product = products.FirstOrDefault(z => z.Id == productId); if (product == null || product.GetHashCode() != hc) { return(Content("商品信息不存在,或非法进入!1002")); } ViewData["product"] = product; var openId = HttpContext.Session.GetString("OpenId"); string sp_billno = Request.Query["order_no"];//out_trade_no if (string.IsNullOrEmpty(sp_billno)) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId /*10位*/, SystemTime.Now.ToString("yyyyMMddHHmmss"), TenPayV3Util.BuildRandomStr(6)); //注意:以上订单号仅作为演示使用,如果访问量比较大,建议增加订单流水号的去重检查。 } else { sp_billno = Request.Query["order_no"]; } //调用下单接口下单 var name = product == null ? "test" : product.Name; var price = product == null ? 100 : (int)(product.Price * 100);//单位:分 var notifyUrl = TenPayV3Info.TenPayV3Notify.Replace("/TenpayV3/", "/TenpayRealV3/").Replace("http://", "https://"); //请求信息 TransactionsRequestData jsApiRequestData = new(TenPayV3Info.AppId, TenPayV3Info.MchId, name + " - 微信支付 V3", sp_billno, new TenpayDateTime(DateTime.Now.AddHours(1), false), null, notifyUrl, null, new() { currency = "CNY", total = price }, new(openId), null, null, null); //请求接口 var result = await _basePayApis.JsApiAsync(jsApiRequestData); if (result.VerifySignSuccess != true) { throw new WeixinException("获取 prepay_id 结果校验出错!"); } //获取 UI 信息包 var jsApiUiPackage = TenPaySignHelper.GetJsApiUiPackage(TenPayV3Info.AppId, result.prepay_id); ViewData["jsApiUiPackage"] = jsApiUiPackage; //临时记录订单信息,留给退款申请接口测试使用(分布式情况下请注意数据同步) HttpContext.Session.SetString("BillNo", sp_billno); HttpContext.Session.SetString("BillFee", price.ToString()); return(View()); } catch (Exception ex) { Senparc.Weixin.WeixinTrace.BaseExceptionLog(ex); throw; } }