コード例 #1
0
        /// <summary>
        /// 企业付款到零钱
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public async Task <PayToWalletResponse> PayToWalletAsync(PayToWalletRequest parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            if (string.IsNullOrEmpty(_password) || string.IsNullOrEmpty(_path))
            {
                throw new InvalidOperationException("证书的路径和密码没有填写");
            }
            parameter.AppId = _appId;
            parameter.MchId = _mchId;
            string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

            parameter.Sign = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string body = XmlSerializeHelper.ObjectToXmlString(parameter);

            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, _path, _password, "xml/text");

            var response = XmlSerializeHelper.StringToObject <PayToWalletResponse>(result);

            Check(response);
            return(response);
        }
コード例 #2
0
        private RefundNotificationRequest RefundParameterCheck(string input, string mchId, string appId, string nonce)
        {
            using (MD5 md5 = MD5.Create())
            {
                var           base64 = Convert.FromBase64String(input);
                StringBuilder output = new StringBuilder();
                var           data   = md5.ComputeHash(Encoding.UTF8.GetBytes(_securityKey));
                foreach (var b in data)
                {
                    output.Append(b.ToString("x2"));
                }

                string          key           = output.ToString();
                byte[]          keyBuffer     = Encoding.UTF8.GetBytes(key);
                byte[]          encryptBuffer = base64;
                RijndaelManaged rDel          = new RijndaelManaged
                {
                    Key = keyBuffer, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7
                };
                ICryptoTransform cTransform   = rDel.CreateDecryptor();
                byte[]           resultBuffer =
                    cTransform.TransformFinalBlock(encryptBuffer, 0, encryptBuffer.Length);
                string xml = Encoding.UTF8.GetString(resultBuffer);
                RefundNotificationRequest notification =
                    XmlSerializeHelper.StringToObject <RefundNotificationRequest>(xml);
                notification.MchId = mchId;
                notification.AppId = appId;
                notification.Nonce = nonce;
                return(notification);
            }
        }
コード例 #3
0
        /// <summary>
        /// 统一下单
        /// </summary>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">传入的参数是null</exception>
        /// <exception cref="WeChatPayException">调用微信接口失败时返回的错误信息</exception>
        public async Task <UnifiedOrderResponse> UnifiedOrderAsync(UnifiedOrderRequest parameter)
        {
            parameter.AppId = _appId;
            parameter.MchId = _mchId;
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            if (parameter.TradeType == WeChatConstant.JsPay && string.IsNullOrEmpty(parameter.OpenId))
            {
                throw new WeChatPayException("支付方式JsPay OpenId 必须传入");
            }
            if (parameter.TradeType == WeChatConstant.NativePay && string.IsNullOrEmpty(parameter.ProductId))
            {
                throw new WeChatPayException("支付方式NativePay ProductId 必须传入");
            }
            parameter.Sign = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string url    = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, "xml/text");

            var response = XmlSerializeHelper.StringToObject <UnifiedOrderResponse>(result);

            Check(response);
            return(response);
        }
コード例 #4
0
        /// <summary>
        /// 获取订单
        /// </summary>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">传入的参数是null</exception>
        /// <exception cref="WeChatPayException">调用微信接口失败时返回的错误信息</exception>
        public async Task <QueryOrderResponse> GetOrderAsync(QueryOrderRequest parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            parameter.AppId = _appId;
            parameter.MchId = _mchId;
            parameter.Sign  = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string url    = "https://api.mch.weixin.qq.com/pay/orderquery";
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, "xml/text");

            XmlDocument xml = new XmlDocument();

            xml.LoadXml(result);

            var response = XmlSerializeHelper.StringToObject <QueryOrderResponse>(result);

            Check(response);
            response.CouponType         = Helper.FormatXmlField("coupon_type", xml);
            response.CouponId           = Helper.FormatXmlField("coupon_refund_id", xml);
            response.SingleCouponAmount = Helper.FormatXmlField("coupon_refund_fee", xml)
                                          .Select(x => Convert.ToInt32(x)).ToArray();


            return(response);
        }
コード例 #5
0
        /// <summary>
        /// 支付异步通知处理
        /// </summary>
        /// <param name="input">微信发送的数据</param>
        /// <returns></returns>
        /// <exception cref="WeChatPayException"></exception>
        public async Task <string> ExecutedAsync(Stream input)
        {
            byte[] buffer = new byte[input.Length];
            input.Position = 0;
            input.Read(buffer);
            var xmlStr = Encoding.UTF8.GetString(buffer);

            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(xmlStr);

            var         node     = xmlDocument.FirstChild;
            PayResponse response = null;

            if (node.SelectSingleNode("return_code").InnerText == WeChatConstant.PaySuccess)
            {
                foreach (var handler in _handlers)
                {
                    if (handler is IRefundNotificationHandler refundHandler &&
                        node.SelectSingleNode("req_info") != null)
                    {
                        var encrypt      = node.SelectSingleNode("req_info").InnerText;
                        var mchId        = node.SelectSingleNode("mch_id").InnerText;
                        var appId        = node.SelectSingleNode("appid").InnerText;
                        var nonce        = node.SelectSingleNode("nonce_str").InnerText;
                        var notification = RefundParameterCheck(encrypt, mchId, appId, nonce);
                        response = await refundHandler.SuccessExecuted(notification);
                    }

                    if (handler is IUnifiedOrderNotificationHandler unifiedHandler &&
                        node.SelectSingleNode("result_code").InnerText == WeChatConstant.PaySuccess)
                    {
                        if (Check.PaySignCheck(xmlDocument, _securityKey) == false)
                        {
                            throw new WeChatPayException("签名验证失败");
                        }

                        var notification =
                            XmlSerializeHelper.StringToObject <UnifiedOrderNotificationRequest>(xmlStr);

                        response = await unifiedHandler.SuccessExecuted(notification);
                    }
                }
            }
            else
            {
                string errorCode = node.SelectSingleNode("err_code").InnerText;
                string errorMsg  = node.SelectSingleNode("err_code_des").InnerText;
                return(Failed(errorCode, errorMsg));
            }

            if (response == null)
            {
                throw new WeChatPayException("未找到可用的Handler");
            }

            return(Succeed());
        }
コード例 #6
0
        /// <summary>
        /// 查询退款订单
        /// </summary>
        /// <param name="parameter">参数</param>
        /// <exception cref="ArgumentNullException">传入的参数是null</exception>
        /// <exception cref="WeChatPayException">调用微信接口失败时返回的错误信息</exception>
        public async Task <QueryRefundOrderResponse> GetRefundOrders(QueryRefundOrderRequest parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            parameter.AppId = _appId;
            parameter.MchId = _mchId;
            parameter.Sign  = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string url    = "https://api.mch.weixin.qq.com/pay/refundquery";
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, "xml/text");

            XmlDocument xml = new XmlDocument();

            xml.Load(result);
            var response = XmlSerializeHelper.StringToObject <QueryRefundOrderResponse>(result);

            Check(response);
            response.WeChatRefundOrderCode = Helper.FormatXmlField("refund_id", xml);
            response.RefundOrderCode       = Helper.FormatXmlField("out_refund_no", xml);
            response.RefundChannel         = Helper.FormatXmlField("refund_channel", xml);
            response.RefundAmount          = Helper.FormatXmlField("	refund_fee", xml)
                                             .Select(x => Convert.ToInt32(x))
                                             .ToArray();
            response.SettlementRefundAmount = Helper.FormatXmlField("settlement_refund_fee", xml)
                                              .Select(x => Convert.ToInt32(x))
                                              .ToArray();
            response.CouponType   = Helper.FormatXmlField("coupon_type", xml);
            response.CouponAmount = Helper.FormatXmlField("coupon_refund_fee", xml)
                                    .Select(x => Convert.ToInt32(x))
                                    .ToArray();
            response.CouponCount = Helper.FormatXmlField("coupon_refund_count", xml)
                                   .Select(x => Convert.ToInt32(x))
                                   .ToArray();
            response.CouponId           = Helper.FormatXmlField("coupon_refund_id", xml);
            response.SingleCouponAmount = Helper.FormatXmlField("coupon_refund_fee", xml)
                                          .Select(x => Convert.ToInt32(x))
                                          .ToArray();
            response.State         = Helper.FormatXmlField("refund_status", xml);
            response.Source        = Helper.FormatXmlField("refund_account", xml);
            response.RefundAccount = Helper.FormatXmlField("refund_recv_accout", xml);
            response.RefundDate    = Helper.FormatXmlField("refund_success_time", xml)
                                     .Select(Convert.ToDateTime)
                                     .ToArray();
            return(response);
        }
コード例 #7
0
        /// <summary>
        /// 发送裂变红包
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public async Task <SendRedPackResponse> SendFissionRedPack(SendFissionRedPackRequest parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            parameter.Sign = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string url    = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, "xml/text");

            var response = XmlSerializeHelper.StringToObject <SendRedPackResponse>(result);

            Check(response);

            return(response);
        }
コード例 #8
0
        public async Task <CancelScanOrderResponse> CancelScanOrderAsync(CancelScanOrderRequest parameter)
        {
            parameter.AppId = _appId;
            parameter.MchId = _mchId;
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            parameter.Sign = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string url    = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, "xml/text");

            var response = XmlSerializeHelper.StringToObject <CancelScanOrderResponse>(result);

            Check(response);

            return(response);
        }
コード例 #9
0
        /// <summary>
        /// 企业付款到银行卡
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public async Task <PayToBankResponse> PayToBankAsync(PayToBankRequest parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            string url = "https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank";

            parameter.MchId = _mchId;
            parameter.Sign  = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, _path, _password, "xml/text");

            var response = XmlSerializeHelper.StringToObject <PayToBankResponse>(result);

            Check(response);
            return(response);
        }
コード例 #10
0
        /// <summary>
        /// 获取红包发送记录
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public async Task <QuerySendRedPackResponse> GetSendRedPackHistory(
            QuerySendRedPackRequest parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            parameter.Sign = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);

            string url    = "https://api.mch.weixin.qq.com/pay/closeorder";
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, "xml/text");

            var response = XmlSerializeHelper.StringToObject <QuerySendRedPackResponse>(result);

            Check(response);

            return(response);
        }
コード例 #11
0
        public async Task <string> GetPublicKeyAsync()
        {
            string      url         = "https://fraud.mch.weixin.qq.com/risk/getpublickey";
            XmlDocument xmlDocument = new XmlDocument();

            var root      = xmlDocument.CreateElement("xml");
            var idElement = xmlDocument.CreateElement("mch_id");

            idElement.InnerText = _mchId;
            var nonceElement = xmlDocument.CreateElement("nonce_str");

            nonceElement.InnerText = Helper.GetNonceStr(32);
            var signTypeElement = xmlDocument.CreateElement("sign_type");

            signTypeElement.InnerText = "MD5";
            root.AppendChild(idElement);
            root.AppendChild(nonceElement);
            root.AppendChild(signTypeElement);
            xmlDocument.AppendChild(root);

            string sign = WeChatSignHelper.CreateMd5SignByXml(xmlDocument, _secretKey);

            var signElement = xmlDocument.CreateElement("sign");

            signElement.InnerText = sign;
            root.AppendChild(signElement);
            string body = XmlSerializeHelper.XmlToString(xmlDocument);

            var result =
                await _client.ExecutePostRequest(url, new Dictionary <string, string>(), body, _path, _password,
                                                 "xml/text");

            Check(XmlSerializeHelper.StringToObject <PayResponse>(result));

            XmlDocument res = new XmlDocument();

            res.LoadXml(result);


            return(res.FirstChild.SelectSingleNode("pub_key").InnerText);
        }
コード例 #12
0
        /// <summary>
        /// 申请退款
        /// </summary>
        /// <param name="parameter">参数</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">传入的参数是null</exception>
        /// <exception cref="WeChatPayException">调用微信接口失败时返回的错误信息</exception>
        public async Task <RefundOrderResponse> RefundOrder(RefundOrderRequest parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            parameter.AppId = _appId;
            parameter.MchId = _mchId;
            parameter.Nonce = Helper.GetNonceStr(32);
            parameter.Sign  = WeChatSignHelper.CreateMd5Sign(parameter, _secretKey);
            string url    = "https://api.mch.weixin.qq.com/pay/closeorder";
            string body   = XmlSerializeHelper.ObjectToXmlString(parameter);
            string result = await _client.ExecutePostRequest(url, new Dictionary <string, string>(),
                                                             body, _path, _password, "xml/text");

            var response = XmlSerializeHelper.StringToObject <RefundOrderResponse>(result);

            Check(response);

            return(response);
        }