You are here

public function MolliePaymentMethodController::execute in Mollie Payment 7

Same name and namespace in other branches
  1. 7.2 includes/mollie_payment.class.inc \MolliePaymentMethodController::execute()

Implements PaymentMethodController::execute().

Overrides PaymentMethodController::execute

File

includes/mollie_payment.class.inc, line 24

Class

MolliePaymentMethodController
Mollie payment method controller.

Code

public function execute(Payment $payment) {
  $method_data = $payment->method_data;
  $controller_data = $payment->method->controller_data;
  $client = mollie_payment_get_client($payment);
  if ($client) {
    $return_path = MOLLIE_PAYMENT_RETURN_PATH;
    $listener_path = MOLLIE_PAYMENT_LISTENER_PATH;
    $recurring_listener_path = MOLLIE_PAYMENT_RECURRING_LISTENER_PATH;
    if (!empty($controller_data['webhook_base_url'])) {
      $return_path = $controller_data['webhook_base_url'] . '/' . $return_path;
      $listener_path = $controller_data['webhook_base_url'] . '/' . $listener_path;
      $recurring_listener_path = $controller_data['webhook_base_url'] . '/' . $recurring_listener_path;
    }
    $payment_data = array(
      'amount' => $payment
        ->totalAmount(TRUE),
      'description' => $payment->description,
      'redirectUrl' => url($return_path . '/' . $payment->pid, array(
        'absolute' => TRUE,
      )),
      'webhookUrl' => url($listener_path . '/' . $payment->pid, array(
        'absolute' => TRUE,
      )),
    );
    if (isset($method_data['mollie_payment_method'])) {
      $payment_data['method'] = $method_data['mollie_payment_method'];
    }
    if (isset($method_data['mollie_payment_issuer'])) {
      $payment_data['issuer'] = $method_data['mollie_payment_issuer'];
    }
    if (module_exists('payment_recurring')) {
      $recurring_info = payment_recurring_recurring_payment_info($payment);
      if (!empty($recurring_info)) {

        // This is a recurring payment.
        $payment_data['recurringType'] = 'first';

        // We need to created a customer first.
        $customer = $client->customers
          ->create(array(
          'name' => $recurring_info['name'],
          'email' => $recurring_info['email'],
        ));
        $payment_data['customerId'] = $customer->id;
      }
    }

    // Create the payment in the Mollie system.
    $mollie_payment = $client->payments
      ->create($payment_data);

    // Context data might not be the best location to store this information
    // because in fact it is not related to the context. We might consider
    // storing it in a separate database table just like the controller data
    // of the payment method. (The method_data in the payment object is
    // currently not stored.)
    $payment->context_data['payment'] = array(
      'id' => $mollie_payment->id,
    );
    entity_save('payment', $payment);
    $redirect_url = $mollie_payment
      ->getPaymentUrl();
    drupal_goto($redirect_url);
  }
}