/app_old/membershipincludes/classes/class.gateway.php

  1. <?php 
  2.  
  3. if(!class_exists('M_Gateway')) { 
  4.  
  5. class M_Gateway { 
  6.  
  7. var $db; 
  8.  
  9. // Class Identification 
  10. var $gateway = 'Not Set'; 
  11. var $title = 'Not Set'; 
  12. var $issingle = false; 
  13. var $haspaymentform = false; 
  14.  
  15. // Tables 
  16. var $tables = array('subscription_transaction'); 
  17. var $subscription_transaction; 
  18.  
  19. function __construct() { 
  20.  
  21. global $wpdb; 
  22.  
  23. $this->db =& $wpdb; 
  24.  
  25. foreach($this->tables as $table) { 
  26. $this->$table = membership_db_prefix($this->db, $table); 
  27.  
  28. // Actions and Filters 
  29. add_filter('M_gateways_list', array(&$this, 'gateways_list')); 
  30.  
  31. add_action( 'membership_process_payment_return', array(&$this, 'process_payment_return') ); 
  32. add_action( 'membership_record_user_gateway', array(&$this, 'record_user_gateway') ); 
  33.  
  34.  
  35. function gateways_list($gateways) { 
  36.  
  37. $gateways[$this->gateway] = $this->title; 
  38.  
  39. return $gateways; 
  40.  
  41.  
  42. function toggleactivation() { 
  43.  
  44. $active = get_option('membership_activated_gateways', array()); 
  45.  
  46. if(array_key_exists($this->gateway, $active)) { 
  47. unset($active[$this->gateway]); 
  48.  
  49. update_option('membership_activated_gateways', $active); 
  50.  
  51. return true; 
  52. } else { 
  53. $active[$this->gateway] = true; 
  54.  
  55. update_option('membership_activated_gateways', $active); 
  56.  
  57. return true; 
  58.  
  59.  
  60. function activate() { 
  61.  
  62. $active = get_option('membership_activated_gateways', array()); 
  63.  
  64. if(in_array($this->gateway, $active)) { 
  65. return true; 
  66. } else { 
  67. $active[$this->gateway] = true; 
  68.  
  69. update_option('membership_activated_gateways', $active); 
  70.  
  71. return true; 
  72.  
  73.  
  74. function deactivate() { 
  75.  
  76. $active = get_option('membership_activated_gateways', array()); 
  77.  
  78. if(in_array($this->gateway, $active)) { 
  79. unset($active[$this->gateway]); 
  80.  
  81. update_option('membership_activated_gateways', $active); 
  82.  
  83. return true; 
  84. } else { 
  85. return true; 
  86.  
  87.  
  88. function is_active() { 
  89.  
  90. $active = get_option('membership_activated_gateways', array()); 
  91.  
  92. if(in_array($this->gateway, $active)) { 
  93. return true; 
  94. } else { 
  95. return false; 
  96.  
  97. function settings() { 
  98.  
  99. global $page, $action; 
  100.  
  101. ?> 
  102. <div class='wrap nosubsub'> 
  103. <div class="icon32" id="icon-plugins"><br></div> 
  104. <h2><?php echo __('Edit "', 'membership') . esc_html($this->title) . __('" settings', 'membership'); ?></h2> 
  105.  
  106. <form action='?page=<?php echo $page; ?>' method='post' name='gatewaysettingsform'> 
  107.  
  108. <input type='hidden' name='action' id='action' value='updated' /> 
  109. <input type='hidden' name='gateway' id='gateway' value='<?php echo $this->gateway; ?>' /> 
  110. <?php 
  111. wp_nonce_field('updated-' . $this->gateway); 
  112.  
  113. do_action('M_gateways_settings_' . $this->gateway); 
  114.  
  115. ?> 
  116.  
  117. <p class="submit"> 
  118. <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" /> 
  119. </p> 
  120. </form> 
  121.  
  122. </div> <!-- wrap --> 
  123. <?php 
  124.  
  125.  
  126. function update() { 
  127.  
  128. // default action is to return true 
  129. return true; 
  130.  
  131.  
  132. function get_transactions($type, $startat, $num) { 
  133.  
  134. switch($type) { 
  135.  
  136. case 'past': 
  137. $sql = $this->db->prepare( "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->subscription_transaction} WHERE transaction_status NOT IN ('Pending', 'Future') AND transaction_gateway = %s ORDER BY transaction_ID DESC LIMIT %d, %d", $this->gateway, $startat, $num ); 
  138. break; 
  139. case 'pending': 
  140. $sql = $this->db->prepare( "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->subscription_transaction} WHERE transaction_status IN ('Pending') AND transaction_gateway = %s ORDER BY transaction_ID DESC LIMIT %d, %d", $this->gateway, $startat, $num ); 
  141. break; 
  142. case 'future': 
  143. $sql = $this->db->prepare( "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->subscription_transaction} WHERE transaction_status IN ('Future') AND transaction_gateway = %s ORDER BY transaction_ID DESC LIMIT %d, %d", $this->gateway, $startat, $num ); 
  144. break; 
  145.  
  146.  
  147. return $this->db->get_results( $sql ); 
  148.  
  149.  
  150. function duplicate_transaction($user_id, $sub_id, $amount, $currency, $timestamp, $paypal_ID, $status, $note) { 
  151. $sql = $this->db->prepare( "SELECT transaction_ID FROM {$this->subscription_transaction} WHERE transaction_subscription_ID = %d AND transaction_user_ID = %d AND transaction_paypal_ID = %s AND transaction_stamp = %d LIMIT 1 ", $sub_id, $user_id, $paypal_ID, $timestamp ); 
  152.  
  153. $trans = $this->db->get_var( $sql ); 
  154. if(!empty($trans)) { 
  155. return true; 
  156. } else { 
  157. return false; 
  158.  
  159. function record_transaction($user_id, $sub_id, $amount, $currency, $timestamp, $paypal_ID, $status, $note) { 
  160.  
  161. $data = array(); 
  162. $data['transaction_subscription_ID'] = $sub_id; 
  163. $data['transaction_user_ID'] = $user_id; 
  164. $data['transaction_paypal_ID'] = $paypal_ID; 
  165. $data['transaction_stamp'] = $timestamp; 
  166. $data['transaction_currency'] = $currency; 
  167. $data['transaction_status'] = $status; 
  168. $data['transaction_total_amount'] = (int) round($amount * 100); 
  169. $data['transaction_note'] = $note; 
  170. $data['transaction_gateway'] = $this->gateway; 
  171.  
  172. $existing_id = $this->db->get_var( $this->db->prepare( "SELECT transaction_ID FROM {$this->subscription_transaction} WHERE transaction_paypal_ID = %s LIMIT 1", $paypal_ID ) ); 
  173.  
  174. if(!empty($existing_id)) { 
  175. // Update 
  176. $this->db->update( $this->subscription_transaction, $data, array('transaction_ID' => $existing_id) ); 
  177. } else { 
  178. // Insert 
  179. $this->db->insert( $this->subscription_transaction, $data ); 
  180.  
  181.  
  182. function get_total() { 
  183. return $this->db->get_var( "SELECT FOUND_ROWS();" ); 
  184.  
  185. function transactions() { 
  186.  
  187. global $page, $action, $type; 
  188.  
  189. wp_reset_vars( array('type') ); 
  190.  
  191. if(empty($type)) $type = 'past'; 
  192.  
  193. ?> 
  194. <div class='wrap'> 
  195. <div class="icon32" id="icon-plugins"><br></div> 
  196. <h2><?php echo esc_html($this->title) . __(' transactions', 'membership'); ?></h2> 
  197.  
  198. <ul class="subsubsub"> 
  199. <li><a href="<?php echo add_query_arg('type', 'past'); ?>" class="rbutton <?php if($type == 'past') echo 'current'; ?>"><?php _e('Recent transactions', 'membership'); ?></a> | </li> 
  200. <li><a href="<?php echo add_query_arg('type', 'pending'); ?>" class="rbutton <?php if($type == 'pending') echo 'current'; ?>"><?php _e('Pending transactions', 'membership'); ?></a> | </li> 
  201. <li><a href="<?php echo add_query_arg('type', 'future'); ?>" class="rbutton <?php if($type == 'future') echo 'current'; ?>"><?php _e('Future transactions', 'membership'); ?></a></li> 
  202. </ul> 
  203.  
  204. <?php 
  205. if(has_action('M_gateways_transactions_' . $this->gateway)) { 
  206. do_action('M_gateways_transactions_' . $this->gateway, $type); 
  207. } else { 
  208. $this->mytransactions($type); 
  209.  
  210. ?> 
  211. </div> <!-- wrap --> 
  212. <?php 
  213.  
  214.  
  215. function mytransactions($type = 'past') { 
  216.  
  217. if(empty($_GET['paged'])) { 
  218. $paged = 1; 
  219. } else { 
  220. $paged = ((int) $_GET['paged']); 
  221.  
  222. $startat = ($paged - 1) * 50; 
  223.  
  224. $transactions = $this->get_transactions($type, $startat, 50); 
  225. $total = $this->get_total(); 
  226.  
  227. $columns = array(); 
  228.  
  229. $columns['subscription'] = __('Subscription', 'membership'); 
  230. $columns['user'] = __('User', 'membership'); 
  231. $columns['date'] = __('Date', 'membership'); 
  232. $columns['amount'] = __('Amount', 'membership'); 
  233. $columns['transid'] = __('Transaction id', 'membership'); 
  234. $columns['status'] = __('Status', 'membership'); 
  235. $columns['note'] = __('Notes', 'membership'); 
  236.  
  237. $trans_navigation = paginate_links( array( 
  238. 'base' => add_query_arg( 'paged', '%#%' ),  
  239. 'format' => '',  
  240. 'total' => ceil($total / 50),  
  241. 'current' => $paged 
  242. )); 
  243.  
  244. echo '<div class="tablenav">'; 
  245. if ( $trans_navigation ) echo "<div class='tablenav-pages'>$trans_navigation</div>"; 
  246. echo '</div>'; 
  247. ?> 
  248.  
  249.  
  250. <table cellspacing="0" class="widefat fixed"> 
  251. <thead> 
  252. <tr> 
  253. <?php 
  254. foreach($columns as $key => $col) { 
  255. ?> 
  256. <th style="" class="manage-column column-<?php echo $key; ?>" id="<?php echo $key; ?>" scope="col"><?php echo $col; ?></th> 
  257. <?php 
  258. ?> 
  259. </tr> 
  260. </thead> 
  261.  
  262. <tfoot> 
  263. <tr> 
  264. <?php 
  265. reset($columns); 
  266. foreach($columns as $key => $col) { 
  267. ?> 
  268. <th style="" class="manage-column column-<?php echo $key; ?>" id="<?php echo $key; ?>" scope="col"><?php echo $col; ?></th> 
  269. <?php 
  270. ?> 
  271. </tr> 
  272. </tfoot> 
  273.  
  274. <tbody> 
  275. <?php 
  276. if($transactions) { 
  277. foreach($transactions as $key => $transaction) { 
  278. ?> 
  279. <tr valign="middle" class="alternate"> 
  280. <td class="column-subscription"> 
  281. <?php 
  282. if(class_exists('M_Subscription')) { 
  283. $subscription = new M_Subscription($transaction->transaction_subscription_ID); 
  284. echo $subscription->sub_name(); 
  285. } else { 
  286. echo __('Subscription not found', 'membership'); 
  287. ?> 
  288. </td> 
  289. <td class="column-user"> 
  290. <?php 
  291. if(class_exists('M_Membership')) { 
  292. $member = new M_Membership($transaction->transaction_user_ID); 
  293. echo $member->user_login; 
  294. } else { 
  295. echo __('User not found', 'membership'); 
  296. ?> 
  297. </td> 
  298. <td class="column-date"> 
  299. <?php 
  300. echo date("d-m-Y", $transaction->transaction_stamp); 
  301. ?> 
  302. </td> 
  303. <td class="column-amount"> 
  304. <?php 
  305. $amount = $transaction->transaction_total_amount / 100; 
  306.  
  307. echo $transaction->transaction_currency; 
  308. echo " " . number_format($amount, 2, '.', ', '); 
  309. ?> 
  310. </td> 
  311. <td class="column-transid"> 
  312. <?php 
  313. if(!empty($transaction->transaction_paypal_ID)) { 
  314. echo $transaction->transaction_paypal_ID; 
  315. } else { 
  316. echo __('None yet', 'membership'); 
  317. ?> 
  318. </td> 
  319. <td class="column-transid"> 
  320. <?php 
  321. if(!empty($transaction->transaction_status)) { 
  322. echo $transaction->transaction_status; 
  323. } else { 
  324. echo __('None yet', 'membership'); 
  325. ?> 
  326. </td> 
  327. <td class="column-transid"> 
  328. <?php 
  329. if(!empty($transaction->transaction_note)) { 
  330. echo esc_html($transaction->transaction_note); 
  331. } else { 
  332. echo __('None', 'membership'); 
  333. ?> 
  334. </td> 
  335. </tr> 
  336. <?php 
  337. } else { 
  338. $columncount = count($columns); 
  339. ?> 
  340. <tr valign="middle" class="alternate" > 
  341. <td colspan="<?php echo $columncount; ?>" scope="row"><?php _e('No Transactions have been found, patience is a virtue.', 'membership'); ?></td> 
  342. </tr> 
  343. <?php 
  344. ?> 
  345.  
  346. </tbody> 
  347. </table> 
  348. <?php 
  349.  
  350. function process_payment_return( $gateway ) { 
  351. if( apply_filters( 'membership_override_payment_return_' . $gateway, false ) ) { 
  352. return; 
  353.  
  354. // Payment return 
  355. do_action( 'membership_handle_payment_return_' . $gateway ); 
  356.  
  357. function record_user_gateway( $user_id ) { 
  358. update_user_meta( $user_id, 'membership_signup_gateway', $this->gateway ); 
  359. if($this->issingle) { 
  360. update_user_meta( $user_id, 'membership_signup_gateway_is_single', 'yes' ); 
  361. } else { 
  362. update_user_meta( $user_id, 'membership_signup_gateway_is_single', 'no' ); 
  363.  
  364.  
  365. function display_upgrade_from_free_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  366. // By default there is no default button available 
  367. echo "<form class=''>"; 
  368. echo "<input type='submit' value=' " . __('Upgrades not available', 'membership') . " ' disabled='disabled' class='button blue' />"; 
  369. echo "</form>"; 
  370.  
  371. function display_upgrade_button($pricing, $subscription, $user_id, $fromsub_id = false) { 
  372. // By default there is no default button available 
  373. echo "<form class=''>"; 
  374. echo "<input type='submit' value=' " . __('Upgrades not available', 'membership') . " ' disabled='disabled' class='button blue' />"; 
  375. echo "</form>"; 
  376.  
  377. function display_cancel_button($subscription, $pricing, $user_id) { 
  378. // By default there is no default button available 
  379. echo '<form class="unsubbutton" action="" method="post">'; 
  380. echo "<input type='button' value=' " . __('Unsubscribe not available', 'membership') . " ' disabled='disabled' class='button blue' />"; 
  381. echo "</form>"; 
  382.  
  383. function display_payment_form() { 
  384. die('You Must Override The display_payment_form() in your gateway'); 
  385.  
  386. /** adding extra functions here to handle free subscriptions across a lot of gateways */ 
  387.  
  388. function single_free_button($pricing, $subscription, $user_id, $sublevel = 0) { 
  389.  
  390. global $M_options; 
  391.  
  392. if(empty($M_options['paymentcurrency'])) { 
  393. $M_options['paymentcurrency'] = 'USD'; 
  394.  
  395. $form = ''; 
  396.  
  397. $form .= '<form action="' . M_get_returnurl_permalink() . '" method="post">'; 
  398. $form .= '<input type="hidden" name="custom" value="' . $this->build_custom($user_id, $subscription->id, '0', $sublevel) .'">'; 
  399.  
  400. if($sublevel == 1) { 
  401. $form .= '<input type="hidden" name="action" value="subscriptionsignup" />'; 
  402. $form .= wp_nonce_field('free-sub_' . $subscription->sub_id(), "_wpnonce", false, false); 
  403. $form .= "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  404. $button = get_option( $this->gateway . "_payment_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  405. } else { 
  406. $form .= wp_nonce_field('renew-sub_' . $subscription->sub_id(), "_wpnonce", false, false); 
  407. //$form .= wp_nonce_field('free-sub_' . $subscription->sub_id(), "_wpnonce", false, false); 
  408. $form .= "<input type='hidden' name='action' value='subscriptionsignup' />"; 
  409. $form .= "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  410. $form .= "<input type='hidden' name='subscription' value='" . $subscription->sub_id() . "' />"; 
  411. $form .= "<input type='hidden' name='user' value='" . $user_id . "' />"; 
  412. $form .= "<input type='hidden' name='level' value='" . $sublevel . "' />"; 
  413. $button = get_option( $this->gateway . "_payment_button", 'http://www.paypal.com/en_US/i/btn/x-click-but23.gif' ); 
  414.  
  415. $form .= '<input type="image" name="submit" border="0" src="' . $button . '" alt="PayPal - The safer, easier way to pay online">'; 
  416. $form .= '</form>'; 
  417.  
  418. return $form; 
  419.  
  420.  
  421. function signup_free_subscription($content, $error) { 
  422.  
  423. if(isset($_POST['custom'])) { 
  424. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  425.  
  426. // create_subscription 
  427. $member = new M_Membership($user_id); 
  428. if($member) { 
  429. $member->create_subscription($sub_id, $this->gateway); 
  430.  
  431. do_action('membership_payment_subscr_signup', $user_id, $sub_id); 
  432.  
  433. $content .= '<div id="reg-form">'; // because we can't have an enclosing form for this part 
  434.  
  435. $content .= '<div class="formleft">'; 
  436.  
  437. $message = get_option( $this->gateway . "_completed_message", $this->defaultmessage ); 
  438. $content .= stripslashes($message); 
  439.  
  440. $content .= '</div>'; 
  441.  
  442. $content .= "</div>"; 
  443.  
  444. $content = apply_filters('membership_subscriptionform_signedup', $content, $user_id, $sub_id); 
  445.  
  446. return $content; 
  447.  
  448.  
  449.  
  450.  
  451. function M_is_gateway_active( $gateway ) { 
  452. global $M_Gateways; 
  453.  
  454. if(isset($M_Gateways[$gateway])) { 
  455. return true; 
  456. } else { 
  457. return false; 
  458.  
  459. function M_register_gateway($gateway, $class) { 
  460.  
  461. global $M_Gateways; 
  462.  
  463. if(!is_array($M_Gateways)) { 
  464. $M_Gateways = array(); 
  465.  
  466. $M_Gateways[$gateway] = new $class; 
  467.  
  468.  
  469. function M_get_class_for_gateway($gateway) { 
  470.  
  471. global $M_Gateways; 
  472.  
  473. if(isset($M_Gateways[$gateway])) { 
  474. return $M_Gateways[$gateway]; 
  475. } else { 
  476. return false; 
  477.  
  478.  
  479. ?> 
.