AlipayController.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. [HttpGet()]
  14. [AllowAnonymous]
  15. public string GetNativePayUrl()
  16. {
  17. var alipay = new PcPay();
  18. return alipay.PayRequest("637263608658642540", "汪峰讲故事", "1", "汪峰讲故事");
  19. }
  20. /// <summary>
  21. /// 回调地址
  22. /// </summary>
  23. [HttpGet("notify")]
  24. [AllowAnonymous]
  25. public void Callback()
  26. {
  27. /* 实际验证过程建议商户添加以下校验。
  28. 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
  29. 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
  30. 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
  31. 4、验证app_id是否为该商户本身。
  32. */
  33. Dictionary<string, string> sArray = GetRequestGet();
  34. if (sArray.Count != 0)
  35. {
  36. bool flag = AlipaySignature.RSACheckV1(sArray, Config.AlipayPublicKey, Config.CharSet, Config.SignType, false);
  37. if (flag)
  38. {
  39. //Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}");
  40. ViewData["PayResult"] = "同步验证通过";
  41. }
  42. else
  43. {
  44. // Console.WriteLine($"同步验证失败,订单号:{sArray["out_trade_no"]}");
  45. ViewData["PayResult"] = "同步验证失败";
  46. }
  47. }
  48. }
  49. private Dictionary<string, string> GetRequestPost()
  50. {
  51. Dictionary<string, string> sArray = new Dictionary<string, string>();
  52. ICollection<string> requestItem = Request.Form.Keys;
  53. foreach (var item in requestItem)
  54. {
  55. sArray.Add(item, Request.Form[item]);
  56. }
  57. return sArray;
  58. }
  59. private Dictionary<string, string> GetRequestGet()
  60. {
  61. Dictionary<string, string> sArray = new Dictionary<string, string>();
  62. ICollection<string> requestItem = Request.Query.Keys;
  63. foreach (var item in requestItem)
  64. {
  65. sArray.Add(item, Request.Query[item]);
  66. }
  67. return sArray;
  68. }
  69. }
  70. }