You are here

function mollie_payment_recurring_listener in Mollie Payment 7.2

Same name and namespace in other branches
  1. 7 mollie_payment.module \mollie_payment_recurring_listener()

Recurring listener callback.

Parameters

string $pid: The id of the payment.

Mollie calls this after a new recurring payment was made or when 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_recurring_listener'
mollie_payment_menu in ./mollie_payment.module
Implements hook_menu().

File

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

Code

function mollie_payment_recurring_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);
    if (module_exists('payment_recurring') && isset($mollie_payment->subscriptionId)) {

      // This is a new payment for a subscription.

      /** @var Payment $new_payment */
      $new_payment = entity_create('payment', array(
        'method' => $payment->method,
        'currency_code' => $mollie_payment->amount->currency,
        'amount' => $mollie_payment->amount->value,
        'description' => $mollie_payment->description,
        'recurring' => array(
          'fpid' => $payment->pid,
        ),
        'context_data' => array(
          'payment' => array(
            'id' => $mollie_payment->id,
            'subscription' => $mollie_payment->subscriptionId,
          ),
        ),
      ));
      $line_items = $payment
        ->getLineItems();

      // Get the first line item to fetch the tax rate.
      $line_item = reset($line_items);
      $new_payment
        ->setLineItem(new PaymentLineItem(array(
        'currency_code' => $mollie_payment->amount->currency,
        'amount' => $mollie_payment->amount->value / (1 + $line_item->tax_rate),
        'quantity' => 1,
        'tax_rate' => $line_item->tax_rate,
        'description' => $mollie_payment->description,
      )));
      entity_save('payment', $new_payment);
      mollie_payment_update_status($new_payment, $mollie_payment->status);
    }
  }
}