using System.Collections.Generic;
using System.Threading.Tasks;
using Alipay.AopSdk.Core.Util;
using GxPress.Common.AliPay;
using GxPress.Common.WechatPay;
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("637263608658642543_7", "汪峰讲故事", "0.01", "汪峰讲故事", "");
}
///
/// 回调地址
///
[HttpPost("notify")]
[AllowAnonymous]
public async Task Callback()
{
Dictionary sArray = GetRequestPost();
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))
{
// var res = new WxPayData();
// res.SetValue("out_trade_no", sArray["out_trade_no"]);
// res.SetValue("attach", sArray["out_trade_no"]);
// res.SetValue("total_fee", sArray["total_amount"]);
// res.SetValue("transaction_id", sArray["trade_no"]);
_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 GetRequestPost()
{
Dictionary sArray = new Dictionary();
ICollection requestItem = Request.Form.Keys;
foreach (var item in requestItem)
{
sArray.Add(item, Request.Form[item]);
}
return sArray;
}
private Dictionary GetRequestGet()
{
Dictionary sArray = new Dictionary();
ICollection requestItem = Request.Query.Keys;
foreach (var item in requestItem)
{
sArray.Add(item, Request.Query[item]);
}
return sArray;
}
}
}