Ejemplo n.º 1
0
        /// <summary>
        /// 上传这个App的相关捆。
        /// </summary>
        /// <param name="model">上传捆模型。</param>
        public void UploadApp(UploadBundleModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            var key   = model.Key;
            var bytes = model.Bytes;
            var url   = model.Url;
            var token = model.Token;

            using (var webClient = WebClientHelper.GetWebClient())
            {
                var createBytes = new CreateBytes();
                var arrayList   = new ArrayList
                {
                    createBytes.CreateFieldData("key", key),
                    createBytes.CreateFieldData("token", token),
                    createBytes.CreateFieldData("file", "App.ipa", "application/octet-stream", bytes)
                };
                webClient.Headers.Add("Content-Type", createBytes.ContentType);
                var data   = createBytes.JoinBytes(arrayList);
                var bytes2 = webClient.UploadData(url, data);
                Console.WriteLine(Encoding.UTF8.GetString(bytes2));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 上传图片。
        /// </summary>
        /// <param name="data">图片数据(大小限制1MB,支持JPG格式)。</param>
        /// <returns>如果上传成功则返回图片的绝对url地址。</returns>
        public string UploadImage(byte[] data)
        {
            var url         = "http://file.api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + _accountModel.GetAccessToken();
            var createBytes = new CreateBytes();
            var postData    = createBytes.JoinBytes(new List <byte[]>
            {
                createBytes.CreateFieldData("buffer", FileHelper.GetRandomFileName(data), "image/jpeg", data)
            });
            var json = WeiXinHttpHelper.PostString(url, postData, createBytes.ContentType);

            return(JObject.Parse(json)["url"].Value <string>());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 上传卡券Logo。
        /// </summary>
        /// <param name="bytes">文件字节组(大小限制1MB,像素为300*300,支持JPG格式。)。</param>
        /// <returns></returns>
        public string UploadLogo(byte[] bytes)
        {
            var url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + _accountModel.GetAccessToken();

            var createBytes = new CreateBytes();

            var data = createBytes.JoinBytes(new ArrayList
            {
                createBytes.CreateFieldData("buffer", FileHelper.GetRandomFileName(bytes),
                                            FileHelper.GetContentType(bytes), bytes)
            });
            var json = WeiXinHttpHelper.PostString(url, data, createBytes.ContentType);

            return(JObject.Parse(json)["url"].Value <string>());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 设置客服账号的头像。
        /// </summary>
        /// <param name="account">完整客服账号,格式为:账号前缀@公众号微信号</param>
        /// <param name="pictureData">客服人员的头像,头像图片文件必须是jpg格式,推荐使用640*640大小的图片以达到最佳效果</param>
        public void SetAccountHeadPicture(string account, byte[] pictureData)
        {
            if (!FileHelper.IsJpg(pictureData))
            {
                throw new NotSupportedException("头像图片文件必须是jpg格式。");
            }

            var url = string.Format("http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token={0}&kf_account={1}", _accountModel.GetAccessToken(), account);

            var createBytes = new CreateBytes();
            var list        = new List <byte[]>
            {
                createBytes.CreateFieldData("media", FileHelper.GetRandomFileName(pictureData), FileHelper.GetContentType(pictureData), pictureData),
            };
            var data = createBytes.JoinBytes(list);

            WeiXinHttpHelper.Post(url, data, createBytes.ContentType);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 上传。
        /// </summary>
        /// <typeparam name="TResult">结果类型。</typeparam>
        /// <param name="url">请求地址。</param>
        /// <param name="bytes">内容。</param>
        /// <param name="fieldName">文件名称。</param>
        /// <param name="func">其他字段数据。</param>
        /// <returns>结果。</returns>
        protected static TResult Upload <TResult>(string url, byte[] bytes, string fieldName, Func <CreateBytes, byte[][]> func = null) where TResult : class
        {
            var createBytes = new CreateBytes();
            var list        = new ArrayList
            {
                createBytes.CreateFieldData(fieldName, FileHelper.GetRandomFileName(bytes), FileHelper.GetContentType(bytes), bytes),
            };

            if (func != null)
            {
                foreach (var b in func(createBytes))
                {
                    list.Add(b);
                }
            }
            var data = createBytes.JoinBytes(list);

            return(WeiXinHttpHelper.PostResultByJson <TResult>(url, data, createBytes.ContentType));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 设置客服账号的头像。
        /// </summary>
        /// <param name="account">完整客服账号,格式为:账号前缀@公众号微信号</param>
        /// <param name="pictureData">客服人员的头像,头像图片文件必须是jpg格式,推荐使用640*640大小的图片以达到最佳效果</param>
        public void SetAccountHeadPicture(string account, byte[] pictureData)
        {
            if (!FileHelper.IsJpg(pictureData))
                throw new NotSupportedException("头像图片文件必须是jpg格式。");

            var url = string.Format("http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token={0}&kf_account={1}", _accountModel.GetAccessToken(), account);

            var createBytes = new CreateBytes();
            var list = new ArrayList
            {
                createBytes.CreateFieldData("media", FileHelper.GetRandomFileName(pictureData), FileHelper.GetContentType(pictureData), pictureData),
            };
            var data = createBytes.JoinBytes(list);

            WeiXinHttpHelper.Post(url, data, createBytes.ContentType);
        }
Ejemplo n.º 7
0
        private void DownLoadResult()   //下载结果
        {
            //业务参数赋值;
            string partner        = OnlinePay_Alipay_ForUserDistill_UserNumber;       //partner		合作伙伴ID			保留字段
            string key            = OnlinePay_Alipay_ForUserDistill_MD5Key_ForPayOut; //partner账户的支付宝安全校验码
            string gateway        = "https://www.alipay.com/cooperate/gateway.do";
            string service        = "bptb_result_file";
            string sign           = "";
            string _input_charset = "GB2312";                                                  //编码类型
            string biz_type       = "d_sale";                                                  //业务类型

            Shove.Alipay.Alipay ap = new Shove.Alipay.Alipay();

            //获取今日之前的处理中的最大日期,按天分组
            DataTable dt = Shove.Database.MSSQL.Select(ConnectionString, " select max([DateTime]) as [DateTime] from T_UserDistillPaymentFiles where Result = 0 and datediff(Day,[DateTime],getdate())>0 group by convert(varchar(8),[DateTime],112)");

            if (dt == null)
            {
                //写日志
                log.Write("获取今日之前的处理中的最大日期,按天分组出错.");
                return;
            }

            foreach (DataRow dr in dt.Rows)
            {
                string   fileName = "";
                DateTime time     = Shove._Convert.StrToDateTime(dr["DateTime"].ToString(), DateTime.Now.ToShortDateString());

                fileName = "result" + time.ToString("yyyyMMdd");   //文件名的格式

                //string ContentType = "application/octet-stream";
                // 生成需要上传的二进制数组
                CreateBytes cb = new CreateBytes();

                // 所有表单数据
                ArrayList bytesArray = new ArrayList();

                // 普通表单
                string[] Sortedstr = ap.GetDownloadParams
                                     (
                    service,
                    _input_charset,
                    partner,
                    biz_type
                                     );

                string[] downFields = new string[Sortedstr.Length + 1];
                int      i          = 0;

                char[] delimiterChars = { '=' };

                for (i = 0; i < Sortedstr.Length; i++)
                {
                    string fieldName  = Sortedstr[i].Split(delimiterChars)[0];
                    string fieldValue = Sortedstr[i].Split(delimiterChars)[1];

                    bytesArray.Add(cb.CreateFieldData(fieldName, fieldValue));
                    downFields[i] = fieldName + "=" + fieldValue;
                }

                string date = time.ToString("yyyyMMdd");
                bytesArray.Add(cb.CreateFieldData("date", date));
                downFields[i] = "date" + "=" + date;
                sign          = AlipayCommon.GetSign(downFields, key).Trim();
                bytesArray.Add(cb.CreateFieldData("sign", sign));
                bytesArray.Add(cb.CreateFieldData("sign_type", "MD5"));

                // 合成所有表单并生成二进制数组
                byte[] bytes = cb.JoinBytes(bytesArray);

                // 返回的内容
                byte[] responseBytes;

                // 上传到指定Url
                bool uploaded = false;

                try
                {
                    uploaded = cb.UploadData(gateway, bytes, out responseBytes);
                }
                catch (Exception ex)
                {
                    log.Write("cb.UploadData出错." + ex.Message);
                    continue;
                }

                if (!uploaded)
                {
                    log.Write("cb.UploadData失败.");
                    continue;
                }

                //PathName = System.AppDomain.CurrentDomain.BaseDirectory + "LogFiles\\" + pathname;
                string DownLoadFolder = System.AppDomain.CurrentDomain.BaseDirectory + "LogFiles\\GetAlipayPayResultTask\\RESULT\\";


                #region 查检下载目录是否存在,不存在则创建
                if (!Directory.Exists(DownLoadFolder))
                {
                    try
                    {
                        Directory.CreateDirectory(DownLoadFolder);
                    }
                    catch
                    {
                        log.Write("创建下载文件目录失败.");
                        return;
                    }
                }
                #endregion

                //判断responseByte是否以PK开头(以PK开头说明可以下载)
                if (responseBytes[0] != 80 && responseBytes[1] != 75)
                {
                    log.Write("判断responseByte不是以PK开头");
                    continue;
                }


                try
                {
                    if (File.Exists(DownLoadFolder + fileName + ".zip"))
                    {
                        File.Delete(DownLoadFolder + fileName + ".zip");
                    }

                    using (FileStream file = new FileStream(DownLoadFolder + fileName + ".zip", FileMode.Create, FileAccess.Write, FileShare.Read))
                    {
                        file.Write(responseBytes, 0, responseBytes.Length);
                    }

                    //解压下载的结果数据包
                    UnZip(DownLoadFolder + fileName + ".zip", DownLoadFolder + fileName);

                    //解压出来的结果文件
                    string strpath = DownLoadFolder + fileName + "\\ALIPAY__RESULT.csv";

                    //保存返回结果数据
                    DataTable dtResult         = new DataTable("TableName");
                    int       dtResultColCount = 0;

                    //保存返回结果中的文件明细表
                    DataTable dtResultFileDetail         = new DataTable("dtResultFileDetail");
                    int       dtResultFileDetailColCount = 0;


                    string   strline;
                    string[] aryline;

                    System.IO.StreamReader mysr = new System.IO.StreamReader(strpath, Encoding.GetEncoding("GB2312"));
                    int resultFileRow           = 0;

                    #region 构建返回结果表 和 返回结果中的文件列表数据表
                    while ((strline = mysr.ReadLine()) != null)
                    {
                        aryline = strline.Split(new char[] { ',' });

                        if (resultFileRow < 2) //构建返回结果表(2行,第1行为标题,第2行为对应的数据)   头部2行
                        {
                            if (resultFileRow == 0)
                            {
                                dtResultColCount = aryline.Length;
                                for (int j = 0; j < aryline.Length; j++)
                                {
                                    DataColumn newCol = new DataColumn(aryline[j]);//付款日期,批次数,已处理批次个数,处理中批次个数
                                    dtResult.Columns.Add(newCol);
                                }
                            }

                            if (resultFileRow == 1)
                            {
                                DataRow newRow = dtResult.NewRow();
                                for (int j = 0; j < dtResultColCount; j++)
                                {
                                    newRow[j] = aryline[j];
                                }
                                dtResult.Rows.Add(newRow);
                            }
                        }
                        else  //构建文件列表数据表
                        {
                            if (resultFileRow == 2) //结果文件的行2:  批次名称,处理状态,处理成功笔数,处理失败笔数,成功付款总金额,失败付款总金额,处理日期,支付宝帐号,备注
                            {
                                dtResultFileDetailColCount = aryline.Length;
                                for (int j = 0; j < aryline.Length; j++)
                                {
                                    DataColumn newCol = new DataColumn(aryline[j]);
                                    dtResultFileDetail.Columns.Add(newCol);
                                }
                            }

                            if (resultFileRow >= 3) //获取:papx_20090416_p623225643.csv,已处理,2,0,2.00,0.00,20090417,[email protected],处理完成
                            {
                                DataRow newResultFileDetailRow = dtResultFileDetail.NewRow();
                                for (int j = 0; j < dtResultFileDetailColCount; j++)
                                {
                                    try
                                    {
                                        newResultFileDetailRow[j] = aryline[j];
                                    }
                                    catch
                                    {
                                        newResultFileDetailRow[j] = "";
                                    }
                                }
                                dtResultFileDetail.Rows.Add(newResultFileDetailRow);
                            }
                        }

                        resultFileRow++;
                    }
                    #endregion 结束  构建返回结果表 和 返回结果中的文件列表数据表


                    #region 循环处理返回结果中的文件列表的文件,以更新提款状态
                    foreach (DataRow curResultFileDetailRow in dtResultFileDetail.Rows) //循环每个提交的文件
                    {
                        string CSVFile = curResultFileDetailRow[0].ToString();          //获取文件名
                        CSVFile = CSVFile.Substring(0, CSVFile.IndexOf(".")) + ".zip".ToLower();

                        if ((curResultFileDetailRow[1].ToString() == "已处理") && (curResultFileDetailRow[8].ToString() == "处理完成"))
                        {
                            string FilePath = DownLoadFolder + fileName + "\\" + curResultFileDetailRow[0].ToString();

                            DataTable dtCSVDetail = GetDetail(FilePath);    //获取当前处理的文件的提款记录明细数据

                            //循环处理当前CSV文件中的所有明细
                            foreach (DataRow drCSVDetail in dtCSVDetail.Rows)
                            {
                                if (drCSVDetail[9].ToString().ToLower() == "s")//成功标记
                                {
                                    //派款成功,更新状态标记
                                    long   distillID         = Shove._Convert.StrToLong(drCSVDetail[0].ToString(), -1);
                                    int    returnValue       = 0;
                                    string returnDescription = "";

                                    if (DAL.Procedures.P_UserDistillPayByAlipaySuccess(ConnectionString, 1, distillID, 0, ref returnValue, ref returnDescription) < 0)
                                    {
                                        log.Write("派款成功,P_UserDistillPayByAlipaySuccessAndUpdate状态更新失败.");
                                        continue;
                                    }
                                    if (returnValue < 0)
                                    {
                                        DAL.Procedures.P_UserDistillPayByAlipayWriteLog(ConnectionString, "出错!提款ID:" + distillID + "派款成功,但状态更新失败:" + returnDescription);
                                        continue;
                                    }
                                }
                                else
                                {
                                    //支付没有成功,返还会员提款的金额
                                    long   distillID = Shove._Convert.StrToLong(drCSVDetail[0].ToString(), -1);
                                    string memo      = "批量付款到银行卡失败:";//失败原因
                                    try
                                    {
                                        memo += drCSVDetail[12].ToString();
                                        memo  = memo.Length > 50 ? memo.Substring(0, 50) : memo;
                                    }
                                    catch { }

                                    int    returnValue       = 0;
                                    string returnDescription = "";

                                    if (DAL.Procedures.P_UserDistillPayByAlipayUnsuccess(ConnectionString, 1, distillID, memo, ref returnValue, ref returnDescription) < 0)
                                    {
                                        log.Write("派款失败,P_UserDistillPayByAlipayUnsuccessAndQuash状态更新失败.");
                                        continue;
                                    }
                                    if (returnValue < 0)
                                    {
                                        log.Write("派款成功,但状态更新失败:" + returnDescription);
                                        DAL.Procedures.P_UserDistillPayByAlipayWriteLog(ConnectionString, "出错!提款ID:" + distillID + "派款成功,但状态更新失败:" + returnDescription);
                                        continue;
                                    }
                                }
                            }//end foreach (DataRow drCSVDetail in dtCSVDetail.Rows)


                            if (Shove.Database.MSSQL.ExecuteNonQuery(ConnectionString, "Update T_UserDistillPaymentFiles set [Result] = 1 where  Lower([FileName]) = '" + CSVFile + "'") < 0)
                            {
                                log.Write("更新T_UserDistillPaymentFiles出错.");
                            }
                        }//end if ((curResultFileDetailRow[1].ToString() == "已处理") && (curResultFileDetailRow[8].ToString() == "处理完成"))
                        else if (curResultFileDetailRow[1].ToString() == "处理中")
                        {
                            continue;
                        }
                        else
                        {
                            //文件格式错误的处理
                            log.Write("文件格式错误的处理,未知处理结果状态.");
                        }
                    }//foreach (DataRow curResultFileDetailRow in dtResultFileDetail.Rows)
                    #endregion 循环处理返回结果中的文件列表的文件,以更新提款状态
                }
                catch (Exception ex)
                {
                    log.Write("未知异常3:" + ex.Message);
                }
            } //end foreach (DataRow dr in dt.Rows)
        }
    protected void OnlinePaymentAlipayToBank(int Count, double SumMoney, string Body, string UserDistillIDs)
    {
        SystemOptions options = new SystemOptions();

        this.partner          = options["OnlinePay_Alipay_ForUserDistill_UserNumber"].ToString("");
        this.key              = options["OnlinePay_Alipay_ForUserDistill_MD5Key"].ToString("");
        this.gateway          = options["MemberSharing_Alipay_Gateway"].ToString("");
        this.service          = "bptb_pay_file";
        this.sign_type        = "MD5";
        this._input_charset   = "GB2312";
        this.file_digest_type = "MD5";
        this.biz_type         = "d_sale";
        this.agentID          = options["OnlinePay_Alipay_ForUserDistill_UserNumber"].ToString("");
        Shove.Alipay.Alipay alipay = new Shove.Alipay.Alipay();
        DateTime            now    = DateTime.Now;
        string str      = "";
        string path     = "";
        string fileName = "";
        string str4     = "";
        int    index    = 0;

        if (((Body != "") && (Count > 0)) && (SumMoney > 0.0))
        {
            string str5 = now.ToString("yyyyMMdd");
            string str7 = "日期,总金额,总笔数,支付宝帐号(Email)\r\n";
            Body = (str7 + str5 + "," + SumMoney.ToString() + "," + Count.ToString() + "," + this.partner + "\r\n") + "商户流水号,收款银行户名,收款银行帐号,收款开户银行,收款银行所在省份,收款银行所在市,收款支行名称,金额,对公对私标志,备注\r\n" + Body;
            Random random = new Random();
            string str8   = (random.Next(1, 0x3e7).ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString()).PadLeft(9, '0');
            str      = "PAPX_" + str5 + "_P" + str8 + ".csv".ToLower();
            path     = base.MapPath("../App_Log/Admin/AlipayPayment/PABX/" + str).ToLower();
            fileName = "PAPX_" + str5 + "_P" + str8 + ".Zip".ToLower();
            str4     = base.MapPath("../App_Log/Admin/AlipayPayment/PABX/" + fileName).ToLower();
            if (!System.IO.File.Exists(path))
            {
                try
                {
                    if (!Shove._IO.File.WriteFile(path, Body))
                    {
                        Procedures.P_UserDistillPayByAlipayWriteLog("CSV文件写入失败:" + path);
                        JavaScript.Alert(this.Page, "CSV文件写入失败");
                        return;
                    }
                    if (System.IO.File.Exists(path))
                    {
                        if (System.IO.File.Exists(str4))
                        {
                            goto Label_0388;
                        }
                        try
                        {
                            Shove._IO.File.Compress(path, str4);
                            goto Label_0388;
                        }
                        catch
                        {
                            Procedures.P_UserDistillPayByAlipayWriteLog("文件压缩出现异常:" + path);
                            JavaScript.Alert(this.Page, "文件压缩出现异常");
                            return;
                        }
                    }
                    Procedures.P_UserDistillPayByAlipayWriteLog("CSV文件不存在(1):" + path);
                    JavaScript.Alert(this.Page, "CSV文件不存在");
                    return;
                }
                catch
                {
                    Procedures.P_UserDistillPayByAlipayWriteLog("CSV文件写入异常:" + path);
                    JavaScript.Alert(this.Page, "CSV文件写入异常");
                    return;
                }
            }
            JavaScript.Alert(this.Page, "文件写已存在");
            return;
        }
Label_0388:
        if (!System.IO.File.Exists(str4))
        {
            Procedures.P_UserDistillPayByAlipayWriteLog("ZIP文件不存在(2):" + path);
            JavaScript.Alert(this.Page, "ZIP文件不存在");
        }
        else
        {
            byte[] buffer4;
            byte[] date = System.IO.File.ReadAllBytes(str4);
            this.digest_bptb_pay_file = Shove.Alipay.Alipay.GetMD5(date, this._input_charset);
            string[]   strArray4   = alipay.GetUploadParams(this.service, this._input_charset, this.partner, this.file_digest_type, this.biz_type, this.agentID);
            FileStream stream      = new FileStream(str4, FileMode.Open, FileAccess.Read, FileShare.Read);
            string     contentType = "application/octet-stream";
            byte[]     buffer      = new byte[stream.Length];
            stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
            stream.Close();
            CreateBytes bytes      = new CreateBytes();
            ArrayList   byteArrays = new ArrayList();
            string[]    fields     = new string[strArray4.Length + 1];
            char[]      separator  = new char[] { '=' };
            index = 0;
            while (index < strArray4.Length)
            {
                string fieldName  = strArray4[index].Split(separator)[0];
                string fieldValue = strArray4[index].Split(separator)[1];
                byteArrays.Add(bytes.CreateFieldData(fieldName, fieldValue));
                fields[index] = fieldName + "=" + fieldValue;
                index++;
            }
            byteArrays.Add(bytes.CreateFieldData("digest_bptb_pay_file", this.digest_bptb_pay_file));
            fields[index] = "digest_bptb_pay_file=" + this.digest_bptb_pay_file;
            this.sign     = AlipayCommon.GetSign(fields, this.key).Trim();
            byteArrays.Add(bytes.CreateFieldData("sign", this.sign));
            byteArrays.Add(bytes.CreateFieldData("sign_type", "MD5"));
            byteArrays.Add(bytes.CreateFieldData("bptb_pay_file", str4, contentType, buffer));
            byte[] buffer3 = bytes.JoinBytes(byteArrays);
            if (!bytes.UploadData(this.gateway, buffer3, out buffer4))
            {
                Procedures.P_UserDistillPayByAlipayWriteLog("上传到指定Url失败:" + path);
                try
                {
                    System.IO.File.Delete(path);
                    System.IO.File.Delete(str4);
                }
                catch
                {
                }
                JavaScript.Alert(this.Page, "上传支付数据到指定Url失败!");
            }
            else
            {
                string str12 = Encoding.Default.GetString(buffer4);
                if (str12.IndexOf("上传成功") >= 0)
                {
                    int    returnValue       = 0;
                    string returnDescription = "";
                    if (Procedures.P_UserDistillPayByAlipay(base._User.ID, fileName, UserDistillIDs, 2, ref returnValue, ref returnDescription) < 0)
                    {
                        Procedures.P_UserDistillPayByAlipayWriteLog("提款ID:" + UserDistillIDs + "写入数据库(FileName)和更新状态失败。");
                    }
                    if (returnDescription != "")
                    {
                        Procedures.P_UserDistillPayByAlipayWriteLog("把上传的支付明细写入数据库(FileName)和更新状态出错:" + returnDescription);
                        JavaScript.Alert(this.Page, "出错:" + returnDescription);
                        this.BindData(true);
                    }
                    else
                    {
                        JavaScript.Alert(this.Page, str12 + "银行处理需要到明天才有结果,请耐心等待处理结果!");
                        this.BindData(true);
                    }
                }
                else
                {
                    Procedures.P_UserDistillPayByAlipayWriteLog("上传支付到银行明细失败:" + str12);
                    JavaScript.Alert(this.Page, str12.Replace("\n", "").Replace("\r", ""));
                }
            }
        }
    }