123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using GxPress.Common.WechatPay;
- using GxPress.Repository.Interface.Order;
- using GxPress.Service.Interface.Order;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.IO;
- using System.Threading.Tasks;
- namespace GxPress.Api.WebControllers
- {
- [Route("api/web/wxpay")]
- [ApiController]
- [Authorize]
- public class WxpayController : Controller
- {
- private IHttpContextAccessor _contextAccessor;
- private HttpContext _context { get { return _contextAccessor.HttpContext; } }
- private readonly IOrderRepository orderRepository;
- private IOrderService orderService;
- public WxpayController(IHttpContextAccessor contextAccessor, IOrderService orderService, IOrderRepository orderRepository)
- {
- _contextAccessor = contextAccessor;
- this.orderService = orderService;
- this.orderRepository = orderRepository;
- }
- [HttpGet()]
- [AllowAnonymous]
- public string GetNativePayUrl()
- {
- var nativePay = new NativePay();
- return nativePay.GetPayUrl("637263608658642540", "汪峰讲故事", "637263608658642540_7_2", 1);
- }
-
-
-
- [HttpPost("notify")]
- [AllowAnonymous]
- public async Task<string> Callback()
- {
- var contentType = Request.ContentType;
- var notifyData = await GetNotifyData();
-
- if (!notifyData.IsSet("transaction_id"))
- {
-
- var res = new WxPayData();
- res.SetValue("return_code", "FAIL");
- res.SetValue("return_msg", "支付结果中微信订单号不存在");
- return (res.ToXml());
- }
- string transaction_id = notifyData.GetValue("transaction_id").ToString();
-
- if (!Common.WechatPay.WxPayApi.QueryOrder(transaction_id))
- {
-
- var res = new WxPayData();
- res.SetValue("return_code", "FAIL");
- res.SetValue("return_msg", "订单查询失败");
- return (res.ToXml());
- }
-
- else
- {
- var res = new WxPayData();
-
- if (await orderService.InsertWxOrderAsync(notifyData))
- {
- res.SetValue("return_code", "SUCCESS");
- res.SetValue("return_msg", "OK");
- return (res.ToXml());
- }
- }
- notifyData = new WxPayData();
- notifyData.SetValue("return_code", "FAIL");
- notifyData.SetValue("return_msg", "支付结果中微信订单号不存在");
- return (notifyData.ToXml());
- }
-
-
-
-
-
- [HttpGet("{orderNumber}")]
- [AllowAnonymous]
- public async Task<string> Refund(string orderNumber)
- {
- var order = await orderRepository.GetOrderAsync(orderNumber);
- var nativePay = new NativePay();
- var price = Convert.ToInt32((order.Price) * 100).ToString();
- return nativePay.Refund(order.TransactionId, order.OutTradeNo, price, price);
- }
- private async Task<WxPayData> GetNotifyData()
- {
- var body = string.Empty;
-
- using (var reader = new StreamReader(Request.Body))
- {
- body = await reader.ReadToEndAsync();
- }
-
- WxPayData data = new WxPayData();
- if (string.IsNullOrEmpty(body))
- {
- data.SetValue("return_code", "FAIL");
- return data;
- }
- try
- {
- data.FromXml(body);
- }
- catch (WxPayException ex)
- {
-
- WxPayData res = new WxPayData();
- res.SetValue("return_code", "FAIL");
- res.SetValue("return_msg", ex.Message);
-
-
-
- }
-
- return data;
- }
- }
- }
|