PayPalExpress.php in PayPal for Payment 8
File
src/Plugin/Payment/Method/PayPalExpress.php
View source
<?php
namespace Drupal\paypal_payment\Plugin\Payment\Method;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Url;
use Drupal\payment\Annotations\PaymentMethod;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
class PayPalExpress extends PayPalBasic {
public function getWebhookUrl() : string {
$configuration = $this
->getPluginDefinition();
list(, $id) = explode(':', $configuration['id']);
return self::webhookUrl($id);
}
public function getWebhookId() : string {
$configuration = $this
->getPluginDefinition();
return $configuration['webhookId'];
}
public function getApiContext($type) : ApiContext {
return self::apiContext($this
->getPluginDefinition(), $type);
}
public static function apiContext($configuration, $type) : ApiContext {
$apiContext = new ApiContext(new OAuthTokenCredential($configuration['clientId'], $configuration['clientSecret']));
$storage = PhpStorageFactory::get('paypal_api_context');
if (!$storage
->exists('auth.cache')) {
$storage
->save('auth.cache', '');
}
$apiContext
->setConfig([
'mode' => $configuration['production'] ? 'live' : 'sandbox',
'log.LogEnabled' => $configuration['logging'][$type],
'log.FileName' => \Drupal::service('file_system')
->getTempDirectory() . '/DrupalPayPal.log',
'log.LogLevel' => $configuration['loglevel'],
'cache.enabled' => TRUE,
'cache.FileName' => DRUPAL_ROOT . '/' . $storage
->getFullPath('auth.cache'),
]);
return $apiContext;
}
public static function webhookUrl($id) : string {
$webhook = new Url('paypal_payment.webhook', [
'payment_method_id' => $id,
], [
'absolute' => TRUE,
]);
return $webhook
->toString(TRUE)
->getGeneratedUrl();
}
}