public override async Task OnRefundStartedAsync(Payment payment, Refund refund)
        {
            if (!Guid.TryParse(payment.GetProperty <string>("AccountId"), out var accountId))
            {
                throw new ArgumentNullException("AccountId");
            }

            var account = await _accountRepository.GetAsync(accountId);

            var configuration = _accountGroupConfigurationProvider.Get(account.AccountGroupName);

            var accountChangedBalance = payment.ActualPaymentAmount;

            var transaction = new Transaction(_guidGenerator.Create(), _currentTenant.Id, account.Id, account.UserId,
                                              payment.Id, TransactionType.Debit, PrepaymentConsts.RefundActionName, payment.PaymentMethod,
                                              payment.ExternalTradingCode, configuration.Currency, accountChangedBalance, account.Balance);

            await _transactionRepository.InsertAsync(transaction, true);

            account.ChangeBalance(accountChangedBalance);

            await _accountRepository.UpdateAsync(account, true);

            await _paymentManager.CompleteRefundAsync(payment, refund);
        }
Ejemplo n.º 2
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;
        }