You are here

Webhook.php in Mollie Payment 8.2

File

src/Controller/Webhook.php
View source
<?php

namespace Drupal\mollie_payment\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\payment\Entity\PaymentInterface;
use Drupal\mollie_payment\Entity\MollieProfile;
use Mollie_API_Client;

/**
 * Handles the "webhook" route.
 */
class Webhook extends ControllerBase {

  /**
   * Mollie calls this after the payment status has been changed. Mollie only
   * gives us an id leaving us with the responsibility to get the payment status.
   *
   * @param \Drupal\payment\Entity\PaymentInterface $payment
   *
   * @return
   */
  public function execute(PaymentInterface $payment) {

    // Get the id of the Mollie transaction from the request.
    $request = \Drupal::request();
    $mollie_transaction_id = $request->request
      ->get('id');

    // Get the Mollie profile.
    $mollie_client = $payment
      ->getPaymentMethod()
      ->getMollieClient();
    try {

      // Load the Mollie transaction.
      $mollie_payment = $mollie_client->payments
        ->get($mollie_transaction_id);

      // Payment status mapping.
      $payment_status = [
        'open' => 'payment_pending',
        'cancelled' => 'payment_cancelled',
        'paid' => 'payment_success',
        'paidout' => 'payment_success',
        'refunded' => 'payment_refunded',
        'expired' => 'payment_expired',
      ];
      $payment
        ->getPaymentMethod()
        ->updatePaymentStatus($payment, $payment_status[$mollie_payment->status]);
    } catch (Mollie_API_Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
    return new Response();
  }

}

Classes

Namesort descending Description
Webhook Handles the "webhook" route.