示例#1
0
        public virtual async Task HandleAsync(WeChatPayHandlerContext context)
        {
            var dict = context.WeChatRequestXmlData.SelectSingleNode("xml").ToDictionary() ?? throw new NullReferenceException();

            if (dict.GetOrDefault("return_code") != "SUCCESS")
            {
                return;
            }

            using var disabledDataFilter = _dataFilter.Disable <IMultiTenant>();

            var record = await _refundRecordRepository.FindAsync(x => x.Id == Guid.Parse(dict.GetOrDefault("out_refund_no")));

            if (record == null)
            {
                return;
            }

            var payment = await _paymentRepository.FindAsync(record.PaymentId);

            var refund = await _refundRepository.FindByPaymentIdAsync(record.PaymentId);

            if (payment == null || refund == null)
            {
                context.IsSuccess = false;

                return;
            }

            record.SetInformationInNotify(
                refundStatus: dict.GetOrDefault("refund_status"),
                successTime: dict.GetOrDefault("success_time"),
                refundRecvAccout: dict.GetOrDefault("refund_recv_accout"),
                refundAccount: dict.GetOrDefault("refund_account"),
                refundRequestSource: dict.GetOrDefault("refund_request_source"),
                settlementRefundFee: Convert.ToInt32(dict.GetOrDefault("settlement_refund_fee")));

            await _refundRecordRepository.UpdateAsync(record, true);

            if (dict.GetOrDefault("refund_status") == "SUCCESS")
            {
                await _paymentManager.CompleteRefundAsync(payment, refund);
            }
            else
            {
                await _paymentManager.RollbackRefundAsync(payment, refund);
            }

            context.IsSuccess = true;
        }
        public async Task <PaymentDto> RefundRollbackAsync(Guid id)
        {
            var payment = await _repository.GetAsync(id);

            if (payment.PendingRefundAmount <= decimal.Zero)
            {
                throw new PaymentIsInUnexpectedStageException(payment.Id);
            }

            var refund = await _refundRepository.FindByPaymentIdAsync(payment.Id);

            await _paymentManager.RollbackRefundAsync(payment, refund);

            return(await MapToGetOutputDtoAsync(payment));
        }
        public virtual async Task StartRefundAsync(Payment payment, CreateRefundInput input)
        {
            var provider = GetProvider(payment);

            input.RefundItems.ForEach(x => x.RefundAmount.EnsureIsNonNegative());

            var refundAmount = input.RefundItems.Sum(x => x.RefundAmount);

            refundAmount.EnsureIsNonNegative();

            var paymentItemIds = input.RefundItems.Select(x => x.PaymentItemId).ToList();

            var exceptItemIds = paymentItemIds.Except(payment.PaymentItems.Select(x => x.Id)).ToList();

            if (exceptItemIds.Any())
            {
                throw new EntityNotFoundException(typeof(PaymentItem), exceptItemIds);
            }

            if (paymentItemIds.Count != paymentItemIds.Distinct().Count())
            {
                throw new DuplicatePaymentItemIdException();
            }

            if (await _refundRepository.FindByPaymentIdAsync(payment.Id) != null)
            {
                throw new AnotherRefundIsInProgressException(payment.Id);
            }

            var refund = CreateRefund(payment, input);

            await _refundRepository.InsertAsync(refund, true);

            payment.StartRefund(refund);

            await _paymentRepository.UpdateAsync(payment, true);

            await provider.OnRefundStartedAsync(payment, refund);
        }