BaseGateway.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Threading.Tasks;
  2. using PaySharp.Core.Request;
  3. using PaySharp.Core.Response;
  4. using PaySharp.Core.Utils;
  5. namespace PaySharp.Core
  6. {
  7. /// <summary>
  8. /// 网关的抽象基类
  9. /// </summary>
  10. public abstract class BaseGateway : IGateway
  11. {
  12. #region 构造函数
  13. /// <summary>
  14. /// 构造函数
  15. /// </summary>
  16. protected BaseGateway()
  17. {
  18. }
  19. /// <summary>
  20. /// 构造函数
  21. /// </summary>
  22. /// <param name="merchant">商户数据</param>
  23. protected BaseGateway(IMerchant merchant)
  24. {
  25. Merchant = merchant;
  26. }
  27. #endregion
  28. #region 属性
  29. /// <summary>
  30. /// 商户数据
  31. /// </summary>
  32. public IMerchant Merchant { get; set; }
  33. /// <summary>
  34. /// 通知数据
  35. /// </summary>
  36. public IResponse NotifyResponse { get; set; }
  37. /// <summary>
  38. /// 网关的地址
  39. /// </summary>
  40. public abstract string GatewayUrl { get; set; }
  41. /// <summary>
  42. /// 网关数据
  43. /// </summary>
  44. protected internal GatewayData GatewayData { get; set; }
  45. /// <summary>
  46. /// 是否支付成功
  47. /// </summary>
  48. protected internal abstract bool IsPaySuccess { get; }
  49. /// <summary>
  50. /// 是否退款成功
  51. /// </summary>
  52. protected internal abstract bool IsRefundSuccess { get; }
  53. /// <summary>
  54. /// 是否撤销成功
  55. /// </summary>
  56. protected internal abstract bool IsCancelSuccess { get; }
  57. /// <summary>
  58. /// 需要验证的参数名称数组,用于识别不同的网关类型。
  59. /// 商户号(AppId)必须放第一位
  60. /// </summary>
  61. protected internal abstract string[] NotifyVerifyParameter { get; }
  62. #endregion
  63. #region 方法
  64. /// <summary>
  65. /// 检验网关返回的通知,确认订单是否支付成功
  66. /// </summary>
  67. protected internal abstract Task<bool> ValidateNotifyAsync();
  68. /// <summary>
  69. /// 当接收到支付网关通知并验证无误时按照支付网关要求格式输出表示成功接收到网关通知的字符串
  70. /// </summary>
  71. protected internal virtual void WriteSuccessFlag()
  72. {
  73. HttpUtil.Write("success");
  74. }
  75. /// <summary>
  76. /// 当接收到支付网关通知并验证有误时按照支付网关要求格式输出表示失败接收到网关通知的字符串
  77. /// </summary>
  78. protected internal virtual void WriteFailureFlag()
  79. {
  80. HttpUtil.Write("failure");
  81. }
  82. /// <summary>
  83. /// 执行请求
  84. /// </summary>
  85. /// <typeparam name="TModel">数据模型</typeparam>
  86. /// <typeparam name="TResponse">返回模型</typeparam>
  87. /// <param name="request">请求</param>
  88. /// <returns></returns>
  89. public abstract TResponse Execute<TModel, TResponse>(Request<TModel, TResponse> request) where TResponse : IResponse;
  90. #endregion
  91. }
  92. }