123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #if NETCOREAPP3_1
- using Microsoft.Extensions.Options;
- #endif
- using System.Threading.Tasks;
- using PaySharp.Alipay.Request;
- using PaySharp.Alipay.Response;
- using PaySharp.Core;
- using PaySharp.Core.Exceptions;
- using PaySharp.Core.Request;
- using PaySharp.Core.Utils;
- namespace PaySharp.Alipay
- {
- /// <summary>
- /// 支付宝网关
- /// </summary>
- public sealed class AlipayGateway : BaseGateway
- {
- #region 私有字段
- private readonly Merchant _merchant;
- #endregion
- #region 构造函数
- /// <summary>
- /// 初始化支付宝网关
- /// </summary>
- /// <param name="merchant">商户数据</param>
- public AlipayGateway(Merchant merchant)
- : base(merchant)
- {
- _merchant = merchant;
- }
- #if NETCOREAPP3_1
- /// <summary>
- /// 初始化支付宝网关
- /// </summary>
- /// <param name="merchant">商户数据</param>
- public AlipayGateway(IOptions<Merchant> merchant)
- : this(merchant.Value)
- {
- }
- #endif
- #endregion
- #region 属性
- public override string GatewayUrl { get; set; } = "https://openapi.alipay.com";
- public new NotifyResponse NotifyResponse => (NotifyResponse)base.NotifyResponse;
- protected override bool IsPaySuccess => NotifyResponse.TradeStatus == "TRADE_SUCCESS" && !IsRefundSuccess;
- protected override bool IsRefundSuccess => NotifyResponse.RefundAmount > 0;
- protected override bool IsCancelSuccess { get; }
- protected override string[] NotifyVerifyParameter => new string[]
- {
- "app_id","version", "charset","trade_no", "sign","sign_type"
- };
- #endregion
- #region 公共方法
- protected override async Task<bool> ValidateNotifyAsync()
- {
- base.NotifyResponse = await GatewayData.ToObjectAsync<NotifyResponse>(StringCase.Snake);
- base.NotifyResponse.Raw = GatewayData.ToUrl(false);
- GatewayData.Remove("sign");
- GatewayData.Remove("sign_type");
- var result = EncryptUtil.Verify(
- GatewayData.ToUrl(false),
- NotifyResponse.Sign,
- _merchant.CertEnvironment == null ? _merchant.AlipayPublicKey : _merchant.CertEnvironment.GetAlipayPublicKey(GatewayData.GetStringValue("alipay_cert_sn"))
- );
- if (result)
- {
- return true;
- }
- throw new GatewayException("签名不一致");
- }
- public override TResponse Execute<TModel, TResponse>(Request<TModel, TResponse> request)
- {
- if (request is WapPayRequest || request is WebPayRequest || request is AppPayRequest)
- {
- return SubmitProcess.SdkExecute(_merchant, request, GatewayUrl);
- }
- return SubmitProcess.Execute(_merchant, request, GatewayUrl);
- }
- #endregion
- }
- }
|