You are here

function mollie_payment_listener in Mollie Payment 7

Same name and namespace in other branches
  1. 7.2 mollie_payment.module \mollie_payment_listener()

Listener callback.

Parameters

string $pid: The id of the payment.

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.

1 string reference to 'mollie_payment_listener'
mollie_payment_menu in ./mollie_payment.module
Implements hook_menu().

File

./mollie_payment.module, line 221
Provides Mollie integration for the Payment platform.

Code

function mollie_payment_listener($pid) {

  // Load the Payment payment.

  /** @var Payment $payment */
  $payment = entity_load_single('payment', $pid);

  // Fetch the Mollie payment id.
  $parameters = drupal_get_query_parameters($_POST);
  $id = $parameters['id'];

  // Initialize the client.
  $client = mollie_payment_get_client($payment);
  if ($payment && $client) {

    // Load the Mollie Payment.
    $mollie_payment = $client->payments
      ->get($id);

    // Update the status of the Payment payment.
    mollie_payment_update_status($payment, $mollie_payment->status);

    // Create a subscription once the mandate is pending or valid.
    if (module_exists('payment_recurring')) {
      $recurring_info = payment_recurring_recurring_payment_info($payment);

      // There is no subscription yet.
      if (!empty($recurring_info) && !isset($payment->context_data['payment']['subscription'])) {

        // This is a recurring payment.
        if (in_array($recurring_info['type'], array(
          'subscription',
          'installments',
        ))) {
          $controller_data = $payment->method->controller_data;
          $recurring_listener_path = MOLLIE_PAYMENT_RECURRING_LISTENER_PATH;
          if (!empty($controller_data['webhook_base_url'])) {
            $recurring_listener_path = $controller_data['webhook_base_url'] . '/' . $recurring_listener_path;
          }

          // Charge automatically.
          $mandates = $client->customers_mandates
            ->withParentId($mollie_payment->customerId)
            ->all();
          foreach ($mandates as $mandate) {
            if (in_array($mandate->status, array(
              'pending',
              'valid',
            ))) {

              // We are allowed to charge automatically.
              $subscription_data = array(
                'amount' => $payment
                  ->totalAmount(TRUE),
                'interval' => $recurring_info['interval'],
                'description' => $payment->description,
                'webhookUrl' => url($recurring_listener_path . '/' . $payment->pid, array(
                  'absolute' => TRUE,
                )),
              );
              if ($recurring_info['type'] == 'installments' && isset($recurring_info['times'])) {
                $subscription_data['times'] = $recurring_info['times'];
              }
              if (isset($recurring_info['startDate'])) {
                $subscription_data['startDate'] = $recurring_info['startDate'];
              }
              $subscription = $client->customers_subscriptions
                ->withParentId($mollie_payment->customerId)
                ->create($subscription_data);
              if ($subscription) {
                $payment->context_data['payment']['customer'] = $mollie_payment->customerId;
                $payment->context_data['payment']['subscription'] = $subscription->id;
                entity_save('payment', $payment);
              }
            }
          }
        }
      }
    }
  }
}