Exemplo n.º 1
0
        /**
         *
         * 统一下单
         * @param WxPaydata inputObj 提交给统一下单API的参数
         * @param int timeOut 超时时间
         * @throws WxPayException
         * @return 成功时返回,其他抛异常
         */
        public PaymentData UnifiedOrder(PaymentData inputObj, int timeOut = 6)
        {
            string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no"))
            {
                throw new Exception("缺少统一支付接口必填参数out_trade_no!");
            }
            else if (!inputObj.IsSet("body"))
            {
                throw new Exception("缺少统一支付接口必填参数body!");
            }
            else if (!inputObj.IsSet("total_fee"))
            {
                throw new Exception("缺少统一支付接口必填参数total_fee!");
            }
            else if (!inputObj.IsSet("trade_type"))
            {
                throw new Exception("缺少统一支付接口必填参数trade_type!");
            }

            //关联参数
            if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
            {
                throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
            }
            if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
            {
                throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
            }

            //异步通知url未设置,则使用配置文件中的url
            if (!inputObj.IsSet("notify_url"))
            {
                inputObj.SetValue("notify_url", NOTIFY_URL);//异步通知url
            }


            inputObj.SetValue("appid", AppId);                  //公众账号ID
            inputObj.SetValue("mch_id", MchId);                 //商户号
            inputObj.SetValue("spbill_create_ip", Ip);          //终端ip
            inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串
            //签名
            inputObj.SetValue("sign", inputObj.MakeSign(SignKey));
            string      xml      = inputObj.ToXml();
            string      response = HttpService.Post(xml, url, false, timeOut, "", "");
            PaymentData result   = new PaymentData();

            result.FromXml(response);
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 申请退款
        /// </summary>
        /// <param name="inputObj">提交给申请退款API的参数</param>
        /// <param name="CertPath">证书路径</param>
        /// <param name="CertPassword">证书密码</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns></returns>
        public PaymentData Refund(PaymentData inputObj, int timeOut = 6)
        {
            string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
            {
                throw new Exception("退款申请接口中,out_trade_no、transaction_id至少填一个!");
            }
            else if (!inputObj.IsSet("out_refund_no"))
            {
                throw new Exception("退款申请接口中,缺少必填参数out_refund_no!");
            }
            else if (!inputObj.IsSet("total_fee"))
            {
                throw new Exception("退款申请接口中,缺少必填参数total_fee!");
            }
            else if (!inputObj.IsSet("refund_fee"))
            {
                throw new Exception("退款申请接口中,缺少必填参数refund_fee!");
            }
            else if (!inputObj.IsSet("op_user_id"))
            {
                throw new Exception("退款申请接口中,缺少必填参数op_user_id!");
            }

            inputObj.SetValue("appid", AppId);                                          //公众账号ID
            inputObj.SetValue("mch_id", MchId);                                         //商户号
            inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串
            inputObj.SetValue("sign", inputObj.MakeSign(SignKey));                      //签名
            string xml      = inputObj.ToXml();
            string response = HttpService.Post(xml, url, true, timeOut, CertPath, CertPassword);
            //将xml格式的结果转换为对象以返回
            PaymentData result = new PaymentData();

            result.FromXml(response);
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 企业付款接口
        /// </summary>
        /// <param name="inputObj">参数</param>
        /// <param name="CertPath">证书路径</param>
        /// <param name="CertPassword">证书密码</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns></returns>
        public PaymentData Transfers(PaymentData inputObj, int timeOut = 6)
        {
            string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

            #region 检测必要参数

            //检测必填参数
            if (!inputObj.IsSet("partner_trade_no") && !inputObj.IsSet("partner_trade_no"))
            {
                throw new Exception("企业付款接口中,缺少必填参数partner_trade_no!");
            }
            if (!inputObj.IsSet("openid"))
            {
                throw new Exception("企业付款接口中,缺少必填参数openid!");
            }
            if (!inputObj.IsSet("check_name"))
            {
                throw new Exception("企业付款接口中,缺少必填参数check_name!");
            }
            else
            {
                string checkName = inputObj.GetValue("check_name").ToString();
                switch (checkName)
                {
                case "FORCE_CHECK":
                case "OPTION_CHECK":
                    if (!inputObj.IsSet("check_name"))
                    {
                        throw new Exception("企业付款接口中,缺少必填参数re_user_name!");
                    }
                    break;

                default:
                    break;
                }
            }
            if (!inputObj.IsSet("amount"))
            {
                throw new Exception("企业付款接口中,缺少必填参数amount!");
            }
            if (!inputObj.IsSet("desc"))
            {
                throw new Exception("企业付款接口中,缺少必填参数desc!");
            }
            if (!inputObj.IsSet("spbill_create_ip"))
            {
                throw new Exception("企业付款接口中,缺少必填参数spbill_create_ip!");
            }
            #endregion

            #region 添加公共参数
            inputObj.SetValue("mch_appid", AppId);                                      //公众账号ID
            inputObj.SetValue("mchid", MchId);                                          //商户号
            inputObj.SetValue("spbill_create_ip", Ip);                                  //随机字符串
            inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串
            inputObj.SetValue("sign", inputObj.MakeSign(SignKey));                      //签名
            #endregion
            string      xml      = inputObj.ToXml();
            var         start    = DateTime.Now;
            string      response = HttpService.Post(xml, url, true, timeOut, CertPath, CertPassword);
            PaymentData result   = new PaymentData();
            result.FromXml(response);

            return(result);
        }