/// <summary>
        /// Process a payment
        /// </summary>
        /// <param name="processPaymentRequest">Payment info required for an order processing</param>
        /// <returns>Process payment result</returns>
        public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            //try to get an order id from custom values
            var orderIdKey = _localizationService.GetResource("Plugins.Payments.PayPalSmartPaymentButtons.OrderId");

            if (!processPaymentRequest.CustomValues.TryGetValue(orderIdKey, out var orderId) || string.IsNullOrEmpty(orderId?.ToString()))
            {
                throw new NopException("Failed to get the PayPal order ID");
            }

            //authorize or capture the order
            var(order, error) = _settings.PaymentType == PaymentType.Capture
                ? _serviceManager.Capture(_settings, orderId.ToString())
                : (_settings.PaymentType == PaymentType.Authorize
                ? _serviceManager.Authorize(_settings, orderId.ToString())
                : (null, null));

            if (!string.IsNullOrEmpty(error))
            {
                return new ProcessPaymentResult {
                           Errors = new[] { error }
                }
            }
            ;

            //request succeeded
            var result = new ProcessPaymentResult();

            var purchaseUnit  = order.PurchaseUnits.FirstOrDefault(item => item.ReferenceId.Equals(processPaymentRequest.OrderGuid.ToString()));
            var authorization = purchaseUnit.Payments?.Authorizations?.FirstOrDefault();

            if (authorization != null)
            {
                result.AuthorizationTransactionId = authorization.Id;

                result.AuthorizationTransactionResult = authorization.Status;
                result.NewPaymentStatus = PaymentStatus.Authorized;
            }
            var capture = purchaseUnit.Payments?.Captures?.FirstOrDefault();

            if (capture != null)
            {
                result.CaptureTransactionId     = capture.Id;
                result.CaptureTransactionResult = capture.Status;
                result.NewPaymentStatus         = PaymentStatus.Paid;
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Process a payment
        /// </summary>
        /// <param name="processPaymentRequest">Payment info required for an order processing</param>
        /// <returns>Process payment result</returns>
        public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            //try to get an order id from custom values
            var orderIdKey = _localizationService.GetResource("Plugins.Payments.PayPalSmartPaymentButtons.OrderId");

            if (!processPaymentRequest.CustomValues.TryGetValue(orderIdKey, out var orderId) || string.IsNullOrEmpty(orderId?.ToString()))
            {
                throw new SmiException("Failed to get the PayPal order ID");
            }

            //authorize or capture the order
            var(order, error) = _settings.PaymentType == PaymentType.Capture
                ? _serviceManager.Capture(_settings, orderId.ToString())
                : (_settings.PaymentType == PaymentType.Authorize
                ? _serviceManager.Authorize(_settings, orderId.ToString())
                : (default, default));