Beispiel #1
0
        /// <summary>
        /// 订单查询
        /// </summary>
        /// <param name="byTransactionID"></param>
        /// <param name="identify"></param>
        /// <returns></returns>
        private PayQueryResult OrderQuery(bool byTransactionID, string identify)
        {
            ParamCheckHelper.WhiteSpaceThrow(identify, "identify");
            ParamCheckHelper.LimitLengthThrow(identify, 32, "identify");

            string nonce_str = Guid.NewGuid().ToString().Replace("-", "");

            SortedDictionary<string, string> parameters = new SortedDictionary<string, string>();
            StringBuilder builder = new StringBuilder();
            builder.Append("<xml>");
            builder.Append("<appid>" + this.AppID + "</appid>");
            parameters.Add("appid", this.AppID);

            builder.Append("<mch_id>" + this.MchID + "</mch_id>");
            parameters.Add("mch_id", this.MchID);

            if (byTransactionID)
            {
                builder.Append("<transaction_id>" + identify + "</transaction_id>");
                parameters.Add("transaction_id", identify);
            }
            else
            {
                builder.Append("<out_trade_no>" + identify + "</out_trade_no>");
                parameters.Add("out_trade_no", identify);
            }

            builder.Append("<nonce_str>" + nonce_str + "</nonce_str>");
            parameters.Add("nonce_str", nonce_str);

            string sign = CalcSign(parameters);

            builder.Append("<sign>" + sign + "</sign>");
            builder.Append("</xml>");

            string xml = string.Empty;
            try
            {
                xml = ZTImage.HttpEx.SyncPost(Order_QUERY_Url, builder.ToString(), Encoding.UTF8);
            }
            catch (Exception ex)
            {
                ZTImage.Log.Trace.Error("订单查询时出现错误", ex);
            }

            return ParseOrderQuery(xml);
        }
Beispiel #2
0
        /// <summary>
        /// 统一下单
        /// </summary>
        /// <param name="tradeType">交易类型</param>
        /// <param name="body">商品描述,128</param>
        /// <param name="attach">附加数据</param>
        /// <param name="tradeNo">商户订单号</param>
        /// <param name="totalFee">标价金额,以‘分’为单位</param>
        /// <param name="clientIP">客户端IP,如果没有客户端,传服务器IP</param>
        /// <param name="productID">商品ID,商户自定义,native时必传</param>
        /// <param name="openid">用户标识,jsapi时必传</param>
        /// <returns></returns>
        public PrepayResult UnifiedOrder(TradeType tradeType,string body,string attach, string tradeNo, int totalFee, string clientIP,string productID,string openid,string notifyUrl=null)
        {
            ParamCheckHelper.LimitLengthThrow(body, 128, "body");
            ParamCheckHelper.LimitLengthThrow(attach, 127, "attach");
            ParamCheckHelper.LimitLengthThrow(tradeNo, 32, "tradeNo");

            if (totalFee <= 0)
            {
                throw new ArgumentOutOfRangeException("totalFee");
            }

            if (!ZTImage.Text.Valid.IsIP(clientIP))
            {
                throw new ArgumentOutOfRangeException("clientIP");
            }

            if (tradeType == TradeType.NATIVE)
            {
                ParamCheckHelper.LimitLengthThrow(productID, 32,"productID");
            }

            if (tradeType == TradeType.JSAPI)
            {
                ParamCheckHelper.LimitLengthThrow(openid, 128, "openid");
            }

            string nonce_str = Guid.NewGuid().ToString().Replace("-", "");
            
            SortedDictionary<string, string> parameters = new SortedDictionary<string, string>();
            StringBuilder builder = new StringBuilder();
            builder.Append("<xml>");            
            builder.Append("<appid>"+this.AppID+"</appid>");
            parameters.Add("appid", this.AppID);

            builder.Append("<attach>"+attach+"</attach>");
            parameters.Add("attach", attach);

            builder.Append("<body>"+body+"</body>");
            parameters.Add("body", body);

            builder.Append("<mch_id>"+this.MchID+"</mch_id>");
            parameters.Add("mch_id", this.MchID);

            builder.Append("<nonce_str>"+nonce_str+"</nonce_str>");
            parameters.Add("nonce_str", nonce_str);

            builder.Append("<notify_url>"+(string.IsNullOrEmpty(notifyUrl)?this.NotifyURL:notifyUrl)+"</notify_url>");
            parameters.Add("notify_url", (string.IsNullOrEmpty(notifyUrl) ? this.NotifyURL : notifyUrl));

            if (!string.IsNullOrEmpty(productID))
            {
                builder.Append("<product_id>"+productID+"</product_id>");
                parameters.Add("product_id", productID);
            }
            

            if (!string.IsNullOrWhiteSpace(openid))
            {
                builder.Append("<openid>"+openid+"</openid>");
                parameters.Add("openid", openid);
            }
            

            builder.Append("<out_trade_no>"+tradeNo+"</out_trade_no>");
            parameters.Add("out_trade_no", tradeNo);

            builder.Append("<spbill_create_ip>"+clientIP+"</spbill_create_ip>");
            parameters.Add("spbill_create_ip", clientIP);

            builder.Append("<total_fee>"+totalFee+"</total_fee>");
            parameters.Add("total_fee", totalFee.ToString());

            string tradeTypeString = "JSAPI";
            switch (tradeType)
            {
                case TradeType.NATIVE:
                    tradeTypeString = "NATIVE ";
                    break;

                case TradeType.APP:
                    tradeTypeString = "APP";
                    break;
            }

            builder.Append("<trade_type>"+ tradeTypeString + "</trade_type>");
            parameters.Add("trade_type", tradeTypeString);

            string sign = CalcSign(parameters);

            builder.Append("<sign>"+sign+"</sign>");
            builder.Append("</xml>");

            string xml = string.Empty;
            try
            {
                xml = ZTImage.HttpEx.SyncPost(Unified_Order_Url,builder.ToString(),Encoding.UTF8);
            }
            catch (Exception ex)
            {
                ZTImage.Log.Trace.Error("请求统一下单时出现错误", ex);
            }

            return ParseUnifiedOrderResult(xml);
        }