Beispiel #1
0
        public static AutoPayResult ParseAutoPayResult(string xmlText)
        {
            var result = new AutoPayResult {
                RawXml = xmlText
            };
            var root = result.GetRootElement();

            ParseTransactionResult(result, root);
            foreach (var node in root.Elements())
            {
                var value = node.Value;
                var name  = node.Name.LocalName.ToLower();
                switch (name)
                {
                case "paymentmethod":
                    result.PaymentMethod = value;
                    break;
                }
            }

            return(result);
        }
        /// <summary>
        /// This method is always invoked right before a customer places an order.
        /// Use it when you need to process a payment before an order is stored into database.
        /// For example, capture or authorize credit card. Usually this method is used when a customer
        /// is not redirected to third-party site for completing a payment and all payments
        /// are handled on your site (for example, PayPal Direct).
        /// </summary>
        /// <param name="processPaymentRequest">Payment info required for an order processing</param>
        /// <returns>Process payment result</returns>
        public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            var result = new ProcessPaymentResult();

            result.NewPaymentStatus = PaymentStatus.Pending;

            // Use an existing agreement to make the payment, if the customer chose this option.
            if (!processPaymentRequest.CustomValues.TryGetValue(AgreementRefKey, out object agreementRefObject) ||
                agreementRefObject == null)
            {
                return(result);
            }

            var agreementRef = TryGetAgreementRef(processPaymentRequest.CustomValues);

            if (!string.IsNullOrEmpty(agreementRef) &&
                agreementRef != "new")
            {
                string currencyCode = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)
                                      .CurrencyCode;
                string         description = string.Format("{0} - Order", _storeContext.CurrentStore.Name);
                AutoPayRequest request     = new AutoPayRequest
                {
                    PurchaseOperation = GetPurchaseOperation(),
                    Amount            = processPaymentRequest.OrderTotal,
                    CurrencyCode      = currencyCode,
                    OrderID           = processPaymentRequest.OrderGuid.ToString(),
                    ProductNumber     = "ncOrder",
                    Description       = description,
                    AgreementRef      = agreementRef,
                };
                PayexInterface payex         = GetPayexInterface();
                AutoPayResult  autopayResult = payex.AutoPay(request).GetAwaiter().GetResult();

                // Check result and set new payment status
                if (autopayResult.IsTransactionSuccessful)
                {
                    result.SubscriptionTransactionId = agreementRef;
                    if (autopayResult.TransactionStatus.Value == Enumerations.TransactionStatusCode.Authorize)
                    {
                        result.NewPaymentStatus               = PaymentStatus.Authorized;
                        result.AuthorizationTransactionId     = autopayResult.TransactionNumber;
                        result.AuthorizationTransactionResult = autopayResult.ErrorCode;
                    }
                    else if (autopayResult.TransactionStatus.Value == Enumerations.TransactionStatusCode.Sale)
                    {
                        result.NewPaymentStatus         = PaymentStatus.Paid;
                        result.CaptureTransactionId     = autopayResult.TransactionNumber;
                        result.CaptureTransactionResult = autopayResult.ErrorCode;
                    }
                }
                else
                {
                    _logger.Error(
                        string.Format("PayEx: AutoPay failed for order {0}.", processPaymentRequest.OrderGuid),
                        new NopException(autopayResult.GetErrorDescription()));
                }
            }

            return(result);
        }