Beispiel #1
0
        // Json 格式化处理
        private static async Task <T> JsonFormat <T>(WechatPayConfig config, HttpResponseMessage resp, bool needCheckSign)
            where T : WechatBaseResp, new()
        {
            if (!resp.IsSuccessStatusCode)
            {
                var content = await resp.Content.ReadAsStringAsync();

                return(string.IsNullOrEmpty(content)
                    ? new T().WithResp(SysRespCodes.NetError, $"微信支付接口请求异常({resp.ReasonPhrase})")
                    : JsonSerializer.Deserialize <T>(content));
            }

            var respDetail = await GetResponseDetail(resp);

            if (needCheckSign)
            {
                var verifyRes = await WechatCertificateHelper.Verify(config, respDetail.signature, respDetail.serial_no,
                                                                     respDetail.nonce, respDetail.timestamp, respDetail.body);

                if (!verifyRes.IsSuccess())
                {
                    verifyRes.request_id = respDetail.request_id;
                    return(verifyRes.ToResp <T>());
                }
            }

            return(string.IsNullOrEmpty(respDetail.body)
                ? new T()
                : JsonSerializer.Deserialize <T>(respDetail.body));
        }
 /// <summary>
 ///  设置当前请求对应的支付配置信息
 ///     仅当前请求下有效,如果配置全局信息,请设置     WechatPayConfigProvider.config
 /// </summary>
 /// <param name="req"></param>
 /// <param name="payConfig"></param>
 /// <returns></returns>
 public static TReq SetContextConfig <TReq>(this TReq req, WechatPayConfig payConfig)
     where TReq : WechatBaseReq
 {
     req.pay_config = payConfig;
     return(req);
 }
Beispiel #3
0
        private static async Task <SendEncryptBodyResp> GetReqContent_JsonEncryptSegment(WechatPayConfig payConfig,
                                                                                         Dictionary <string, string> dics)
        {
            var certRes = await WechatCertificateHelper.GetLatestCertsByConfig(payConfig);

            if (!certRes.IsSuccess())
            {
                return(certRes.ToResp <SendEncryptBodyResp>());
            }

            var cert = certRes.item;

            return(new SendEncryptBodyResp()
            {
                body = dics.ToDictionary(
                    d => d.Key,
                    d => WechatCertificateHelper.OAEPEncrypt(cert.cert_public_key, d.Value)
                    )
            });
        }
Beispiel #4
0
 /// <summary>
 /// 根据微信商户配置,验证结果签名
 /// </summary>
 /// <param name="payConfig">支付配置</param>
 /// <param name="signature">微信返回头信息中的签名</param>
 /// <param name="serialNo">微信返回头信息中的平台证书编号</param>
 /// <param name="nonce">微信返回头信息中的随机串</param>
 /// <param name="timestamp">微信返回头信息中的时间戳</param>
 /// <param name="respBody">微信返回的内容字符串</param>
 /// <returns></returns>
 public static Task <WechatBaseResp> Verify(WechatPayConfig payConfig, string signature,
                                            string serialNo, string nonce, long timestamp, string respBody)
 {
     return(WechatCertificateHelper.Verify(payConfig, signature, serialNo, nonce, timestamp, respBody));
 }