123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Threading.Tasks;
- using Essensoft.AspNetCore.Payment.WeChatPay;
- using Essensoft.AspNetCore.Payment.WeChatPay.V2;
- using Essensoft.AspNetCore.Payment.WeChatPay.V2.Notify;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- namespace WebApplicationSample.Controllers
- {
- [Route("wechatpay/notify")]
- public class WeChatPayNotifyController : Controller
- {
- private readonly IWeChatPayNotifyClient _client;
- private readonly IOptions<WeChatPayOptions> _optionsAccessor;
- public WeChatPayNotifyController(IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
- {
- _client = client;
- _optionsAccessor = optionsAccessor;
- }
- /// <summary>
- /// 统一下单支付结果通知
- /// </summary>
- [Route("unifiedorder")]
- [HttpPost]
- public async Task<IActionResult> Unifiedorder()
- {
- try
- {
- var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
- if (notify.ReturnCode == WeChatPayCode.Success)
- {
- if (notify.ResultCode == WeChatPayCode.Success)
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return WeChatPayNotifyResult.Success;
- }
- }
- return WeChatPayNotifyResult.Failure;
- }
- catch (WeChatPayException ex)
- {
- Console.WriteLine("出现异常: " + ex.Message);
- return WeChatPayNotifyResult.Failure;
- }
- }
- /// <summary>
- /// 退款结果通知
- /// </summary>
- [Route("refund")]
- [HttpPost]
- public async Task<IActionResult> Refund()
- {
- try
- {
- var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
- if (notify.ReturnCode == WeChatPayCode.Success)
- {
- if (notify.RefundStatus == WeChatPayCode.Success)
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return WeChatPayNotifyResult.Success;
- }
- }
- return WeChatPayNotifyResult.Failure;
- }
- catch (WeChatPayException ex)
- {
- Console.WriteLine("出现异常: " + ex.Message);
- return WeChatPayNotifyResult.Failure;
- }
- }
- }
- }
|