You are here

private function PayPalExpress::updateWebhook in PayPal for Payment 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Payment/MethodConfiguration/PayPalExpress.php \Drupal\paypal_payment\Plugin\Payment\MethodConfiguration\PayPalExpress::updateWebhook()

Parameters

$configuration:

$id:

Return value

string

1 call to PayPalExpress::updateWebhook()
PayPalExpress::submitConfigurationForm in src/Plugin/Payment/MethodConfiguration/PayPalExpress.php
{}

File

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

Class

PayPalExpress
Provides the configuration for the PayPal Express payment method plugin.

Namespace

Drupal\paypal_payment\Plugin\Payment\MethodConfiguration

Code

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;
}