Пример #1
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request) where T : WeChatPayResponse
        {
            // 字典排序
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { mch_id, Options.MchId },
                { nonce_str, Guid.NewGuid().ToString("N") }
            };

            if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
            {
                sortedTxtParams.Add(appid, Options.AppId);
            }

            sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, Options.Key));
            var content = WeChatPayUtility.BuildContent(sortedTxtParams);

            Logger?.LogTrace(0, "Request:{content}", content);

            using (var client = ClientFactory.CreateClient())
            {
                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);

                Logger?.LogTrace(1, "Response:{body}", body);

                var parser = new WeChatPayXmlParser <T>();
                var rsp    = parser.Parse(body);
                CheckResponseSign(rsp);
                return(rsp);
            }
        }
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request, string optionsName) where T : WeChatPayResponse
        {
            var signType = true; // ture:MD5,false:HMAC-SHA256
            var options  = _optionsSnapshotAccessor.Get(optionsName);

            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { mch_id, options.MchId },
                { nonce_str, Guid.NewGuid().ToString("N") }
            };

            if (request is WeChatPayDepositMicroPayRequest || request is WeChatPayDepositOrderQueryRequest || request is WeChatPayDepositRefundQueryRequest)
            {
                sortedTxtParams.Add(sign_type, SIGN_TYPE_HMAC_SHA256);
                signType = false; // HMAC-SHA256
            }

            if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
            {
                sortedTxtParams.Add(appid, options.AppId);
            }

            sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType));
            var content = WeChatPayUtility.BuildContent(sortedTxtParams);

            _logger.Log(options.LogLevel, "Request:{content}", content);

            using (var client = _clientFactory.CreateClient())
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(), content);

                _logger.Log(options.LogLevel, "Response:{body}", body);

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

                if (request.IsCheckResponseSign())
                {
                    CheckResponseSign(response, options, signType);
                }

                return(response);
            }
        }
Пример #3
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayRequest <T> request, string optionsName) where T : WeChatPayResponse
        {
            var options = string.IsNullOrEmpty(optionsName) ? _optionsSnapshotAccessor.Value : _optionsSnapshotAccessor.Get(optionsName);

            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { mch_id, options.MchId },
                { nonce_str, Guid.NewGuid().ToString("N") }
            };

            if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
            {
                sortedTxtParams.Add(appid, options.AppId);
            }

            sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key));
            var content = WeChatPayUtility.BuildContent(sortedTxtParams);

            _logger.Log(options.LogLevel, "Request:{content}", content);

            using (var client = _clientFactory.CreateClient())
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(), content);

                _logger.Log(options.LogLevel, "Response:{body}", body);

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

                if (request.IsCheckResponseSign())
                {
                    CheckResponseSign(response, options);
                }

                return(response);
            }
        }
        public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request, string optionsName, string certificateName) where T : WeChatPayResponse
        {
            var signType        = true; // ture:MD5,false:HMAC-SHA256
            var options         = _optionsSnapshotAccessor.Get(optionsName);
            var sortedTxtParams = new WeChatPayDictionary(request.GetParameters())
            {
                { nonce_str, Guid.NewGuid().ToString("N") }
            };

            if (request is WeChatPayTransfersRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(mch_appid)))
                {
                    sortedTxtParams.Add(mch_appid, options.AppId);
                }

                sortedTxtParams.Add(mchid, options.MchId);
            }
            else if (request is WeChatPayGetPublicKeyRequest)
            {
                sortedTxtParams.Add(mch_id, options.MchId);
                sortedTxtParams.Add(sign_type, SIGN_TYPE_MD5);
            }
            else if (request is WeChatPayPayBankRequest)
            {
                if (options.PublicKey == null)
                {
                    throw new WeChatPayException("WeChatPayPayBankRequest: PublicKey is null!");
                }

                var no = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_bank_no), options.PublicKey);
                sortedTxtParams.SetValue(enc_bank_no, no);

                var name = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_true_name), options.PublicKey);
                sortedTxtParams.SetValue(enc_true_name, name);

                sortedTxtParams.Add(mch_id, options.MchId);
            }
            else if (request is WeChatPayQueryBankRequest)
            {
                sortedTxtParams.Add(mch_id, options.MchId);
            }
            else if (request is WeChatPayGetTransferInfoRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);
            }
            else if (request is WeChatPayDownloadFundFlowRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);
                sortedTxtParams.Add(sign_type, SIGN_TYPE_HMAC_SHA256);
                signType = false; // HMAC-SHA256
            }
            else if (request is WeChatPayRefundRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);
            }
            else if (request is WeChatPaySendRedPackRequest || request is WeChatPaySendGroupRedPackRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(wxappid)))
                {
                    sortedTxtParams.Add(wxappid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);
            }
            else if (request is WeChatPaySendWorkWxRedPackRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(wxappid)))
                {
                    sortedTxtParams.Add(wxappid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);

                var sign_list = new List <string>
                {
                    "act_name",
                    "mch_billno",
                    "mch_id",
                    "nonce_str",
                    "re_openid",
                    "total_amount",
                    "wxappid",
                };

                sortedTxtParams.Add(workwx_sign, WeChatPaySignature.SignWithSecret(sortedTxtParams, options.Secret, sign_list));
            }
            else if (request is WeChatPayPayWwSpTrans2PockeRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);

                var sign_list = new List <string>
                {
                    "amount",
                    "appid",
                    "desc",
                    "mch_id",
                    "nonce_str",
                    "openid",
                    "partner_trade_no",
                    "ww_msg_type",
                };

                sortedTxtParams.Add(workwx_sign, WeChatPaySignature.SignWithSecret(sortedTxtParams, options.Secret, sign_list));
            }
            else if (request is WeChatPayDepositReverseRequest || request is WeChatPayDepositConsumeRequest || request is WeChatPayDepositRefundRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);
                sortedTxtParams.Add(sign_type, SIGN_TYPE_HMAC_SHA256);
                signType = false; // HMAC-SHA256
            }
            else // 其他接口
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, options.AppId);
                }

                sortedTxtParams.Add(mch_id, options.MchId);
            }

            sortedTxtParams.Add(sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType));

            var content = WeChatPayUtility.BuildContent(sortedTxtParams);

            _logger.Log(options.LogLevel, "Request:{content}", content);

            using (var client = _clientFactory.CreateClient(certificateName))
            {
                var body = await client.DoPostAsync(request.GetRequestUrl(), content);

                _logger.Log(options.LogLevel, "Response:{body}", body);

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

                if (request.IsCheckResponseSign())
                {
                    CheckResponseSign(response, options, signType);
                }

                return(response);
            }
        }
Пример #5
0
        public async Task <T> ExecuteAsync <T>(IWeChatPayCertificateRequest <T> request, string certificateName = "Default") where T : WeChatPayResponse
        {
            var signType        = true; // ture:MD5,false:HMAC-SHA256
            var excludeSignType = true;

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

            if (request is WeChatPayTransfersRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(mch_appid)))
                {
                    sortedTxtParams.Add(mch_appid, 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)
            {
                if (Options.PublicKey == null)
                {
                    throw new ArgumentNullException(nameof(Options.RsaPublicKey));
                }

                var no = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_bank_no), Options.PublicKey);
                sortedTxtParams.SetValue(enc_bank_no, no);

                var name = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(enc_true_name), Options.PublicKey);
                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)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, Options.AppId);
                }

                sortedTxtParams.Add(mch_id, Options.MchId);
                sortedTxtParams.Add(sign_type, "MD5");
            }
            else if (request is WeChatPayDownloadFundFlowRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, Options.AppId);
                }

                sortedTxtParams.Add(mch_id, Options.MchId);
                signType = false; // HMAC-SHA256
            }
            else if (request is WeChatPayRefundRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    sortedTxtParams.Add(appid, Options.AppId);
                }

                sortedTxtParams.Add(mch_id, Options.MchId);
            }
            else if (request is WeChatPaySendRedPackRequest || request is WeChatPaySendGroupRedPackRequest)
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(wxappid)))
                {
                    sortedTxtParams.Add(wxappid, Options.AppId);
                }

                sortedTxtParams.Add(mch_id, Options.MchId);
            }
            else // 其他接口
            {
                if (string.IsNullOrEmpty(sortedTxtParams.GetValue(appid)))
                {
                    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, signType, excludeSignType));

            var content = WeChatPayUtility.BuildContent(sortedTxtParams);

            Logger?.LogTrace(0, "Request:{content}", content);

            using (var client = ClientFactory.CreateClient(WeChatPayOptions.CertificateClientName + "." + certificateName))
            {
                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);

                Logger?.LogTrace(1, "Response:{body}", body);

                var parser = new WeChatPayXmlParser <T>();
                var rsp    = parser.Parse(body);
                CheckResponseSign(rsp, signType, excludeSignType);
                return(rsp);
            }
        }