You are here

public function Webhook::execute in PayPal for Payment 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/Webhook.php \Drupal\paypal_payment\Controller\Webhook::execute()

PayPal calls this after the payment status has been changed.

Parameters

string $payment_method_id:

Return value

\Symfony\Component\HttpFoundation\Response

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'Webhook::execute'
paypal_payment.routing.yml in ./paypal_payment.routing.yml
paypal_payment.routing.yml

File

src/Controller/Webhook.php, line 101

Class

Webhook
Handles the "webhook" route.

Namespace

Drupal\paypal_payment\Controller

Code

public function execute(string $payment_method_id) : Response {
  $webhook = new WebhookEvent($this->request
    ->getContent());
  $event_type_status_map = [
    'PAYMENT.AUTHORIZATION.CREATED' => 'payment_authorized',
    'PAYMENT.AUTHORIZATION.VOIDED' => 'payment_authorization_failed',
    'PAYMENT.CAPTURE.COMPLETED' => 'payment_success',
    'PAYMENT.CAPTURE.REFUNDED' => 'payment_refunded',
    'PAYMENT.SALE.COMPLETED' => 'payment_success',
    'PAYMENT.SALE.REFUNDED' => 'payment_refunded',
  ];
  $resource = $webhook
    ->getResource();
  $payment_id = $resource->invoice_number;
  $payment = $this
    ->entityTypeManager()
    ->getStorage('payment')
    ->load($payment_id);
  if ($payment && $payment
    ->getPaymentMethod()
    ->getPaymentId() === $resource->parent_payment) {
    $payment_status = Payment::statusManager()
      ->createInstance($event_type_status_map[$webhook
      ->getEventType()]);
    $payment
      ->setPaymentStatus($payment_status)
      ->save();
  }
  return new Response();
}