private ProcessOrderResponse HandleSuccess(ICart cart, IPayment payment, IVippsPaymentDetails paymentDetails, IVippsUserDetails userDetails, string orderId)
        {
            if (payment == null)
            {
                OrderNoteHelper.AddNoteAndSaveChanges(cart, "Cancel", $"No vipps payment found for vipps order id {orderId}. Canceling payment");
                PaymentHelper.CancelPayment(cart, paymentDetails.Amount, orderId);

                return(new ProcessOrderResponse
                {
                    ErrorMessage = $"No vipps payment found for vipps order id {orderId}.",
                    ProcessResponseErrorType = ProcessResponseErrorType.NOVIPPSPAYMENTINCART
                });
            }

            EnsureExpressPaymentAndShipping(cart, payment, paymentDetails, userDetails);
            payment.Status = PaymentStatus.Processed.ToString();
            AddNote(cart, payment, orderId, paymentDetails);

            var loadOrCreatePurchaseOrderResponse = CreatePurchaseOrder(cart);

            if (loadOrCreatePurchaseOrderResponse.PurchaseOrder != null)
            {
                return(loadOrCreatePurchaseOrderResponse);
            }

            PaymentHelper.CancelPayment(cart, payment);
            return(loadOrCreatePurchaseOrderResponse);
        }
 private static void EnsurePayment(IPayment payment, ICart cart, IVippsPaymentDetails paymentDetails, IVippsUserDetails details)
 {
     if (string.IsNullOrEmpty(payment.BillingAddress?.Id))
     {
         payment.BillingAddress =
             AddressHelper.UserDetailsAndShippingDetailsToOrderAddress(details.UserDetails,
                                                                       details.ShippingDetails, cart);
         payment.Amount = paymentDetails.Amount.FormatAmountFromVipps();
     }
 }
        private bool TransactionCancelled(IVippsPaymentDetails paymentDetails)
        {
            if (paymentDetails is TransactionLogHistory transactionLogHistory)
            {
                return(transactionLogHistory.Operation == VippsDetailsResponseOperation.CANCEL.ToString());
            }

            if (paymentDetails is TransactionInfo transactionInfo)
            {
                return(transactionInfo.Status == VippsCallbackStatus.CANCELLED.ToString());
            }

            throw new InvalidCastException(nameof(paymentDetails));
        }
        private ProcessOrderResponse HandleCancelled(ICart cart, IPayment payment, IVippsPaymentDetails paymentDetails, string orderId)
        {
            if (payment == null)
            {
                AddNote(cart, orderId, paymentDetails);
                return(new ProcessOrderResponse
                {
                    ProcessResponseErrorType = ProcessResponseErrorType.NONE
                });
            }

            return(new ProcessOrderResponse
            {
                ProcessResponseErrorType = ProcessResponseErrorType.NONE
            });
        }
        private bool TransactionFailed(IVippsPaymentDetails paymentDetails)
        {
            if (paymentDetails is TransactionLogHistory transactionLogHistory)
            {
                return(transactionLogHistory.Operation == VippsDetailsResponseOperation.RESERVE.ToString() && !transactionLogHistory.OperationSuccess ||
                       transactionLogHistory.Operation == VippsDetailsResponseOperation.SALE.ToString() && !transactionLogHistory.OperationSuccess);
            }

            if (paymentDetails is TransactionInfo transactionInfo)
            {
                return(transactionInfo.Status == VippsCallbackStatus.SALE_FAILED.ToString() ||
                       transactionInfo.Status == VippsCallbackStatus.RESERVE_FAILED.ToString() ||
                       transactionInfo.Status == VippsCallbackStatus.REJECTED.ToString());
            }

            throw new InvalidCastException(nameof(paymentDetails));
        }
        private void AddNote(ICart cart, string orderId, IVippsPaymentDetails paymentDetails)
        {
            if (paymentDetails is TransactionLogHistory transactionLogHistory)
            {
                OrderNoteHelper.AddNoteAndSaveChanges(cart, transactionLogHistory.Operation,
                                                      $"Payment with order id: {orderId}. Operation: {transactionLogHistory.Operation} Success: {transactionLogHistory.OperationSuccess}");
                return;
            }

            if (paymentDetails is TransactionInfo transactionInfo)
            {
                OrderNoteHelper.AddNoteAndSaveChanges(cart, transactionInfo.Status,
                                                      $"Payment with order id: {orderId}. Status: {transactionInfo.Status}");
                return;
            }

            throw new InvalidCastException(nameof(paymentDetails));
        }
        private ProcessOrderResponse HandleFailed(ICart cart, IPayment payment, IVippsPaymentDetails paymentDetails, string orderId)
        {
            if (payment != null)
            {
                payment.Status = PaymentStatus.Failed.ToString();
                AddNote(cart, payment, orderId, paymentDetails);
            }

            else
            {
                AddNote(cart, orderId, paymentDetails);
            }

            return(new ProcessOrderResponse
            {
                ProcessResponseErrorType = ProcessResponseErrorType.FAILED
            });
        }
Esempio n. 8
0
        private ProcessOrderResponse HandleCancelled(ICart cart, IPayment payment, IVippsPaymentDetails paymentDetails, string orderId)
        {
            if (payment == null)
            {
                AddNote(cart, orderId, paymentDetails);
                return(new ProcessOrderResponse
                {
                    ProcessResponseErrorType = ProcessResponseErrorType.NONE
                });
            }

            payment.Status = PaymentStatus.Failed.ToString();
            _orderRepository.Save(cart);
            return(new ProcessOrderResponse
            {
                ProcessResponseErrorType = ProcessResponseErrorType.NONE
            });
        }
 private void EnsureExpressPaymentAndShipping(ICart cart, IPayment payment, IVippsPaymentDetails paymentDetails, IVippsUserDetails userDetails)
 {
     EnsureShipping(cart, userDetails);
     EnsurePayment(payment, cart, paymentDetails, userDetails);
 }
        private ProcessOrderResponse ProcessPayment(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, ICart cart)
        {
            ProcessOrderResponse response;

            var payment = cart.GetFirstPayment(x =>
                                               x.IsVippsPayment() &&
                                               x.TransactionID == orderId &&
                                               x.TransactionType.Equals(nameof(TransactionType.Authorization)));

            if (TransactionSuccess(paymentDetails))
            {
                response = HandleSuccess(cart, payment, paymentDetails, vippsUserDetails, orderId);
            }
            else if (TransactionCancelled(paymentDetails))
            {
                response = HandleCancelled(cart, payment, paymentDetails, orderId);
            }
            else if (TransactionFailed(paymentDetails))
            {
                response = HandleFailed(cart, payment, paymentDetails, orderId);
            }
            else
            {
                response = new ProcessOrderResponse
                {
                    ProcessResponseErrorType = ProcessResponseErrorType.OTHER,
                    ErrorMessage             = $"No action taken on order id: {orderId}."
                };
            }

            return(EnsurePaymentType(response, cart));
        }
        private ProcessOrderResponse GetCartThenProcessPayment(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName)
        {
            var cart          = _vippsService.GetCartByContactId(contactId, marketId, cartName);
            var errorResponse = ValidateCartAndSetProcessing(orderId, cart);

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

            return(ProcessPayment(vippsUserDetails, paymentDetails, orderId, cart));
        }
        private ProcessOrderResponse CheckDependenciesThenProcessPayment(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName)
        {
            var purchaseOrderResponse = EnsureNoPurchaseOrder(orderId);

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

            return(GetCartThenProcessPayment(vippsUserDetails, paymentDetails, orderId, contactId, marketId, cartName));
        }
        private ProcessOrderResponse ProcessOrder(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, ICart cart)
        {
            var lockInfo = GetLock(orderId, cart);

            return(ProcessLocked(lockInfo, () => ProcessPayment(vippsUserDetails, paymentDetails, orderId, cart)));
        }
        private ProcessOrderResponse ProcessOrder(IVippsUserDetails vippsUserDetails, IVippsPaymentDetails paymentDetails, string orderId, Guid contactId, string marketId, string cartName)
        {
            var lockInfo = GetLock(orderId, contactId, marketId, cartName);

            return(ProcessLocked(lockInfo, () => CheckDependenciesThenProcessPayment(vippsUserDetails, paymentDetails, orderId, contactId, marketId, cartName)));
        }
Esempio n. 15
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);
                }
            }
        }