Exemple #1
0
        private async Task UpdateStatusAsync(IPaymentRequest paymentRequest, PaymentRequestStatusInfo statusInfo = null)
        {
            PaymentRequestStatusInfo newStatusInfo =
                statusInfo ?? await _paymentRequestStatusResolver.GetStatus(paymentRequest.WalletAddress);

            PaymentRequestStatus          previousStatus          = paymentRequest.Status;
            PaymentRequestProcessingError previousProcessingError = paymentRequest.ProcessingError;

            paymentRequest.Status   = newStatusInfo.Status;
            paymentRequest.PaidDate = newStatusInfo.Date;

            if (newStatusInfo.Amount.HasValue)
            {
                paymentRequest.PaidAmount = newStatusInfo.Amount.Value;
            }

            paymentRequest.ProcessingError = (paymentRequest.Status == PaymentRequestStatus.Error || paymentRequest.Status == PaymentRequestStatus.SettlementError)
                ? newStatusInfo.ProcessingError
                : PaymentRequestProcessingError.None;

            await _paymentRequestRepository.UpdateAsync(paymentRequest);

            // if we are updating status from "InProcess" to any other - we have to release the lock
            if (previousStatus == PaymentRequestStatus.InProcess)
            {
                await _paymentLocksService.ReleaseLockAsync(paymentRequest.Id, paymentRequest.MerchantId);
            }

            PaymentRequestRefund refundInfo = await GetRefundInfoAsync(paymentRequest.WalletAddress);

            if (paymentRequest.Status != previousStatus ||
                (paymentRequest.Status == PaymentRequestStatus.Error &&
                 paymentRequest.ProcessingError != previousProcessingError))
            {
                await _paymentRequestPublisher.PublishAsync(paymentRequest, refundInfo);

                IAssetGeneralSettings assetSettings =
                    await _assetSettingsService.GetGeneralAsync(paymentRequest.PaymentAssetId);

                // doing auto settlement only once
                // Some flows assume we can get updates from blockchain multiple times for the same transaction
                // which leads to the same payment request status
                if (paymentRequest.StatusValidForSettlement() && (assetSettings?.AutoSettle ?? false))
                {
                    if (paymentRequest.Status != PaymentRequestStatus.Confirmed &&
                        !_autoSettleSettingsResolver.AllowToMakePartialAutoSettle(paymentRequest.PaymentAssetId))
                    {
                        return;
                    }

                    await SettleAsync(paymentRequest.MerchantId, paymentRequest.Id);
                }
            }
        }
Exemple #2
0
        public async Task <SettlementResult> SettleAsync(string merchantId, string paymentRequestId)
        {
            IPaymentRequest paymentRequest = await _paymentRequestRepository.GetAsync(merchantId, paymentRequestId);

            if (paymentRequest == null)
            {
                throw new PaymentRequestNotFoundException(merchantId, paymentRequestId);
            }

            if (!paymentRequest.StatusValidForSettlement())
            {
                throw new SettlementValidationException("Invalid status");
            }

            string sourceWalletAddress = await _walletsManager.ResolveBlockchainAddressAsync(
                paymentRequest.WalletAddress,
                paymentRequest.PaymentAssetId);

            string destWalletAddress;

            // check asset to settle to merchant wallet
            if (_autoSettleSettingsResolver.AllowToSettleToMerchantWallet(paymentRequest.PaymentAssetId))
            {
                destWalletAddress = (await _merchantWalletService.GetDefaultAsync(
                                         paymentRequest.MerchantId,
                                         paymentRequest.PaymentAssetId,
                                         PaymentDirection.Incoming))?.WalletAddress;
            }
            else
            {
                BlockchainType network = await _assetSettingsService.GetNetworkAsync(paymentRequest.PaymentAssetId);

                destWalletAddress = _autoSettleSettingsResolver.GetAutoSettleWallet(network);
            }

            if (string.IsNullOrEmpty(destWalletAddress))
            {
                throw new SettlementValidationException(
                          $"Destination wallet address is empty. Details: {new {paymentRequest.MerchantId, paymentRequest.PaymentAssetId}.ToJson()}");
            }

            TransferResult transferResult = await _settlementRetryPolicy
                                            .ExecuteAsync(() => _transferService.SettleThrowFail(
                                                              paymentRequest.PaymentAssetId,
                                                              sourceWalletAddress,
                                                              destWalletAddress));

            foreach (var transferResultTransaction in transferResult.Transactions)
            {
                IPaymentRequestTransaction settlementTx = await _transactionsService.CreateTransactionAsync(
                    new CreateTransactionCommand
                {
                    Amount                = transferResultTransaction.Amount,
                    Blockchain            = transferResult.Blockchain,
                    AssetId               = transferResultTransaction.AssetId,
                    WalletAddress         = paymentRequest.WalletAddress,
                    DueDate               = paymentRequest.DueDate,
                    IdentityType          = transferResultTransaction.IdentityType,
                    Identity              = transferResultTransaction.Identity,
                    Confirmations         = 0,
                    Hash                  = transferResultTransaction.Hash,
                    TransferId            = transferResult.Id,
                    Type                  = TransactionType.Settlement,
                    SourceWalletAddresses = transferResultTransaction.Sources.ToArray()
                });

                await _transactionPublisher.PublishAsync(settlementTx);
            }

            return(new SettlementResult
            {
                Blockchain = transferResult.Blockchain,
                WalletAddress = destWalletAddress,
                AssetId = paymentRequest.PaymentAssetId,
                Amount = transferResult.GetSuccedeedTxs().Sum(x => x.Amount)
            });
        }