Beispiel #1
0
        /// <summary>
        /// 退款信息
        /// </summary>
        /// <param name="request">退款参数</param>
        /// <param name="config">配置</param>
        /// <returns>退款结果</returns>
        public static RefundPayResult RefundPay(RefundPayRequest request, Config config)
        {
            var result           = new RefundPayResult();
            var orderqueryResult = OrderQuery(new OrderQueryRequest
            {
                OutTradeNo    = request.OutTradeNo,
                TransactionId = request.TransactionId
            }, config);

            if (orderqueryResult.TradeState == "SUCCESS" || orderqueryResult.TradeState == "REFUND")
            {
                if (request.RefundFee > orderqueryResult.TotalFee)
                {
                    result.IsSuccess = false;
                    result.Message   = "退款金额大于总订单金额";
                    return(result);
                }
                else
                {
                    // 转入退款的订单先去查询退款金额够不够
                    if (orderqueryResult.TradeState == "REFUND")
                    {
                        var refundQueryResult = RefundPayQuery(new RefundQueryRequest
                        {
                            OutTradeNo = request.OutTradeNo
                        }, config);
                        if (refundQueryResult.IsSuccess)
                        {
                            // 已退款金额
                            var oldRefundAmount = refundQueryResult.Details.Sum(item => item.RefundFee);
                            if (request.RefundFee > orderqueryResult.TotalFee - oldRefundAmount)
                            {
                                result.IsSuccess = false;
                                result.Message   = "退款金额大于总订单金额";
                                return(result);
                            }

                            var refundOrder = refundQueryResult.Details.FirstOrDefault(item => item.OutRefundNo == request.OutRefundNo);
                            if (refundOrder != null)
                            {
                                if (refundOrder.RefundStatus == "SUCCESS" || refundOrder.RefundStatus == "PROCESSING")
                                {
                                    result.IsSuccess     = true;
                                    result.TransactionId = orderqueryResult.TransactionId;
                                    result.OutTradeNo    = request.OutTradeNo;
                                    result.OutRefundNo   = request.OutRefundNo;
                                    result.RefundId      = refundOrder.RefundId;
                                    result.RefundFee     = refundOrder.RefundFee;
                                    return(result);
                                }
                            }
                        }
                        else
                        {
                            result.Message   = "退款查询失败";
                            result.IsSuccess = false;
                            return(result);
                        }
                    }
                    request.TotalFee = orderqueryResult.TotalFee;
                    var requestStr = TenpayUtil.GetRefundPayRequestXml(request, config);
                    var response   = HttpHelp.PostXmlToUrl(Config.RefundPayUrl, requestStr, config.GetRefundCert(), config.GetPid());
                    result = TenpayUtil.ConvertToRefundPayResult(response);
                }
            }
            else
            {
                result.IsSuccess = false;
                result.Message   = orderqueryResult.Message;
            }
            return(result);
        }