Exemple #1
0
        public virtual void CreateAndAddPaymentToCart(ICart cart, CheckoutViewModel viewModel)
        {
            var total         = viewModel.OrderSummary.PaymentTotal;
            var paymentMethod = viewModel.Payment;

            if (paymentMethod == null)
            {
                return;
            }

            var payment = cart.GetFirstForm().Payments.FirstOrDefault(x => x.PaymentMethodId == paymentMethod.PaymentMethodId);

            if (payment == null)
            {
                payment = paymentMethod.CreatePayment(total, cart);
                cart.AddPayment(payment, _orderGroupFactory);
            }
            else
            {
                payment.Amount = viewModel.OrderSummary.PaymentTotal;
            }
        }
        private IHttpActionResult Finalize(ICart cart)
        {
            cart.ApplyDiscounts();
            var total = _orderGroupCalculator.GetSubTotal(cart);
            var vippsPaymentMethodDto = PaymentHelper.GetVippsPaymentMethodDto().PaymentMethod.FirstOrDefault();

            if (vippsPaymentMethodDto == null)
            {
                _logger.Warning("No payment method is setup for vipps");
                return(GetUnsuccessfulResult("No payment method is setup for vipps"));
            }

            cart.GetFirstForm().Payments.Clear();
            var payment = PaymentHelper.CreateVippsPayment(cart, total, vippsPaymentMethodDto.PaymentMethodId);

            cart.AddPayment(payment);

            _orderRepository.Save(cart);

            try
            {
                var paymentProcessingResults = cart.ProcessPayments().ToArray();
                var successfulResult         = paymentProcessingResults.FirstOrDefault(x => x.IsSuccessful && !string.IsNullOrEmpty(x.RedirectUrl));

                if (successfulResult != null)
                {
                    return(GetSuccessfulResult(paymentProcessingResults.FirstOrDefault(x => x.IsSuccessful) ?? paymentProcessingResults.FirstOrDefault()));
                }

                return(GetUnsuccessfulResult(paymentProcessingResults.FirstOrDefault(x => !string.IsNullOrEmpty(x.Message))?.Message));
            }

            catch (Exception ex)
            {
                _logger.Error($"{ex.Message} {ex.StackTrace}");
                return(GetUnsuccessfulResult(ex.Message));
            }
        }
Exemple #3
0
        // RoCe - this can also be simplified
        private void AddPaymentToOrder(ICart cart, Guid selectedPaymentGuid)
        {
            if (cart.GetFirstForm().Payments.Any())
            {
                // should maybe clean up in the cart here
            }

            var selectedPaymentMethod =
                PaymentManager.GetPaymentMethod(selectedPaymentGuid).PaymentMethod.First();

            var payment = _orderGroupFactory.CreatePayment(cart);

            payment.PaymentMethodId   = selectedPaymentMethod.PaymentMethodId;
            payment.PaymentType       = PaymentType.Other;
            payment.PaymentMethodName = selectedPaymentMethod.Name; // check if any string still works :) ... it does

            // RoCe: need to get the Diff from the firts payment
            // ...we also have - cart.GetTotal(_orderGroupCalculator)
            // ... this is from Fund. and the first (and only payment)
            payment.Amount = _orderGroupCalculator.GetTotal(cart).Amount; // need a debug here

            cart.AddPayment(payment);
            // could add payment.BillingAddress = theAddress ... if we had it here
        }
Exemple #4
0
        public IPurchaseOrder CreatePurchaseOrderForKlarna(string klarnaOrderId, CheckoutOrderData order, ICart cart)
        {
            var paymentRow = PaymentManager.GetPaymentMethodBySystemName(Constants.KlarnaCheckoutSystemKeyword, ContentLanguage.PreferredCulture.Name).PaymentMethod.FirstOrDefault();

            var payment = cart.CreatePayment(_orderGroupFactory);

            payment.PaymentType       = PaymentType.Other;
            payment.PaymentMethodId   = paymentRow.PaymentMethodId;
            payment.PaymentMethodName = Constants.KlarnaCheckoutSystemKeyword;
            payment.Amount            = cart.GetTotal(_orderGroupCalculator).Amount;
            payment.Status            = PaymentStatus.Pending.ToString();
            payment.TransactionType   = TransactionType.Authorization.ToString();

            cart.AddPayment(payment, _orderGroupFactory);

            var billingAddress = new AddressModel
            {
                Name               = $"{order.BillingAddress.StreetAddress}{order.BillingAddress.StreetAddress2}{order.BillingAddress.City}",
                FirstName          = order.BillingAddress.GivenName,
                LastName           = order.BillingAddress.FamilyName,
                Email              = order.BillingAddress.Email,
                DaytimePhoneNumber = order.BillingAddress.Phone,
                Line1              = order.BillingAddress.StreetAddress,
                Line2              = order.BillingAddress.StreetAddress2,
                PostalCode         = order.BillingAddress.PostalCode,
                City               = order.BillingAddress.City,
                CountryName        = order.BillingAddress.Country
            };

            payment.BillingAddress = _addressBookService.ConvertToAddress(billingAddress, cart);

            cart.ProcessPayments(_paymentProcessor, _orderGroupCalculator);

            var totalProcessedAmount = cart.GetFirstForm().Payments.Where(x => x.Status.Equals(PaymentStatus.Processed.ToString())).Sum(x => x.Amount);

            if (totalProcessedAmount != cart.GetTotal(_orderGroupCalculator).Amount)
            {
                throw new InvalidOperationException("Wrong amount");
            }

            if (payment.HasFraudStatus(FraudStatus.PENDING))
            {
                payment.Status = PaymentStatus.Pending.ToString();
            }

            var orderReference = _orderRepository.SaveAsPurchaseOrder(cart);
            var purchaseOrder  = _orderRepository.Load <IPurchaseOrder>(orderReference.OrderGroupId);

            _orderRepository.Delete(cart.OrderLink);

            if (purchaseOrder == null)
            {
                _klarnaCheckoutService.CancelOrder(cart);

                return(null);
            }
            else
            {
                _klarnaCheckoutService.Complete(purchaseOrder);
                purchaseOrder.Properties[Klarna.Common.Constants.KlarnaOrderIdField] = klarnaOrderId;

                _orderRepository.Save(purchaseOrder);
                return(purchaseOrder);
            }
        }