123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Alipay.AopSdk.Core.Util;
- using GxPress.Common.AliPay;
- using GxPress.Service.Interface.Order;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- namespace GxPress.Api.WebControllers
- {
- [Route("api/web/alipay")]
- [ApiController]
- [Authorize]
- public class AlipayController : Controller
- {
- private readonly IOrderService orderService;
- private IHttpContextAccessor _contextAccessor;
- private HttpContext _context { get { return _contextAccessor.HttpContext; } }
- public AlipayController(IOrderService orderService, IHttpContextAccessor contextAccessor)
- {
- this.orderService = orderService;
- _contextAccessor = contextAccessor;
- }
- [HttpGet()]
- [AllowAnonymous]
- public string GetNativePayUrl()
- {
- var alipay = new PcPay();
- return alipay.PayRequest("637263608658642540_7", "汪峰讲故事", "0.01", "汪峰讲故事");
- }
- /// <summary>
- /// 回调地址
- /// </summary>
- [HttpGet("notify")]
- [AllowAnonymous]
- public async Task Callback()
- {
- Dictionary<string, string> sArray = GetRequestGet();
- if (sArray.Count != 0)
- {
- try
- {
- //校验数据的正确性
- bool flag = AlipaySignature.RSACheckV1(sArray, Config.AlipayPublicKey, Config.CharSet, Config.SignType, false);
- if (flag)
- {
- //Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}");
- if (await orderService.InsertAliPayOrderAsync(sArray))
- {
- _context.Response.StatusCode = 200;
- }
- else
- {
- _context.Response.StatusCode = 500;
- }
- }
- else
- {
- // Console.WriteLine($"同步验证失败,订单号:{sArray["out_trade_no"]}");
- _context.Response.StatusCode = 500;
- }
- }
- catch (System.Exception)
- {
- _context.Response.StatusCode = 500;
- }
- }
- else
- {
- _context.Response.StatusCode = 500;
- }
- }
- private Dictionary<string, string> GetRequestPost()
- {
- Dictionary<string, string> sArray = new Dictionary<string, string>();
- ICollection<string> requestItem = Request.Form.Keys;
- foreach (var item in requestItem)
- {
- sArray.Add(item, Request.Form[item]);
- }
- return sArray;
- }
- private Dictionary<string, string> GetRequestGet()
- {
- Dictionary<string, string> sArray = new Dictionary<string, string>();
- ICollection<string> requestItem = Request.Query.Keys;
- foreach (var item in requestItem)
- {
- sArray.Add(item, Request.Query[item]);
- }
- return sArray;
- }
- }
- }
|