Beispiel #1
0
        public virtual ShippingDetailsResponse GetShippingDetails(string orderId, string contactId, string marketId, string cartName, ShippingRequest shippingRequest)
        {
            var cart = _vippsService.GetCartByContactId(contactId, marketId, cartName);

            var shippingMethods = ShippingManager.GetShippingMethodsByMarket(cart.MarketId.Value, false).ShippingMethod.ToList().OrderBy(x => x.Ordering);

            var shippingDetails = new List <ShippingDetails>();

            var counter = 1;

            foreach (var shippingMethod in shippingMethods)
            {
                lock (cart)
                {
                    var shipment = cart.GetFirstShipment();
                    shipment.ShippingAddress  = AddressHelper.ShippingRequestToOrderAddress(shippingRequest, cart);
                    shipment.ShippingMethodId = shippingMethod.ShippingMethodId;
                    cart.ApplyDiscounts();

                    shippingDetails.Add(new ShippingDetails
                    {
                        ShippingMethodId = shippingMethod.ShippingMethodId.ToString(),
                        ShippingCost     = Convert.ToDouble(cart.GetShippingTotal().Amount),
                        ShippingMethod   = shippingMethod.DisplayName,
                        Priority         = shippingMethod.IsDefault ? 0 : counter++
                    });
                }
            }

            return(new ShippingDetailsResponse
            {
                OrderId = orderId,
                ShippingDetails = shippingDetails
            });
        }
        public virtual async Task <ProcessAuthorizationResponse> ProcessAuthorizationAsync(Guid contactId, string marketId, string cartName, string orderId)
        {
            var purchaseOrder = _vippsService.GetPurchaseOrderByOrderId(orderId);

            if (purchaseOrder != null)
            {
                return(new ProcessAuthorizationResponse
                {
                    PurchaseOrder = purchaseOrder,
                    Processed = true
                });
            }

            var cart        = _vippsService.GetCartByContactId(contactId, marketId, cartName);
            var paymentType = GetVippsPaymentType(cart);

            var orderDetails = await _vippsService.GetOrderDetailsAsync(orderId, marketId);

            var result = await _vippsOrderCreator.ProcessOrderDetails(orderDetails, orderId, contactId, marketId, cartName);

            if (result.PurchaseOrder != null)
            {
                return(new ProcessAuthorizationResponse(result)
                {
                    Processed = true
                });
            }

            return(new ProcessAuthorizationResponse(result)
            {
                PaymentType = paymentType,
                Processed = false
            });
        }
        private ProcessOrderResponse CheckDependenciesThenProcessPayment(string orderId, Guid contactId, string marketId, string cartName)
        {
            var errorResponse = EnsureNoPurchaseOrder(orderId);

            if (errorResponse != null)
            {
                return(errorResponse);
            }

            var cart = _vippsService.GetCartByContactId(contactId, marketId, cartName);

            errorResponse = ValidateCartAndSetProcessing(orderId, cart);
            if (errorResponse != null)
            {
                return(errorResponse);
            }

            var detailsResponse = AsyncHelper.RunSync(() => _vippsService.GetOrderDetailsAsync(orderId, marketId));
            var lastTransaction = GetLastTransaction(detailsResponse);

            return(ProcessPayment(detailsResponse, lastTransaction, orderId, cart));
        }
        public async Task <RedirectResult> Index(string orderId, string contactId, string marketId, string cartName)
        {
            var result = await _vippsPaymentService.ProcessAuthorization(Guid.Parse(contactId), marketId, cartName, orderId);

            //If ProcessAuthorization fails user needs to be redirected back to checkout or product page
            if (!result.Processed)
            {
                //Example method for dealing with different error types and what error message to show
                var errorMessage = GetErrorMessage(result);

                if (result.PaymentType == VippsPaymentType.CHECKOUT)
                {
                    //Redirect to checkout (preferably with error message)
                    return(new RedirectResult("/en/checkout"));
                }

                //Redirect back to product if express checkout (preferably with error message)
                if (result.PaymentType == VippsPaymentType.PRODUCTEXPRESS)
                {
                    var cart            = _vippsService.GetCartByContactId(contactId, marketId, cartName);
                    var item            = cart.GetFirstForm().GetAllLineItems().FirstOrDefault();
                    var itemContentLink = _referenceConverter.GetContentLink(item?.Code);
                    var entryContent    = _contentLoader.Get <EntryContentBase>(itemContentLink);
                    return(new RedirectResult(entryContent.GetUrl()));
                }

                //Redirect to cart page if your website has one
                if (result.PaymentType == VippsPaymentType.CARTEXPRESS)
                {
                    return(new RedirectResult("/"));
                }

                if (result.PaymentType == VippsPaymentType.WISHLISTEXPRESS)
                {
                    return(new RedirectResult("/en/my-pages/wish-list/"));
                }

                if (result.PaymentType == VippsPaymentType.UNKNOWN)
                {
                    return(new RedirectResult("/"));
                }
            }


            //If wishlist payment, delete wishlist as well
            if (result.PaymentType == VippsPaymentType.WISHLISTEXPRESS)
            {
                var wishList = _cartService.LoadCart(_cartService.DefaultWishListName);

                if (wishList != null)
                {
                    _orderRepository.Delete(wishList.OrderLink);
                }
            }

            var queryCollection = new NameValueCollection
            {
                { "contactId", _customerContext.CurrentContactId.ToString() },
                { "orderNumber", result.PurchaseOrder.OrderLink.OrderGroupId.ToString(CultureInfo.InvariantCulture) }
            };

            return(new RedirectResult(new UrlBuilder("/en/checkout/order-confirmation/")
            {
                QueryCollection = queryCollection
            }.ToString()));
        }
Beispiel #5
0
        private async Task <ProcessOrderResponse> ProcessOrder(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName)
        {
            var readLock = _synchronizer.Get(orderId);

            try
            {
                await readLock.WaitAsync();

                var purchaseOrder = _vippsService.GetPurchaseOrderByOrderId(orderId);
                if (purchaseOrder != null)
                {
                    return(new ProcessOrderResponse
                    {
                        PurchaseOrder = purchaseOrder
                    });
                }

                var cart = _vippsService.GetCartByContactId(contactId, marketId, cartName);
                if (cart == null)
                {
                    _logger.Warning($"No cart found for vipps order id {orderId}");
                    return(new ProcessOrderResponse
                    {
                        ProcessResponseErrorType = ProcessResponseErrorType.NOCARTFOUND,
                        ErrorMessage = $"No cart found for vipps order id {orderId}"
                    });
                }

                var payment = cart.GetFirstForm().Payments.FirstOrDefault(x =>
                                                                          x.IsVippsPayment() && x.TransactionID == orderId &&
                                                                          x.TransactionType.Equals(TransactionType.Authorization.ToString()));


                if (TransactionSuccess(paymentDetails))
                {
                    return(await HandleSuccess(cart, payment, paymentDetails, vippsUserDetails, orderId));
                }

                if (TransactionCancelled(paymentDetails))
                {
                    return(HandleCancelled(cart, payment, paymentDetails, orderId));
                }

                if (TransactionFailed(paymentDetails))
                {
                    return(HandleFailed(cart, payment, paymentDetails, orderId));
                }

                return(new ProcessOrderResponse
                {
                    ProcessResponseErrorType = ProcessResponseErrorType.OTHER,
                    ErrorMessage = $"No action taken on order id: {orderId}."
                });
            }

            catch (Exception ex)
            {
                _logger.Error(ex.Message, ex);
                return(new ProcessOrderResponse
                {
                    ErrorMessage = ex.Message,
                    ProcessResponseErrorType = ProcessResponseErrorType.EXCEPTION
                });
            }

            finally
            {
                readLock.Release();

                if (readLock.CurrentCount > 0)
                {
                    _synchronizer.Remove(orderId);
                }
            }
        }