protected virtual void OnOrderImportComplate(object sender, OrderCreateResponseResult e) { var handler = m_OrderImportComplateEvent; if (handler != null) { handler(sender, e); } }
void processor_OrderImportComplateEvent(object sender, OrderCreateResponseResult result) { this.Invoke(new Action <OrderCreateResponseResult>(e => { progressBar1.Value += 1; string message = string.Format("订单{0}创建失败:{1}", e.Data.MerchantOrderID, e.Desc); if (e.Code == 0) { ResultData.Add(result.Data); message = string.Format("订单{0}创建成功:{1}", e.Data.MerchantOrderID, JsonSerializer.Serialize(e.Data)); } listBoxLog.Items.Add(new ListItem(message)); if (progressBar1.Value == progressBar1.Maximum) { btnImportFile.Enabled = true; this.btnExportFile_Click(this, null); } }), result); }
private void ImportOrder(SOCreateRequestInfo contractInfo, ConfigData configData) { OrderCreateResponseResult result = new OrderCreateResponseResult() { Data = new OrderCreateResponseData() { MerchantOrderID = contractInfo.MerchantOrderID } }; var parameters = new NameValueCollection(); parameters.Add("method", "Order.SOCreate"); parameters.Add("data", JsonSerializer.Serialize(contractInfo)); parameters.Add("format", "json"); parameters.Add("version", "1.0"); parameters.Add("nonce", new Random().NextDouble().ToString()); parameters.Add("appid", configData.AppId); parameters.Add("timestamp", DateTime.Now.ToString("yyyyMMddhhmmss")); string sign = SignatureUtil.Build(parameters, configData.SecretKey); StringBuilder httpContentStringBuilder = new StringBuilder(); foreach (string key in parameters.AllKeys) { httpContentStringBuilder.AppendFormat("&{0}={1}", key, SignatureUtil.UrlEncode(parameters[key])); } httpContentStringBuilder.AppendFormat("&{0}={1}", "sign", sign); var content = httpContentStringBuilder.ToString().TrimStart('&'); WebRequest request = WebRequest.Create(configData.OpenApiUrl); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; using (Stream requestStream = request.GetRequestStream()) { byte[] postData = Encoding.UTF8.GetBytes(content); requestStream.Write(postData, 0, postData.Length); } try { string responseData = ""; WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { responseData = reader.ReadToEnd(); } } result = JsonSerializer.Deserialize <OrderCreateResponseResult>(responseData); if (result.Data == null) { result.Data = new OrderCreateResponseData() { MerchantOrderID = contractInfo.MerchantOrderID }; } } catch (AggregateException aex) { result.Code = -10; var egex = (AggregateException)aex; Exception innerExp = null; if (egex.InnerExceptions != null && egex.InnerExceptions.Count > 0) { innerExp = egex.InnerExceptions[0]; } if (innerExp == null) { result.Desc = egex.Message; } else { Exception tempExp; GetInnerException(innerExp, out tempExp); if (tempExp != null) { result.Desc = tempExp.Message; } else { result.Desc = innerExp.Message; } } } catch (Exception ex) { result.Code = -10; result.Desc = ex.Message; } OnOrderImportComplate(this, result); }