PcPay.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /// <param name="notifyUrl">设置异步通知接收地址</param>
  17. /// <returns></returns>
  18. public string PayRequest(string tradeno, string subject, string totalAmout, string itemBody, string notifyUrl)
  19. {
  20. DefaultAopClient client = new DefaultAopClient(Config.Gatewayurl, Config.AppId, Config.PrivateKey, "json", "2.0",
  21. Config.SignType, Config.AlipayPublicKey, Config.CharSet, false);
  22. // 组装业务参数model
  23. AlipayTradePagePayModel model = new AlipayTradePagePayModel();
  24. model.Body = itemBody;
  25. model.Subject = subject;
  26. model.TotalAmount = totalAmout;
  27. model.OutTradeNo = tradeno;
  28. model.ProductCode = "FAST_INSTANT_TRADE_PAY";
  29. AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
  30. // 设置同步回调地址
  31. request.SetReturnUrl(notifyUrl);
  32. // 设置异步通知接收地址
  33. //request.SetNotifyUrl("http://payzzz.top/api/web/alipay/notify");
  34. request.SetNotifyUrl("https://apk.tederen.com/service/api/web/alipay/notify");
  35. // 将业务model载入到request
  36. request.SetBizModel(model);
  37. var response = client.SdkExecute(request);
  38. // Console.WriteLine($"订单支付发起成功,订单号:{tradeno}");
  39. //跳转支付宝支付
  40. return (Config.Gatewayurl + "?" + response.Body);
  41. }
  42. /// <summary>
  43. /// 订单退款
  44. /// </summary>
  45. /// <param name="tradeno">商户订单号</param>
  46. /// <param name="alipayTradeNo">支付宝交易号</param>
  47. /// <param name="refundAmount">退款金额</param>
  48. /// <param name="refundReason">退款原因</param>
  49. /// <param name="refundNo">退款单号</param>
  50. /// <returns></returns>
  51. public async Task<bool> Refund(string tradeno, string alipayTradeNo, string refundAmount, string refundReason, string refundNo)
  52. {
  53. DefaultAopClient client = new DefaultAopClient(Config.Gatewayurl, Config.AppId, Config.PrivateKey, "json", "2.0",
  54. Config.SignType, Config.AlipayPublicKey, Config.CharSet, false);
  55. AlipayTradeRefundModel model = new AlipayTradeRefundModel();
  56. model.OutTradeNo = tradeno;
  57. model.TradeNo = alipayTradeNo;
  58. model.RefundAmount = refundAmount;
  59. model.RefundReason = refundReason;
  60. model.OutRequestNo = refundNo;
  61. AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
  62. request.SetBizModel(model);
  63. var response = await client.ExecuteAsync(request);
  64. var jsonModel = JsonConvert.DeserializeObject<dynamic>(response.Body);
  65. return true;
  66. }
  67. /// <summary>
  68. /// 关闭订单
  69. /// </summary>
  70. /// <param name="tradeno">商户订单号</param>
  71. /// <param name="alipayTradeNo">支付宝交易号</param>
  72. /// <returns></returns>
  73. public async Task<bool> OrderClose(string tradeno, string alipayTradeNo)
  74. {
  75. DefaultAopClient client = new DefaultAopClient(Config.Gatewayurl, Config.AppId, Config.PrivateKey, "json", "2.0",
  76. Config.SignType, Config.AlipayPublicKey, Config.CharSet, false);
  77. AlipayTradeCloseModel model = new AlipayTradeCloseModel();
  78. model.OutTradeNo = tradeno;
  79. model.TradeNo = alipayTradeNo;
  80. AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
  81. request.SetBizModel(model);
  82. var response = await client.ExecuteAsync(request);
  83. var jsonModel = JsonConvert.DeserializeObject<dynamic>(response.Body);
  84. return true;
  85. }
  86. }
  87. }