You are here

function commerce_mollie_webhook in Commerce Mollie 7

Handles the callback/webhook call from Mollie and set the payment status accordingly.

1 string reference to 'commerce_mollie_webhook'
commerce_mollie_menu in ./commerce_mollie.module
Implements hook_menu().

File

./commerce_mollie.module, line 300

Code

function commerce_mollie_webhook() {
  $payment_method = _commerce_mollie_get_payment_method_instance();
  try {
    $mollie = new Mollie_API_Client();
    $mollie
      ->setApiKey($payment_method['settings']['commerce_mollie_test_mode'] ? $payment_method['settings']['commerce_mollie_api_key_test'] : $payment_method['settings']['commerce_mollie_api_key_live']);

    // Fetch the payment information made offsite from Mollie.
    $payment = $mollie->payments
      ->get($_POST["id"]);

    // Load the previously stored Drupal Commerce payment transaction.
    $transaction = commerce_payment_transaction_load($payment->metadata->transaction_id);
    $order = commerce_order_load($payment->metadata->order_id);
    if ($payment
      ->isPaid() == TRUE) {
      $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
      $transaction->message = t('Successful Mollie payment with transaction ID: %transaction_id for order ID: %order_id', array(
        '%transaction_id' => $payment->metadata->transaction_id,
        '%order_id' => $payment->metadata->order_id,
      ));
      commerce_payment_redirect_pane_next_page($order);
    }
    else {
      if ($payment
        ->isOpen() == FALSE) {
        $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
        $transaction->message = t('Mollie payment with transaction ID: %transaction_id for order ID: %order_id failed', array(
          '%transaction_id' => $payment->metadata->transaction_id,
          '%order_id' => $payment->metadata->order_id,
        ));
        commerce_payment_redirect_pane_previous_page($order);
      }
    }
    watchdog('commerce_mollie', $transaction->message, array(), WATCHDOG_NOTICE);
    commerce_payment_transaction_save($transaction);
  } catch (Mollie_API_Exception $e) {
    watchdog('commerce_mollie', 'Could not load Mollie payment from webhook call. Error: %error', array(
      '%error' => htmlspecialchars($e
        ->getMessage()),
    ), WATCHDOG_ERROR);
  }
}