Example #1
0
        public async Task SaveAsync(IVirtualWallet wallet)
        {
            string partitionKey = VirtualWalletEntity.ByMerchantId.GeneratePartitionKey(wallet.MerchantId);

            string rowKey = VirtualWalletEntity.ByMerchantId.GenerateRowKey(wallet.Id);

            VirtualWalletEntity exItem =
                wallet.Id != null ? await _tableStorage.GetDataAsync(partitionKey, rowKey) : null;

            if (exItem != null)
            {
                await _tableStorage.MergeAsync(partitionKey, rowKey, entity =>
                {
                    entity.BlockchainWallets = wallet.BlockchainWallets;

                    return(entity);
                });

                return;
            }

            VirtualWalletEntity newEntity = VirtualWalletEntity.ByMerchantId.Create(wallet);

            await _tableStorage.InsertAsync(newEntity);
        }
Example #2
0
        public async Task <IVirtualWallet> AddAssetAsync(string merchantId, string walletId, string assetId)
        {
            IVirtualWallet virtualWallet = await _virtualWalletService.GetAsync(merchantId, walletId);

            if (virtualWallet == null)
            {
                throw new WalletNotFoundException(walletId);
            }

            BlockchainType blockchainType = assetId.GetBlockchainType();

            IBlockchainApiClient blockchainClient = _blockchainClientProvider.Get(blockchainType);

            WalletAllocationPolicy policy = _walletAllocationSettings.GetPolicy(blockchainType);

            IBcnWalletUsage walletUsage;

            switch (policy)
            {
            case WalletAllocationPolicy.New:
                string address = await blockchainClient.CreateAddressAsync();

                walletUsage = await _bcnWalletUsageService.OccupyAsync(address, blockchainType, virtualWallet.Id);

                break;

            case WalletAllocationPolicy.Reuse:
                try
                {
                    walletUsage = await _bcnWalletUsageService.OccupyAsync(blockchainType, virtualWallet.Id);
                }
                catch (WalletAddressAllocationException)
                {
                    string newAddress = await blockchainClient.CreateAddressAsync();

                    walletUsage =
                        await _bcnWalletUsageService.OccupyAsync(newAddress, blockchainType, virtualWallet.Id);
                }

                break;

            default:
                throw new UnknownWalletAllocationPolicyException(policy.ToString());
            }

            IVirtualWallet updatedWallet = await _virtualWalletService.AddAddressAsync(
                virtualWallet.MerchantId,
                virtualWallet.Id,
                new BlockchainWallet
            {
                AssetId    = assetId,
                Address    = walletUsage.WalletAddress,
                Blockchain = walletUsage.Blockchain
            });

            await _walletEventsPublisher.PublishAsync(walletUsage.WalletAddress, blockchainType, virtualWallet.DueDate);

            return(updatedWallet);
        }
Example #3
0
        public async Task <IVirtualWallet> CreateAsync(string merchantId, DateTime dueDate, string assetId = null)
        {
            IVirtualWallet wallet = await _virtualWalletService.CreateAsync(merchantId, dueDate);

            if (assetId != null)
            {
                wallet = await AddAssetAsync(wallet.MerchantId, wallet.Id, assetId);
            }

            _log.Info("New virtual wallet created", wallet.ToJson());

            return(wallet);
        }
Example #4
0
        public async Task <IVirtualWallet> CreateAsync(string merchantId, DateTime dueDate, string assetId = null)
        {
            IVirtualWallet wallet = await _virtualWalletService.CreateAsync(merchantId, dueDate);

            if (assetId != null)
            {
                wallet = await AddAssetAsync(wallet.MerchantId, wallet.Id, assetId);
            }

            await _log.WriteInfoAsync(nameof(WalletManager), nameof(CreateAsync), wallet.ToJson(),
                                      "New virtual wallet created");

            return(wallet);
        }
Example #5
0
        public async Task <string> ResolveBlockchainAddressAsync(string virtualAddress, string assetId)
        {
            IVirtualWallet wallet = await _virtualWalletService.FindAsync(virtualAddress);

            if (wallet == null)
            {
                throw new WalletNotFoundException(virtualAddress);
            }

            BlockchainWallet bcnWallet =
                wallet.BlockchainWallets.SingleOrDefault(x => x.AssetId == assetId);

            return(bcnWallet?.Address);
        }
Example #6
0
        public async Task <IVirtualWallet> CreateAsync(IVirtualWallet wallet)
        {
            VirtualWalletEntity entity = VirtualWalletEntity.ByMerchantId.Create(wallet);

            await _tableStorage.InsertAsync(entity);

            AzureIndex indexByWalletId = VirtualWalletEntity.IndexByWalletId.Create(entity);

            await _walletIdIndexStorage.InsertAsync(indexByWalletId);

            AzureIndex indexByDueDate = VirtualWalletEntity.IndexByDueDate.Create(entity);

            await _dueDateIndexStorage.InsertAsync(indexByDueDate);

            return(Mapper.Map <VirtualWallet>(entity));
        }
Example #7
0
        public async Task <IPaymentRequest> CreateAsync(IPaymentRequest paymentRequest)
        {
            paymentRequest.Status = PaymentRequestStatus.New;

            paymentRequest.Timestamp = DateTime.UtcNow;

            DateTime walletDueDate = paymentRequest.DueDate.Add(_expirationPeriods.WalletExtra);

            IVirtualWallet wallet = await _walletsManager.CreateAsync(paymentRequest.MerchantId, walletDueDate);

            paymentRequest.WalletAddress = wallet.Id;

            IPaymentRequest createdPaymentRequest = await _paymentRequestRepository.InsertAsync(paymentRequest);

            _log.Info("Payment request created", paymentRequest.ToJson());

            return(createdPaymentRequest);
        }
Example #8
0
        public async Task <IVirtualWallet> EnsureBcnAddressAllocated(string merchantId, string walletId, string assetId)
        {
            IVirtualWallet virtualWallet = await _virtualWalletService.GetAsync(merchantId, walletId);

            if (virtualWallet == null)
            {
                throw new WalletNotFoundException(walletId);
            }

            BlockchainType blockchainType = assetId.GetBlockchainType();

            if (virtualWallet.BlockchainWallets.Any(x => x.Blockchain == blockchainType))
            {
                return(virtualWallet);
            }

            return(await AddAssetAsync(merchantId, walletId, assetId));
        }
Example #9
0
        public async Task <IVirtualWallet> AddAddressAsync(string merchantId, string walletId, BlockchainWallet blockchainWallet)
        {
            IVirtualWallet wallet = await _virtualWalletRepository.GetAsync(merchantId, walletId);

            if (wallet == null)
            {
                throw new WalletNotFoundException(walletId);
            }

            bool exists = wallet.BlockchainWallets.Any(x =>
                                                       x.Blockchain == blockchainWallet.Blockchain && x.Address == blockchainWallet.Address);

            if (exists)
            {
                throw new WalletAlreadyExistsException(walletId);
            }

            wallet.BlockchainWallets.Add(blockchainWallet);

            await _virtualWalletRepository.SaveAsync(wallet);

            return(wallet);
        }