You are here

public function Mollie::onNotify in Commerce Mollie 8

Processes the notification request.

This method should only be concerned with creating/completing payments, the parent order does not need to be touched. The order state is updated automatically when the order is paid in full, or manually by the merchant (via the admin UI).

Note: This method can't throw exceptions on failure because some payment providers expect an error response to be returned in that case. Therefore, the method can log the error itself and then choose which response to return.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

\Symfony\Component\HttpFoundation\Response|null The response, or NULL to return an empty HTTP 200 response.

Overrides OffsitePaymentGatewayBase::onNotify

File

src/Plugin/Commerce/PaymentGateway/Mollie.php, line 167

Class

Mollie
Provides the Mollie payment gateway.

Namespace

Drupal\commerce_mollie\Plugin\Commerce\PaymentGateway

Code

public function onNotify(Request $request) {

  /*
   * Handle status that Mollie returns.
   */
  $mollie_payment_remote_id = $request
    ->get('id');
  if (empty($mollie_payment_remote_id)) {
    $data = json_decode($request
      ->getContent(), TRUE);
    $request->request
      ->replace(is_array($data) ? $data : []);
    $mollie_payment_remote_id = $request->request
      ->get('id');
  }

  /** @var \Drupal\commerce_payment\PaymentStorage $payment_storage */
  $payment_storage = $this->entityTypeManager
    ->getStorage('commerce_payment');

  /** @var \Drupal\commerce_payment\Entity\Payment $payment */
  $payment = $payment_storage
    ->loadByRemoteId($mollie_payment_remote_id);

  // Only proceed if payment is created.
  if ($payment === NULL) {
    return new JsonResponse();
  }

  // Evaluate the remote status.

  /** @var \Mollie\Api\Resources\Payment $mollie_payment_remote_object */
  $mollie_payment_remote_object = $this
    ->getApi()->payments
    ->get($mollie_payment_remote_id);

  // Commerce payment and order state.
  switch ($mollie_payment_remote_object->status) {
    case MolliePaymentStatus::STATUS_PAID:

      // Capture payment.
      $payment_transition = $payment
        ->getState()
        ->getWorkflow()
        ->getTransition('authorize_capture');
      $payment
        ->getState()
        ->applyTransition($payment_transition);
      break;
    case MolliePaymentStatus::STATUS_CANCELED:

      // Cancel payment.
      $payment_transition = $payment
        ->getState()
        ->getWorkflow()
        ->getTransition('void');
      $payment
        ->getState()
        ->applyTransition($payment_transition);
      break;
    case MolliePaymentStatus::STATUS_OPEN:

      // Authorize payment.
      $payment_transition = $payment
        ->getState()
        ->getWorkflow()
        ->getTransition('authorize');
      $payment
        ->getState()
        ->applyTransition($payment_transition);
      break;
    case MolliePaymentStatus::STATUS_FAILED:

      // Void payment.
      $payment_transition = $payment
        ->getState()
        ->getWorkflow()
        ->getTransition('void');
      $payment
        ->getState()
        ->applyTransition($payment_transition);
      break;
    case MolliePaymentStatus::STATUS_EXPIRED:

      // Expire payment.
      $payment_transition = $payment
        ->getState()
        ->getWorkflow()
        ->getTransition('expire');
      $payment
        ->getState()
        ->applyTransition($payment_transition);
      break;
  }
  $payment
    ->setRemoteState($mollie_payment_remote_object->status);
  $payment
    ->save();

  // Return empty response with 200 status code.
  return new JsonResponse();
}