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);
        }
Exemplo n.º 2
0
        public virtual PaymentProcessingResult Refund(
            IOrderGroup orderGroup,
            IPayment payment)
        {
            var configuration = _configurationLoader.GetConfiguration(orderGroup.MarketId);

            try
            {
                var orderId    = payment.TransactionID;
                var serviceApi = _vippsServiceApiFactory.Create(configuration);

                var refundPaymentRequest =
                    _requestFactory.CreateUpdatePaymentRequest(payment, configuration);

                var response = AsyncHelper.RunSync(() => serviceApi.Refund(orderId, refundPaymentRequest));

                var creditTotal = orderGroup.GetFirstForm().Payments.Where(x => x.TransactionType == "Credit")
                                  .Sum(x => x.Amount).FormatAmountToVipps();

                if (response.TransactionSummary.RefundedAmount == creditTotal)
                {
                    OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType,
                                                          $"{payment.Amount} kr refunded on vipps order {orderId}");
                    return(PaymentProcessingResult.CreateSuccessfulResult(
                               $"{payment.Amount} kr captured on refunded order {orderId}"));
                }

                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType,
                                                      $"Vipps refund payment failed. Order status: {response.TransactionInfo.Status}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult(
                           $"Vipps refund payment failed. Order status: {response.TransactionInfo.Status}"));
            }

            catch (ApiException apiException)
            {
                var errorMessage = GetErrorMessage(apiException);
                _logger.Log(Level.Error, $"Vipps Refund failed: {errorMessage}");
                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType,
                                                      $"Vipps refund payment 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 refund payment failed. Error message: {ex.Message}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult(ex.Message));
            }
        }
Exemplo n.º 3
0
        public virtual PaymentProcessingResult Capture(
            IOrderGroup orderGroup,
            IPayment payment)
        {
            var configuration = _configurationLoader.GetConfiguration(orderGroup.MarketId);

            try
            {
                var orderId        = payment.TransactionID;
                var idempotencyKey = GetIdempotencyKey(orderGroup, payment, orderId);
                var serviceApi     = _vippsServiceApiFactory.Create(configuration);

                var capturePaymentRequest =
                    _requestFactory.CreateUpdatePaymentRequest(payment, configuration);

                var response = AsyncHelper.RunSync(() => serviceApi.Capture(orderId, capturePaymentRequest, idempotencyKey));

                if (response.TransactionInfo.Status == VippsUpdatePaymentResponseStatus.Captured.ToString())
                {
                    OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType,
                                                          $"{payment.Amount} kr captured on vipps order {orderId}");
                    return(PaymentProcessingResult.CreateSuccessfulResult(
                               $"{payment.Amount} kr captured on vipps order {orderId}"));
                }

                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType,
                                                      $"Vipps capture payment failed. Order status: {response.TransactionInfo.Status}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult(
                           $"Vipps capture payment failed. Order status: {response.TransactionInfo.Status}"));
            }

            catch (ApiException apiException)
            {
                var errorMessage = GetErrorMessage(apiException);
                _logger.Log(Level.Error, $"Vipps Capture failed: {errorMessage}");
                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType,
                                                      $"Vipps capture payment 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 capture payment failed. Error message: {ex.Message}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult(ex.Message));
            }
        }
        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));
            }
        }
        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));
        }
        public virtual async Task <PaymentProcessingResult> CancelAsync(IOrderGroup orderGroup, IPayment payment)
        {
            var configuration = _configurationLoader.GetConfiguration(orderGroup.MarketId);

            try
            {
                var orderId    = payment.TransactionID;
                var serviceApi = _vippsServiceApiFactory.Create(configuration);

                var cancelPaymentRequest =
                    _requestFactory.CreateUpdatePaymentRequest(payment, configuration);

                var response = await serviceApi.Cancel(orderId, cancelPaymentRequest).ConfigureAwait(false);

                if (response.TransactionInfo.Status == VippsUpdatePaymentResponseStatus.Cancelled.ToString())
                {
                    OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType, $"Payment cancelled for vipps order {orderId}");
                    return(PaymentProcessingResult.CreateSuccessfulResult($"{payment.Amount} Payment cancelled vipps order {orderId}"));
                }

                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType, $"Vipps cancel payment failed. Order status: {response.TransactionInfo.Status}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult($"Vipps cancel payment failed. Order status: {response.TransactionInfo.Status}"));
            }

            catch (ApiException apiException)
            {
                var errorMessage = GetErrorMessage(apiException);
                _logger.Log(Level.Error, $"Vipps cancel failed: {errorMessage}");
                OrderNoteHelper.AddNoteAndSaveChanges(orderGroup, payment, payment.TransactionType, $"Vipps cancel payment 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 cancel payment failed. Error message: {ex.Message}");
                return(PaymentProcessingResult.CreateUnsuccessfulResult(ex.Message));
            }
        }