You are here

webform_paypal.module in Webform Paypal 7.2

Same filename and directory in other branches
  1. 7 webform_paypal.module

File

webform_paypal.module
View source
<?php

/**
 *  Define components to Webform.
 *
 */
function webform_paypal_webform_component_info() {
  $components = array();
  $components['paypal'] = array(
    'label' => 'Paypal Button',
    'description' => 'Paypal button to replace the submit button for this form',
    'features' => array(
      'title_display' => FALSE,
    ),
  );
  return $components;
}

/**
 * Implements hook_permission().
 */
function webform_paypal_permission() {
  return array(
    'update payment status' => array(
      'title' => t('Update PayPal Payment Status'),
    ),
    'use paypal sandbox' => array(
      'title' => t('Use PayPal in Sandbox Mode'),
    ),
  );
}

/**
 * Implements _webform_defaults_component().
 */
function _webform_defaults_paypal() {
  return array(
    'value' => '',
    'extra' => array(
      'private' => 1,
      'transaction' => 'live',
    ),
  );
}

/**
 * Implements _webform_theme_component().
 */
function _webform_theme_paypal() {
  return array(
    'webform_display_paypal' => array(
      'render element' => 'element',
    ),
  );
}

/**
 * Generate the form for editing a component.
 * When someone builds a Webform, the first step is to add a component to the form.
 * Next, they have to edit the settings for the component that they just added.
 * This function, then, allows you to define the settings that appear on that "edit component" page.
 *
 */
function _webform_edit_paypal($component) {
  $form = array();
  $form['value'] = array(
    '#title' => t('Paypal Button ID'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => $component['value'],
    '#element_validate' => array(
      '_webform_edit_paypal_validate',
    ),
    '#description' => "The id of the button generated within your Paypal account. This can be found by visiting Merchant Services > My Saved Buttons or by copying the value for 'hosted-button-id' in the button code provided by Paypal",
  );
  if (user_access('use paypal sandbox')) {
    $form['paypal'] = array(
      '#type' => 'fieldset',
      '#title' => t('Development Settings'),
    );
    $form['paypal']['transaction'] = array(
      "#type" => 'select',
      '#title' => t('Choose the type of transaction to process.'),
      '#options' => array(
        'live' => 'Live Transaction',
        'testing' => 'Testing/Sandbox',
      ),
      '#description' => t('In order to use "Testing/Sandbox", you must have a <a href="https://developer.paypal.com/">developer account</a> set up with PayPal.'),
      '#default_value' => $component['extra']['transaction'],
      '#parents' => array(
        'extra',
        'transaction',
      ),
    );
  }

  //12 lines to avoid 3 notices when first adding component. See Issue #1408658
  $type = '';
  $style = '';
  $language = '';
  if (!empty($component['extra']['type'])) {
    $type = $component['extra']['type'];
  }
  if (!empty($component['extra']['style'])) {
    $style = $component['extra']['style'];
  }
  if (!empty($component['extra']['language'])) {
    $language = $component['extra']['language'];
  }
  $form['extra']['type'] = array(
    '#title' => t('Button Type'),
    '#type' => 'select',
    '#options' => array(
      'btn_cart' => 'Shopping Cart',
      'btn_buynow' => 'Buy Now',
      'btn_donate' => 'Donation',
      'btn_git' => 'Gift Certificate',
      'btn_subscribe' => 'Subscribe',
    ),
    '#required' => TRUE,
    '#default_value' => $type,
  );
  $form['extra']['style'] = array(
    '#title' => t('Button Style'),
    '#type' => 'select',
    '#options' => array(
      '_SM' => 'Small',
      '_LG' => 'Large',
      'CC_LG' => 'Large with Credit Card logos',
    ),
    '#required' => TRUE,
    '#default_value' => $style,
  );
  $form['extra']['language'] = array(
    '#title' => t('Button Language'),
    '#type' => 'select',
    '#options' => array(
      'en_US' => 'English',
      'es_ES' => 'Spanish',
      'fr_FR' => 'French',
      'it_IT' => 'Italian',
      'de_DE' => 'German',
    ),
    '#required' => TRUE,
    //TODO:lookup if site has language value
    '#default_value' => $language,
  );
  return $form;
}

/**
 * Element validate handler
 */
function _webform_edit_paypal_validate($form, &$form_state) {
  $value = $form_state['input']['value'];

  //TODO: update regex to exactly match format
  if (preg_match("/[^A-Z0-9]/", $value)) {
    form_set_error('webform_paypal', 'Please enter a valid button ID');
  }
}

/**
 * Render a Webform component to be part of a form.
 * This function allows you to define how your webform component will be rendered when a "regular" user is filling it out.
 */
function _webform_render_paypal($component, $value = NULL, $filter = TRUE) {

  // adjust the form element based on where the user is
  $item = menu_get_item();
  if ($value['status'] == 'paid') {
    $value = 1;
  }
  else {
    $value = 0;
  }
  if (strpos($item['path'], 'edit') && user_access('update payment status')) {
    $element = array(
      '#title' => 'Mark Status as Paid',
      '#type' => 'checkbox',
      '#theme_wrappers' => array(
        'webform_element',
      ),
      '#webform_component' => $component,
      '#default_value' => $value,
    );
  }
  else {
    $element = array(
      '#type' => 'hidden',
      '#default_value' => $component['value'],
      '#theme_wrappers' => array(
        'webform_element',
      ),
      '#webform_component' => $component,
    );
  }
  return $element;
}

/**
 * A hook for changing the input values before saving to the database.
 *
 * Note that Webform will save the result of this function directly into the
 * database.
 *
 * @param $component
 *   A Webform component array.
 * @param $value
 *   The POST data associated with the user input.
 * @return
 *   An array of values to be saved into the database. Note that this should be
 *   a numerically keyed array.
 */
function _webform_submit_paypal($component, $value) {
  $return = array();
  $return['id'] = $component['value'];
  $return['status'] = 'unpaid';
  if (is_int($value) && $value == 1) {
    $return['status'] = 'paid';
  }
  return $return;
}

/**
 * Implements _webform_display_component().
 */
function _webform_display_paypal($component, $value) {
  if (!empty($value['status']) && $value['status'] == 'unpaid') {
    $value = t('Submitted to PayPal');
  }
  if (!empty($value['status']) && $value['status'] == 'paid') {
    $value = t('Submitted to PayPal - Payment Confirmed');
  }
  return array(
    '#title' => t('PayPal Status'),
    '#weight' => $component['weight'],
    '#theme' => 'webform_display_paypal',
    '#theme_wrappers' => array(
      'webform_element',
    ),
    '#value' => $value,
    '#webform_component' => $component,
  );
}

/**
 * Format the output of data for this component.
 */
function theme_webform_display_paypal($variables) {
  $element = $variables['element'];
  if (empty($element['#value'])) {
    return;
  }
  else {
    return $element['#value'];
  }
}

/**
 * Implements of hook_form_alter().
 */
function webform_paypal_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {

    // adjust the form for "edit" screens so people don't resubmit to PayPal
    if (strpos($form['#action'], 'edit')) {
      $form['actions']['submit']['#value'] = t('Update');
    }
    else {
      $webform_components = $form_state['webform']['component_tree']['children'];
      foreach ($webform_components as $key => $value) {
        if ($value['type'] == 'paypal') {
          $form['actions']['submit']['#theme_wrappers'] = array(
            "image_button",
          );
          $form['actions']['submit']['#button_type'] = "image";
          $form['actions']['submit']['#src'] = webform_paypal_button_url($value['extra']['type'], $value['extra']['style'], $value['extra']['language']);
          $form['#submit'][] = 'webform_paypal_submit';
        }
      }
    }
  }
}

/**
 * Build a button url based on required parameters
 */
function webform_paypal_button_url($type, $style, $language) {
  $url = 'https://www.paypal.com/' . $language . '/i/btn/' . $type . $style . '.gif';
  return $url;
}

/**
 * Submit function to send user to paypal after form values saved.
 */
function webform_paypal_submit(&$form, &$form_state) {
  $submission_id = $form_state['values']['details']['sid'];
  $nid = $form_state['values']['details']['nid'];

  //I guess its better than a node load
  if (drupal_lookup_path('alias', 'node/' . $nid)) {
    $node_path = drupal_lookup_path('alias', 'node/' . $nid);
  }
  else {
    $node_path = base_path() . 'node/' . $nid;
  }
  global $base_url;
  $submission_url = $base_url . $node_path . '/submission/' . $submission_id;

  //lookup our hidden webform values from the form

  //we don't know the name because that is editable by end user
  $webform_components = $form_state['webform']['component_tree']['children'];
  foreach ($webform_components as $key => $value) {
    if ($value['type'] == 'paypal') {
      $paypal_component_name = $value['form_key'];
      if ($value['extra']['transaction'] == 'live') {
        $url = 'https://www.paypal.com/cgi-bin/webscr';
      }
      else {
        $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
      }
    }
  }
  $post_data = array();
  $post_data['cmd'] = '_s-xclick';
  $post_data['hosted_button_id'] = $form['submitted'][$paypal_component_name]['#default_value'];

  //send return path to submission for better tracking
  $post_data['custom'] = $submission_url;
  drupal_goto($url, array(
    'query' => $post_data,
  ));
}

/**
 * Implements _webform_table_component().
 */
function _webform_table_paypal($component, $value) {
  if (empty($value['status'])) {
    return;
  }
  else {
    return ucfirst($value['status']);
  }
}

/**
 * Implements _webform_csv_headers_component().
 */
function _webform_csv_headers_paypal($component, $export_options) {
  $header = array();
  $header[0] = '';
  $header[1] = '';
  $header[2] = $component['name'];
  return $header;
}

/**
 * Implements _webform_csv_data_component().
 */
function _webform_csv_data_paypal($component, $export_options, $value) {
  if (empty($value['status'])) {
    return;
  }
  else {
    return ucfirst($value['status']);
  }
}

Functions

Namesort descending Description
theme_webform_display_paypal Format the output of data for this component.
webform_paypal_button_url Build a button url based on required parameters
webform_paypal_form_alter Implements of hook_form_alter().
webform_paypal_permission Implements hook_permission().
webform_paypal_submit Submit function to send user to paypal after form values saved.
webform_paypal_webform_component_info Define components to Webform.
_webform_csv_data_paypal Implements _webform_csv_data_component().
_webform_csv_headers_paypal Implements _webform_csv_headers_component().
_webform_defaults_paypal Implements _webform_defaults_component().
_webform_display_paypal Implements _webform_display_component().
_webform_edit_paypal Generate the form for editing a component. When someone builds a Webform, the first step is to add a component to the form. Next, they have to edit the settings for the component that they just added. This function, then, allows you to define the…
_webform_edit_paypal_validate Element validate handler
_webform_render_paypal Render a Webform component to be part of a form. This function allows you to define how your webform component will be rendered when a "regular" user is filling it out.
_webform_submit_paypal A hook for changing the input values before saving to the database.
_webform_table_paypal Implements _webform_table_component().
_webform_theme_paypal Implements _webform_theme_component().