View source
<?php
namespace Drupal\paypal_payment\Plugin\Payment\MethodConfiguration;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\payment\Annotations\PaymentMethodConfiguration;
use Drupal\paypal_payment\Plugin\Payment\Method\PayPalExpress as PayPalExpressMethod;
use Exception;
use PayPal\Api\Patch;
use PayPal\Api\PatchRequest;
use PayPal\Api\Webhook;
use PayPal\Api\WebhookEventType;
class PayPalExpress extends PayPalBasic {
public function getClientId() : string {
return $this->configuration['clientId'] ?? '';
}
public function getClientSecret() : string {
return $this->configuration['clientSecret'] ?? '';
}
public function getWebhookId() : string {
return $this->configuration['webhookId'] ?? '';
}
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;
}
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'));
}
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 {
$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;
}
public function getDerivativeConfiguration() : array {
return parent::getDerivativeConfiguration() + [
'clientId' => $this
->getClientId(),
'clientSecret' => $this
->getClientSecret(),
'webhookId' => $this
->getWebhookId(),
];
}
}