You are here

basic_cart_payment.module in Basic cart 7.3

File

basic_cart_payment/basic_cart_payment.module
View source
<?php

/**
 * Implementes hook_form_FORM_ID_alter().
 */
function basic_cart_payment_form_order_node_form_alter(&$form, &$form_state, $form_id) {

  /**
   * See https://www.drupal.org/node/921398
   * Because of the way the node/form is created, we have this bug.
   */
  form_load_include($form_state, 'inc', 'node', 'node.pages');
  $pmids = payment_method_options();
  if (isset($form_state['values']['basic_cart_payment_methods'])) {
    $pmid = $form_state['values']['basic_cart_payment_methods'];
  }
  else {
    foreach ($pmids as $key => $value) {
      $pmid = $key;
      break;
    }
  }
  $form['basic_cart_payment_methods'] = array(
    '#type' => 'radios',
    '#title' => t('Available payment method'),
    '#default_value' => $pmid,
    '#options' => $pmids,
    '#weight' => 50,
    '#ajax' => array(
      'callback' => 'basic_cart_payment_payment_form_generate',
      'wrapper' => 'basic-cart-payment-options',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['payment'] = array(
    '#title' => t("Payment options"),
    '#prefix' => '<div id="basic-cart-payment-options">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#weight' => 51,
  );
  $payment_method = entity_load_single('payment_method', $pmid);
  $currency = variable_get('basic_cart_currency');
  $payment = new Payment(array(
    'context' => 'basic_cart_payment',
    'context_data' => array(
      'destination' => $_GET['q'],
    ),
    'currency_code' => $currency,
    'description' => $payment_method->title_specific,
    'finish_callback' => 'basic_cart_payment_finish',
    'method' => $payment_method,
  ));

  // Getting the cart.
  $cart = basic_cart_get_cart();
  if (!empty($cart) && is_array($cart)) {
    foreach ($cart as $i => $item) {
      $payment
        ->setLineItem(new PaymentLineItem(array(
        'amount' => $item->basic_cart_unit_price,
        'description' => $item->title,
        'name' => $item->title,
        'quantity' => $item->basic_cart_quantity,
      )));
    }
  }
  $form_info = payment_form_embedded($form_state, $payment, array(
    $pmid,
  ));
  unset($form_info['elements']['payment_status']);
  $form['payment']['pay'] = $form_info['elements'];
  return $form;
}

/**
 * Implements hook_node_submit().
 */
function basic_cart_payment_node_submit($node, $form, &$form_state) {
  if ($node->type == 'order') {

    // Executing the payment.
    $payment = $form_state['payment'];
    entity_save('payment', $payment);

    // Setting the pid for this payment so that it can be registered when hook_node_insert is called.
    basic_cart_payment_pid($payment->pid);

    // Every payment method redirects the user to the payment site, except for the dummy test one.
    if ($payment->method->name != 'basic_payment_method') {

      // Saving the node. Since we are about to redirect to the payment site, the
      // order node hasn't been saved yet.
      node_save($node);
    }
    $payment
      ->execute();
  }
}

/**
 * Sets the unique payment ID variable, so that it can be recovered by other modules.
 * 
 * @param int $pid
 *   The current payment ID registered for the current order id.
 * @return int $payment_id
 */
function basic_cart_payment_pid($pid = NULL) {
  static $payment_id;
  if (!empty($pid) && !isset($payment_id)) {
    $payment_id = $pid;
  }
  return $payment_id;
}

/**
 * Ajax callback function.
 */
function basic_cart_payment_payment_form_generate($form, $form_state) {
  return $form['payment'];
}

/**
 * Implements Payment::finish_callback.
 */
function basic_cart_payment_finish(Payment $payment) {
  if (payment_access('view', $payment)) {
    $view = ' ' . l(t('View payment'), 'payment/' . $payment->pid) . '.';
  }
  if (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_PENDING)) {
    drupal_set_message(t('Your payment is still being processed.') . $view);

    //drupal_goto('checkout/thank-you');
  }
  elseif (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_SUCCESS)) {
    drupal_set_message(t('Your payment was successfully completed.') . $view);

    //drupal_goto('checkout/thank-you');
  }
  elseif (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_FAILED)) {
    drupal_set_message(t('Your payment failed.') . $view);

    //drupal_goto('checkout/thank-you');
  }
}

Functions

Namesort descending Description
basic_cart_payment_finish Implements Payment::finish_callback.
basic_cart_payment_form_order_node_form_alter Implementes hook_form_FORM_ID_alter().
basic_cart_payment_node_submit Implements hook_node_submit().
basic_cart_payment_payment_form_generate Ajax callback function.
basic_cart_payment_pid Sets the unique payment ID variable, so that it can be recovered by other modules.