/// <summary> /// 超出支付时限,订单取消后,才支付成功引起的退款 /// </summary> //internal static void TradeRefund(Order.Domain.Order order, string tradeNo) { // // 构建退款请求信息 // var tradeRefundView = new TradeRefundDTO() { // TradeNo = tradeNo, // RefundBatchNo = order.Id.ToString(), // TradeRefundType = RefundType.TradeRefund, // RefundAmount = Math.Abs(order.Bill.PayBill.Purchaser.Amount), // RefundReason = RefundBusinessType.PayTimeout.GetDescription() // }; // RefundResult result = null; // var failedMessage = string.Empty; // //RefundBatch refundBatch = null; // try { // // 调用退款接口 // //refundBatch = PoolPay.Service.AccountTradeService.TradeRefund(tradeRefundView); // //result = getRefundResult(refundBatch.TradeRefund, false); // var refundRequestProcess = new RefundRequestProcess(tradeRefundView.TradeNo, tradeRefundView.RefundBatchNo, tradeRefundView.RefundReason, Math.Abs(order.Bill.PayBill.Purchaser.Amount).ToString()); // if (refundRequestProcess.Execute()) // { // result = getRefundResult(RefundRequestProcess.ParseRefundInfo(refundRequestProcess.outPayer)); // } // else // { // failedMessage = refundRequestProcess.Message; // } // } catch(Exception ex) { // LogService.SaveExceptionLog(ex, "提交交易退款"); // failedMessage = ex.Message; // } // // 记录交互日志 // var refundRequest = string.Format("支付交易流水号:{0} 退款批次号:{1} 退款类型:{2} 退款金额:{3} 退款原因{4}", // tradeRefundView.TradeNo, tradeRefundView.RefundBatchNo, // tradeRefundView.TradeRefundType, tradeRefundView.RefundAmount, // tradeRefundView.RefundReason); // Service.LogService.SaveTradementLog(new TradementLog { // OrderId = order.Id, // Type = TradementBusinessType.Refund, // Request = refundRequest, // Response = result == null ? failedMessage : getRefundResponse(result), // Time = DateTime.Now, // Remark = tradeRefundView.RefundReason // }); // // 如果退款失败,记录退款失败信息 // var repository = Order.Repository.Factory.CreateRefundRepository(); // if(result != null && result.Success) { // repository.Delete(order.Id); // } else { // repository.Save(new RefundFailedRecord() { // OrderId = order.Id, // ApplyformId = order.Id, // BusinessType = RefundBusinessType.PayTimeout, // PayTradeNo = tradeNo, // RefundFailedInfo = failedMessage, // RefundTime = DateTime.Now // }); // } //} /// <summary> /// 账单退款 /// </summary> internal static List <RefundResult> Refund(Distribution.Domain.Bill.Refund.NormalRefundBill refundBill, RefundBusinessType refundBusinessType) { var result = new List <RefundResult>(); // 构建退款请求信息 var tradeRefundView = new TradeRefundDTO { TradeNo = refundBill.Tradement.Payment.TradeNo, RefundBatchNo = refundBill.Tradement.TradeNo, RoyaltyRefunds = getRefundRoyaltyViews(refundBill), RefundReason = refundBusinessType.GetDescription() }; if (refundBill.Purchaser.Success) { tradeRefundView.TradeRefundType = RefundType.RoyaltyRefund; } else { tradeRefundView.RefundAmount = refundBill.Purchaser.Amount; tradeRefundView.TradeRefundType = RefundType.TradeRoayltyRefund; if (refundBill.PayBill.RequireSubPay && refundBill.Platform.TotalAmount > 0) { tradeRefundView.TradeSupplRefund = getTradeRoyaltyView(refundBill.Platform.Account, refundBill.Platform.TotalAmount, "退还贴点票款"); var payAmount = Math.Abs(refundBill.PayBill.Platform.TotalAmount); var refundedAmount = Math.Abs(refundBill.PayBill.RefundBills.Where(rb => rb.Platform.Success).Sum(rb => rb.Platform.TotalAmount)); var refundableAmount = payAmount - refundedAmount; var requireRefundAmount = tradeRefundView.TradeSupplRefund.RoyaltyAmount; tradeRefundView.TradeSupplRefund.RoyaltyAmount = refundableAmount >= requireRefundAmount ? requireRefundAmount : refundableAmount; } } RefundBatch refundBatch = null; var failedMessage = string.Empty; try { // 调用退款接口 RefundRequestProcess refundRequestProcess = new RefundRequestProcess(tradeRefundView.TradeNo, tradeRefundView.RefundBatchNo, tradeRefundView.RefundReason, refundBill.Purchaser.Success?string.Empty:refundBill.Purchaser.Amount.ToString(), tradeRefundView.TradeSupplRefund != null ? string.Format("{0}|{1}|{2}|{3}", tradeRefundView.TradeSupplRefund.RoyaltyAccountNo, tradeRefundView.TradeSupplRefund.RoyaltyAmount, tradeRefundView.TradeSupplRefund.Note, tradeRefundView.TradeSupplRefund.ExtendParameter) : string.Empty, string.Join("^", tradeRefundView.RoyaltyRefunds.Select(r => string.Format("{0}|{1}|{2}|{3}", r.RoyaltyAccountNo, r.RoyaltyAmount, r.Note, r.ExtendParameter)))); if (refundRequestProcess.Execute()) { var tradeRefund = RefundRequestProcess.ParseRefundInfo(refundRequestProcess.outPayer); if (tradeRefund != null) { result.Add(getRefundResult(tradeRefund)); } var tradeSubRefund = RefundRequestProcess.ParseRefundInfo(refundRequestProcess.outSubPayment); if (tradeSubRefund != null) { result.Add(getRefundResult(tradeSubRefund)); } result.AddRange(refundRequestProcess.outRoyalties.Split('^').Select(item => getRefundResult(RefundRequestProcess.ParseRefundInfo(item)))); } else { failedMessage = refundRequestProcess.Message; } } catch (Exception ex) { LogService.SaveExceptionLog(ex, "提交订单退款"); failedMessage = ex.Message; } // 记录交互日志 var refundRequest = string.Format("支付交易流水号:{0} 退款批次号:{1} 退款类型:{2} (买家)退款金额:{3} 退款原因:{4}", tradeRefundView.TradeNo, tradeRefundView.RefundBatchNo, tradeRefundView.TradeRefundType, tradeRefundView.RefundAmount, tradeRefundView.RefundReason); refundRequest += tradeRefundView.RoyaltyRefunds.Join(" ", item => string.Format("退款账号:{0} 退款金额:{1} 备注:{2} 扩展参数:{3}", item.RoyaltyAccountNo, item.RoyaltyAmount, item.Note, item.ExtendParameter)); if (tradeRefundView.TradeSupplRefund != null) { refundRequest += string.Format("退款账号:{0} 退款金额:{1} 备注:{2} 扩展参数:{3}", tradeRefundView.TradeSupplRefund.RoyaltyAccountNo, tradeRefundView.TradeSupplRefund.RoyaltyAmount, tradeRefundView.TradeSupplRefund.Note, tradeRefundView.TradeSupplRefund.ExtendParameter); } LogService.SaveTradementLog(new TradementLog { OrderId = refundBill.OrderId, ApplyformId = refundBill.ApplyformId, Type = TradementBusinessType.Refund, Request = refundRequest, Response = string.IsNullOrEmpty(failedMessage)?"处理成功":failedMessage, Time = DateTime.Now, Remark = refundBusinessType.GetDescription() }); // 如果退款失败,记录退款失败信息 var repository = Order.Repository.Factory.CreateRefundRepository(); if (result.Any() && result.All(item => item.Success)) { repository.Delete(refundBill.ApplyformId); } else { repository.Save(new RefundFailedRecord { OrderId = refundBill.OrderId, ApplyformId = refundBill.ApplyformId, BusinessType = refundBusinessType, PayTradeNo = tradeRefundView.TradeNo, RefundFailedInfo = refundBatch == null ? failedMessage : (getRefundFailedInfo(refundBatch.TradeRefund) + refundBatch.RoyaltyRefunds.Join(" ", getRefundFailedInfo)), RefundTime = DateTime.Now }); } return(result); }
/// <summary> /// 改期退款 /// </summary> /// <param name="orderId">原订单号</param> /// <param name="refundBill">退款账单</param> internal static RefundResult Refund(decimal orderId, Distribution.Domain.Bill.Refund.PostponeRefundBill refundBill) { // 构建退款请求信息 var tradeRefundView = new TradeRefundDTO() { TradeNo = refundBill.Tradement.Payment.TradeNo, RefundBatchNo = refundBill.Tradement.TradeNo, TradeRefundType = RefundType.TradeRefund, RefundAmount = refundBill.Applier.Amount, RefundReason = RefundBusinessType.DenyPostpone.GetDescription() }; // 调用退款接口 #if (DEBUG) var refundBatch = ChinaPay.PoolPay.Service.AccountTradeService.TradeRefund(tradeRefundView); var result = new RefundResult() { Success = refundBatch.TradeRefund.Status == RefundStatus.RefundSuccess, Account = refundBatch.TradeRefund.IncomeAccount.AccountNo, RefundTime = refundBatch.TradeRefund.RefundDate }; #else RefundRequestProcess refundRequestProcess = new RefundRequestProcess(refundBill.Tradement.Payment.TradeNo, refundBill.Tradement.TradeNo, RefundBusinessType.DenyPostpone.GetDescription(), refundBill.Applier.Amount.ToString()); RefundResult result = new RefundResult(); if (refundRequestProcess.Execute()) { var refundInfo = RefundRequestProcess.ParseRefundInfo(refundRequestProcess.outPayer); result = new RefundResult() { Success = refundInfo.RefundStatus == Common.Enums.RefundStatus.RefundSuccess, Account = refundInfo.Account, RefundTime = refundInfo.RefundTime }; } #endif // 记录交互日志 var refundRequest = string.Format("支付交易流水号:{0} 退款批次号:{1} 退款类型:{2} 退款金额:{3} 退款原因:{4}", tradeRefundView.TradeNo, tradeRefundView.RefundBatchNo, tradeRefundView.TradeRefundType, tradeRefundView.RefundAmount, tradeRefundView.RefundReason); Service.LogService.SaveTradementLog(new TradementLog { OrderId = orderId, ApplyformId = refundBill.ApplyformId, Type = TradementBusinessType.Refund, Request = refundRequest, Response = getRefundResponse(result), Time = DateTime.Now, Remark = RefundBusinessType.DenyPostpone.GetDescription() }); // 如果退款失败,记录退款失败信息 var repository = Order.Repository.Factory.CreateRefundRepository(); if (result.Success) { repository.Delete(refundBill.ApplyformId); } else { repository.Save(new RefundFailedRecord() { OrderId = orderId, ApplyformId = refundBill.ApplyformId, BusinessType = RefundBusinessType.DenyPostpone, PayTradeNo = tradeRefundView.TradeNo, RefundFailedInfo = getRefundResponse(result), RefundTime = DateTime.Now }); } return(result); }
/// <summary> /// 交易退款 /// </summary> /// <param name="refundBill">退款账单</param> /// <param name="refundBusinessType">退款类型</param> internal static RefundResult TradeRefund(Distribution.Domain.Bill.Refund.NormalRefundBill refundBill, RefundBusinessType refundBusinessType) { // 构建退款请求信息 var tradeRefundView = new TradeRefundDTO() { TradeNo = refundBill.Tradement.Payment.TradeNo, RefundBatchNo = refundBill.Tradement.TradeNo, TradeRefundType = RefundType.TradeRefund, RefundAmount = refundBill.Purchaser.Amount, RefundReason = refundBusinessType.GetDescription() }; RefundResult result = null; var failedMessage = string.Empty; //RefundBatch refundBatch = null; try { // 调用退款接口 //refundBatch = PoolPay.Service.AccountTradeService.TradeRefund(tradeRefundView); //result = getRefundResult(refundBatch.TradeRefund, false); var refundRequestProcess = new RefundRequestProcess(tradeRefundView.TradeNo, tradeRefundView.RefundBatchNo, tradeRefundView.RefundReason, refundBill.Purchaser.Amount.ToString()); if (refundRequestProcess.Execute()) { result = getRefundResult(RefundRequestProcess.ParseRefundInfo(refundRequestProcess.outPayer)); } else { failedMessage = refundRequestProcess.Message; } } catch (Exception ex) { LogService.SaveExceptionLog(ex, "提交交易退款"); failedMessage = ex.Message; } // 记录交互日志 var refundRequest = string.Format("支付交易流水号:{0} 退款批次号:{1} 退款类型:{2} 退款金额:{3} 退款原因{4}", tradeRefundView.TradeNo, tradeRefundView.RefundBatchNo, tradeRefundView.TradeRefundType, tradeRefundView.RefundAmount, tradeRefundView.RefundReason); Service.LogService.SaveTradementLog(new TradementLog { OrderId = refundBill.OrderId, ApplyformId = refundBill.ApplyformId, Type = TradementBusinessType.Refund, Request = refundRequest, Response = result == null ? failedMessage : getRefundResponse(result), Time = DateTime.Now, Remark = refundBusinessType.GetDescription() }); // 如果退款失败,记录退款失败信息 var repository = Order.Repository.Factory.CreateRefundRepository(); if (result != null && result.Success) { repository.Delete(refundBill.ApplyformId); } else { repository.Save(new RefundFailedRecord() { OrderId = refundBill.OrderId, ApplyformId = refundBill.ApplyformId, BusinessType = refundBusinessType, PayTradeNo = tradeRefundView.TradeNo, RefundFailedInfo = failedMessage, RefundTime = DateTime.Now }); } return(result); }