private void cancelThreadFunc(object o) { int RETRYCOUNT = 10; int INTERVAL = 10000; for (int i = 0; i < RETRYCOUNT; ++i) { Thread.Sleep(INTERVAL); AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); string outTradeNo = o.ToString(); AlipayTradeCancelResponse cancelResponse = tradeCancel(outTradeNo); if (null != cancelResponse) { if (cancelResponse.Code == ResultCode.FAIL) { if (cancelResponse.RetryFlag == "N") { break; } } if ((cancelResponse.Code == ResultCode.SUCCESS)) { break; } } if (i == RETRYCOUNT - 1) { /** !!!!!!!注意!!!!!!!! * 处理到最后一次,还是未撤销成功,需要在商户数据库中对此单最标记,人工介入处理*/ } } }
/// <summary> /// 取消订单接口 /// </summary> /// <param name="biz_content"></param> /// <returns></returns> private AlipayTradeCancelResponse Cancel(string biz_content) { LogHelper.AlipayLog(string.Format("正在取消订单")); AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = biz_content; AlipayTradeCancelResponse cancelResponse = client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == ResultCode.FAIL && cancelResponse.RetryFlag == "Y") { //if (cancelResponse.Body.Contains("\"retry_flag\":\"Y\"")) //cancelOrderRetry(biz_content); // 新开一个线程重试撤销 ParameterizedThreadStart ParStart = new ParameterizedThreadStart(cancelOrderRetry); Thread myThread = new Thread(ParStart); object o = biz_content; myThread.Start(o); LogHelper.AlipayLog(string.Format("取消-结果:{0}-{1}", cancelResponse.Code, cancelResponse.Body)); } if (cancelResponse.Code == ResultCode.SUCCESS) { //lblMessage.Text = cancelResponse.Body; LogHelper.AlipayLog(string.Format("取消-结果:{0}", cancelResponse.Code)); } } return(cancelResponse); }
public AlipayTradeCancelResponse Cancel(string OutTradeNo) { List <StrPair> list = new List <StrPair>() { new StrPair("out_trade_no", OutTradeNo) }; string content = List2Json(list); AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = content; AlipayTradeCancelResponse cancelResponse = m_Client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == ResultCode.FAIL && cancelResponse.RetryFlag == "Y") { //if (cancelResponse.Body.Contains("\"retry_flag\":\"Y\"")) //cancelOrderRetry(biz_content); // 新开一个线程重试撤销 ParameterizedThreadStart ParStart = new ParameterizedThreadStart(cancelOrderRetry); Thread myThread = new Thread(ParStart); object o = content; myThread.Start(o); } } return(cancelResponse); }
/// <summary> /// 交易撤销 /// </summary> public bool Cancel(string orderNo, string tradeNo = null) { bool success = false; var model = new AlipayTradeCancelModel() { OutTradeNo = orderNo, TradeNo = tradeNo }; var request = new AlipayTradeCancelRequest(); request.SetBizModel(model); AlipayTradeCancelResponse response = aopClient.Execute(request); if (response != null && response.Code == "10000") { success = true; //response.RetryFlag 是否需要重试,Y / N //response.Action 本次撤销触发的交易动作 close:关闭交易,无退款; refund:产生了退款 } return(success); }
public void cancelOrderRetry(object o) { int retryCount = 10; for (int i = 0; i < retryCount; ++i) { Thread.Sleep(5000); AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = o.ToString(); Dictionary <string, string> paramsDict = (Dictionary <string, string>)cancelRequest.GetParameters(); AlipayTradeCancelResponse cancelResponse = m_Client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == ResultCode.FAIL) { //if (cancelResponse.Body.Contains("\"retry_flag\":\"N\"")) if (cancelResponse.RetryFlag == "N") // 再重試也不會成功 { break; } } if ((cancelResponse.Code == ResultCode.SUCCESS)) { break; } } if (i == retryCount - 1) { // 处理到最后一次,还是未撤销成功,需要在商户数据库中对此单最标记,人工介入处理 } } }
private AlipayTradeCancelResponse tradeCancel(string outTradeNo) { try { AlipayTradeCancelRequest request = new AlipayTradeCancelRequest(); StringBuilder sb2 = new StringBuilder(); sb2.Append("{\"out_trade_no\":\"" + outTradeNo + "\"}"); request.BizContent = sb2.ToString(); AlipayTradeCancelResponse response = client.Execute(request); return(response); } catch { return(null); } }
private AlipayTradeCancelResponse Cancel(string biz_content) { AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = biz_content; AlipayTradeCancelResponse cancelResponse = client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == qrcode._2.ResultCode.FAIL && cancelResponse.RetryFlag == "Y") { for (int i = 0; i < retryCount; ++i) { Thread.Sleep(3000); cancelResponse = client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == qrcode._2.ResultCode.FAIL) { //if (cancelResponse.Body.Contains("\"retry_flag\":\"N\"")) if (cancelResponse.RetryFlag == "N") { break; } } if ((cancelResponse.Code == qrcode._2.ResultCode.SUCCESS)) { break; } } if (i == retryCount - 1) { throw new Exception("撤销订单失败!"); } } } } return(cancelResponse); }
/// <summary> /// 订单撤销 /// </summary> /// <param name="queryRequset">请求内容</param> /// <returns></returns> public static CancelResponseModel Cancel(CancelModel requestModel) { AlipayTradeCancelRequest cancelRequst = new AlipayTradeCancelRequest(); cancelRequst.BizContent = JsonConvert.SerializeObject(requestModel); LogUtil.WriteAlipayLog("订单撤销请求", "请求参数", cancelRequst.BizContent); Dictionary <string, string> paramsDict = (Dictionary <string, string>)cancelRequst.GetParameters(); AlipayTradeCancelResponse cancelResponse = _client.Execute(cancelRequst); LogUtil.WriteAlipayLog("撤销订单响应", "响应参数", cancelResponse.Body); return(new CancelResponseModel { msg = cancelResponse.Msg, code = cancelResponse.Code, subcode = cancelResponse.SubCode, submsg = cancelResponse.SubMsg, action = cancelResponse.Action, out_trade_no = cancelResponse.OutTradeNo, retry_flag = cancelResponse.RetryFlag, trade_no = cancelResponse.TradeNo }); }
private AlipayTradeCancelResponse Cancel(string biz_content) { AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = biz_content; AlipayTradeCancelResponse cancelResponse = client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == qrcode._2.ResultCode.FAIL && cancelResponse.RetryFlag == "Y") { //if (cancelResponse.Body.Contains("\"retry_flag\":\"Y\"")) //cancelOrderRetry(biz_content); // 新开一个线程重试撤销 ParameterizedThreadStart ParStart = new ParameterizedThreadStart(cancelOrderRetry); Thread myThread = new Thread(ParStart); object o = biz_content; myThread.Start(o); } } return(cancelResponse); }
/// <summary> /// 取消订单方法 /// </summary> /// <param name="o"></param> public void cancelOrderRetry(object o) { int retryCount = 50; for (int i = 0; i < retryCount; ++i) { Thread.Sleep(10000); AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = o.ToString(); // Dictionary<string, string> paramsDict = (Dictionary<string, string>)cancelRequest.GetParameters(); AlipayTradeCancelResponse cancelResponse = client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == ResultCode.FAIL) { LogHelper.AlipayLog(string.Format("取消-结果:{0},请求内容:{1}", cancelResponse.Code, o)); if (cancelResponse.RetryFlag == "N") { break; } } if ((cancelResponse.Code == ResultCode.SUCCESS)) { LogHelper.AlipayLog(string.Format("取消-结果:{0}", cancelResponse.Code)); break; } } if (i == retryCount - 1) { // 处理到最后一次,还是未撤销成功,需要在商户数据库中对此单最标记,人工介入处理 //lblMessage.Text = cancelResponse.Body; LogHelper.AlipayLog(string.Format("处理到最后一次,还是未撤销成功,需要在商户数据库中对此单最标记,人工介入处理,请求内容:{0}", o)); } } }
/// <summary> /// 统一收单交易撤销接口 /// 支付交易返回失败或支付系统超时,调用该接口撤销交易。如果此订单用户支付失败,支付宝系统会将此订单关闭;如果用户支付成功,支付宝系统会将此订单资金退还给用户 /// </summary> /// <param name="out_trade_no">支付宝订单号</param> /// <param name="trade_no">平台编号</param> /// <returns></returns> public static bool CancelOrder(string companyId, string out_trade_no, string trade_no) { try { AlipayTradeCancelRequest request = new AlipayTradeCancelRequest(); request.BizContent = "{" + "\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"" + "}"; AlipayTradeCancelResponse response = GetDefaultAopClient(companyId).Execute(request); if (response.IsError) { TxtLogServices.WriteTxtLogEx("AliPayApiServices", string.Format("取消订单失败:{0}", response.Body)); return(false); } TxtLogServices.WriteTxtLogEx("AliPayApiServices", string.Format("取消订单结果:{0}", response.Body)); return(response.Code == "10000"); } catch (Exception ex) { TxtLogServices.WriteTxtLogEx("AliPayApiServices", string.Format("取消订单失败:{0}", ex.Message)); return(false); } }
/// <summary> /// 撤销 /// </summary> /// <param name="cancelOrder">撤销订单</param> /// <param name="cancelOrderBack">撤销订单返回实体</param> /// <param name="message">错误消息</param> /// <returns></returns> internal bool Cancel(CancelOrder cancelOrder, out CancelOrderBack cancelOrderBack, out string message) { _log.Info("撤销 CancelOrder 开始"); var cancelRequst = new AlipayTradeCancelRequest(); cancelRequst.BizContent = Newtonsoft.Json.JsonConvert.SerializeObject(cancelOrder, new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore }).ToLower(); var cancelResponse = client.Execute(cancelRequst); if (cancelResponse.Code == "10000") { _log.Info("撤销成功!"); cancelOrderBack = new CancelOrderBack() { Action = cancelResponse.Action, Code = cancelResponse.Code, Msg = cancelResponse.Msg, Out_Trade_No = cancelResponse.OutTradeNo, Retry_Flag = cancelResponse.RetryFlag, Sub_Code = cancelResponse.SubCode, Sub_Msg = cancelResponse.SubMsg, Trade_No = cancelResponse.TradeNo }; message = ""; return(true); } else { cancelOrderBack = null; message = $"{ cancelResponse.Msg},{cancelResponse.SubMsg}"; _log.Error($"撤销失败:{message}"); return(false); } }