From 05a276dfe1e9c93967de29b2bfc01eb8a5d6a2ff Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Sat, 31 Jul 2021 21:21:15 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E5=AE=8C=E5=96=84=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=95=86=E6=88=B7=E5=B9=B3=E5=8F=B0=E6=A8=A1=E5=9D=97=E7=9A=84?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WechatUserController.cs | 2 +- .../Controllers/WxpayOrderController.cs | 64 ++++++++++++++++++ .../Controllers/WxpayRefundController.cs | 67 +++++++++++++++++++ .../Models/CreateOrderByJsapiRequest.cs | 19 ++++++ .../Models/CreateRefundRequest.cs | 19 ++++++ .../Options/WxpayOptions.cs | 2 +- 6 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayOrderController.cs create mode 100644 samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayRefundController.cs create mode 100644 samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateOrderByJsapiRequest.cs create mode 100644 samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateRefundRequest.cs diff --git a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatUserController.cs b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatUserController.cs index 517a5254..ccf7f878 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatUserController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5/Controllers/WechatUserController.cs @@ -44,7 +44,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers var entity = _wechatAccessTokenEntityRepository.FirstOrDefault(e => e.AppId == appId); var client = _wechatApiHttpClientFactory.Create(appId); var request = new CgibinUserInfoRequest() { AccessToken = entity?.AccessToken, OpenId = openId }; - var response = await client.ExecuteCgibinUserInfoAsync(request, HttpContext.RequestAborted); + var response = await client.ExecuteCgibinUserInfoAsync(request, cancellationToken: HttpContext.RequestAborted); if (!response.IsSuccessful()) { _logger.LogWarning( diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayOrderController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayOrderController.cs new file mode 100644 index 00000000..edfe9720 --- /dev/null +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayOrderController.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SKIT.FlurlHttpClient.Wechat.TenpayV3; +using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models; + +namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers +{ + [ApiController] + [Route("order")] + public class WxpayOrderController : ControllerBase + { + private readonly ILogger _logger; + + private readonly Options.WxpayOptions _wxpayOptions; + + private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _wechatTenpayHttpClientFactory; + + public WxpayOrderController( + ILoggerFactory loggerFactory, + IOptions wxpayOptions, + Services.HttpClients.IWechatTenpayHttpClientFactory wechatTenpayHttpClientFactory) + { + _logger = loggerFactory.CreateLogger(GetType()); + _wxpayOptions = wxpayOptions.Value; + _wechatTenpayHttpClientFactory = wechatTenpayHttpClientFactory; + } + + [HttpPost] + [Route("jsapi")] + public async Task CreateOrderByJsapi([FromBody] Models.CreateOrderByJsapiRequest requestModel) + { + // JSAPI 下单 + // 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml + + var client = _wechatTenpayHttpClientFactory.Create(requestModel.MerchantId); + var request = new CreatePayTransactionJsapiRequest() + { + OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"), + AppId = requestModel.AppId, + Description = "演示订单", + NotifyUrl = _wxpayOptions.CallbackUrl, + Amount = new CreatePayTransactionJsapiRequest.Types.Amount() { Total = requestModel.Amount }, + Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { OpenId = requestModel.OpenId } + }; + var response = await client.ExecuteCreatePayTransactionJsapiAsync(request, cancellationToken: HttpContext.RequestAborted); + if (!response.IsSuccessful()) + { + _logger.LogWarning( + "JSAPI 下单失败(状态码:{0},错误代码:{1},错误描述:{2})。", + response.RawStatus, response.ErrorCode, response.ErrorMessage + ); + } + + return new JsonResult(response); + } + } +} diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayRefundController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayRefundController.cs new file mode 100644 index 00000000..9ded710c --- /dev/null +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayRefundController.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SKIT.FlurlHttpClient.Wechat.TenpayV3; +using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models; + +namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers +{ + [ApiController] + [Route("refund")] + public class WxpayRefundController : ControllerBase + { + private readonly ILogger _logger; + + private readonly Options.WxpayOptions _wxpayOptions; + + private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _wechatTenpayHttpClientFactory; + + public WxpayRefundController( + ILoggerFactory loggerFactory, + IOptions wxpayOptions, + Services.HttpClients.IWechatTenpayHttpClientFactory wechatTenpayHttpClientFactory) + { + _logger = loggerFactory.CreateLogger(GetType()); + _wxpayOptions = wxpayOptions.Value; + _wechatTenpayHttpClientFactory = wechatTenpayHttpClientFactory; + } + + [HttpPost] + [Route("")] + public async Task CreateRefund([FromBody] Models.CreateRefundRequest requestModel) + { + // 申请退款 + // 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml + + var client = _wechatTenpayHttpClientFactory.Create(requestModel.MerchantId); + var request = new CreateRefundDomesticRefundRequest() + { + TransactionId = requestModel.TransactionId, + OutRefundNumber = "SAMPLE_ORN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"), + Amount = new CreateRefundDomesticRefundRequest.Types.Amount() + { + Total = requestModel.OrderAmount, + Refund = requestModel.RefundAmount + }, + Reason = "示例退款", + NotifyUrl = _wxpayOptions.CallbackUrl + }; + var response = await client.ExecuteCreateRefundDomesticRefundAsync(request, cancellationToken: HttpContext.RequestAborted); + if (!response.IsSuccessful()) + { + _logger.LogWarning( + "申请退款失败(状态码:{0},错误代码:{1},错误描述:{2})。", + response.RawStatus, response.ErrorCode, response.ErrorMessage + ); + } + + return new JsonResult(response); + } + } +} diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateOrderByJsapiRequest.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateOrderByJsapiRequest.cs new file mode 100644 index 00000000..4ea4d13f --- /dev/null +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateOrderByJsapiRequest.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Models +{ + public class CreateOrderByJsapiRequest + { + public string MerchantId { get; set; } = default!; + + public string AppId { get; set; } = default!; + + public string OpenId { get; set; } = default!; + + // NOTICE: 单机演示时金额来源于客户端请求,生产项目请替换成服务端计算生成 + public int Amount { get; set; } + } +} diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateRefundRequest.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateRefundRequest.cs new file mode 100644 index 00000000..7cee475e --- /dev/null +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Models/CreateRefundRequest.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Models +{ + public class CreateRefundRequest + { + public string MerchantId { get; set; } = default!; + + public string TransactionId { get; set; } = default!; + + // NOTICE: 单机演示时金额来源于客户端请求,生产项目请替换成服务端获取生成 + public int OrderAmount { get; set; } + + public int RefundAmount { get; set; } + } +} diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs index 76d47027..8213161e 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs @@ -12,7 +12,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Options public WechatMerchant[] Merchants { get; set; } = Array.Empty(); - public string CallbackEntry { get; set; } = string.Empty; + public string CallbackUrl { get; set; } = string.Empty; } partial class WxpayOptions