You are here

class PayPalExpress in PayPal for Payment 2.0.x

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

Provides the configuration for the PayPal Express payment method plugin.

Plugin annotation


@PaymentMethodConfiguration(
  description = @Translation("PayPal Express payment method type."),
  id = "paypal_payment_express",
  label = @Translation("PayPal Express")
)

Hierarchy

  • class \Drupal\paypal_payment\Plugin\Payment\MethodConfiguration\PayPalBasic extends \Drupal\payment\Plugin\Payment\MethodConfiguration\Basic
    • class \Drupal\paypal_payment\Plugin\Payment\MethodConfiguration\PayPalExpress

Expanded class hierarchy of PayPalExpress

File

src/Plugin/Payment/MethodConfiguration/PayPalExpress.php, line 24

Namespace

Drupal\paypal_payment\Plugin\Payment\MethodConfiguration
View source
class PayPalExpress extends PayPalBasic {

  /**
   * Gets the client ID of this configuration.
   *
   * @return string
   */
  public function getClientId() : string {
    return $this->configuration['clientId'] ?? '';
  }

  /**
   * Gets the client secret of this configuration.
   *
   * @return string
   */
  public function getClientSecret() : string {
    return $this->configuration['clientSecret'] ?? '';
  }

  /**
   * Gets the webhook ID of this configuration.
   *
   * @return string
   */
  public function getWebhookId() : string {
    return $this->configuration['webhookId'] ?? '';
  }

  /**
   * {@inheritdoc}
   */
  public function processBuildConfigurationForm(array &$element, FormStateInterface $form_state, array &$form) {
    parent::processBuildConfigurationForm($element, $form_state, $form);
    $element['paypal']['clientId'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Client ID'),
      '#default_value' => $this
        ->getClientId(),
      '#maxlength' => 255,
      '#required' => TRUE,
    ];
    $element['paypal']['clientSecret'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Client Secret'),
      '#default_value' => $this
        ->getClientSecret(),
      '#maxlength' => 255,
      '#required' => TRUE,
    ];
    return $element;
  }

  /**
   * {{@inheritdoc}}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $parents = $form['plugin_form']['paypal']['#parents'];
    array_pop($parents);
    $values = $form_state
      ->getValues();
    $values = NestedArray::getValue($values, $parents);
    $this->configuration['clientId'] = $values['paypal']['clientId'];
    $this->configuration['clientSecret'] = $values['paypal']['clientSecret'];
    $this->configuration['webhookId'] = $this
      ->updateWebhook($this->configuration, $form_state
      ->getValue('id'));
  }

  /**
   * @param $configuration
   * @param $id
   * @return string
   */
  private function updateWebhook($configuration, $id) : string {
    $webhookId = $this
      ->getWebhookId();
    $webhookUrl = PayPalExpressMethod::webhookUrl($id);
    $apiContext = PayPalExpressMethod::apiContext($configuration, PayPalExpressMethod::PAYPAL_CONTEXT_TYPE_ADMIN);
    if (!empty($webhookId)) {
      try {
        $webhook = Webhook::get($webhookId, $apiContext);
        if ($webhookUrl !== $webhook
          ->getUrl()) {
          $patch = new Patch();
          $patch
            ->setOp('replace')
            ->setPath('/url')
            ->setValue($webhookUrl);
          $patchRequest = new PatchRequest();
          $patchRequest
            ->addPatch($patch);
          $webhook
            ->update($patchRequest, $apiContext);
        }
      } catch (Exception $ex) {
        $webhookId = '';
      }
    }
    if (empty($webhookId)) {
      try {

        // Create a new webhook
        $webhook = new Webhook();
        $webhook
          ->setUrl($webhookUrl);
        $eventType = new WebhookEventType();
        $eventType
          ->setName('*');
        $webhook
          ->setEventTypes([
          $eventType,
        ]);
        $webhook = $webhook
          ->create($apiContext);
        $webhookId = $webhook
          ->getId();
      } catch (Exception $ex) {
        $this
          ->messenger()
          ->addError($this
          ->t('Something went wrong when creating the webhook for your PayPal Express payment method.'));
      }
    }
    return $webhookId;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeConfiguration() : array {
    return parent::getDerivativeConfiguration() + [
      'clientId' => $this
        ->getClientId(),
      'clientSecret' => $this
        ->getClientSecret(),
      'webhookId' => $this
        ->getWebhookId(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PayPalBasic::getLogLevel public function Gets the setting for the log level.
PayPalBasic::isLogging public function Gets the setting for logging the PayPal API traffic.
PayPalBasic::isProduction public function Gets the setting for the production server.
PayPalExpress::getClientId public function Gets the client ID of this configuration.
PayPalExpress::getClientSecret public function Gets the client secret of this configuration.
PayPalExpress::getDerivativeConfiguration public function Overrides PayPalBasic::getDerivativeConfiguration
PayPalExpress::getWebhookId public function Gets the webhook ID of this configuration.
PayPalExpress::processBuildConfigurationForm public function Overrides PayPalBasic::processBuildConfigurationForm
PayPalExpress::submitConfigurationForm public function {} Overrides PayPalBasic::submitConfigurationForm
PayPalExpress::updateWebhook private function