PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-includes/merchant.class.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 290 lines | 176 code | 35 blank | 79 comment | 23 complexity | cc93355b7296e00fdeaa8fe2702d790b MD5 | raw file
  1. <?php
  2. /**
  3. * WP eCommerce Base Merchant Class
  4. *
  5. * This is the base merchant class, all merchant files that use the new API extend this class.
  6. *
  7. *
  8. * @package wp-e-commerce
  9. * @since 3.7.6
  10. * @abstract
  11. * @subpackage wpsc-merchants
  12. */
  13. class wpsc_merchant {
  14. var $name = 'Base Merchant';
  15. var $is_receiving = false;
  16. var $purchase_id = null;
  17. var $session_id = null;
  18. var $received_data = array();
  19. /**
  20. * This is where the cart data, like the address, country and email address is held
  21. * @var array
  22. */
  23. var $cart_data = array();
  24. /**
  25. * This is where the cart items are stored
  26. @var array
  27. */
  28. var $cart_items = array();
  29. /**
  30. * This is where the data to be sent is gathered before being converted to the necessary format and sent.
  31. * @var array
  32. */
  33. var $collected_gateway_data = array();
  34. /**
  35. * collate_data method, collate purchase data, like addresses, like country
  36. * @access public
  37. */
  38. function __construct($purchase_id = null, $is_receiving = false ) {
  39. global $wpdb;
  40. //$purchase_logs = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `id` = {$purchase_id} LIMIT 1") ;
  41. if(($purchase_id == null) && ($is_receiving == true)) {
  42. $this->is_receiving = true;
  43. $this->parse_gateway_notification();
  44. }
  45. if($purchase_id > 0) {
  46. $this->purchase_id = $purchase_id;
  47. }
  48. $this->collate_data();
  49. $this->collate_cart();
  50. }
  51. function wpsc_merchant($purchase_id = null, $is_receiving = false){
  52. if(version_compare(PHP_VERSION,"5.0.0","<")){
  53. $this->__construct($purchase_id, $is_receiving);
  54. }
  55. }
  56. /**
  57. * collate_data method, collate purchase data, like addresses, like country
  58. * @access public
  59. */
  60. function collate_data() {
  61. global $wpdb;
  62. // get purchase data, regardless of being fed the ID or the sessionid
  63. if($this->purchase_id > 0) {
  64. $purchase_id = & $this->purchase_id;
  65. $purchase_logs = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `id` = {$purchase_id} LIMIT 1", ARRAY_A) ;
  66. } else if($this->session_id != null) {
  67. $purchase_logs = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid` = {$this->session_id} LIMIT 1", ARRAY_A) ;
  68. $this->purchase_id = $purchase_logs['id'];
  69. $purchase_id = & $this->purchase_id;
  70. }
  71. $email_address = $wpdb->get_var("SELECT `value` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` AS `form_field` INNER JOIN `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` AS `collected_data` ON `form_field`.`id` = `collected_data`.`form_id` WHERE `form_field`.`type` IN ( 'email' ) AND `collected_data`.`log_id` IN ( '{$purchase_id}' )");
  72. $currency_code = $wpdb->get_var("SELECT `code` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id`='".get_option('currency_type')."' LIMIT 1");
  73. $collected_form_data = $wpdb->get_results("SELECT `data_names`.`id`, `data_names`.`unique_name`, `collected_data`.`value` FROM `".WPSC_TABLE_SUBMITED_FORM_DATA."` AS `collected_data` JOIN `".WPSC_TABLE_CHECKOUT_FORMS."` AS `data_names` ON `collected_data`.`form_id` = `data_names`.`id` WHERE `log_id` = '".$purchase_id."'", ARRAY_A);
  74. $address_keys = array(
  75. 'billing' => array(
  76. 'first_name' => 'billingfirstname',
  77. 'last_name' => 'billinglastname',
  78. 'address' => 'billingaddress',
  79. 'city' => 'billingcity',
  80. 'state' => 'billingstate',
  81. 'country' => 'billingcountry',
  82. 'post_code' => 'billingpostcode',
  83. ),
  84. 'shipping' => array(
  85. 'first_name' => 'shippingfirstname',
  86. 'last_name' => 'shippinglastname',
  87. 'address' => 'shippingaddress',
  88. 'city' => 'shippingcity',
  89. 'state' => 'shippingstate',
  90. 'country' => 'shippingcountry',
  91. 'post_code' => 'shippingpostcode',
  92. )
  93. );
  94. $address_data = array(
  95. 'billing' => array(),
  96. 'shipping' => array()
  97. );
  98. foreach((array)$collected_form_data as $collected_form_row) {
  99. $address_data_set = 'billing';
  100. $address_key = array_search($collected_form_row['unique_name'], $address_keys['billing']);
  101. if($address_key == null) {
  102. $address_data_set = 'shipping';
  103. // exit('<pre>'.print_r($collected_form_row,true).'</pre>');
  104. $address_key = array_search($collected_form_row['unique_name'], $address_keys['shipping']);
  105. }
  106. if($address_key == null) {
  107. continue;
  108. }
  109. if($collected_form_row['unique_name'] == 'billingcountry' || $collected_form_row['unique_name'] == 'shippingcountry'){
  110. $country = maybe_unserialize($collected_form_row['value']);
  111. $address_data[$address_data_set][$address_key] =$country[0];
  112. }elseif($collected_form_row['unique_name'] == 'shippingstate'){
  113. $address_data[$address_data_set][$address_key] = wpsc_get_state_by_id($collected_form_row['value'], 'code');
  114. }else{
  115. $address_data[$address_data_set][$address_key] = $collected_form_row['value'];
  116. }
  117. }
  118. // exit('<pre>'.print_r($address_data,true).'</pre>');
  119. if(count($address_data['shipping']) < 1) {
  120. $address_data['shipping'] = $address_data['billing'];
  121. }
  122. $this->cart_data = array(
  123. 'software_name' => 'WP e-Commerce/'.WPSC_PRESENTABLE_VERSION.'',
  124. // 'store_name' => '', /// is this useful or needed?
  125. 'store_location' => get_option('base_country'),
  126. 'store_currency' => $currency_code,
  127. 'is_subscription' => false,
  128. 'has_discounts' => false,
  129. 'notification_url' => add_query_arg('wpsc_action', 'gateway_notification', (get_option('siteurl')."/index.php")),
  130. 'transaction_results_url' => get_option('transact_url'),
  131. 'shopping_cart_url' => get_option('shopping_cart_url'),
  132. 'products_page_url' => get_option('product_list_url'),
  133. 'total_price' => $purchase_logs['totalprice'],
  134. 'session_id' => $purchase_logs['sessionid'],
  135. 'transaction_id' => $purchase_logs['transaction_id'], // Transaction ID might not be set yet
  136. 'email_address' => $email_address,
  137. 'billing_address' => $address_data['billing'],
  138. 'shipping_address' => $address_data['shipping']
  139. );
  140. }
  141. /**
  142. * collate_cart method, collate cart data
  143. * @access public
  144. *
  145. */
  146. function collate_cart() {
  147. global $wpdb;
  148. $purchase_id = & $this->purchase_id;
  149. $original_cart_data = $wpdb->get_results("SELECT * FROM `".WPSC_TABLE_CART_CONTENTS."` WHERE `purchaseid` = {$purchase_id}", ARRAY_A);
  150. //print_r($original_cart_data);
  151. //return;
  152. foreach((array)$original_cart_data as $cart_row) {
  153. $is_downloadable = false;
  154. if($wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_DOWNLOAD_STATUS."` WHERE `cartid` = {$cart_row['id']}")) {
  155. $is_downloadable = true;
  156. }
  157. $is_recurring= (bool) wpsc_get_cartmeta($cart_row['id'],'is_recurring',true);
  158. if($is_recurring == true) {
  159. $this->cart_data['is_subscription'] = true;
  160. }
  161. $rebill_interval = wpsc_get_cartmeta($cart_row['id'],'rebill_interval',true);
  162. $new_cart_item = array(
  163. "cart_item_id" => $cart_row['id'],
  164. "product_id" => $cart_row['prodid'],
  165. "name" => $cart_row['name'],
  166. "price" => $cart_row['price'],
  167. "shipping" => $cart_row['pnp'],
  168. "tax" => $cart_row['tax_charged'],
  169. "quantity" => $cart_row['quantity'],
  170. "is_downloadable" => $is_downloadable,
  171. "is_capability" => (bool) wpsc_get_cartmeta($cart_row['id'],'provided_capabilities',true),
  172. "is_recurring" => $is_recurring,
  173. "is_subscription" => $is_recurring,
  174. "recurring_data" => array(
  175. "rebill_interval" => array(
  176. 'unit' => $rebill_interval['unit'],
  177. 'length' => $rebill_interval['interval']
  178. ),
  179. "charge_to_expiry" => (bool) wpsc_get_cartmeta($cart_row['id'],'charge_to_expiry',true),
  180. "times_to_rebill" => wpsc_get_cartmeta($cart_row['id'],'times_to_rebill',true),
  181. )
  182. );
  183. $this->cart_items[] = $new_cart_item;
  184. }
  185. }
  186. /**
  187. * set_error_message, please don't extend this without very good reason
  188. * saves error message, data it is stored in may need to change, hence the need to not extend this.
  189. */
  190. function set_error_message($error_message) {
  191. global $wpdb;
  192. $_SESSION['wpsc_checkout_misc_error_messages'][] = $error_message;
  193. }
  194. /**
  195. * return_to_checkout, please don't extend this without very good reason
  196. * returns to checkout, if this changes and you extend this, your merchant module may go to the wrong place
  197. */
  198. function return_to_checkout() {
  199. global $wpdb;
  200. wp_redirect(get_option('shopping_cart_url'));
  201. exit(); // follow the redirect with an exit, just to be sure.
  202. }
  203. /**
  204. * go_to_transaction_results, please don't extend this without very good reason
  205. * go to transaction results, if this changes and you extend this, your merchant module may go to the wrong place
  206. */
  207. function go_to_transaction_results($session_id) {
  208. global $wpdb;
  209. $transaction_url_with_sessionid = add_query_arg('sessionid', $session_id, get_option('transact_url'));
  210. wp_redirect($transaction_url_with_sessionid);
  211. exit(); // follow the redirect with an exit, just to be sure.
  212. }
  213. /**
  214. * set_transaction_details, maybe extended in merchant files
  215. */
  216. function set_transaction_details($transaction_id, $status = 1) {
  217. global $wpdb;
  218. $transaction_id = $wpdb->escape($transaction_id);
  219. $wpdb->query("UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `processed` = '".absint($status)."', `transactid` ='{$transaction_id}' WHERE `id` = ".absint($this->purchase_id)." LIMIT 1");
  220. //echo("UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `processed` = '".absint($status)."', `transactid` ='{$transaction_id}' WHERE `id` IN ('".absint($this->purchase_id)."') LIMIT 1");
  221. }
  222. /**
  223. * construct_value_array gateway specific data array, extended in merchant files
  224. * @abstract
  225. * @todo When we drop support for PHP 4, make this a proper abstract method
  226. */
  227. function construct_value_array() {
  228. return false;
  229. }
  230. /**
  231. * submit to gateway, extended in merchant files
  232. * @abstract
  233. * @todo When we drop support for PHP 4, make this a proper abstract method
  234. */
  235. function submit() {
  236. return false;
  237. }
  238. /**
  239. * parse gateway notification, recieves and converts the notification to an array, if possible, extended in merchant files
  240. * @abstract
  241. * @todo When we drop support for PHP 4, make this a proper abstract method
  242. */
  243. function parse_gateway_notification() {
  244. return false;
  245. }
  246. /**
  247. * process gateway notification, checks and decides what to do with the data from the gateway, extended in merchant files
  248. * @abstract
  249. * @todo When we drop support for PHP 4, make this a proper abstract method
  250. */
  251. function process_gateway_notification() {
  252. return false;
  253. }
  254. }
  255. ?>