PcPay.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Threading.Tasks;
  2. using Alipay.AopSdk.Core;
  3. using Alipay.AopSdk.Core.Domain;
  4. using Alipay.AopSdk.Core.Request;
  5. using Newtonsoft.Json;
  6. namespace GxPress.Common.AliPay
  7. {
  8. public class PcPay
  9. {
  10. /// 发起支付请求
  11. /// </summary>
  12. /// <param name="tradeno">外部订单号,商户网站订单系统中唯一的订单号</param>
  13. /// <param name="subject">订单名称</param>
  14. /// <param name="totalAmout">付款金额</param>
  15. /// <param name="itemBody">商品描述</param>
  16. /// <returns></returns>
  17. public string PayRequest(string tradeno, string subject, string totalAmout, string itemBody)
  18. {
  19. DefaultAopClient client = new DefaultAopClient(Config.Gatewayurl, Config.AppId, Config.PrivateKey, "json", "2.0",
  20. Config.SignType, Config.AlipayPublicKey, Config.CharSet, false);
  21. // 组装业务参数model
  22. AlipayTradePagePayModel model = new AlipayTradePagePayModel();
  23. model.Body = itemBody;
  24. model.Subject = subject;
  25. model.TotalAmount = totalAmout;
  26. model.OutTradeNo = tradeno;
  27. model.ProductCode = "FAST_INSTANT_TRADE_PAY";
  28. AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
  29. // 设置同步回调地址
  30. request.SetReturnUrl("http://localhost:5000/Pay/Callback");
  31. // 设置异步通知接收地址
  32. request.SetNotifyUrl("");
  33. // 将业务model载入到request
  34. request.SetBizModel(model);
  35. var response = client.SdkExecute(request);
  36. // Console.WriteLine($"订单支付发起成功,订单号:{tradeno}");
  37. //跳转支付宝支付
  38. return (Config.Gatewayurl + "?" + response.Body);
  39. }
  40. /// <summary>
  41. /// 订单退款
  42. /// </summary>
  43. /// <param name="tradeno">商户订单号</param>
  44. /// <param name="alipayTradeNo">支付宝交易号</param>
  45. /// <param name="refundAmount">退款金额</param>
  46. /// <param name="refundReason">退款原因</param>
  47. /// <param name="refundNo">退款单号</param>
  48. /// <returns></returns>
  49. public async Task<bool> Refund(string tradeno, string alipayTradeNo, string refundAmount, string refundReason, string refundNo)
  50. {
  51. DefaultAopClient client = new DefaultAopClient(Config.Gatewayurl, Config.AppId, Config.PrivateKey, "json", "2.0",
  52. Config.SignType, Config.AlipayPublicKey, Config.CharSet, false);
  53. AlipayTradeRefundModel model = new AlipayTradeRefundModel();
  54. model.OutTradeNo = tradeno;
  55. model.TradeNo = alipayTradeNo;
  56. model.RefundAmount = refundAmount;
  57. model.RefundReason = refundReason;
  58. model.OutRequestNo = refundNo;
  59. AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
  60. request.SetBizModel(model);
  61. var response = await client.ExecuteAsync(request);
  62. var jsonModel = JsonConvert.DeserializeObject<dynamic>(response.Body);
  63. return true;
  64. }
  65. /// <summary>
  66. /// 关闭订单
  67. /// </summary>
  68. /// <param name="tradeno">商户订单号</param>
  69. /// <param name="alipayTradeNo">支付宝交易号</param>
  70. /// <returns></returns>
  71. public async Task<bool> OrderClose(string tradeno, string alipayTradeNo)
  72. {
  73. DefaultAopClient client = new DefaultAopClient(Config.Gatewayurl, Config.AppId, Config.PrivateKey, "json", "2.0",
  74. Config.SignType, Config.AlipayPublicKey, Config.CharSet, false);
  75. AlipayTradeCloseModel model = new AlipayTradeCloseModel();
  76. model.OutTradeNo = tradeno;
  77. model.TradeNo = alipayTradeNo;
  78. AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
  79. request.SetBizModel(model);
  80. var response = await client.ExecuteAsync(request);
  81. var jsonModel = JsonConvert.DeserializeObject<dynamic>(response.Body);
  82. return true;
  83. }
  84. }
  85. }