private WeChatPayHeaders GetWeChatPayHeadersFromRequest(Microsoft.AspNetCore.Http.HttpRequest request) { var headers = new WeChatPayHeaders(); if (request.Headers.TryGetValue(WeChatPayConsts.Wechatpay_Serial, out var serialValues)) { headers.Serial = serialValues.First(); } if (request.Headers.TryGetValue(WeChatPayConsts.Wechatpay_Timestamp, out var timestampValues)) { headers.Timestamp = timestampValues.First(); } if (request.Headers.TryGetValue(WeChatPayConsts.Wechatpay_Nonce, out var nonceValues)) { headers.Nonce = nonceValues.First(); } if (request.Headers.TryGetValue(WeChatPayConsts.Wechatpay_Signature, out var signatureValues)) { headers.Signature = signatureValues.First(); } return(headers); }
private async Task CheckNotifySignAsync(WeChatPayHeaders headers, string body, WeChatPayOptions options) { if (string.IsNullOrEmpty(headers.Serial)) { throw new WeChatPayException($"sign check fail: {nameof(headers.Serial)} is empty!"); } if (string.IsNullOrEmpty(headers.Signature)) { throw new WeChatPayException($"sign check fail: {nameof(headers.Signature)} is empty!"); } if (string.IsNullOrEmpty(body)) { throw new WeChatPayException("sign check fail: body is empty!"); } var cert = await LoadPlatformCertificateAsync(headers.Serial, options); var signatureSourceData = BuildSignatureSourceData(headers.Timestamp, headers.Nonce, body); if (!SHA256WithRSA.Verify(cert.GetRSAPublicKey(), signatureSourceData, headers.Signature)) { throw new WeChatPayException("sign check fail: check Sign and Data Fail!"); } }
public async Task <T> ExecuteAsync <T>(WeChatPayHeaders headers, string body, WeChatPayOptions options) where T : WeChatPayNotify { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrEmpty(options.V3Key)) { throw new ArgumentNullException(nameof(options.V3Key)); } await CheckNotifySignAsync(headers, body, options); var parser = new WeChatPayNotifyJsonParser <T>(); var notify = parser.Parse(body, options.V3Key); return(notify); }