[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/modules/payment/ -> paypal_express.php (source)

   1  <?php
   2  /*
   3    $Id: paypal_express.php 1803 2008-01-11 18:16:37Z user $
   4  
   5    osCMax Power E-Commerce
   6    http://oscdox.com
   7  
   8    Copyright 2008 osCMax
   9  
  10    Released under the GNU General Public License
  11  */
  12  
  13    class paypal_express {
  14      var $code, $title, $description, $enabled;
  15  
  16  // class constructor
  17      function paypal_express() {
  18        global $order;
  19  
  20        $this->signature = 'paypal|paypal_express|1.0|2.2';
  21  
  22        $this->code = 'paypal_express';
  23        $this->title = MODULE_PAYMENT_PAYPAL_EXPRESS_TEXT_TITLE;
  24        $this->public_title = MODULE_PAYMENT_PAYPAL_EXPRESS_TEXT_PUBLIC_TITLE;
  25        $this->description = MODULE_PAYMENT_PAYPAL_EXPRESS_TEXT_DESCRIPTION;
  26        $this->sort_order = MODULE_PAYMENT_PAYPAL_EXPRESS_SORT_ORDER;
  27        $this->enabled = ((MODULE_PAYMENT_PAYPAL_EXPRESS_STATUS == 'True') ? true : false);
  28  
  29        if ((int)MODULE_PAYMENT_PAYPAL_EXPRESS_ORDER_STATUS_ID > 0) {
  30          $this->order_status = MODULE_PAYMENT_PAYPAL_EXPRESS_ORDER_STATUS_ID;
  31        }
  32  
  33        if (is_object($order)) $this->update_status();
  34      }
  35  
  36  // class methods
  37      function update_status() {
  38        global $order;
  39  
  40        if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_PAYPAL_EXPRESS_ZONE > 0) ) {
  41          $check_flag = false;
  42          $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_PAYPAL_EXPRESS_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
  43          while ($check = tep_db_fetch_array($check_query)) {
  44            if ($check['zone_id'] < 1) {
  45              $check_flag = true;
  46              break;
  47            } elseif ($check['zone_id'] == $order->delivery['zone_id']) {
  48              $check_flag = true;
  49              break;
  50            }
  51          }
  52  
  53          if ($check_flag == false) {
  54            $this->enabled = false;
  55          }
  56        }
  57      }
  58  
  59      function checkout_initialization_method() {
  60        global $language;
  61  
  62        if (file_exists(DIR_FS_CATALOG . 'ext/modules/payment/paypal/images/btn_express_' . basename($language) . '.gif')) {
  63          $image = 'ext/modules/payment/paypal/images/btn_express_' . basename($language) . '.gif';
  64        } else {
  65          $image = 'ext/modules/payment/paypal/images/btn_express.gif';
  66        }
  67  
  68        $string = '<a href="' . tep_href_link('ext/modules/payment/paypal/express.php', '', 'SSL') . '"><img src="' . $image . '" border="0" alt="" title="' . tep_output_string_protected(MODULE_PAYMENT_PAYPAL_EXPRESS_TEXT_BUTTON) . '" /></a>';
  69  
  70        return $string;
  71      }
  72  
  73      function javascript_validation() {
  74        return false;
  75      }
  76  
  77      function selection() {
  78        return array('id' => $this->code,
  79                     'module' => $this->public_title);
  80      }
  81  
  82      function pre_confirmation_check() {
  83        if (!tep_session_is_registered('ppe_token')) {
  84          tep_redirect(tep_href_link('ext/modules/payment/paypal/express.php', '', 'SSL'));
  85        }
  86      }
  87  
  88      function confirmation() {
  89        global $comments;
  90  
  91        if (!isset($comments)) {
  92          $comments = null;
  93        }
  94  
  95        $confirmation = false;
  96  
  97        if (empty($comments)) {
  98          $confirmation = array('fields' => array(array('title' => MODULE_PAYMENT_PAYPAL_EXPRESS_TEXT_COMMENTS,
  99                                                        'field' => tep_draw_textarea_field('ppecomments', 'soft', '60', '5', $comments))));
 100        }
 101  
 102        return $confirmation;
 103      }
 104  
 105      function process_button() {
 106        return false;
 107      }
 108  
 109      function before_process() {
 110        global $order, $sendto, $ppe_token, $ppe_payerid, $HTTP_POST_VARS, $comments;
 111  
 112        if (empty($comments)) {
 113          if (isset($HTTP_POST_VARS['ppecomments']) && tep_not_null($HTTP_POST_VARS['ppecomments'])) {
 114            $comments = tep_db_prepare_input($HTTP_POST_VARS['ppecomments']);
 115  
 116            $order->info['comments'] = $comments;
 117          }
 118        }
 119  
 120        if (MODULE_PAYMENT_PAYPAL_EXPRESS_TRANSACTION_SERVER == 'Live') {
 121          $api_url = 'https://api-3t.paypal.com/nvp';
 122        } else {
 123          $api_url = 'https://api-3t.sandbox.paypal.com/nvp';
 124        }
 125  
 126        $params = array('USER' => MODULE_PAYMENT_PAYPAL_EXPRESS_API_USERNAME,
 127                        'PWD' => MODULE_PAYMENT_PAYPAL_EXPRESS_API_PASSWORD,
 128                        'VERSION' => '3.2',
 129                        'SIGNATURE' => MODULE_PAYMENT_PAYPAL_EXPRESS_API_SIGNATURE,
 130                        'METHOD' => 'DoExpressCheckoutPayment',
 131                        'TOKEN' => $ppe_token,
 132                        'PAYMENTACTION' => ((MODULE_PAYMENT_PAYPAL_EXPRESS_TRANSACTION_METHOD == 'Sale') ? 'Sale' : 'Authorization'),
 133                        'PAYERID' => $ppe_payerid,
 134                        'AMT' => $this->format_raw($order->info['total']),
 135                        'CURRENCYCODE' => $order->info['currency'],
 136                        'BUTTONSOURCE' => 'osCommerce22_Default_EC');
 137  
 138        if (is_numeric($sendto) && ($sendto > 0)) {
 139          $params['SHIPTONAME'] = $order->delivery['firstname'] . ' ' . $order->delivery['lastname'];
 140          $params['SHIPTOSTREET'] = $order->delivery['street_address'];
 141          $params['SHIPTOCITY'] = $order->delivery['city'];
 142          $params['SHIPTOSTATE'] = tep_get_zone_code($order->delivery['country']['id'], $order->delivery['zone_id'], $order->delivery['state']);
 143          $params['SHIPTOCOUNTRYCODE'] = $order->delivery['country']['iso_code_2'];
 144          $params['SHIPTOZIP'] = $order->delivery['postcode'];
 145        }
 146  
 147        $post_string = '';
 148  
 149        foreach ($params as $key => $value) {
 150          $post_string .= $key . '=' . urlencode(trim($value)) . '&';
 151        }
 152  
 153        $post_string = substr($post_string, 0, -1);
 154  
 155        $response = $this->sendTransactionToGateway($api_url, $post_string);
 156        $response_array = array();
 157        parse_str($response, $response_array);
 158  
 159        if (($response_array['ACK'] != 'Success') && ($response_array['ACK'] != 'SuccessWithWarning')) {
 160          tep_redirect(tep_href_link(FILENAME_SHOPPING_CART, 'error_message=' . stripslashes($response_array['L_LONGMESSAGE0']), 'SSL'));
 161        }
 162      }
 163  
 164      function after_process() {
 165        tep_session_unregister('ppe_token');
 166        tep_session_unregister('ppe_payerid');
 167      }
 168  
 169      function get_error() {
 170        return false;
 171      }
 172  
 173      function check() {
 174        if (!isset($this->_check)) {
 175          $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYPAL_EXPRESS_STATUS'");
 176          $this->_check = tep_db_num_rows($check_query);
 177        }
 178        return $this->_check;
 179      }
 180  
 181      function install() {
 182        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable PayPal Express Checkout', 'MODULE_PAYMENT_PAYPAL_EXPRESS_STATUS', 'False', 'Do you want to accept PayPal Express Checkout payments?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
 183        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('API Username', 'MODULE_PAYMENT_PAYPAL_EXPRESS_API_USERNAME', '', 'The username to use for the PayPal API service', '6', '0', now())");
 184        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('API Password', 'MODULE_PAYMENT_PAYPAL_EXPRESS_API_PASSWORD', '', 'The password to use for the PayPal API service', '6', '0', now())");
 185        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('API Signature', 'MODULE_PAYMENT_PAYPAL_EXPRESS_API_SIGNATURE', '', 'The signature to use for the PayPal API service', '6', '0', now())");
 186        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Server', 'MODULE_PAYMENT_PAYPAL_EXPRESS_TRANSACTION_SERVER', 'Live', 'Use the live or testing (sandbox) gateway server to process transactions?', '6', '0', 'tep_cfg_select_option(array(\'Live\', \'Sandbox\'), ', now())");
 187        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Method', 'MODULE_PAYMENT_PAYPAL_EXPRESS_TRANSACTION_METHOD', 'Sale', 'The processing method to use for each transaction.', '6', '0', 'tep_cfg_select_option(array(\'Authorization\', \'Sale\'), ', now())");
 188        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_PAYPAL_EXPRESS_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
 189        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_PAYPAL_EXPRESS_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
 190        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_PAYPAL_EXPRESS_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'tep_cfg_pull_down_order_statuses(', 'tep_get_order_status_name', now())");
 191        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('cURL Program Location', 'MODULE_PAYMENT_PAYPAL_EXPRESS_CURL', '/usr/bin/curl', 'The location to the cURL program application.', '6', '0' , now())");
 192     }
 193  
 194      function remove() {
 195        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
 196      }
 197  
 198      function keys() {
 199        return array('MODULE_PAYMENT_PAYPAL_EXPRESS_STATUS', 'MODULE_PAYMENT_PAYPAL_EXPRESS_API_USERNAME', 'MODULE_PAYMENT_PAYPAL_EXPRESS_API_PASSWORD', 'MODULE_PAYMENT_PAYPAL_EXPRESS_API_SIGNATURE', 'MODULE_PAYMENT_PAYPAL_EXPRESS_TRANSACTION_SERVER', 'MODULE_PAYMENT_PAYPAL_EXPRESS_TRANSACTION_METHOD', 'MODULE_PAYMENT_PAYPAL_EXPRESS_ZONE', 'MODULE_PAYMENT_PAYPAL_EXPRESS_ORDER_STATUS_ID', 'MODULE_PAYMENT_PAYPAL_EXPRESS_SORT_ORDER', 'MODULE_PAYMENT_PAYPAL_EXPRESS_CURL');
 200      }
 201  
 202      function sendTransactionToGateway($url, $parameters) {
 203        $server = parse_url($url);
 204  
 205        if (!isset($server['port'])) {
 206          $server['port'] = ($server['scheme'] == 'https') ? 443 : 80;
 207        }
 208  
 209        if (!isset($server['path'])) {
 210          $server['path'] = '/';
 211        }
 212  
 213        if (isset($server['user']) && isset($server['pass'])) {
 214          $header[] = 'Authorization: Basic ' . base64_encode($server['user'] . ':' . $server['pass']);
 215        }
 216  
 217        if (function_exists('curl_init')) {
 218          $curl = curl_init($server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : ''));
 219          curl_setopt($curl, CURLOPT_PORT, $server['port']);
 220          curl_setopt($curl, CURLOPT_HEADER, 0);
 221          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
 222          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 223          curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
 224          curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
 225          curl_setopt($curl, CURLOPT_POST, 1);
 226          curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
 227  
 228          $result = curl_exec($curl);
 229  
 230          curl_close($curl);
 231        } else {
 232          exec(escapeshellarg(MODULE_PAYMENT_PAYPAL_EXPRESS_CURL) . ' -d ' . escapeshellarg($parameters) . ' "' . $server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : '') . '" -P ' . $server['port'] . ' -k', $result);
 233          $result = implode("\n", $result);
 234        }
 235  
 236        return $result;
 237      }
 238  
 239  // format prices without currency formatting
 240      function format_raw($number, $currency_code = '', $currency_value = '') {
 241        global $currencies, $currency;
 242  
 243        if (empty($currency_code) || !$this->is_set($currency_code)) {
 244          $currency_code = $currency;
 245        }
 246  
 247        if (empty($currency_value) || !is_numeric($currency_value)) {
 248          $currency_value = $currencies->currencies[$currency_code]['value'];
 249        }
 250  
 251        return number_format(tep_round($number * $currency_value, $currencies->currencies[$currency_code]['decimal_places']), $currencies->currencies[$currency_code]['decimal_places'], '.', '');
 252      }
 253    }
 254  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7