Exemplo n.º 1
0
        public async Task <IActionResult> InitiateCardless(Provider provider, int paymentSum, Intent intent)
        {
            TempData["actionInitiated"] = true;
            if (intent == Intent.NoIntent)
            {
                setError(InvalidIntent);
                return(redirectToLandingPage());
            }

            InitiateCardlessResponse response = await rcpgClient.InitiateCardless(provider, intent, paymentSum, DEFAULT_CURRENCY, CONFIRM_RETURN_URL, CANCELED_RETURN_URL);

            if (response.PaymentErrors.Any())
            {
                TempData["hasErrors"]        = true;
                TempData["notificationText"] = $"Notikusi kļūda. Kļūdas avots: \"{response.PaymentErrors.First().Source}\", kļūdas kods: \"{response.PaymentErrors.First().ErrorCode}\"";
                return(redirectToLandingPage());
            }

            Payment payment = new Payment()
            {
                Token       = response.Token,
                Sum         = paymentSum,
                Currency    = DEFAULT_CURRENCY,
                Provider    = provider.ToString(),
                Intent      = intent.ToString(),
                Status      = PaymentStatus.Initiated.ToString(),
                InitiatedOn = DateTime.Parse(response.InitiatedOn)
            };
            await paymentRepository.Add(payment);

            // Redirects to provider where buyer can either confirm or cancel purchase.
            return(Redirect(response.ConfirmInitiationUrl));
        }
Exemplo n.º 2
0
        public async Task <InitiateCardlessResponse> InitiateCardless(Provider provider, Intent intent, int paymentSum, string currency, string returnUrl, string cancelUrl)
        {
            if (channel.State == ChannelState.Shutdown)
            {
                var response = new InitiateCardlessResponse();
                response.PaymentErrors.Add(setClientShutDownError());
                return(response);
            }

            InitiateCardlessRequest request = new InitiateCardlessRequest()
            {
                Provider           = provider,
                Intent             = intent,
                PaymentSum         = paymentSum,
                ReturnUrl          = returnUrl,
                CancelReturnUrl    = cancelUrl,
                Currency           = currency,
                AllowGuestCheckout = true // Allows/disallows buyers that are not registered in providers service.
            };

            // Dummy item list. Total sum of items must be equal to paymentSum.
            var items = new List <Item>()
            {
                new Item()
                {
                    Name        = "Bike",
                    Description = "A brand new bike!",
                    Quantity    = 1,
                    Amount      = paymentSum
                }
            };

            request.Items.AddRange(items);

            try
            {
                return(await client.InitiateCardlessAsync(request));
            }
            catch (RpcException ex)
            {
                var response = new InitiateCardlessResponse();
                response.PaymentErrors.Add(setGatewayError(ex.Message));
                return(response);
            }
        }