You are here

abstract class PayPalBasic in PayPal for Payment 8

Same name in this branch
  1. 8 src/Plugin/Payment/MethodConfiguration/PayPalBasic.php \Drupal\paypal_payment\Plugin\Payment\MethodConfiguration\PayPalBasic
  2. 8 src/Plugin/Payment/Method/PayPalBasic.php \Drupal\paypal_payment\Plugin\Payment\Method\PayPalBasic
Same name and namespace in other branches
  1. 2.0.x src/Plugin/Payment/Method/PayPalBasic.php \Drupal\paypal_payment\Plugin\Payment\Method\PayPalBasic

Abstract class for PayPal payment methods.

Hierarchy

Expanded class hierarchy of PayPalBasic

3 files declare their use of PayPalBasic
PayPalBasic.php in src/Plugin/Payment/MethodConfiguration/PayPalBasic.php
Redirect.php in src/Controller/Redirect.php
Webhook.php in src/Controller/Webhook.php

File

src/Plugin/Payment/Method/PayPalBasic.php, line 22

Namespace

Drupal\paypal_payment\Plugin\Payment\Method
View source
abstract class PayPalBasic extends Basic {
  const PAYPAL_CONTEXT_TYPE_ADMIN = 'admin';
  const PAYPAL_CONTEXT_TYPE_CREATE = 'create';
  const PAYPAL_CONTEXT_TYPE_WEBHOOK = 'webhook';
  const PAYPAL_CONTEXT_TYPE_REDIRECT = 'redirect';
  const PAYPAL_DEFAULT_CURRENCY = 'USD';

  /**
   * @var \Drupal\payment\Response\Response
   */
  protected $paymentExecutionResult;

  /**
   * @return string
   */
  public abstract function getWebhookUrl() : string;

  /**
   * @return string
   */
  public abstract function getWebhookId() : string;

  /**
   * @param string $type
   * @return \PayPal\Rest\ApiContext
   */
  public abstract function getApiContext($type) : ApiContext;

  /**
   * @param $paymentId
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  private function setPaymentId($paymentId) {
    $this->configuration['paymentID'] = $paymentId;
    $this
      ->getPayment()
      ->save();
  }

  /**
   * @return mixed|null
   */
  public function getPaymentId() {
    return $this->configuration['paymentID'] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getPaymentExecutionResult() {
    return new OperationResult($this->paymentExecutionResult);
  }

  /**
   * {@inheritdoc}
   */
  protected function doExecutePayment() {
    parent::doExecutePayment();
    $payer = new Payer();
    $payer
      ->setPaymentMethod('paypal');
    $itemList = new ItemList();
    $totalAmount = 0;
    $currency = self::PAYPAL_DEFAULT_CURRENCY;
    foreach ($this
      ->getPayment()
      ->getLineItems() as $line_item) {
      $totalAmount += $line_item
        ->getTotalAmount();
      $line_item_currency = $line_item
        ->getCurrencyCode();
      $item = new Item();
      $item
        ->setName($line_item
        ->getName())
        ->setCurrency($line_item_currency)
        ->setQuantity($line_item
        ->getQuantity())
        ->setPrice($line_item
        ->getAmount());
      $itemList
        ->addItem($item);
      if ($line_item_currency !== $currency) {
        if ($currency !== self::PAYPAL_DEFAULT_CURRENCY) {

          // This is the second time we are changing the currency which means
          // that our line items have mixed currencies. This aion't gonna work!

          # TODO: clarify with the payment maintainer how we should handle this
          $this
            ->messenger()
            ->addError($this
            ->t('Mixed currencies detected which is not yet supported.'));
          return;
        }
        $currency = $line_item_currency;
      }
    }
    $redirectSuccess = new Url('paypal_payment.redirect.success', [
      'payment' => $this
        ->getPayment()
        ->id(),
    ], [
      'absolute' => TRUE,
    ]);
    $redirectCancel = new Url('paypal_payment.redirect.cancel', [
      'payment' => $this
        ->getPayment()
        ->id(),
    ], [
      'absolute' => TRUE,
    ]);
    $redirectUrls = new RedirectUrls();
    $redirectUrls
      ->setReturnUrl($redirectSuccess
      ->toString(TRUE)
      ->getGeneratedUrl())
      ->setCancelUrl($redirectCancel
      ->toString(TRUE)
      ->getGeneratedUrl());
    $amount = new Amount();
    $amount
      ->setCurrency($currency)
      ->setTotal($totalAmount);
    $transaction = new Transaction();
    $transaction
      ->setAmount($amount)
      ->setItemList($itemList)
      ->setDescription($this
      ->getPayment()
      ->id())
      ->setInvoiceNumber($this
      ->getPayment()
      ->id())
      ->setNotifyUrl($this
      ->getWebhookUrl());
    $payment = new Payment();
    $payment
      ->setIntent('sale')
      ->setPayer($payer)
      ->setRedirectUrls($redirectUrls)
      ->setTransactions([
      $transaction,
    ]);
    try {
      $payment
        ->create($this
        ->getApiContext(self::PAYPAL_CONTEXT_TYPE_CREATE));
      $this
        ->setPaymentId($payment
        ->getId());
      $url = Url::fromUri($payment
        ->getApprovalLink());
      $this->paymentExecutionResult = new Response($url);
    } catch (Exception $ex) {

      # TODO: clarify with the payment maintainer how we should handle Exceptions
      $this->paymentExecutionResult = NULL;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Basic::$paymentStatusManager protected property The payment status manager.
Basic::create public static function Creates an instance of the plugin. Overrides PaymentMethodBase::create
Basic::doCapturePayment public function Performs the actual payment capture. Overrides PaymentMethodBase::doCapturePayment
Basic::doCapturePaymentAccess public function Performs a payment method-specific access check for payment capture. Overrides PaymentMethodBase::doCapturePaymentAccess
Basic::doRefundPayment public function Performs the actual payment refund. Overrides PaymentMethodBase::doRefundPayment
Basic::doRefundPaymentAccess public function Performs a payment method-specific access check for payment refunds. Overrides PaymentMethodBase::doRefundPaymentAccess
Basic::getCapture public function Gets whether or not capture is supported.
Basic::getCaptureStatusId public function Gets the status to set on payment capture.
Basic::getEntityId public function Gets the ID of the payment method this plugin is for.
Basic::getExecuteStatusId public function Gets the status to set on payment execution.
Basic::getRefund public function Gets whether or not capture is supported.
Basic::getRefundStatusId public function Gets the status to set on payment refund.
Basic::getSettablePaymentStatuses public function Returns the statuses that can be set on a payment. Overrides PaymentMethodUpdatePaymentStatusInterface::getSettablePaymentStatuses
Basic::getSupportedCurrencies public function Returns the supported currencies. Overrides PaymentMethodBase::getSupportedCurrencies
Basic::updatePaymentStatusAccess public function Checks if the payment status can be updated. Overrides PaymentMethodUpdatePaymentStatusInterface::updatePaymentStatusAccess
Basic::__construct public function Constructs a new instance. Overrides PaymentMethodBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PaymentAwareTrait::$payment protected property The payment.
PaymentAwareTrait::getPayment public function
PaymentAwareTrait::setPayment public function
PaymentMethodBase::$eventDispatcher protected property The event dispatcher.
PaymentMethodBase::$moduleHandler protected property The module handler.
PaymentMethodBase::$token protected property The token API.
PaymentMethodBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
PaymentMethodBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
PaymentMethodBase::capturePayment public function Captures the payment. Overrides PaymentMethodCapturePaymentInterface::capturePayment
PaymentMethodBase::capturePaymentAccess public function Checks if the payment can be captured. Overrides PaymentMethodCapturePaymentInterface::capturePaymentAccess
PaymentMethodBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
PaymentMethodBase::doExecutePaymentAccess protected function Performs a payment method-specific access check for payment execution.
PaymentMethodBase::executePayment public function Executes the payment. Overrides PaymentMethodInterface::executePayment
PaymentMethodBase::executePaymentAccess public function Checks if the payment can be executed. Overrides PaymentMethodInterface::executePaymentAccess
PaymentMethodBase::executePaymentAccessCurrency protected function Checks a payment's currency against this plugin.
PaymentMethodBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
PaymentMethodBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
PaymentMethodBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
PaymentMethodBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PaymentMethodBase::getMessageText public function Gets the payer message text.
PaymentMethodBase::getMessageTextFormat public function Gets the payer message text format.
PaymentMethodBase::getPaymentCaptureResult public function Gets the payment capture status. Overrides PaymentMethodCapturePaymentInterface::getPaymentCaptureResult
PaymentMethodBase::getPaymentRefundResult public function Gets the payment refund status. Overrides PaymentMethodRefundPaymentInterface::getPaymentRefundResult
PaymentMethodBase::refundPayment public function Refunds the payment. Overrides PaymentMethodRefundPaymentInterface::refundPayment
PaymentMethodBase::refundPaymentAccess public function Checks if the payment can be refunded. Overrides PaymentMethodRefundPaymentInterface::refundPaymentAccess
PaymentMethodBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
PaymentMethodBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
PaymentMethodBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
PayPalBasic::$paymentExecutionResult protected property
PayPalBasic::doExecutePayment protected function Performs the actual payment execution. Overrides Basic::doExecutePayment
PayPalBasic::getApiContext abstract public function 2
PayPalBasic::getPaymentExecutionResult public function Gets the payment execution status. Overrides PaymentMethodBase::getPaymentExecutionResult
PayPalBasic::getPaymentId public function
PayPalBasic::getWebhookId abstract public function 2
PayPalBasic::getWebhookUrl abstract public function 2
PayPalBasic::PAYPAL_CONTEXT_TYPE_ADMIN constant
PayPalBasic::PAYPAL_CONTEXT_TYPE_CREATE constant
PayPalBasic::PAYPAL_CONTEXT_TYPE_REDIRECT constant
PayPalBasic::PAYPAL_CONTEXT_TYPE_WEBHOOK constant
PayPalBasic::PAYPAL_DEFAULT_CURRENCY constant
PayPalBasic::setPaymentId private function
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.