/app_old/membershipincludes/gateways/gateway.freesubscriptions.php

  1. <?php 
  2. /** 
  3. Addon Name: Free subscriptions gateway 
  4. Author: Barry (Incsub) 
  5. Author URI: http://caffeinatedb.com 
  6. Gateway ID: freesubscriptions 
  7. */ 
  8.  
  9. class freesubscriptions extends M_Gateway { 
  10.  
  11. var $gateway = 'freesubscriptions'; 
  12. var $title = 'Free Subscriptions'; 
  13. var $issingle = true; 
  14.  
  15. var $defaultmessage = "<h2>Completed: Thank you for signing up</h2>\n<p>\nYour subscription to our site is now set up and you should be able to visit the members only content.\n</p>\n"; 
  16.  
  17. function freesubscriptions() { 
  18.  
  19. if(M_is_gateway_active('paypalsolo')) { 
  20. return; 
  21.  
  22. parent::M_Gateway(); 
  23.  
  24. add_action('M_gateways_settings_' . $this->gateway, array(&$this, 'mysettings')); 
  25.  
  26. // If I want to override the transactions output - then I can use this action 
  27. add_action('M_gateways_transactions_' . $this->gateway, array(&$this, 'mytransactions')); 
  28.  
  29. if($this->is_active()) { 
  30. // Subscription form gateway 
  31. add_action('membership_purchase_button', array(&$this, 'display_subscribe_button'), 1, 3); 
  32. add_filter( 'membership_subscription_form_subscription_process', array(&$this, 'signup_free_subscription'), 10, 2 ); 
  33.  
  34.  
  35.  
  36.  
  37. function mytransactions() { 
  38.  
  39. echo '<div class="tablenav">'; 
  40. echo '</div>'; 
  41.  
  42. echo "<p>" . __('No transactions data for the Free gateway', 'membership') . "</p>"; 
  43.  
  44. function mysettings() { 
  45.  
  46. ?> 
  47. <table class="form-table"> 
  48. <tbody> 
  49. <tr valign="top"> 
  50. <th scope="row"><?php _e('Subscription button', 'membership') ?></th> 
  51. <?php 
  52. $button = get_option( $this->gateway . "_payment_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  53. ?> 
  54. <td><input type="text" name="payment_button" value="<?php esc_attr_e($button); ?>" style='width: 40em;' /> 
  55. <br /> 
  56. </td> 
  57. </tr> 
  58.  
  59. </tbody> 
  60. </table> 
  61. <?php 
  62.  
  63. function build_custom($user_id, $sub_id, $amount) { 
  64.  
  65. $custom = ''; 
  66.  
  67. //fake:user:sub:key 
  68.  
  69. $custom = time() . ':' . $user_id . ':' . $sub_id . ':'; 
  70. $key = md5('MEMBERSHIP' . $amount); 
  71.  
  72. $custom .= $key; 
  73.  
  74. return $custom; 
  75.  
  76.  
  77. function not_yet_display_upgrade_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  78.  
  79. echo '<form class="upgradebutton" action="" method="post">'; 
  80. wp_nonce_field('upgrade-sub_' . $subscription->sub_id()); 
  81. echo "<input type='hidden' name='action' value='upgradefree' />"; 
  82. echo "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  83. echo "<input type='hidden' name='subscription' value='" . $subscription->sub_id() . "' />"; 
  84. echo "<input type='hidden' name='user' value='" . $user_id . "' />"; 
  85. echo "<input type='hidden' name='fromsub_id' value='" . $fromsub_id . "' />"; 
  86. echo "<input type='submit' name='submit' value=' " . __('Upgrade', 'membership') . " ' />"; 
  87. echo "</form>"; 
  88.  
  89. function display_cancel_button($subscription, $pricing, $user_id) { 
  90.  
  91. echo '<form class="unsubbutton" action="" method="post">'; 
  92. wp_nonce_field('cancel-sub_' . $subscription->sub_id()); 
  93. echo "<input type='hidden' name='action' value='unsubscribe' />"; 
  94. echo "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  95. echo "<input type='hidden' name='subscription' value='" . $subscription->sub_id() . "' />"; 
  96. echo "<input type='hidden' name='user' value='" . $user_id . "' />"; 
  97. echo "<input type='submit' name='submit' value=' " . __('Unsubscribe', 'membership') . " ' />"; 
  98. echo "</form>"; 
  99.  
  100. function signup_free_subscription($content, $error) { 
  101.  
  102. if(isset($_POST['custom'])) { 
  103. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  104.  
  105. // create_subscription 
  106. $member = new M_Membership($user_id); 
  107. if($member) { 
  108. $member->create_subscription($sub_id, $this->gateway); 
  109.  
  110. do_action('membership_payment_subscr_signup', $user_id, $sub_id); 
  111.  
  112. $content .= '<div id="reg-form">'; // because we can't have an enclosing form for this part 
  113.  
  114. $content .= '<div class="formleft">'; 
  115.  
  116. $message = get_option( $this->gateway . "_completed_message", $this->defaultmessage ); 
  117. $content .= stripslashes($message); 
  118.  
  119. $content .= '</div>'; 
  120.  
  121. $content .= "</div>"; 
  122.  
  123. $content = apply_filters('membership_subscriptionform_signedup', $content, $user_id, $sub_id); 
  124.  
  125. return $content; 
  126.  
  127.  
  128. function single_free_button($pricing, $subscription, $user_id, $norepeat = false) { 
  129.  
  130. global $M_options; 
  131.  
  132. if(empty($M_options['paymentcurrency'])) { 
  133. $M_options['paymentcurrency'] = 'USD'; 
  134.  
  135. $form = ''; 
  136.  
  137. $form .= '<form action="' . M_get_returnurl_permalink() . '" method="post">'; 
  138. $form .= wp_nonce_field('free-sub_' . $subscription->sub_id(), "_wpnonce", true, false); 
  139. $form .= "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  140. $form .= '<input type="hidden" name="action" value="subscriptionsignup" />'; 
  141. $form .= '<input type="hidden" name="custom" value="' . $this->build_custom($user_id, $subscription->id, '0') .'">'; 
  142.  
  143. $button = get_option( $this->gateway . "_payment_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  144.  
  145. $form .= '<input type="image" name="submit" border="0" src="' . $button . '" alt="PayPal - The safer, easier way to pay online">'; 
  146. $form .= '</form>'; 
  147.  
  148. return $form; 
  149.  
  150.  
  151. function build_subscribe_button($subscription, $pricing, $user_id) { 
  152.  
  153. if(!empty($pricing)) { 
  154.  
  155. $free = true; 
  156. foreach($pricing as $key => $price) { 
  157. if(!empty($price['amount']) && $price['amount'] > 0 ) { 
  158. $free = false; 
  159.  
  160. if($free) { 
  161. return $this->single_free_button($pricing, $subscription, $user_id, true); 
  162.  
  163.  
  164.  
  165. function display_subscribe_button($subscription, $pricing, $user_id) { 
  166.  
  167. echo $this->build_subscribe_button($subscription, $pricing, $user_id); 
  168.  
  169.  
  170. function display_upgrade_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  171.  
  172. echo '<form class="upgradebutton" action="' . M_get_subscription_permalink() . '" method="post">'; 
  173. wp_nonce_field('upgrade-sub_' . $subscription->sub_id()); 
  174. echo "<input type='hidden' name='action' value='upgradefromfree' />"; 
  175. echo "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  176. echo "<input type='hidden' name='subscription' value='" . $subscription->sub_id() . "' />"; 
  177. echo "<input type='hidden' name='user' value='" . $user_id . "' />"; 
  178. echo "<input type='hidden' name='fromsub_id' value='" . $fromsub_id . "' />"; 
  179. echo "<input type='submit' name='submit' value=' " . __('Upgrade', 'membership') . " ' class='button blue' />"; 
  180. echo "</form>"; 
  181.  
  182. function display_upgrade_from_free_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  183. if($pricing[0]['amount'] < 1) { 
  184. // a free first level 
  185. $this->display_upgrade_button($subscription, $pricing, $user_id, $fromsub_id); 
  186. } else { 
  187. //do_action('membership_purchase_button', $subscription, $pricing, $user_id); 
  188. echo "<form class=''>"; 
  189. echo "<input type='submit' value=' " . __('Upgrades not available', 'membership') . " ' disabled='disabled' class='button blue' />"; 
  190. echo "</form>"; 
  191.  
  192.  
  193. function update() { 
  194.  
  195. if(isset($_POST['payment_button'])) { 
  196. update_option( $this->gateway . "_payment_button", $_POST[ 'payment_button' ] ); 
  197. update_option( $this->gateway . "_completed_message", $_POST[ 'completed_message' ] ); 
  198.  
  199. // default action is to return true 
  200. return true; 
  201.  
  202.  
  203. // IPN stuff 
  204. function handle_paypal_return() { 
  205. // PayPal IPN handling code 
  206.  
  207.  
  208.  
  209. M_register_gateway('freesubscriptions', 'freesubscriptions'); 
  210.  
  211. ?> 
.