<?php

/**
 * @file
 * Install, update and uninstall functions for the uc_cybersource module.
 */

/**
 * Implements hook_requirements().
 */
function uc_cybersource_requirements($phase) {
  $t = get_t();

  $has_curl = function_exists('curl_init');
  $has_dom = class_exists('DOMDocument');
  $has_soap = class_exists('SoapClient');

  // Valid methods are 'soap' and 'post'.  Defaults here to 'post' on install.
  $method = variable_get('uc_cybersource_method', 'post');

  // Using SOAP.
  if ($method == 'soap') {
    $requirements['uc_cybersource_soap_and_dom'] = array(
      'title' => $t('SOAP and DOM'),
      'value' => $has_soap && $has_dom ? $t('Enabled') : $t('Not found'),
    );
    if (!$has_soap || !$has_dom) {
      $requirements['uc_cybersource_soap_and_dom']['severity'] = REQUIREMENT_ERROR;
      $requirements['uc_cybersource_soap_and_dom']['description'] = $t("CyberSource's SOAP Toolkit API requires the PHP <a href='!soap_url'>SOAP</a> and <a href='!dom_url'>DOM</a> libraries.", array('!soap_url' => 'http://php.net/manual/en/soap.setup.php', '!dom_url' => 'http://php.net/manual/en/dom.setup.php'));
    }
  }

  // Using POST with cURL.
  elseif ($method == 'post') {
    $requirements['uc_cybersource_curl'] = array(
      'title' => $t('cURL'),
      'value' => $has_curl ? $t('Enabled') : $t('Not found'),
    );
    if (!$has_curl) {
      $requirements['uc_cybersource_curl']['severity'] = REQUIREMENT_ERROR;
      $requirements['uc_cybersource_curl']['description'] = $t("CyberSource's Silent Order POST requires the PHP <a href='!curl_url'>cURL</a> library.", array('!curl_url' => 'http://php.net/manual/en/curl.setup.php'));
    }
  }

  // We need HOP.php if we're using the post method and also the Hosted Order
  // page, which is a payment method rather than a gateway.
  if ($method != 'soap') {
    require_once('uc_cybersource.module');
    $has_cybersource_hop = uc_cybersource_hop_include();
    $requirements['uc_cybersource'] = array(
      'title' => t('CyberSource HOP.php'),
      'value' => $has_cybersource_hop ? $t('Enabled') : $t('Not found'),
    );
    if (!$has_cybersource_hop) {
      $requirements['uc_cybersource']['description'] = t('For instructions on how to generate this file, please refer to <a href="http://www.cybersource.com/support_center/implementation/downloads/hosted_order_page/">CyberSource\'s Hosted Order Pages Documentation</a>, specifically the "Downloading Security Scripts" in Chapter 2 of the <a href="http://apps.cybersource.com/library/documentation/sbc/HOP_UG/html/">HOP User\'s Guide</a>.');
      $severity = $phase == 'install' ? REQUIREMENT_INFO : REQUIREMENT_ERROR;
      $requirements['uc_cybersource']['severity'] = $severity;
    }
    else {
      $requirements['uc_cybersource']['severity'] = REQUIREMENT_OK;
      $requirements['uc_cybersource']['description'] = t('The HOP.php library is readable.');
    }
  }
  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function uc_cybersource_uninstall() {
  // Delete related variables all at once.
  db_delete('variable')
    ->condition(db_or()
      ->condition('name', 'uc_cybersource_%', 'LIKE')
      ->condition('name', 'cs_ship_from_%', 'LIKE')
    )
    ->execute();
}

/**
 * Implements hook_schema().
 */
function uc_cybersource_schema() {
  $schema['uc_payment_cybersource_hop_post'] = array(
    'fields' => array(
      'order_id' => array(
        'description' => 'The order ID as provided by the store.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'request_id' => array(
        'description' => 'Unique id assigned by CyberSource that identifies payment request.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'request_token' => array(
        'description' => 'Verification token associated with the request ID.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'reconciliation_id' => array(
        'description' => 'Reference number generated by CyberSource that you use to reconcile your CyberSource reports with your processor reports.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'gross' => array(
        'description' => 'The approved payment amount from CyberSource.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'decision' => array(
        'description' => 'CyberSource decision for payment request.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'reason_code' => array(
        'description' => 'A code for decision.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'payer_email' => array(
        'description' => 'The e-mail address of the buyer.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
     'received' => array(
        'description' => 'The IPN receipt timestamp.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_update_last_removed().
 */
function uc_cybersource_update_last_removed() {
  return 2;
}
