M_Gateway

The Membership 2 M Gateway class.

Defined (1)

The class is defined in the following location(s).

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