You are here

class pay_method_direct in Pay 6

Same name and namespace in other branches
  1. 7 includes/handlers/pay_method_direct.inc \pay_method_direct

@file The base class for 'direct' payment activities, where payments are collected on third-party sites such as PayPal, and the responses are returned to Drupal on a URL callback.

Hierarchy

Expanded class hierarchy of pay_method_direct

File

includes/handlers/pay_method_direct.inc, line 9
The base class for 'direct' payment activities, where payments are collected on third-party sites such as PayPal, and the responses are returned to Drupal on a URL callback.

View source
class pay_method_direct extends pay_method {

  /**
   * This method will be called when the user needs to be sent to the gateway
   * provider for processing.  Override it to include valid request values.
   */
  function direct_request() {
  }

  /**
   * This method will be called on a return response (e.g. PayPal IPN)
   * including an unfiltered version of the $_REQUEST array.  This method must
   * be extended in order for your direct method to work!
   */
  function direct_response($response = array()) {
  }

  /**
   * Augment the available payment actions for transactions that include
   * direct payment methods.
   *
   * The 'response' action will allow us to respond to updates from the gateway.
   */
  function set_valid_actions($pay_form, &$actions) {
    $actions['response'] = array(
      'title' => 'Response',
      'callback' => 'response_action',
      'valid states' => array(
        'pending',
      ),
    );
  }

  /**
   * Create a stub method to ensure that do_action('authorize') is executed.
   * If your subclass wants to change, store, or fixup data before it's sent to
   * the gateway, do it by overriding this method.
   *
   * The 'real' work will be done in the form_submit() function.
   */
  function authorize_action($values = array()) {
    $this
      ->set_direct_pending($values);
  }

  /**
   * Create a stub method to ensure that do_action('complete') is executed.
   * If your subclass wants to change, store, or fixup data before it's sent to
   * the gateway, do it by overriding this method.
   *
   * The 'real' work will be done in the form_submit() function.
   */
  function complete_action($values = array()) {
    $this
      ->set_direct_pending($values);

    // Look for an 'authorize' action in this activity's history.
    foreach ($this->activity
      ->history() as $previous) {

      // If there was a successful authorization, copy its details.
      if ($previous->action == 'authorize' && $previous->result) {
        $this->activity->identifier = $previous->identifier;
        $this->activity->data = $previous->data;
        $this->activity->total = $previous->total;
        $this->payment_type = $previous->payment_type;
        $this->activity->action = 'complete';
      }
    }
  }

  /**
   * Implement a 'response' action, which interprets the response from a
   * direct payment gateway.
   */
  function response_action($values = array()) {

    // Copy the submitted/stored values from the previous activity.
    foreach ($this->activity
      ->history() as $previous) {
      if (in_array($previous->action, array(
        'pending',
        'authorize',
      )) && $previous->result) {
        $this
          ->__construct($previous->data);
        if ($previous->action == 'pending') {
          $this->activity->action = $previous->data['action'];
        }
      }
    }

    // Get a boolean response from the response parser.
    return $this
      ->direct_response($values) ? 'complete' : 'canceled';
  }

  /**
   * Store values submitted by a form function, etc.  This will allow us to
   * construct a request to the gateway processor when necessary.
   */
  function set_direct_pending($values = array()) {
    $this->activity->data = $values;
    $this->activity->result = TRUE;

    // Store the current activity for response handling, but store 'pending'.
    $this->activity->data['action'] = $this->activity->action;
    $this->activity->action = 'pending';

    // The direct handler may hardcode its payment_type.
    if (isset($this->payment_type)) {
      $this->activity->payment_type = $this->payment_type;
    }
  }
  function form(&$form, &$form_state) {
    global $user;
    parent::form($form, $form_state);
    if (isset($this->gateway_testmode) && $this->gateway_testmode) {
      drupal_set_message(t('The @name payment method is in test mode. This transaction will not be fully processed.', array(
        '@name' => $this
          ->title(),
      )), 'warning');
    }
    $method_form = array();
    $method_form['first_name'] = array(
      '#type' => 'textfield',
      '#title' => t('First name'),
      '#required' => TRUE,
    );
    $method_form['last_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Last name'),
      '#required' => TRUE,
    );
    if ($user->uid) {
      $method_form['mail'] = array(
        '#type' => 'value',
        '#value' => $user->mail,
      );
    }
    else {
      $method_form['mail'] = array(
        '#type' => 'textfield',
        '#title' => t('E-mail'),
        '#required' => TRUE,
      );
    }
    $method_form['billto'] = array(
      '#type' => 'postal',
      '#postal_user' => $user,
      '#title' => t('Billing address'),
      '#required' => TRUE,
    );

    // Add this method_form to the expected place on the parent form.
    $form[$this->pay_form
      ->handler()]['pay_method'][$this->pmid] = $method_form;
  }
  function form_submit(&$form, &$form_state) {

    // Find our transaction object provided by pay_form::form_submit().
    if (isset($form_state['pay_activity']) && $form_state['pay_activity']) {
      foreach ($form_state['pay_activity'] as $key => $activity) {
        if ($activity->pmid == $this->pmid) {

          // Load the activity as the 'current' one.
          $this->activity = pay_activity_load($activity->paid);
          $this
            ->__construct($this->activity->data);
          $this->activity->action = $this->pay_form_action;

          // Effect a redirect to the direct payment gateway.
          if ($this->activity->action && $this->activity->action != 'pending') {
            unset($form_state['storage']);
            $form_state['rebuild'] = FALSE;
            $form_state['redirect'] = $this
              ->direct_request();
            break;
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
pay::$permissions property
pay::access function
pay::disable function
pay::drupal_invoke function Execute an named Drupal hook function, passing $this as the first parameter.
pay::enable function
pay::form_setup function
pay::form_validate function 1
pay::form_values function
pay::handler function
pay::handler_title function
pay::menu_path function 1
pay::notes function
pay::pay_activity function
pay::pay_form function
pay::pay_transaction function 1
pay::permissions_settings function
pay::save function
pay::settings_form_submit function
pay::settings_form_validate function
pay::set_completed function
pay::set_created function
pay::set_handler function
pay::set_hostname function
pay::set_identifer function
pay::set_key function Do not allow this value to be automatically set.
pay::set_mail function
pay::set_notes function
pay::set_status function
pay::set_table function Do not allow this value to be automatically set.
pay::set_timestamp function
pay::set_total function
pay::set_total_paid function
pay::set_uid function
pay::timestamp_value function
pay::title function 2
pay::total function 1
pay::uid function
pay::user function
pay::__construct function
pay_method::$billto property
pay_method::$description property
pay_method::$first_name property
pay_method::$key property Overrides pay::$key
pay_method::$last_name property
pay_method::$mail property
pay_method::$max_amount property
pay_method::$min_amount property
pay_method::$pay_form_action property
pay_method::$pmid property
pay_method::$status property
pay_method::$table property Overrides pay::$table
pay_method::$title property
pay_method::$total property
pay_method::available_currencies function Your payment method should return an array of valid currencies, using 3-digit currency codes. Example:
pay_method::cancel_action function Cancel a transaction by marking it as 'canceled'. 1
pay_method::pay_method_validate function 1
pay_method::settings_form function Overrides pay::settings_form 2
pay_method::set_description function Set a default description if none is specified.
pay_method::set_max_amount function Set a default max_amount if none is specified.
pay_method::set_min_amount function Set a default min_amount if none is specified.
pay_method::set_title function Set a default title if none is specified.
pay_method::valid_action function Determine whether an action is valid and appropraite for a transaction.
pay_method_direct::authorize_action function Create a stub method to ensure that do_action('authorize') is executed. If your subclass wants to change, store, or fixup data before it's sent to the gateway, do it by overriding this method.
pay_method_direct::complete_action function Create a stub method to ensure that do_action('complete') is executed. If your subclass wants to change, store, or fixup data before it's sent to the gateway, do it by overriding this method.
pay_method_direct::direct_request function This method will be called when the user needs to be sent to the gateway provider for processing. Override it to include valid request values.
pay_method_direct::direct_response function This method will be called on a return response (e.g. PayPal IPN) including an unfiltered version of the $_REQUEST array. This method must be extended in order for your direct method to work!
pay_method_direct::form function Overrides pay::form
pay_method_direct::form_submit function Overrides pay::form_submit
pay_method_direct::response_action function Implement a 'response' action, which interprets the response from a direct payment gateway.
pay_method_direct::set_direct_pending function Store values submitted by a form function, etc. This will allow us to construct a request to the gateway processor when necessary.
pay_method_direct::set_valid_actions function Augment the available payment actions for transactions that include direct payment methods. Overrides pay_method::set_valid_actions