Ejemplo n.º 1
0
        public bool ProcessSuccessfulTransaction(PayExPayment payExPayment, string orderNumber, string orderRef, Cart cart, out string transactionErrorCode)
        {
            transactionErrorCode = null;

            Log.InfoFormat("Processing a transaction for payment with ID:{0} belonging to order with ID: {1}. Order number: {2}. Order reference: {3}",
                           payExPayment.Id, payExPayment.OrderGroupId, orderNumber, orderRef);

            PaymentMethod currentPayment = _paymentMethodFactory.Create(payExPayment);

            if (currentPayment == null)
            {
                Log.ErrorFormat("As the PaymentMethod for payment with ID:{0} could not be resolved, it cannot be processed by the PayEx Payment Provider!", payExPayment.Id);
                return(false);
            }

            Log.InfoFormat("Completing payment with ID:{0} belonging to order with ID: {1}", payExPayment.Id, payExPayment.OrderGroupId);
            PaymentCompleteResult result = currentPayment.Complete(orderRef);

            transactionErrorCode = result.TransactionErrorCode;

            if (!result.Success)
            {
                Log.ErrorFormat("Could not complete payment with ID:{0} belonging to order with ID: {1}. TransactionErrorCode: {2}", payExPayment.Id, payExPayment.OrderGroupId, transactionErrorCode);
            }
            else
            {
                Log.InfoFormat("Successfully completed payment with ID:{0} belonging to order with ID: {1}", payExPayment.Id, payExPayment.OrderGroupId);
            }

            return(result.Success);
        }
        public bool Credit(PaymentMethod currentPayment)
        {
            Mediachase.Commerce.Orders.Payment payment = (Mediachase.Commerce.Orders.Payment)currentPayment.Payment;
            Log.InfoFormat("Crediting payment with ID:{0} belonging to order with ID: {1}, by order lines", payment.Id, payment.OrderGroupId);

            int transactionId;

            if (!int.TryParse(payment.AuthorizationCode, out transactionId))
            {
                Log.ErrorFormat("Could not get PayEx Transaction Id from purchase order with ID: {0}", currentPayment.PurchaseOrder.Id);
                return(false);
            }
            Log.InfoFormat("PayEx transaction ID is {0} on payment with ID:{1} belonging to order with ID: {2}", transactionId, payment.Id, payment.OrderGroupId);

            CreditResult result = null;

            foreach (OrderForm orderForm in currentPayment.PurchaseOrder.OrderForms)
            {
                foreach (LineItem lineItem in orderForm.LineItems)
                {
                    result = _paymentManager.CreditOrderLine(transactionId, lineItem.CatalogEntryId, currentPayment.PurchaseOrder.TrackingNumber);
                }
            }

            bool success = false;

            if (result != null && !string.IsNullOrWhiteSpace(result.TransactionNumber))
            {
                Log.InfoFormat("Setting PayEx transaction number to {0} on payment with ID:{1} belonging to order with ID: {2} during credit", result.TransactionNumber, payment.Id, payment.OrderGroupId);
                payment.TransactionID = result.TransactionNumber;
                payment.AcceptChanges();
                success = true;
                Log.InfoFormat("Successfully credited payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            }

            if (_paymentCreditor != null)
            {
                return(_paymentCreditor.Credit(currentPayment) && success);
            }
            return(success);
        }
        public PaymentCompleteResult Complete(PaymentMethod currentPayment, string orderRef)
        {
            Log.InfoFormat("Updating transaction details for payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            string transactionString = ((Mediachase.Commerce.Orders.Payment)currentPayment.Payment).AuthorizationCode;

            Log.InfoFormat("Transaction number is:{0} for payment with ID:{1} belonging to order with ID: {2}", transactionString, currentPayment.Payment.Id, currentPayment.OrderGroupId);

            int transactionNumber;

            if (!Int32.TryParse(transactionString, out transactionNumber))
            {
                Log.ErrorFormat("Could not parse Transaction number:{0} to an Int for payment with ID:{1} belonging to order with ID: {2}", transactionString, currentPayment.Payment.Id, currentPayment.OrderGroupId);
                return(new PaymentCompleteResult());
            }

            TransactionResult transactionDetails = _paymentManager.GetTransactionDetails(transactionNumber);
            bool updated = false;

            if (transactionDetails != null)
            {
                updated = UpdateOrderAddress(currentPayment, transactionDetails);
            }

            PaymentCompleteResult result = new PaymentCompleteResult {
                Success = true
            };

            if (_paymentCompleter != null)
            {
                result = _paymentCompleter.Complete(currentPayment, orderRef);
            }

            if (updated)
            {
                Log.InfoFormat("Successfully updated transaction details for payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            }

            result.Success = updated;
            return(result);
        }
        public PaymentCompleteResult Complete(PaymentMethod currentPayment, string orderRef)
        {
            Log.InfoFormat("Completing payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            CompleteResult completeResult = _paymentManager.Complete(orderRef);

            if (!completeResult.Success || string.IsNullOrWhiteSpace(completeResult.TransactionNumber))
            {
                return new PaymentCompleteResult {
                           TransactionErrorCode = completeResult.ErrorDetails != null ? completeResult.ErrorDetails.TransactionErrorCode : string.Empty
                }
            }
            ;

            if (completeResult.GetTransactionDetails)
            {
                Log.InfoFormat("Retrieving transaction details for payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);

                if (_paymentCompleter == null)
                {
                    _paymentCompleter = new UpdateTransactionDetails(null, _paymentManager);
                }
                _paymentCompleter = new UpdateTransactionDetails(_paymentCompleter, _paymentManager);
            }

            _paymentActions.UpdatePaymentInformation(currentPayment, completeResult.TransactionNumber, completeResult.PaymentMethod);

            PaymentCompleteResult result = new PaymentCompleteResult {
                Success = true
            };

            if (_paymentCompleter != null)
            {
                result = _paymentCompleter.Complete(currentPayment, orderRef);
            }

            Log.InfoFormat("Successfully completed payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            return(result);
        }
    }
        public PaymentCompleteResult Complete(PaymentMethod currentPayment, string orderRef)
        {
            Log.InfoFormat("Completing payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            CompleteResult completeResult = _paymentManager.Complete(orderRef);
            if (!completeResult.Success || string.IsNullOrWhiteSpace(completeResult.TransactionNumber))
                return new PaymentCompleteResult { TransactionErrorCode = completeResult.ErrorDetails != null ? completeResult.ErrorDetails.TransactionErrorCode : string.Empty };

            if (completeResult.GetTransactionDetails)
            {
                Log.InfoFormat("Retrieving transaction details for payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
                if (_paymentCompleter == null)
                    _paymentCompleter = new UpdateTransactionDetails(null, _paymentManager);
                _paymentCompleter = new UpdateTransactionDetails(_paymentCompleter, _paymentManager);
            }

            _paymentActions.UpdatePaymentInformation(currentPayment, completeResult.TransactionNumber, completeResult.PaymentMethod);

            PaymentCompleteResult result = new PaymentCompleteResult { Success = true };
            if (_paymentCompleter != null)
                result = _paymentCompleter.Complete(currentPayment, orderRef);

            Log.InfoFormat("Successfully completed payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            return result;
        }
        public bool Credit(PaymentMethod currentPayment)
        {
            Mediachase.Commerce.Orders.Payment payment = (Mediachase.Commerce.Orders.Payment)currentPayment.Payment;
            Log.InfoFormat("Crediting payment with ID:{0} belonging to order with ID: {1}, by order lines", payment.Id, payment.OrderGroupId);

            int transactionId;
            if (!int.TryParse(payment.AuthorizationCode, out transactionId))
            {
                Log.ErrorFormat("Could not get PayEx Transaction Id from purchase order with ID: {0}", currentPayment.PurchaseOrder.Id);
                return false;
            }
            Log.InfoFormat("PayEx transaction ID is {0} on payment with ID:{1} belonging to order with ID: {2}", transactionId, payment.Id, payment.OrderGroupId);

            CreditResult result = null;
            foreach (OrderForm orderForm in currentPayment.PurchaseOrder.OrderForms)
            {
                foreach (LineItem lineItem in orderForm.LineItems)
                {
                    result = _paymentManager.CreditOrderLine(transactionId, lineItem.CatalogEntryId, currentPayment.PurchaseOrder.TrackingNumber);
                }
            }

            bool success = false;
            if (result != null && !string.IsNullOrWhiteSpace(result.TransactionNumber))
            {
                Log.InfoFormat("Setting PayEx transaction number to {0} on payment with ID:{1} belonging to order with ID: {2} during credit", result.TransactionNumber, payment.Id, payment.OrderGroupId);
                payment.TransactionID = result.TransactionNumber;
                payment.AcceptChanges();
                success = true;
                Log.InfoFormat("Successfully credited payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            }

            if (_paymentCreditor != null)
                return _paymentCreditor.Credit(currentPayment) && success;
            return success;
        }
        private bool UpdateOrderAddress(PaymentMethod currentPayment, TransactionResult transactionDetails2)
        {
            Log.InfoFormat("Updating order address for payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
            if (!currentPayment.RequireAddressUpdate)
            {
                Log.InfoFormat("This payment method ({0}) does not require an order address update. Payment with ID:{1} belonging to order with ID: {2}",
                               currentPayment.PaymentMethodCode, currentPayment.Payment.Id, currentPayment.OrderGroupId);
                return(true);
            }

            Address newAddress = currentPayment.GetAddressFromPayEx(transactionDetails2);

            if (newAddress == null)
            {
                return(false);
            }

            Cart         cart            = currentPayment.Cart;
            OrderAddress shippingAddress = GetShippingAddress(cart);

            if (shippingAddress == null)
            {
                Log.ErrorFormat("Could not update address for payment with ID:{0} belonging to order with ID: {1}. Reason: Shipping address was not found!", currentPayment.Payment.Id, currentPayment.OrderGroupId);
                return(false);
            }

            Dictionary <string, string> propertiesToUpdate = new Dictionary <string, string>()
            {
                { GetPropertyName(() => shippingAddress.FirstName), newAddress.FirstName },
                { GetPropertyName(() => shippingAddress.LastName), newAddress.LastName },
                { GetPropertyName(() => shippingAddress.Line1), newAddress.Line1 },
                { GetPropertyName(() => shippingAddress.PostalCode), newAddress.PostCode },
                { GetPropertyName(() => shippingAddress.City), newAddress.City },
                { GetPropertyName(() => shippingAddress.CountryName), newAddress.Country },
                { GetPropertyName(() => shippingAddress.Email), newAddress.Email },
            };

            bool updated = UpdatePropertyValues(shippingAddress, propertiesToUpdate);

            if (!string.IsNullOrWhiteSpace(newAddress.Fullname))
            {
                Log.InfoFormat("Setting customer name of cart to {0} on payment with ID:{1} belonging to order with ID: {2}",
                               newAddress.Fullname, currentPayment.Payment.Id, currentPayment.OrderGroupId);
                cart.CustomerName = newAddress.Fullname;
            }

            try
            {
                if (updated)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        shippingAddress.AcceptChanges();
                        cart.AcceptChanges();
                        scope.Complete();
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                Log.Error("Could not update address for payment. See next log statement for more information", e);
                Log.ErrorFormat("Could not update address for payment with ID:{0} belonging to order with ID: {1}", currentPayment.Payment.Id, currentPayment.OrderGroupId);
                return(false);
            }
        }
Ejemplo n.º 8
0
        public override bool ProcessPayment(Mediachase.Commerce.Orders.Payment payment, ref string message)
        {
            Log.InfoFormat("Processing payment with ID:{0} belonging to order with ID: {1}", payment.Id, payment.OrderGroupId);

            if (HttpContext.Current == null)
            {
                Log.ErrorFormat("HttpContent.Current is null");
                return(false);
            }

            PaymentMethod currentPayment = _paymentMethodFactory.Create(payment);

            if (currentPayment == null)
            {
                Log.ErrorFormat("As the PaymentMethod for payment with ID:{0} could not be resolved, it cannot be processed by the PayEx Payment Provider!", payment.Id);
                return(false);
            }
            Log.InfoFormat("Successfully resolved the PaymentMethod for payment with ID:{0}. The PaymentMethodCode is {1}", payment.Id, currentPayment.PaymentMethodCode);

            if (currentPayment.IsPurchaseOrder)
            {
                Log.InfoFormat("Payment with ID:{0} is a purchase order. It's transaction type is {1}", payment.Id, payment.TransactionType);

                // when user click complete order in commerce manager the transaction type will be Capture
                if (currentPayment.IsCapture)
                {
                    Log.InfoFormat("Begin CapturePayment for payment with ID:{0}", payment.Id);
                    return(currentPayment.Capture());
                }

                // When "Refund" shipment in Commerce Manager, this method will be invoked with the TransactionType is Credit
                if (currentPayment.IsCredit)
                {
                    Log.InfoFormat("Begin CreditPayment for payment with ID:{0}", payment.Id);
                    return(currentPayment.Credit());
                }

                Log.ErrorFormat("The transaction type for payment with ID:{0} is {1}. The PayEx Payment Provider expected a Credit or Capture transaction type!", payment.Id, payment.TransactionType);
                return(false);
            }

            // When "Complete" or "Refund" shipment in Commerce Manager, this method will be run again with the TransactionType is Capture/Credit
            // PayEx will always return true to bypass the payment process again.
            if (!currentPayment.IsAuthorization)
            {
                Log.InfoFormat("The transaction type for payment with ID:{0} is {1}, meaning the payment process has already been run once.", payment.Id, payment.TransactionType);
                return(true);
            }

            if (!currentPayment.IsCart)
            {
                Log.ErrorFormat("Payment with ID:{0} is not a cart. That should not be possible at this stage!", payment.Id);
                return(false);
            }

            Log.InfoFormat("Initializing payment with ID:{0} belonging to order with ID: {1}", payment.Id, payment.OrderGroupId);
            PaymentInitializeResult result = currentPayment.Initialize();

            message = result.ErrorMessage ?? string.Empty;

            if (!result.Success)
            {
                Log.ErrorFormat("Could not initialize payment with ID:{0} belonging to order with ID: {1}. Message: {2}", payment.Id, payment.OrderGroupId, message);
            }
            else
            {
                Log.InfoFormat("Successfully initialized payment with ID:{0} belonging to order with ID: {1}", payment.Id, payment.OrderGroupId);
            }

            return(result.Success);
        }