예제 #1
0
        public PaymentStepResult ProcessPayment(IPayment payment)
        {
            try
            {
                Logger.Debug("Klarna checkout gateway. Processing Payment ....");

                if (_orderForm == null)
                {
                    _orderForm = OrderGroup.Forms.FirstOrDefault(form => form.Payments.Contains(payment));
                }

                var authorizePaymentStep = new AuthorizePaymentStep(payment, OrderGroup.MarketId, KlarnaOrderServiceFactory);
                var capturePaymentStep   = new CapturePaymentStep(payment, OrderGroup.MarketId, KlarnaOrderServiceFactory);
                var creditPaymentStep    = new CreditPaymentStep(payment, OrderGroup.MarketId, KlarnaOrderServiceFactory);
                var cancelPaymentStep    = new CancelPaymentStep(payment, OrderGroup.MarketId, KlarnaOrderServiceFactory);

                authorizePaymentStep.SetSuccessor(capturePaymentStep);
                capturePaymentStep.SetSuccessor(creditPaymentStep);
                creditPaymentStep.SetSuccessor(cancelPaymentStep);

                return(AsyncHelper.RunSync(() => authorizePaymentStep.Process(payment, _orderForm, OrderGroup, OrderGroup.GetFirstShipment())));
            }
            catch (Exception ex)
            {
                Logger.Error("Process checkout failed with error: " + ex.Message, ex);

                var paymentStepResult = new PaymentStepResult {
                    Message = ex.Message
                };
                return(paymentStepResult);
            }
        }
예제 #2
0
        private PaymentStepResult ProcessFraudUpdate(IPayment payment, IOrderGroup orderGroup)
        {
            var paymentStepResult = new PaymentStepResult();

            if (payment.HasFraudStatus(NotificationFraudStatus.FRAUD_RISK_ACCEPTED))
            {
                payment.Status = PaymentStatus.Processed.ToString();
                OrderStatusManager.ReleaseHoldOnOrder((PurchaseOrder)orderGroup);
                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, "Klarna fraud risk accepted");
                paymentStepResult.Status = true;
                return(paymentStepResult);
            }

            if (payment.HasFraudStatus(NotificationFraudStatus.FRAUD_RISK_REJECTED))
            {
                payment.Status = PaymentStatus.Failed.ToString();
                OrderStatusManager.HoldOrder((PurchaseOrder)orderGroup);
                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, "Klarna fraud risk rejected");
                paymentStepResult.Status = true;
                return(paymentStepResult);
            }

            if (payment.HasFraudStatus(NotificationFraudStatus.FRAUD_RISK_STOPPED))
            {
                payment.Status = PaymentStatus.Failed.ToString();
                OrderStatusManager.HoldOrder((PurchaseOrder)orderGroup);
                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, "Klarna fraud risk stopped");
                paymentStepResult.Status = true;
                return(paymentStepResult);
            }

            paymentStepResult.Message = $"Can't process authorization. Unknown fraud notification: {payment.Properties[Common.Constants.FraudStatusPaymentField]} or no fraud notifications received so far.";

            return(paymentStepResult);
        }
        public override PaymentStepResult ProcessAuthorization(IPayment payment, IOrderGroup orderGroup)
        {
            var paymentStepResult = new PaymentStepResult();

            var orderId = orderGroup.Properties[Constants.SwedbankPayOrderIdField]?.ToString();

            if (!string.IsNullOrEmpty(orderId))
            {
                try
                {
                    var result      = AsyncHelper.RunSync(() => SwedbankPayClient.PaymentOrders.Get(new Uri(orderId, UriKind.Relative), Sdk.PaymentOrders.PaymentOrderExpand.All));
                    var transaction = result.PaymentOrderResponse.CurrentPayment.Payment.Transactions.TransactionList?.FirstOrDefault();
                    if (transaction != null)
                    {
                        payment.ProviderTransactionID = transaction.Number;
                        AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Authorize completed at SwedbankPay, Transaction number: {transaction.Number}");
                        paymentStepResult.Status = true;
                    }
                }
                catch (Exception ex)
                {
                    payment.Status            = PaymentStatus.Failed.ToString();
                    paymentStepResult.Message = ex.Message;
                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {ex.Message}");
                    Logger.Error(ex.Message, ex);
                }

                return(paymentStepResult);
            }

            return(paymentStepResult);
        }
        public PaymentStepResult ProcessPayment(IPayment payment, IShipment shipment)
        {
            var paymentStepResult = new PaymentStepResult();

            _shipment = shipment;

            if (_orderForm == null)
            {
                _orderForm = (payment as Payment)?.Parent ?? OrderGroup?.Forms.FirstOrDefault(form => form.Payments.Contains(payment));
            }
            if (OrderGroup == null)
            {
                OrderGroup = (_orderForm as OrderForm)?.Parent;
            }

            if (OrderGroup == null)
            {
                paymentStepResult.Message = "OrderGroup is null";
                throw new Exception(paymentStepResult.Message);
            }

            try
            {
                Logger.Debug("SwedbankPay checkout gateway. Processing Payment ....");

                if (_orderForm == null)
                {
                    _orderForm = OrderGroup.Forms.FirstOrDefault(form => form.Payments.Contains(payment));
                }

                var market = MarketService.GetMarket(OrderGroup.MarketId);
                var authorizePaymentStep = new AuthorizePaymentStep(payment, market, SwedbankPayClientFactory);
                var capturePaymentStep   = new CapturePaymentStep(payment, market, SwedbankPayClientFactory, RequestFactory);
                var creditPaymentStep    = new CreditPaymentStep(payment, market, SwedbankPayClientFactory, RequestFactory);
                var cancelPaymentStep    = new CancelPaymentStep(payment, market, SwedbankPayClientFactory, RequestFactory);

                authorizePaymentStep.SetSuccessor(capturePaymentStep);
                capturePaymentStep.SetSuccessor(creditPaymentStep);
                creditPaymentStep.SetSuccessor(cancelPaymentStep);

                return(authorizePaymentStep.Process(payment, _orderForm, OrderGroup, _shipment));
            }
            catch (Exception ex)
            {
                Logger.Error("Process checkout failed with error: " + ex.Message, ex);
                paymentStepResult.Message = ex.Message;
                throw;
            }
        }
예제 #5
0
        public override async Task <PaymentStepResult> ProcessAuthorization(IPayment payment, IOrderGroup orderGroup)
        {
            var paymentStepResult = new PaymentStepResult();

            var authorizationToken = payment.Properties[Constants.AuthorizationTokenPaymentField]?.ToString();

            if (!string.IsNullOrEmpty(authorizationToken))
            {
                try
                {
                    var result = await _klarnaPaymentService.CreateOrder(authorizationToken, orderGroup as ICart)
                                 .ConfigureAwait(false);

                    orderGroup.Properties[Common.Constants.KlarnaOrderIdField]      = result.OrderId;
                    payment.Properties[Common.Constants.FraudStatusPaymentField]    = result.FraudStatus;
                    payment.Properties[Constants.KlarnaConfirmationUrlPaymentField] = result.RedirectUrl;

                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType,
                                          $"Order created at Klarna, order id: {result.OrderId}, fraud status: {result.FraudStatus}");

                    if (result.FraudStatus == FraudStatus.REJECTED)
                    {
                        paymentStepResult.Message = "Klarna fraud status rejected";
                        payment.Status            = PaymentStatus.Failed.ToString();
                    }
                    else
                    {
                        paymentStepResult.Status = true;
                    }
                }
                catch (Exception ex) when(ex is ApiException || ex is WebException || ex is AggregateException)
                {
                    var exceptionMessage = GetExceptionMessage(ex);

                    payment.Status            = PaymentStatus.Failed.ToString();
                    paymentStepResult.Message = exceptionMessage;
                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {exceptionMessage}");
                    Logger.Error(exceptionMessage, ex);
                    paymentStepResult.Status = false;
                }
            }
            else
            {
                paymentStepResult.Message = "authorizationToken is null or empty";
            }
            return(paymentStepResult);
        }
        public override PaymentStepResult Process(IPayment payment, IOrderForm orderForm, IOrderGroup orderGroup, IShipment shipment)
        {
            if (payment.TransactionType != TransactionType.Authorization.ToString())
            {
                var paymentStepResult = new PaymentStepResult();
                if (Successor != null)
                {
                    paymentStepResult        = Successor.Process(payment, orderForm, orderGroup, shipment);
                    paymentStepResult.Status = true;
                    paymentStepResult.Status = Successor != null && paymentStepResult.Status;
                }

                return(paymentStepResult);
            }

            return(ProcessAuthorization(payment, orderGroup));
        }
예제 #7
0
        public override async Task <PaymentStepResult> Process(IPayment payment, IOrderForm orderForm, IOrderGroup orderGroup, IShipment shipment)
        {
            var paymentStepResult = new PaymentStepResult();

            if (payment.TransactionType == TransactionType.Credit.ToString())
            {
                try
                {
                    var orderId = orderGroup.Properties[Common.Constants.KlarnaOrderIdField]?.ToString();
                    if (!string.IsNullOrEmpty(orderId))
                    {
                        var purchaseOrder = orderGroup as IPurchaseOrder;
                        if (purchaseOrder != null)
                        {
                            var returnForm = purchaseOrder.ReturnForms.FirstOrDefault(x => ((OrderForm)x).Status == ReturnFormStatus.Complete.ToString() && ((OrderForm)x).ObjectState == MetaObjectState.Modified);

                            if (returnForm != null)
                            {
                                await KlarnaOrderService.Refund(orderId, orderGroup, (OrderForm)returnForm, payment).ConfigureAwait(false);
                            }
                        }
                        payment.Status = PaymentStatus.Processed.ToString();

                        AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Refunded {payment.Amount}");

                        paymentStepResult.Status = true;
                    }
                }
                catch (Exception ex) when(ex is ApiException || ex is WebException || ex is AggregateException)
                {
                    var exceptionMessage = GetExceptionMessage(ex);

                    payment.Status = PaymentStatus.Failed.ToString();
                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {exceptionMessage}");
                    Logger.Error(exceptionMessage, ex);

                    paymentStepResult.Message = exceptionMessage;
                }
            }
            else if (Successor != null)
            {
                return(await Successor.Process(payment, orderForm, orderGroup, shipment).ConfigureAwait(false));
            }

            return(paymentStepResult);
        }
        public virtual PaymentProcessingResult ProcessPayment(IOrderGroup orderGroup, IPayment payment)
        {
            PaymentStepResult stepResult;

            try
            {
                Logger.Debug("Netaxept checkout gateway. Processing Payment ....");

                if (_orderForm == null)
                {
                    _orderForm = orderGroup.Forms.FirstOrDefault(form => form.Payments.Contains(payment));
                }

                var registerPaymentStep      = new RegisterPaymentStep(payment);
                var authorizationPaymentStep = new AuthorizationPaymentStep(payment);
                var capturePaymentStep       = new CapturePaymentStep(payment);
                var creditPaymentStep        = new CreditPaymentStep(payment);
                var annulPaymentStep         = new AnnulPaymentStep(payment);

                registerPaymentStep.SetSuccessor(authorizationPaymentStep);
                authorizationPaymentStep.SetSuccessor(capturePaymentStep);
                capturePaymentStep.SetSuccessor(creditPaymentStep);
                creditPaymentStep.SetSuccessor(annulPaymentStep);

                stepResult = registerPaymentStep.Process(payment, _orderForm, orderGroup);
            }
            catch (Exception exception)
            {
                Logger.Error("Process payment failed with error: " + exception.Message, exception);
                stepResult = new PaymentStepResult {
                    IsSuccessful = false, Message = exception.Message
                };
            }

            if (stepResult.IsSuccessful)
            {
                return(PaymentProcessingResult.CreateSuccessfulResult(stepResult.Message, stepResult.RedirectUrl));
            }

            // COM-10168 Payment: bug in Commerce 12 but fixed in Commerce 13.13. The unsuccessfull result is ignored by Episerver and payment is set to processed. When package
            // is made compatible for Commerce 13 this code can be removed and the commented out code can be restored.
            // return PaymentProcessingResult.CreateUnsuccessfulResult(stepResult.Message);

            throw new PaymentException(PaymentException.ErrorType.ProviderError, string.Empty, stepResult.Message);
        }
예제 #9
0
        public override async Task<PaymentStepResult> ProcessAuthorization(IPayment payment, IOrderGroup orderGroup)
        {
            var paymentStepResult = new PaymentStepResult();
            var orderId = orderGroup.Properties[Constants.KlarnaCheckoutOrderIdCartField]?.ToString();

            try
            {
                var result = await KlarnaOrderService.GetOrder(orderId).ConfigureAwait(false);

                if (result != null)
                {
                    payment.Properties[Common.Constants.FraudStatusPaymentField] = result.FraudStatus;

                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Fraud status: {result.FraudStatus}");

                    if (result.FraudStatus == OrderManagementFraudStatus.REJECTED)
                    {
                        paymentStepResult.Message = "Klarna fraud status rejected";
                        payment.Status = PaymentStatus.Failed.ToString();
                    }
                    else
                    {
                        paymentStepResult.Status = true;
                    }
                }

                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, "Authorize completed");
            }
            catch (Exception ex) when (ex is ApiException || ex is WebException || ex is AggregateException)
            {
                var exceptionMessage = GetExceptionMessage(ex);

                payment.Status = PaymentStatus.Failed.ToString();
                paymentStepResult.Message = exceptionMessage;
                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {exceptionMessage}");
                Logger.Error(exceptionMessage, ex);
            }

            return paymentStepResult;
        }
        public virtual PaymentProcessingResult ProcessPayment(IOrderGroup orderGroup, IPayment payment)
        {
            PaymentStepResult stepResult;

            try
            {
                Logger.Debug("Netaxept checkout gateway. Processing Payment ....");

                if (_orderForm == null)
                {
                    _orderForm = orderGroup.Forms.FirstOrDefault(form => form.Payments.Contains(payment));
                }

                var registerPaymentStep = new RegisterPaymentStep(payment);
                var capturePaymentStep  = new CapturePaymentStep(payment);
                var creditPaymentStep   = new CreditPaymentStep(payment);
                var annulPaymentStep    = new AnnulPaymentStep(payment);

                registerPaymentStep.SetSuccessor(capturePaymentStep);
                capturePaymentStep.SetSuccessor(creditPaymentStep);
                creditPaymentStep.SetSuccessor(annulPaymentStep);

                stepResult = registerPaymentStep.Process(payment, _orderForm, orderGroup);
            }
            catch (Exception exception)
            {
                Logger.Error("Process payment failed with error: " + exception.Message, exception);
                stepResult = new PaymentStepResult {
                    IsSuccessful = false, Message = exception.Message
                };
            }

            if (stepResult.IsSuccessful)
            {
                return(PaymentProcessingResult.CreateSuccessfulResult(stepResult.Message, stepResult.RedirectUrl));
            }

            return(PaymentProcessingResult.CreateUnsuccessfulResult(stepResult.Message));
        }
예제 #11
0
        public override async Task <PaymentStepResult> Process(IPayment payment, IOrderForm orderForm, IOrderGroup orderGroup, IShipment shipment)
        {
            var paymentStepResult = new PaymentStepResult();

            if (payment.TransactionType == TransactionType.Void.ToString())
            {
                try
                {
                    var orderId = orderGroup.Properties[Common.Constants.KlarnaOrderIdField]?.ToString();
                    if (!string.IsNullOrEmpty(orderId))
                    {
                        await KlarnaOrderService.CancelOrder(orderId).ConfigureAwait(false);

                        payment.Status = PaymentStatus.Processed.ToString();

                        AddNoteAndSaveChanges(orderGroup, payment.TransactionType, "Order cancelled at Klarna");

                        paymentStepResult.Status = true;
                    }
                }
                catch (Exception ex) when(ex is ApiException || ex is WebException)
                {
                    var exceptionMessage = GetExceptionMessage(ex);

                    payment.Status = PaymentStatus.Failed.ToString();
                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {exceptionMessage}");
                    Logger.Error(exceptionMessage, ex);

                    paymentStepResult.Message = exceptionMessage;
                }
            }
            else if (Successor != null)
            {
                return(await Successor.Process(payment, orderForm, orderGroup, shipment).ConfigureAwait(false));
            }

            return(paymentStepResult);
        }
예제 #12
0
        /// <summary>
        /// When an order needs to be captured the cartridge needs to make an API Call
        /// POST /ordermanagement/v1/orders/{order_id}/captures for the amount which needs to be captured.
        ///     If it's a partial capture the API call will be the same just for the
        /// amount that should be captured. Many captures can be made up to the whole amount authorized.
        /// The shipment information can be added in this call or amended afterwards using the method
        /// "Add shipping info to a capture".
        ///     The amount captured never can be greater than the authorized.
        /// When an order is Partially Captured the status of the order in Klarna is PART_CAPTURED
        /// </summary>
        public override async Task <PaymentStepResult> Process(IPayment payment, IOrderForm orderForm, IOrderGroup orderGroup, IShipment shipment)
        {
            var paymentStepResult = new PaymentStepResult();

            if (payment.TransactionType == TransactionType.Capture.ToString())
            {
                var amount  = AmountHelper.GetAmount(payment.Amount);
                var orderId = orderGroup.Properties[Common.Constants.KlarnaOrderIdField]?.ToString();
                if (!string.IsNullOrEmpty(orderId))
                {
                    try
                    {
                        var captureData = await KlarnaOrderService.CaptureOrder(orderId, amount, "Capture the payment", orderGroup, orderForm, payment, shipment).ConfigureAwait(false);

                        AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Captured {payment.Amount}, id {captureData.CaptureId}");
                        paymentStepResult.Status = true;
                    }
                    catch (Exception ex) when(ex is ApiException || ex is WebException || ex is AggregateException)
                    {
                        var exceptionMessage = GetExceptionMessage(ex);

                        payment.Status = PaymentStatus.Failed.ToString();

                        AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {exceptionMessage}");
                        Logger.Error(exceptionMessage, ex);

                        paymentStepResult.Message = exceptionMessage;
                    }
                }
            }
            else if (Successor != null)
            {
                return(await Successor.Process(payment, orderForm, orderGroup, shipment).ConfigureAwait(false));
            }

            return(paymentStepResult);
        }
        public override PaymentStepResult Process(IPayment payment, IOrderForm orderForm, IOrderGroup orderGroup, IShipment shipment)
        {
            var paymentStepResult = new PaymentStepResult();

            if (payment.TransactionType == TransactionType.Void.ToString())
            {
                try
                {
                    var orderId         = orderGroup.Properties[Constants.SwedbankPayOrderIdField]?.ToString();
                    var previousPayment = orderForm.Payments.FirstOrDefault(x => x.IsSwedbankPayPayment());

                    //If payed by swish, do a reversal
                    if (previousPayment != null && previousPayment.TransactionType == TransactionType.Sale.ToString() && !string.IsNullOrWhiteSpace(orderId))
                    {
                        var paymentOrder = AsyncHelper.RunSync(() => SwedbankPayClient.PaymentOrders.Get(new Uri(orderId, UriKind.Relative)));
                        if (paymentOrder.Operations.Reverse == null)
                        {
                            paymentStepResult.Message = "Reversal is not a valid operation";

                            AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"{paymentStepResult.Message}");
                        }
                        else
                        {
                            var reversalRequest  = _requestFactory.GetReversalRequest(payment, orderForm.GetAllLineItems(), _market, shipment, description: "Cancelling purchase order");
                            var reversalResponse = AsyncHelper.RunSync(() => paymentOrder.Operations.Reverse(reversalRequest));
                            if (reversalResponse.Reversal.Transaction.Type == Sdk.TransactionType.Reversal)
                            {
                                payment.Status = PaymentStatus.Processed.ToString();
                                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Refunded {payment.Amount}");
                                paymentStepResult.Status = true;
                                return(paymentStepResult);
                            }
                            else
                            {
                                paymentStepResult.Message = "Error when executing reversal";
                                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {paymentStepResult.Message}");
                            }
                        }
                    }

                    else if (!string.IsNullOrWhiteSpace(orderId))
                    {
                        var paymentOrder = AsyncHelper.RunSync(() => SwedbankPayClient.PaymentOrders.Get(new Uri(orderId, UriKind.Relative)));
                        if (paymentOrder.Operations.Cancel == null)
                        {
                            AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Cancel is not possible on this order {orderId}");
                            return(paymentStepResult);
                        }

                        var cancelRequest  = _requestFactory.GetCancelRequest();
                        var cancelResponse = AsyncHelper.RunSync(() => paymentOrder.Operations.Cancel(cancelRequest));
                        if (cancelResponse.Cancellation.Transaction.Type == Sdk.TransactionType.Cancellation && cancelResponse.Cancellation.Transaction.State.Equals(State.Completed))
                        {
                            payment.Status                = PaymentStatus.Processed.ToString();
                            payment.TransactionID         = cancelResponse.Cancellation.Transaction.Number;
                            payment.ProviderTransactionID = cancelResponse.Cancellation.Transaction.Id.ToString();
                            AddNoteAndSaveChanges(orderGroup, payment.TransactionType, "Order cancelled at SwedbankPay");
                            return(paymentStepResult);
                        }
                    }

                    return(paymentStepResult);
                }
                catch (Exception ex)
                {
                    payment.Status            = PaymentStatus.Failed.ToString();
                    paymentStepResult.Message = ex.Message;
                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {ex.Message}");
                    Logger.Error(ex.Message, ex);
                }
            }

            if (Successor != null)
            {
                return(Successor.Process(payment, orderForm, orderGroup, shipment));
            }

            return(paymentStepResult);
        }
예제 #14
0
        public override PaymentStepResult Process(IPayment payment, IOrderForm orderForm, IOrderGroup orderGroup, IShipment shipment)
        {
            var paymentStepResult = new PaymentStepResult();

            if (payment.TransactionType == TransactionType.Credit.ToString())
            {
                try
                {
                    var orderId = orderGroup.Properties[Constants.SwedbankPayOrderIdField]?.ToString();
                    if (!string.IsNullOrEmpty(orderId))
                    {
                        if (orderGroup is IPurchaseOrder purchaseOrder)
                        {
                            var returnForm = purchaseOrder.ReturnForms.FirstOrDefault(x => ((OrderForm)x).Status == ReturnFormStatus.Complete.ToString() && ((OrderForm)x).ObjectState == MetaObjectState.Modified);

                            if (returnForm != null)
                            {
                                var transactionDescription = string.IsNullOrWhiteSpace(returnForm.ReturnComment)
                                        ? "Reversing payment."
                                        : returnForm.ReturnComment;


                                var reversalRequest = _requestFactory.GetReversalRequest(payment, returnForm.GetAllReturnLineItems(), _market, returnForm.Shipments.FirstOrDefault(), description: transactionDescription);
                                var paymentOrder    = AsyncHelper.RunSync(() => SwedbankPayClient.PaymentOrders.Get(new Uri(orderId, UriKind.Relative)));

                                if (paymentOrder.Operations.Reverse == null)
                                {
                                    payment.Status            = PaymentStatus.Failed.ToString();
                                    paymentStepResult.Message = "Reversal is not a valid operation";
                                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {paymentStepResult.Message}");
                                    Logger.Error($"Reversal is not a valid operation for {orderId}");
                                    return(paymentStepResult);
                                }

                                var reversalResponse = AsyncHelper.RunSync(() => paymentOrder.Operations.Reverse(reversalRequest));
                                if (reversalResponse.Reversal.Transaction.Type == Sdk.TransactionType.Reversal && reversalResponse.Reversal.Transaction.State.Equals(State.Completed))
                                {
                                    payment.Status                = PaymentStatus.Processed.ToString();
                                    payment.TransactionID         = reversalResponse.Reversal.Transaction.Number;
                                    payment.ProviderTransactionID = reversalResponse.Reversal.Transaction.Id.ToString();
                                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Refunded {payment.Amount}");

                                    paymentStepResult.Status = true;
                                }
                            }
                        }

                        return(paymentStepResult);
                    }
                }
                catch (Exception ex)
                {
                    payment.Status            = PaymentStatus.Failed.ToString();
                    paymentStepResult.Message = ex.Message;
                    AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {ex.Message}");
                    Logger.Error(ex.Message, ex);
                }
            }
            else if (Successor != null)
            {
                return(Successor.Process(payment, orderForm, orderGroup, shipment));
            }

            return(paymentStepResult);
        }
예제 #15
0
        public override PaymentStepResult Process(IPayment payment, IOrderForm orderForm, IOrderGroup orderGroup, IShipment shipment)
        {
            var paymentStepResult = new PaymentStepResult();

            if (payment.TransactionType == TransactionType.Capture.ToString())
            {
                var orderId = orderGroup.Properties[Constants.SwedbankPayOrderIdField]?.ToString();
                if (!string.IsNullOrEmpty(orderId))
                {
                    long?remainingCaptureAmount = null;
                    try
                    {
                        if (shipment == null)
                        {
                            throw new InvalidOperationException("Can't find correct shipment");
                        }

                        var paymentOrder = AsyncHelper.RunSync(() => SwedbankPayClient.PaymentOrders.Get(new Uri(orderId, UriKind.Relative)));
                        if (paymentOrder.Operations.Capture == null)
                        {
                            remainingCaptureAmount = paymentOrder.PaymentOrderResponse.RemainingCaptureAmount?.Value;
                            if (!remainingCaptureAmount.HasValue || remainingCaptureAmount.Value == 0)
                            {
                                AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Capture is not possible on this order {orderId}, capture already performed");
                                paymentStepResult.Status = true;
                                return(paymentStepResult);
                            }

                            AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Capture is not possible on this order {orderId}");
                            return(paymentStepResult);
                        }

                        var captureRequest  = _requestFactory.GetCaptureRequest(payment, _market, shipment);
                        var captureResponse = AsyncHelper.RunSync(() => paymentOrder.Operations.Capture(captureRequest));

                        if (captureResponse.Capture.Transaction.Type == Sdk.TransactionType.Capture && captureResponse.Capture.Transaction.State.Equals(State.Completed))
                        {
                            payment.ProviderTransactionID = captureResponse.Capture.Transaction.Number;
                            AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Order captured at SwedbankPay, Transaction number: {captureResponse.Capture.Transaction.Number}");
                            paymentStepResult.Status = true;
                        }

                        return(paymentStepResult);
                    }
                    catch (Exception ex)
                    {
                        payment.Status            = PaymentStatus.Failed.ToString();
                        paymentStepResult.Message = ex.Message;
                        AddNoteAndSaveChanges(orderGroup, payment.TransactionType, $"Error occurred {ex.Message}, remaining capture amount: {remainingCaptureAmount}");
                        Logger.Error(ex.Message, ex);
                    }
                }

                return(paymentStepResult);
            }

            if (Successor != null)
            {
                return(Successor.Process(payment, orderForm, orderGroup, shipment));
            }

            return(paymentStepResult);
        }