Exemplo n.º 1
0
        public async Task <bool> CreateOrUpdateSession(ICart cart, SessionSettings settings)
        {
            var sessionRequest = CreateSessionRequest(cart, settings);
            var sessionId      = cart.GetKlarnaSessionId();

            if (string.IsNullOrEmpty(sessionId))
            {
                return(await CreateSession(sessionRequest, cart, settings).ConfigureAwait(false));
            }

            try
            {
                await _klarnaServiceApiFactory.Create(GetConfiguration(cart.MarketId))
                .UpdateSession(sessionId, sessionRequest)
                .ConfigureAwait(false);

                return(true);
            }
            catch (ApiException apiException)
            {
                if (apiException.StatusCode == HttpStatusCode.NotFound)
                {
                    return(await CreateSession(sessionRequest, cart, settings).ConfigureAwait(false));
                }

                _logger.Error(apiException.Message, apiException);
                return(false);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message, ex);
                return(false);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> CreateOrUpdateSession(ICart cart, IDictionary <string, object> dic = null)
        {
            // Check if we shared PI before, if so it allows us to share it again
            var canSendPersonalInformation = AllowedToSharePersonalInformation(cart);
            var config = GetConfiguration(cart.Market.MarketId);

            var sessionRequest = GetSessionRequest(cart, config, canSendPersonalInformation);

            if (ServiceLocator.Current.TryGetExistingInstance(out ISessionBuilder sessionBuilder))
            {
                sessionRequest = sessionBuilder.Build(sessionRequest, cart, config, dic);
            }
            var currentCountry = cart.Market.Countries.FirstOrDefault();

            // Clear PI if we're not allowed to send it yet (can be set by custom session builder)
            if (!canSendPersonalInformation && !CanSendPersonalInformation(currentCountry))
            {
                // Can't share PI yet, will be done during the first authorize call
                sessionRequest.ShippingAddress = null;
                sessionRequest.BillingAddress  = null;

                // If the pre assessment is not enabled then don't send the customer information to Klarna
                if (!config.CustomerPreAssessment)
                {
                    sessionRequest.Customer = null;
                }
            }
            var sessionId = cart.Properties[Constants.KlarnaSessionIdCartField]?.ToString();

            if (!string.IsNullOrEmpty(sessionId))
            {
                try
                {
                    await _klarnaServiceApiFactory.Create(GetConfiguration(cart.Market))
                    .UpdateSession(sessionId, sessionRequest)
                    .ConfigureAwait(false);

                    return(true);
                }
                catch (ApiException apiException)
                {
                    // Create new session if current one is not found
                    if (apiException.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(await CreateSession(sessionRequest, cart));
                    }

                    _logger.Error(apiException.Message, apiException);
                    return(false);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message, ex);

                    return(false);
                }
            }
            return(await CreateSession(sessionRequest, cart));
        }