[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/simple_paypal/ -> simple_paypal.module (source)

   1  <?php
   2  
   3  define('SIMPLE_PAYPAL_URL_LIVE', 0);
   4  define('SIMPLE_PAYPAL_URL_TEST', 1);
   5  
   6  define('SIMPLE_PAYPAL_URL',      'simple_paypal_url');
   7  define('SIMPLE_PAYPAL_DEFAULT_CURRENCY', 'simple_paypal_default_currency');
   8  
   9  function simple_paypal_get_urls() {
  10    return array(
  11      SIMPLE_PAYPAL_URL_LIVE => 'https://www.paypal.com/cgi-bin/webscr',
  12      SIMPLE_PAYPAL_URL_TEST => 'https://www.sandbox.paypal.com/cgi-bin/webscr',
  13    );
  14  }
  15  
  16  /**
  17   * Implementation of hook_perm().
  18   */
  19  function simple_paypal_perm () {
  20      return array(
  21                   'administer paypal',
  22                   );
  23  }
  24  
  25  /**
  26   * Implementation of hook_menu().
  27   */
  28  function simple_paypal_menu() {
  29      $items = array();
  30    
  31      $items['admin/settings/paypal'] = array(
  32        'title' => 'Paypal',
  33        'page callback' => 'drupal_get_form',
  34        'page arguments' => array('simple_paypal_admin'),
  35        'description' => 'Administer Paypal',
  36        'access arguments' => array('administer paypal'),
  37        'type' => MENU_NORMAL_ITEM,
  38        );
  39    
  40      return $items;
  41    }
  42  
  43  /**
  44   * Implementation of hook_admin().
  45   */
  46  function simple_paypal_admin() {
  47    $form[SIMPLE_PAYPAL_URL] = array(
  48      '#type'           => 'select',
  49      '#title'          => t('Payment URL for Paypal'),
  50      '#default_value'  => variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_LIVE),
  51      '#options'        => simple_paypal_get_urls(),
  52      '#description' => t('Select whether you want to use a live URL, or a test one.'),
  53    );
  54    $form[SIMPLE_PAYPAL_DEFAULT_CURRENCY] = array(
  55        '#type'          => 'select',
  56        '#title'         => t('Default Currency'),
  57        '#default_value' => variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD'),
  58        '#options'       => simple_paypal_get_currencies(),
  59        );
  60    return system_settings_form($form);
  61  }
  62  
  63  function simple_paypal_get_url() {
  64    $urls = simple_paypal_get_urls();
  65    return $urls[variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_LIVE)];
  66  }
  67  
  68  function simple_paypal_get_currencies() {
  69    $default_currency = variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD');
  70    $country_currencies = array(
  71      'AUD' => t('Australian Dollar'),
  72      'GBP' => t('British Pound'),
  73      'CAD' => t('Canadian Dollar'),
  74      'CZK' => t('Czech Koruna'),
  75      'DKK' => t('Danish Kroner'),
  76      'EUR' => t('Euro'),
  77      'HKD' => t('Hong Kong Dollar'),
  78      'HUF' => t('Hungarian Forint'),
  79      'ILS' => t('Israeli New Shekel'),
  80      'JPY' => t('Japanese Yen'),
  81      'MXN' => t('Mexican Peso'),
  82      'NZD' => t('New Zealand Dollar'),
  83      'NOK' => t('Norwegian Kroner'),
  84      'PLN' => t('Polish Zlotych'),
  85      'SGD' => t('Singapore Dollar'),
  86      'SEK' => t('Swedish Kronor'),
  87      'CHF' => t('Swiss Franc'),
  88      'USD' => t('U.S. Dollar'),
  89    );
  90    
  91    if (variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD')) {
  92      $ordered_currencies[$default_currency] = $country_currencies[$default_currency];
  93      foreach ($country_currencies as $key => $value) {
  94        if($key != $default_currency) {
  95          $ordered_currencies[$key] = $value;
  96        }
  97      }
  98      return $ordered_currencies;
  99    }
 100    return $country_currencies;
 101  }
 102  
 103  function simple_paypal_format_amount($amount, $currency) {
 104    $amount = number_format($amount, 2);
 105    switch ($currency) {
 106      case 'EUR':
 107        return "€ $amount";
 108      case 'GBP':
 109        return "£ $amount";
 110      case 'USD':
 111        return "$ $amount";
 112      default:
 113        return check_plain($currency). " $amount";
 114    }
 115  }
 116  
 117  function simple_paypal_ipn_verify($vars = array()) {
 118    // If we are in test mode, log the variables.
 119    if (SIMPLE_PAYPAL_URL_TEST == variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_TEST)) {
 120      watchdog('simple_paypal', 'Post variables from Paypal IPN <pre>@vars</pre>', array(
 121        '@vars' => print_r($vars, TRUE)), WATCHDOG_DEBUG);
 122    }
 123  
 124    $url = simple_paypal_get_url();
 125    $ch = curl_init();
 126  
 127    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 128    curl_setopt($ch, CURLOPT_URL, $url);
 129    curl_setopt($ch, CURLOPT_POST, 1);
 130    curl_setopt($ch, CURLOPT_POSTFIELDS, simple_paypal_post($vars));
 131  
 132    ob_start();
 133  
 134    if (curl_exec($ch)) {
 135      $info = ob_get_contents();
 136      curl_close($ch);
 137      ob_end_clean();
 138  
 139      if (eregi('VERIFIED', $info)) {
 140        return TRUE;
 141      }
 142      else {
 143        return FALSE;
 144      }
 145    }
 146    else {
 147      watchdog('simple_paypal', 'Call to curl_exec() failed. url=@url vars=@vars', array(
 148        '@vars' => print_r($vars, TRUE),
 149        '@url'  => $url,
 150        ), WATCHDOG_ERROR);
 151      return FALSE;
 152    }
 153  }
 154  
 155  function simple_paypal_post($data = array()) {
 156    $post = '';
 157    foreach ($data as $key => $value) {
 158      $post .= $key. '='. urlencode($value). '&';
 159    }
 160    $post .= 'cmd=_notify-validate';
 161  
 162    return $post;
 163  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7