public virtual async Task <PaymentProcessingResult> InitiateAsync(IOrderGroup orderGroup, IPayment payment)
        {
            var orderId = OrderNumberHelper.GenerateOrderNumber();

            orderGroup.Properties[VippsConstants.VippsOrderIdField] = orderId;
            payment.TransactionID = orderId;
            _orderRepository.Save(orderGroup);

            var configuration = _configurationLoader.GetConfiguration(orderGroup.MarketId);

            var serviceApi = _vippsServiceApiFactory.Create(configuration);

            try
            {
                var initiatePaymentRequest =
                    _requestFactory.CreateInitiatePaymentRequest(payment, orderGroup, configuration, orderId, orderGroup.CustomerId, orderGroup.MarketId.Value);

                var response = await serviceApi.Initiate(initiatePaymentRequest).ConfigureAwait(false);

                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType,
                                                      $"Vipps payment initiated. Vipps reference: {initiatePaymentRequest.Transaction.OrderId}");

                _vippsPollingService.Start(new VippsPollingEntity
                {
                    OrderId   = orderId,
                    CartName  = orderGroup.Name,
                    ContactId = orderGroup.CustomerId,
                    MarketId  = orderGroup.MarketId.Value
                });

                return(PaymentProcessingResult.CreateSuccessfulResult("", response.Url));
            }

            catch (ApiException apiException)
            {
                var errorMessage = GetErrorMessage(apiException);
                _logger.Log(Level.Error, $"Vipps initiate failed: {errorMessage}");
                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType, $"Vipps payment initiation failed. Error message: {errorMessage}, {apiException}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult(errorMessage));
            }

            catch (Exception ex)
            {
                _logger.Log(Level.Error, $"{ex.Message}, {ex.StackTrace}");
                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType, $"Vipps payment initiation failed. Error message: {ex.Message}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult(ex.Message));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Initiate()
        {
            var configuration = _configurationLoader.GetConfiguration(_currentMarket.GetCurrentMarket().MarketId);
            var vippsApi      = _vippsServiceApiFactory.Create(configuration);
            var viewModel     = new VippsTestViewModel();

            try
            {
                var orderId = OrderNumberHelper.GenerateOrderNumber();
                var initiatePaymentRequest = new InitiatePaymentRequest
                {
                    CustomerInfo = new CustomerInfo(),
                    MerchantInfo = new MerchantInfo
                    {
                        CallbackPrefix        = EnsureCorrectUrl(configuration.SiteBaseUrl, "vippscallback/"),
                        ConsentRemovalPrefix  = EnsureCorrectUrl(configuration.SiteBaseUrl, "vippscallback/"),
                        FallBack              = EnsureCorrectUrl(configuration.SiteBaseUrl, $"vippstest/fallback?orderId={orderId}"),
                        ShippingDetailsPrefix = EnsureCorrectUrl(configuration.SiteBaseUrl, "vippscallback/"),
                        IsApp = false,
                        MerchantSerialNumber = Convert.ToInt32(configuration.MerchantSerialNumber),
                        PaymentType          = VippsConstants.RegularCheckout
                    },
                    Transaction = new Transaction
                    {
                        Amount          = 20000,
                        OrderId         = orderId,
                        TimeStamp       = DateTime.Now,
                        TransactionText = "test"
                    }
                };

                var result = vippsApi.Initiate(initiatePaymentRequest).Result;

                return(Redirect(result.Url));
            }

            catch (Exception ex)
            {
                viewModel.Message = $"Initiate payment failed. Exception: {ex.Message} {ex.InnerException} {ex.StackTrace}";
                return(View("Index", viewModel));
            }
        }
Ejemplo n.º 3
0
        public virtual async Task <DetailsResponse> GetOrderDetailsAsync(string orderId, string marketId)
        {
            var configuration = _configurationLoader.GetConfiguration(marketId);
            var serviceApi    = _vippsServiceApiFactory.Create(configuration);

            try
            {
                return(await serviceApi.Details(orderId).ConfigureAwait(false));
            }

            catch (ApiException apiException)
            {
                var errorMessage = GetErrorMessage(apiException);
                _logger.Log(Level.Error, $"Error getting payment details. Error message: {errorMessage}");
            }

            catch (Exception ex)
            {
                _logger.Error($"Error getting payment details. Exception: {ex.Message} {ex.StackTrace}");
            }

            return(null);
        }