AlipayController.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using Alipay.AopSdk.Core.Util;
  3. using GxPress.Common.AliPay;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Mvc;
  6. namespace GxPress.Api.WebControllers
  7. {
  8. [Route("api/web/alipay")]
  9. [ApiController]
  10. [Authorize]
  11. public class AlipayController : Controller
  12. {
  13. /// <summary>
  14. /// 回调地址
  15. /// </summary>
  16. [HttpGet("notify")]
  17. [AllowAnonymous]
  18. public void Callback()
  19. {
  20. /* 实际验证过程建议商户添加以下校验。
  21. 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
  22. 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
  23. 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
  24. 4、验证app_id是否为该商户本身。
  25. */
  26. Dictionary<string, string> sArray = GetRequestGet();
  27. if (sArray.Count != 0)
  28. {
  29. bool flag = AlipaySignature.RSACheckV1(sArray, Config.AlipayPublicKey, Config.CharSet, Config.SignType, false);
  30. if (flag)
  31. {
  32. //Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}");
  33. ViewData["PayResult"] = "同步验证通过";
  34. }
  35. else
  36. {
  37. // Console.WriteLine($"同步验证失败,订单号:{sArray["out_trade_no"]}");
  38. ViewData["PayResult"] = "同步验证失败";
  39. }
  40. }
  41. }
  42. private Dictionary<string, string> GetRequestPost()
  43. {
  44. Dictionary<string, string> sArray = new Dictionary<string, string>();
  45. ICollection<string> requestItem = Request.Form.Keys;
  46. foreach (var item in requestItem)
  47. {
  48. sArray.Add(item, Request.Form[item]);
  49. }
  50. return sArray;
  51. }
  52. private Dictionary<string, string> GetRequestGet()
  53. {
  54. Dictionary<string, string> sArray = new Dictionary<string, string>();
  55. ICollection<string> requestItem = Request.Query.Keys;
  56. foreach (var item in requestItem)
  57. {
  58. sArray.Add(item, Request.Query[item]);
  59. }
  60. return sArray;
  61. }
  62. }
  63. }