mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-21 02:58:06 +08:00
chore(tenpayv3): 新增基于 .NET Framework 4.7 的示例项目
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
|
||||
[RoutePrefix("api/notify")]
|
||||
public class TenpayNotifyController : ApiController
|
||||
{
|
||||
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
|
||||
|
||||
public TenpayNotifyController(
|
||||
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
|
||||
{
|
||||
_tenpayHttpClientFactory = tenpayHttpClientFactory;
|
||||
}
|
||||
|
||||
|
||||
[HttpPost]
|
||||
[Route("m-{merchant_id}/message-push")]
|
||||
public async Task<IHttpActionResult> ReceiveMessage([FromUri(Name = "merchant_id")] string merchantId, CancellationToken cancellationToken)
|
||||
{
|
||||
using (var stream = await Request.Content.ReadAsStreamAsync())
|
||||
using (var reader = new StreamReader(stream, Encoding.UTF8))
|
||||
{
|
||||
string timestamp = Request.Headers.TryGetValues("Wechatpay-Timestamp", out _) ? Request.Headers.GetValues("Wechatpay-Timestamp").First() : null;
|
||||
string nonce = Request.Headers.TryGetValues("Wechatpay-Nonce", out _) ? Request.Headers.GetValues("Wechatpay-Nonce").First() : null;
|
||||
string signature = Request.Headers.TryGetValues("Wechatpay-Signature", out _) ? Request.Headers.GetValues("Wechatpay-Signature").First() : null;
|
||||
string serialNumber = Request.Headers.TryGetValues("Wechatpay-Serial", out _) ? Request.Headers.GetValues("Wechatpay-Serial").First() : null;
|
||||
string content = await reader.ReadToEndAsync();
|
||||
Debug.WriteLine("接收到微信支付推送的数据:{0}", content);
|
||||
|
||||
var client = _tenpayHttpClientFactory.Create(merchantId);
|
||||
bool valid = client.VerifyEventSignature(
|
||||
callbackTimestamp: timestamp,
|
||||
callbackNonce: nonce,
|
||||
callbackBody: content,
|
||||
callbackSignature: signature,
|
||||
callbackSerialNumber: serialNumber
|
||||
);
|
||||
if (!valid)
|
||||
{
|
||||
// NOTICE:
|
||||
// 需提前注入 CertificateManager、并添加平台证书,才可以使用扩展方法执行验签操作。
|
||||
// 有关 CertificateManager 的用法请参阅《开发文档 / 高级技巧 / 如何验证回调通知事件签名?》。
|
||||
// 后续如何解密并反序列化,请参阅《开发文档 / 高级技巧 / 如何解密回调通知事件中的敏感数据?》。
|
||||
|
||||
return Json(new { code = "FAIL", message = "验签失败" });
|
||||
}
|
||||
|
||||
return Json(new { code = "SUCCESS", message = "成功" });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
[RoutePrefix("api/order")]
|
||||
public class TenpayOrderController : ApiController
|
||||
{
|
||||
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
|
||||
|
||||
public TenpayOrderController(
|
||||
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
|
||||
{
|
||||
_tenpayHttpClientFactory = tenpayHttpClientFactory;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("jsapi")]
|
||||
public async Task<IHttpActionResult> CreateOrderByJsapi([FromBody] Models.CreateOrderByJsapiRequest requestModel, CancellationToken cancellationToken)
|
||||
{
|
||||
if (requestModel == null)
|
||||
return BadRequest();
|
||||
|
||||
var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId);
|
||||
var request = new CreatePayTransactionJsapiRequest()
|
||||
{
|
||||
OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"),
|
||||
AppId = requestModel.AppId,
|
||||
Description = "演示订单",
|
||||
NotifyUrl = Options.TenpayOptions.Instance.Value.NotifyUrl,
|
||||
Amount = new CreatePayTransactionJsapiRequest.Types.Amount() { Total = requestModel.Amount },
|
||||
Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { OpenId = requestModel.OpenId }
|
||||
};
|
||||
var response = await client.ExecuteCreatePayTransactionJsapiAsync(request, cancellationToken);
|
||||
if (!response.IsSuccessful())
|
||||
{
|
||||
Debug.WriteLine(
|
||||
"JSAPI 下单失败(状态码:{0},错误代码:{1},错误描述:{2})。",
|
||||
response.RawStatus, response.ErrorCode, response.ErrorMessage
|
||||
);
|
||||
}
|
||||
|
||||
return Json(response);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
[RoutePrefix("api/refund")]
|
||||
public class TenpayRefundController : ApiController
|
||||
{
|
||||
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
|
||||
|
||||
public TenpayRefundController(
|
||||
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
|
||||
{
|
||||
_tenpayHttpClientFactory = tenpayHttpClientFactory;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("")]
|
||||
public async Task<IHttpActionResult> CreateRefund([FromBody] Models.CreateRefundRequest requestModel, CancellationToken cancellationToken)
|
||||
{
|
||||
if (requestModel == null)
|
||||
return BadRequest();
|
||||
|
||||
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 = Options.TenpayOptions.Instance.Value.NotifyUrl
|
||||
};
|
||||
var response = await client.ExecuteCreateRefundDomesticRefundAsync(request, cancellationToken);
|
||||
if (!response.IsSuccessful())
|
||||
{
|
||||
Debug.WriteLine(
|
||||
"申请退款失败(状态码:{0},错误代码:{1},错误描述:{2})。",
|
||||
response.RawStatus, response.ErrorCode, response.ErrorMessage
|
||||
);
|
||||
}
|
||||
|
||||
return Json(response);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user