Exemple #1
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { APPID, Options.AppId },
                { MCH_ID, Options.MchId },
                { NONCE_STR, Guid.NewGuid().ToString("N") }
            };

            sortedTxtParams.Add(SIGN, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key));

            var rspContent = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams);

            if (string.IsNullOrEmpty(rspContent))
            {
                throw new Exception("rspContent is Empty!");
            }

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(rspContent);

            CheckResponseSign(rsp);
            return(rsp);
        }
Exemple #2
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { APPID, Options.AppId },
                { MCH_ID, Options.MchId },
                { NONCE_STR, Guid.NewGuid().ToString("N") }
            };

            sortedTxtParams.Add(SIGN, Md5.GetMD5WithKey(sortedTxtParams, Options.Key));

            var body = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams);

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(body);

            CheckResponseSign(rsp);
            return(rsp);
        }
        public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters());

            if (request is WeChatPayTransfersRequest) // 企业付款到零钱
            {
                sortedTxtParams.Add(MCHAPPID, Options.AppId);
                sortedTxtParams.Add(MCHID, Options.MchId);
            }
            else if (request is WeChatPayGetPublicKeyRequest || request is WeChatPayPayBankRequest || request is WeChatPayQueryBankRequest)
            {
                sortedTxtParams.Add(MCH_ID, Options.MchId);
                sortedTxtParams.Add(SIGN_TYPE, "MD5");
            }
            else if (request is WeChatPayGetTransferInfoRequest)
            {
                sortedTxtParams.Add(APPID, Options.AppId);
                sortedTxtParams.Add(MCH_ID, Options.MchId);
                sortedTxtParams.Add(SIGN_TYPE, "MD5");
            }
            else // 其他接口
            {
                sortedTxtParams.Add(APPID, Options.AppId);
                sortedTxtParams.Add(MCH_ID, Options.MchId);
            }

            if (request is WeChatPayPayBankRequest) // 企业付款到银行卡接口
            {
                var no = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_BANK_NO), Options.RsaPublicKey);
                sortedTxtParams.SetValue(ENC_BANK_NO, no);

                var name = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_TRUE_NAME), Options.RsaPublicKey);
                sortedTxtParams.SetValue(ENC_TRUE_NAME, name);
            }

            sortedTxtParams.Add(NONCE_STR, Guid.NewGuid().ToString("N"));
            sortedTxtParams.Add(SIGN, Md5.GetMD5WithKey(sortedTxtParams, Options.Key, !(request is WeChatPayGetPublicKeyRequest))); // 获取公钥 不排除sign_type

            var body = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams);

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(body);

            CheckResponseSign(rsp);
            return(rsp);
        }
        public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request) where T : WeChatPayResponse
        {
            var useMD5          = true;
            var excludeSignType = true;

            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters());

            if (request is WeChatPayTransfersRequest)
            {
                sortedTxtParams.Add(MCHAPPID, Options.AppId);
                sortedTxtParams.Add(MCHID, Options.MchId);
            }
            else if (request is WeChatPayGetPublicKeyRequest)
            {
                sortedTxtParams.Add(MCH_ID, Options.MchId);
                sortedTxtParams.Add(SIGN_TYPE, "MD5");
                excludeSignType = false;
            }
            else if (request is WeChatPayPayBankRequest)
            {
                var no = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_BANK_NO), Options.RsaPublicParameters);
                sortedTxtParams.SetValue(ENC_BANK_NO, no);

                var name = WeChatPaySignature.Encrypt(sortedTxtParams.GetValue(ENC_TRUE_NAME), Options.RsaPublicParameters);
                sortedTxtParams.SetValue(ENC_TRUE_NAME, name);

                sortedTxtParams.Add(MCH_ID, Options.MchId);
                sortedTxtParams.Add(SIGN_TYPE, "MD5");
            }
            else if (request is WeChatPayQueryBankRequest)
            {
                sortedTxtParams.Add(MCH_ID, Options.MchId);
                sortedTxtParams.Add(SIGN_TYPE, "MD5");
            }
            else if (request is WeChatPayGetTransferInfoRequest)
            {
                sortedTxtParams.Add(APPID, Options.AppId);
                sortedTxtParams.Add(MCH_ID, Options.MchId);
                sortedTxtParams.Add(SIGN_TYPE, "MD5");
            }
            else if (request is WeChatPayDownloadFundFlowRequest)
            {
                sortedTxtParams.Add(APPID, Options.AppId);
                sortedTxtParams.Add(MCH_ID, Options.MchId);
                sortedTxtParams.Add(SIGN_TYPE, "HMAC-SHA256");
                useMD5          = false;
                excludeSignType = false;
            }
            else // 其他接口
            {
                sortedTxtParams.Add(APPID, Options.AppId);
                sortedTxtParams.Add(MCH_ID, Options.MchId);
            }

            sortedTxtParams.Add(NONCE_STR, Guid.NewGuid().ToString("N"));
            sortedTxtParams.Add(SIGN, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key, useMD5, excludeSignType));

            var body = await Client.DoPostAsync(request.GetRequestUrl(), sortedTxtParams);

            var parser = new WeChatPayXmlParser <T>();
            var rsp    = parser.Parse(body);

            CheckResponseSign(rsp, useMD5, excludeSignType);
            return(rsp);
        }