/// <summary> /// 使用 Native 支付 /// </summary> /// <param name="productId"></param> /// <param name="hc"></param> /// <returns></returns> public async Task <IActionResult> NativePayCode(int productId, int hc) { var products = ProductModel.GetFakeProductList(); var product = products.FirstOrDefault(z => z.Id == productId); if (product == null || product.GetHashCode() != hc) { return(Content("商品信息不存在,或非法进入!2004")); } //使用 Native 支付,输出二维码并展示 MemoryStream fileStream = null;//输出图片的URL var price = (int)(product.Price * 100); var name = product.Name + " - 微信支付 V3 - Native 支付"; var sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId /*10位*/, SystemTime.Now.ToString("yyyyMMddHHmmss"), TenPayV3Util.BuildRandomStr(6)); var notifyUrl = TenPayV3Info.TenPayV3Notify.Replace("/TenpayV3/", "/TenpayRealV3/"); TransactionsRequestData requestData = new(TenPayV3Info.AppId, TenPayV3Info.MchId, name, sp_billno, new TenpayDateTime(DateTime.Now.AddHours(1)), null, notifyUrl, null, new() { currency = "CNY", total = price }, null, null, null, null); BasePayApis basePayApis = new BasePayApis(); var result = await basePayApis.NativeAsync(requestData); //进行安全签名验证 if (result.VerifySignSuccess == true) { fileStream = QrCodeHelper.GerQrCodeStream(result.code_url); } else { fileStream = QrCodeHelper.GetTextImageStream("Native Pay 未能通过签名验证,无法显示二维码"); } return(File(fileStream, "image/png")); }
public void NativeAsyncTest() { 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)); TransactionsRequestData requestData = new(TenPayV3Info.AppId, TenPayV3Info.MchId, name, sp_billno, new TenpayDateTime(DateTime.Now.AddHours(1)), null, TenPayV3Info.TenPayV3Notify, null, new() { currency = "CNY", total = price }, null, null, null, null); BasePayApis basePayApis = new BasePayApis(); var result = basePayApis.NativeAsync(requestData).GetAwaiter().GetResult(); Console.WriteLine("微信支付 V3 预支付结果:" + result.ToJson(true)); Assert.IsNotNull(result); Assert.IsTrue(result.ResultCode.Success); Assert.IsTrue(result.VerifySignSuccess == true);//通过验证 }