mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-20 02:29:40 +08:00
chore: 升级示例项目为基于 .NET 6.0 的实现
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("notify")]
|
||||
public class TenpayNotifyController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
|
||||
|
||||
public TenpayNotifyController(
|
||||
ILoggerFactory loggerFactory,
|
||||
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger(GetType());
|
||||
_tenpayHttpClientFactory = tenpayHttpClientFactory;
|
||||
}
|
||||
|
||||
|
||||
[HttpPost]
|
||||
[Route("m-{merchant_id}/message-push")]
|
||||
public async Task<IActionResult> ReceiveMessage(
|
||||
[FromRoute(Name = "merchant_id")] string merchantId,
|
||||
[FromHeader(Name = "Wechatpay-Timestamp")] string timestamp,
|
||||
[FromHeader(Name = "Wechatpay-Nonce")] string nonce,
|
||||
[FromHeader(Name = "Wechatpay-Signature")] string signature,
|
||||
[FromHeader(Name = "Wechatpay-Serial")] string serialNumber)
|
||||
{
|
||||
// 接收服务器推送
|
||||
// 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_2.shtml
|
||||
|
||||
using var reader = new StreamReader(Request.Body, Encoding.UTF8);
|
||||
string content = await reader.ReadToEndAsync();
|
||||
_logger.LogInformation("接收到微信支付推送的数据:{0}", content);
|
||||
|
||||
var client = _tenpayHttpClientFactory.Create(merchantId);
|
||||
bool valid = client.VerifyEventSignature(
|
||||
callbackTimestamp: timestamp,
|
||||
callbackNonce: nonce,
|
||||
callbackBody: content,
|
||||
callbackSignature: signature,
|
||||
callbackSerialNumber: serialNumber
|
||||
);
|
||||
if (!valid)
|
||||
{
|
||||
// 注意:需提前注入 CertificateManager、并添加平台证书,才可以使用扩展方法执行验签操作
|
||||
// 有关 CertificateManager 的用法请参阅《开发文档 / 高级技巧 / 如何验证回调通知事件签名?》
|
||||
return new JsonResult(new { code = "FAIL", message = "验签失败" });
|
||||
}
|
||||
|
||||
return new JsonResult(new { code = "SUCCESS", message = "成功" });
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
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 TenpayOrderController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly Options.TenpayOptions _tenpayOptions;
|
||||
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
|
||||
|
||||
public TenpayOrderController(
|
||||
ILoggerFactory loggerFactory,
|
||||
IOptions<Options.TenpayOptions> tenpayOptions,
|
||||
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger(GetType());
|
||||
_tenpayOptions = tenpayOptions.Value;
|
||||
_tenpayHttpClientFactory = tenpayHttpClientFactory;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("jsapi")]
|
||||
public async Task<IActionResult> CreateOrderByJsapi([FromBody] Models.CreateOrderByJsapiRequest requestModel)
|
||||
{
|
||||
// JSAPI 下单
|
||||
// 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
|
||||
|
||||
var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId);
|
||||
var request = new CreatePayTransactionJsapiRequest()
|
||||
{
|
||||
OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"),
|
||||
AppId = requestModel.AppId,
|
||||
Description = "演示订单",
|
||||
NotifyUrl = _tenpayOptions.NotifyUrl,
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
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 TenpayRefundController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly Options.TenpayOptions _tenpayOptions;
|
||||
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
|
||||
|
||||
public TenpayRefundController(
|
||||
ILoggerFactory loggerFactory,
|
||||
IOptions<Options.TenpayOptions> tenpayOptions,
|
||||
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger(GetType());
|
||||
_tenpayOptions = tenpayOptions.Value;
|
||||
_tenpayHttpClientFactory = tenpayHttpClientFactory;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("")]
|
||||
public async Task<IActionResult> CreateRefund([FromBody] Models.CreateRefundRequest requestModel)
|
||||
{
|
||||
// 申请退款
|
||||
// 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml
|
||||
|
||||
var client = _tenpayHttpClientFactory.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 = _tenpayOptions.NotifyUrl
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user