Exemplo n.º 1
0
        protected override async Task <IBankResponse> CallBank(PayingAttempt payingAttempt, CancellationToken cancellationToken)
        {
            var neverChangingBankPaymentId = Guid.Parse("d6589823-2bfa-4e1a-9f3f-699cb9a0a0a2");
            var response = new BankResponse(bankPaymentId: neverChangingBankPaymentId, gatewayPaymentId: payingAttempt.GatewayPaymentId, paymentStatus: BankPaymentStatus.Accepted);

            _paymentIdsMapper.RememberMapping(new AcquiringBankPaymentId(neverChangingBankPaymentId), new GatewayPaymentId(response.GatewayPaymentId));

            return(await Task.FromResult(response));
        }
Exemplo n.º 2
0
        protected override async Task <IBankResponse> CallBank(PayingAttempt payingAttempt, CancellationToken cancellationToken)
        {
            // Simulate bank response delay
            var delay = DelayProvider.Delays();
            await Task.Delay(delay, cancellationToken);

            // Call bank's service
            var         request  = new BNPPaymentRequest(payingAttempt.GatewayPaymentId, payingAttempt.Amount, payingAttempt.Currency, payingAttempt.CardCvv, payingAttempt.CardExpiry, payingAttempt.CardNumber);
            BNPResponse response = await _bnp.RespondToPayment(request);

            _paymentIdsMapper.RememberMapping(new AcquiringBankPaymentId(response.BankPaymentId), new GatewayPaymentId(response.GatewayPaymentId));
            return(AdaptToBankResponse(response));
        }
        public async Task <IBankResponse> RespondToPaymentAttempt(PayingAttempt paymentAttempt, CancellationToken cancellationToken)
        {
            // Connection to bank
            var policy = Policy.Handle <FailedConnectionToBankException>()
                         .WaitAndRetryAsync(3, retry => TimeSpan.FromMilliseconds(Math.Pow(2, retry)));

            var policyResult = await policy.ExecuteAndCaptureAsync(async() => await Connect());

            if (!policyResult.Result)
            {
                return(new BankDoesNotRespond(paymentAttempt.GatewayPaymentId));
            }

            return(await CallBank(paymentAttempt, cancellationToken));
        }
 protected abstract Task <IBankResponse> CallBank(PayingAttempt payingAttempt, CancellationToken cancellationToken);
        private CircuitBreaker CircuitBreaker(IAdaptToBank bankAdapter, Payment payment, PayingAttempt payingAttempt)
        {
            var bankAdapterType = bankAdapter.GetType();

            if (!_circuitBreakers.TryGet(bankAdapterType, out CircuitBreaker circuitBreaker))
            {
                circuitBreaker = MakeCircuitBreaker(bankAdapter, payment);
                _circuitBreakers.Add(bankAdapterType, circuitBreaker);
            }

            return(circuitBreaker);
        }
 public PaymentRequestBuffer(IAdaptToBank bankAdapter, PayingAttempt payingAttempt, Payment payment)
 {
     BankAdapter   = bankAdapter;
     PayingAttempt = payingAttempt;
     Payment       = payment;
 }