paypalexpress

Addon Name: PayPal Express Gateway Author: Barry (Incsub) Author URI: http://caffeinatedb.com Gateway ID: paypalexpress.

Defined (1)

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

/app_old/membershipincludes/gateways/gateway.paypalexpress.php  
  1. class paypalexpress extends M_Gateway { 
  2.  
  3. var $gateway = 'paypalexpress'; 
  4. var $title = 'PayPal Express - with Subscriptions'; 
  5.  
  6. function paypalexpress() { 
  7. parent::M_Gateway(); 
  8.  
  9. //echo "booboo"; 
  10.  
  11. add_action('M_gateways_settings_' . $this->gateway, array(&$this, 'mysettings')); 
  12.  
  13. // If I want to override the transactions output - then I can use this action 
  14. //add_action('M_gateways_transactions_' . $this->gateway, array(&$this, 'mytransactions')); 
  15.  
  16. if($this->is_active()) { 
  17. // Subscription form gateway 
  18. add_action('membership_purchase_button', array(&$this, 'display_subscribe_button'), 1, 3); 
  19.  
  20. // Payment return 
  21. add_action('membership_handle_payment_return_' . $this->gateway, array(&$this, 'handle_paypal_return')); 
  22. add_filter( 'membership_subscription_form_subscription_process', array(&$this, 'signup_free_subscription'), 10, 2 ); 
  23.  
  24.  
  25. function mysettings() { 
  26.  
  27. global $M_options; 
  28.  
  29. ?> 
  30. <table class="form-table"> 
  31. <tbody> 
  32. <tr valign="top"> 
  33. <th scope="row"><?php _e('PayPal Email', 'membership') ?></th> 
  34. <td><input type="text" name="paypal_email" value="<?php esc_attr_e(get_option( $this->gateway . "_paypal_email" )); ?>" /> 
  35. <br /> 
  36. </td> 
  37. </tr> 
  38. <tr valign="top"> 
  39. <th scope="row"><?php _e('PayPal Site', 'membership') ?></th> 
  40. <td><select name="paypal_site"> 
  41. <?php 
  42. $paypal_site = get_option( $this->gateway . "_paypal_site" ); 
  43. $sel_locale = empty($paypal_site) ? 'US' : $paypal_site; 
  44. $locales = array( 
  45. 'AU' => __('Australia', 'membership'),  
  46. 'AT' => __('Austria', 'membership'),  
  47. 'BE' => __('Belgium', 'membership'),  
  48. 'CA' => __('Canada', 'membership'),  
  49. 'CN' => __('China', 'membership'),  
  50. 'FR' => __('France', 'membership'),  
  51. 'DE' => __('Germany', 'membership'),  
  52. 'HK' => __('Hong Kong', 'membership'),  
  53. 'IT' => __('Italy', 'membership'),  
  54. 'jp_JP' => __('Japan', 'membership'),  
  55. 'MX' => __('Mexico', 'membership'),  
  56. 'NL' => __('Netherlands', 'membership'),  
  57. 'NZ' => __('New Zealand', 'membership'),  
  58. 'PL' => __('Poland', 'membership'),  
  59. 'SG' => __('Singapore', 'membership'),  
  60. 'ES' => __('Spain', 'membership'),  
  61. 'SE' => __('Sweden', 'membership'),  
  62. 'CH' => __('Switzerland', 'membership'),  
  63. 'GB' => __('United Kingdom', 'membership'),  
  64. 'US' => __('United States', 'membership') 
  65. ); 
  66.  
  67. $locales = apply_filters('membership_gateway_locals', $locales, $this->gateway); 
  68.  
  69. foreach ($locales as $key => $value) { 
  70. echo '<option value="' . esc_attr($key) . '"'; 
  71. if($key == $sel_locale) echo 'selected="selected"'; 
  72. echo '>' . esc_html($value) . '</option>' . "\n"; 
  73. ?> 
  74. </select> 
  75. <br /> 
  76. <?php //_e('Format: 00.00 - Ex: 1.25', 'supporter') ?></td> 
  77. </tr> 
  78. <tr valign="top"> 
  79. <th scope="row"><?php _e('Paypal Currency', 'membership') ?></th> 
  80. <td><?php 
  81. if(empty($M_options['paymentcurrency'])) { 
  82. $M_options['paymentcurrency'] = 'USD'; 
  83. echo esc_html($M_options['paymentcurrency']); ?></td> 
  84. </tr> 
  85. <tr valign="top"> 
  86. <th scope="row"><?php _e('PayPal Mode', 'membership') ?></th> 
  87. <td><select name="paypal_status"> 
  88. <option value="live" <?php if (get_option( $this->gateway . "_paypal_status" ) == 'live') echo 'selected="selected"'; ?>><?php _e('Live Site', 'membership') ?></option> 
  89. <option value="test" <?php if (get_option( $this->gateway . "_paypal_status" ) == 'test') echo 'selected="selected"'; ?>><?php _e('Test Mode (Sandbox)', 'membership') ?></option> 
  90. </select> 
  91. <br /> 
  92. </td> 
  93. </tr> 
  94. <tr valign="top"> 
  95. <th scope="row"><?php _e('Subscription button', 'membership') ?></th> 
  96. <?php 
  97. $button = get_option( $this->gateway . "_paypal_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  98. ?> 
  99. <td><input type="text" name="paypal_button" value="<?php esc_attr_e($button); ?>" style='width: 40em;' /> 
  100. <br /> 
  101. </td> 
  102. </tr> 
  103. <tr valign="top"> 
  104. <th scope="row"><?php _e('Upgrade button', 'membership') ?></th> 
  105. <?php 
  106. $button = get_option( $this->gateway . "_paypal_upgrade_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  107. ?> 
  108. <td><input type="text" name="_paypal_upgrade_button" value="<?php esc_attr_e($button); ?>" style='width: 40em;' /> 
  109. <br /> 
  110. </td> 
  111. </tr> 
  112. <tr valign="top"> 
  113. <th scope="row"><?php _e('Cancel button', 'membership') ?></th> 
  114. <?php 
  115. $button = get_option( $this->gateway . "_paypal_cancel_button", 'https://www.paypal.com/en_US/i/btn/btn_unsubscribe_LG.gif' ); 
  116. ?> 
  117. <td><input type="text" name="_paypal_cancel_button" value="<?php esc_attr_e($button); ?>" style='width: 40em;' /> 
  118. <br /> 
  119. </td> 
  120. </tr> 
  121. </tbody> 
  122. </table> 
  123. <?php 
  124.  
  125. function build_custom($user_id, $sub_id, $amount, $fromsub_id = false) { 
  126.  
  127. global $M_options; 
  128.  
  129. $custom = ''; 
  130.  
  131. //fake:user:sub:key 
  132.  
  133. $custom = time() . ':' . $user_id . ':' . $sub_id . ':'; 
  134. $key = md5('MEMBERSHIP' . apply_filters('membership_amount_' . $M_options['paymentcurrency'], $amount)); 
  135.  
  136. $custom .= $key; 
  137.  
  138. if($fromsub_id !== false) { 
  139. $custom .= ":" . $fromsub_id; 
  140.  
  141. return $custom; 
  142.  
  143.  
  144. function single_sub_button($pricing, $subscription, $user_id, $norepeat = false) { 
  145.  
  146. global $M_options; 
  147.  
  148. if(empty($M_options['paymentcurrency'])) { 
  149. $M_options['paymentcurrency'] = 'USD'; 
  150.  
  151. $form = ''; 
  152.  
  153. //if($pricing[0]['type'] == 'indefinite') $pricing[0]['days'] = 365; 
  154.  
  155. if (get_option( $this->gateway . "_paypal_status" ) == 'live') { 
  156. $form .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">'; 
  157. } else { 
  158. $form .= '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">'; 
  159. $form .= '<input type="hidden" name="charset" value="utf-8">'; 
  160. $form .= '<input type="hidden" name="business" value="' . esc_attr(get_option( $this->gateway . "_paypal_email" )) . '">'; 
  161. $form .= '<input type="hidden" name="cmd" value="_xclick-subscriptions">'; 
  162. $form .= '<input type="hidden" name="item_name" value="' . $subscription->sub_name() . '">'; 
  163. $form .= '<input type="hidden" name="item_number" value="' . $subscription->sub_id() . '">'; 
  164. $form .= '<input type="hidden" name="currency_code" value="' . $M_options['paymentcurrency'] .'">'; 
  165. $form .= '<input type="hidden" name="a3" value="' . apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($pricing[0]['amount'], 2)) . '">'; 
  166. $form .= '<input type="hidden" name="p3" value="' . $pricing[0]['period'] . '">'; 
  167. $form .= '<input type="hidden" name="t3" value="' . strtoupper($pricing[0]['unit']) . '"> <!-- Set recurring payments until canceled. -->'; 
  168.  
  169. $form .= '<input type="hidden" name="custom" value="' . $this->build_custom($user_id, $subscription->id, number_format($pricing[0]['amount'], 2)) .'">'; 
  170.  
  171. $form .= '<input type="hidden" name="return" value="' . apply_filters( 'membership_return_url_' . $this->gateway, M_get_returnurl_permalink()) . '">'; 
  172. $form .= '<input type="hidden" name="cancel_return" value="' . apply_filters( 'membership_cancel_url_' . $this->gateway, M_get_subscription_permalink()) . '">'; 
  173.  
  174. $form .= '<input type="hidden" name="lc" value="' . esc_attr(get_option( $this->gateway . "_paypal_site" )) . '">'; 
  175. $form .= '<input type="hidden" name="notify_url" value="' . apply_filters( 'membership_notify_url_' . $this->gateway, trailingslashit(get_option('home')) . 'paymentreturn/' . esc_attr($this->gateway)) . '">'; 
  176.  
  177. if($norepeat) { 
  178. $form .= '<input type="hidden" name="src" value="0">'; 
  179. } else { 
  180. $form .= '<input type="hidden" name="src" value="1">'; 
  181.  
  182. $button = get_option( $this->gateway . "_paypal_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  183.  
  184. $form .= '<!-- Display the payment button. --> <input type="image" name="submit" border="0" src="' . $button . '" alt="PayPal - The safer, easier way to pay online">'; 
  185. $form .= '<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >'; 
  186. $form .= '</form>'; 
  187.  
  188. return $form; 
  189.  
  190.  
  191. function complex_sub_button($pricing, $subscription, $user_id) { 
  192.  
  193. global $M_options; 
  194.  
  195. if(empty($M_options['paymentcurrency'])) { 
  196. $M_options['paymentcurrency'] = 'USD'; 
  197.  
  198. $form = ''; 
  199.  
  200. if (get_option( $this->gateway . "_paypal_status" ) == 'live') { 
  201. $form .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">'; 
  202. } else { 
  203. $form .= '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">'; 
  204. $form .= '<input type="hidden" name="charset" value="utf-8">'; 
  205. $form .= '<input type="hidden" name="business" value="' . esc_attr(get_option( $this->gateway . "_paypal_email" )) . '">'; 
  206. $form .= '<input type="hidden" name="cmd" value="_xclick-subscriptions">'; 
  207. $form .= '<input type="hidden" name="item_name" value="' . $subscription->sub_name() . '">'; 
  208. $form .= '<input type="hidden" name="item_number" value="' . $subscription->sub_id() . '">'; 
  209. $form .= '<input type="hidden" name="currency_code" value="' . $M_options['paymentcurrency'] .'">'; 
  210.  
  211. // complex bits here 
  212. $count = 1; 
  213. $ff = array(); 
  214. foreach((array) $pricing as $key => $price) { 
  215.  
  216. switch($price['type']) { 
  217.  
  218. case 'finite': if(empty($price['amount'])) $price['amount'] = '0'; 
  219. if($count < 3) { 
  220. $ff['a' . $count] = apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($price['amount'], 2, '.' , '')); 
  221. $ff['p' . $count] = $price['period']; 
  222. $ff['t' . $count] = strtoupper($price['unit']); 
  223. } else { 
  224. // Or last finite is going to be the end of the subscription payments 
  225. $ff['a3'] = apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($price['amount'], 2, '.' , '')); 
  226. $ff['p3'] = $price['period']; 
  227. $ff['t3'] = strtoupper($price['unit']); 
  228. $ff['src'] = '0'; 
  229. $count++; 
  230. break; 
  231.  
  232. case 'indefinite': 
  233. if(empty($price['amount'])) $price['amount'] = '0'; 
  234.  
  235. if($price['amount'] == '0') { 
  236. // The indefinite rule is free, we need to move any previous 
  237. // steps up to this one as we can't have a free a3 
  238. if( isset($ff['a2']) && $ff['a2'] != '0.00' ) { 
  239. // we have some other earlier rule so move it up 
  240. $ff['a3'] = $ff['a2']; 
  241. $ff['p3'] = $ff['p2']; 
  242. $ff['t3'] = $ff['t2']; 
  243. unset($ff['a2']); 
  244. unset($ff['p2']); 
  245. unset($ff['t2']); 
  246. $ff['src'] = '0'; 
  247. } elseif( isset($ff['a1']) && $ff['a1'] != '0.00' ) { 
  248. $ff['a3'] = $ff['a1']; 
  249. $ff['p3'] = $ff['p1']; 
  250. $ff['t3'] = $ff['t1']; 
  251. unset($ff['a1']); 
  252. unset($ff['p1']); 
  253. unset($ff['t1']); 
  254. $ff['src'] = '0'; 
  255. } else { 
  256. $ff['a3'] = apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($price['amount'], 2, '.' , '')); 
  257. $ff['p3'] = 1; 
  258. $ff['t3'] = 'Y'; 
  259. $ff['src'] = '0'; 
  260. break; 
  261. case 'serial': 
  262. if(empty($price['amount'])) $price['amount'] = '0'; 
  263.  
  264. if($price['amount'] == '0') { 
  265. // The serial rule is free, we need to move any previous 
  266. // steps up to this one as we can't have a free a3 
  267. if( isset($ff['a2']) && $ff['a2'] != '0.00' ) { 
  268. // we have some other earlier rule so move it up 
  269. $ff['a3'] = $ff['a2']; 
  270. $ff['p3'] = $ff['p2']; 
  271. $ff['t3'] = $ff['t2']; 
  272. unset($ff['a2']); 
  273. unset($ff['p2']); 
  274. unset($ff['t2']); 
  275. $ff['src'] = '1'; 
  276. } elseif( isset($ff['a1']) && $ff['a1'] != '0.00' ) { 
  277. $ff['a3'] = $ff['a1']; 
  278. $ff['p3'] = $ff['p1']; 
  279. $ff['t3'] = $ff['t1']; 
  280. unset($ff['a1']); 
  281. unset($ff['p1']); 
  282. unset($ff['t1']); 
  283. $ff['src'] = '1'; 
  284. } else { 
  285. $ff['a3'] = apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($price['amount'], 2, '.' , '')); 
  286. $ff['p3'] = $price['period']; 
  287. $ff['t3'] = strtoupper($price['unit']); 
  288. $ff['src'] = '1'; 
  289.  
  290. break; 
  291.  
  292. if(!empty($ff)) { 
  293. foreach($ff as $key => $value) { 
  294. $form .= '<input type="hidden" name="' . $key . '" value="' . $value . '">'; 
  295.  
  296. $form .= '<input type="hidden" name="custom" value="' . $this->build_custom($user_id, $subscription->id, $ff['a3']) .'">'; 
  297.  
  298. // Remainder of the easy bits 
  299.  
  300. $form .= '<input type="hidden" name="return" value="' . apply_filters( 'membership_return_url_' . $this->gateway, M_get_returnurl_permalink()) . '">'; 
  301. $form .= '<input type="hidden" name="cancel_return" value="' . apply_filters( 'membership_cancel_url_' . $this->gateway, M_get_subscription_permalink()) . '">'; 
  302.  
  303.  
  304. $form .= '<input type="hidden" name="lc" value="' . esc_attr(get_option( $this->gateway . "_paypal_site" )) . '">'; 
  305. $form .= '<input type="hidden" name="notify_url" value="' . apply_filters( 'membership_notify_url_' . $this->gateway, trailingslashit(get_option('home')) . 'paymentreturn/' . esc_attr($this->gateway)) . '">'; 
  306.  
  307. $button = get_option( $this->gateway . "_paypal_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  308.  
  309. $form .= '<!-- Display the payment button. --> <input type="image" name="submit" border="0" src="' . $button . '" alt="PayPal - The safer, easier way to pay online">'; 
  310. $form .= '<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >'; 
  311. $form .= '</form>'; 
  312.  
  313. return $form; 
  314.  
  315.  
  316. function build_subscribe_button($subscription, $pricing, $user_id) { 
  317.  
  318. if(!empty($pricing)) { 
  319.  
  320. // check to make sure there is a price in the subscription 
  321. // we don't want to display free ones for a payment system 
  322. $free = true; 
  323. foreach($pricing as $key => $price) { 
  324. if(!empty($price['amount']) && $price['amount'] > 0 ) { 
  325. $free = false; 
  326.  
  327. if(!$free) { 
  328.  
  329. if(count($pricing) == 1) { 
  330. // A basic price or a single subscription 
  331. if(in_array($pricing[0]['type'], array('indefinite', 'finite'))) { 
  332. // one-off payment 
  333. return $this->single_sub_button($pricing, $subscription, $user_id, true); 
  334. } else { 
  335. // simple subscription 
  336. return $this->single_sub_button($pricing, $subscription, $user_id); 
  337. } else { 
  338. // something much more complex 
  339.  
  340. return $this->complex_sub_button($pricing, $subscription, $user_id); 
  341.  
  342. } else { 
  343. // Free subscription - so we'll use the free code 
  344. return $this->single_free_button($pricing, $subscription, $user_id, true); 
  345.  
  346.  
  347.  
  348. function single_upgrade_button($pricing, $subscription, $user_id, $norepeat = false, $fromsub_id = false) { 
  349.  
  350. global $M_options; 
  351.  
  352. if(empty($M_options['paymentcurrency'])) { 
  353. $M_options['paymentcurrency'] = 'USD'; 
  354.  
  355. $form = ''; 
  356.  
  357. //if($pricing[0]['type'] == 'indefinite') $pricing[0]['days'] = 365; 
  358.  
  359. if (get_option( $this->gateway . "_paypal_status" ) == 'live') { 
  360. $form .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">'; 
  361. } else { 
  362. $form .= '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">'; 
  363. $form .= '<input type="hidden" name="charset" value="utf-8">'; 
  364. $form .= '<input type="hidden" name="business" value="' . esc_attr(get_option( $this->gateway . "_paypal_email" )) . '">'; 
  365. $form .= '<input type="hidden" name="cmd" value="_xclick-subscriptions">'; 
  366. $form .= '<input type="hidden" name="item_name" value="' . $subscription->sub_name() . '">'; 
  367. $form .= '<input type="hidden" name="item_number" value="' . $subscription->sub_id() . '">'; 
  368. $form .= '<input type="hidden" name="currency_code" value="' . $M_options['paymentcurrency'] .'">'; 
  369. $form .= '<input type="hidden" name="a3" value="' . apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($pricing[0]['amount'], 2, '.' , '')) . '">'; 
  370. $form .= '<input type="hidden" name="p3" value="' . $pricing[0]['period'] . '">'; 
  371. $form .= '<input type="hidden" name="t3" value="' . strtoupper($pricing[0]['unit']) . '"> <!-- Set recurring payments until canceled. -->'; 
  372.  
  373. $form .= '<input type="hidden" name="custom" value="' . $this->build_custom($user_id, $subscription->id, number_format($pricing[0]['amount'], 2, '.' , ''), $fromsub_id) .'">'; 
  374.  
  375. $form .= '<input type="hidden" name="return" value="' . apply_filters( 'membership_return_url_' . $this->gateway, M_get_returnurl_permalink()) . '">'; 
  376. $form .= '<input type="hidden" name="cancel_return" value="' . apply_filters( 'membership_cancel_url_' . $this->gateway, M_get_subscription_permalink()) . '">'; 
  377.  
  378. $form .= '<input type="hidden" name="lc" value="' . esc_attr(get_option( $this->gateway . "_paypal_site" )) . '">'; 
  379. $form .= '<input type="hidden" name="notify_url" value="' . apply_filters( 'membership_notify_url_' . $this->gateway, trailingslashit(get_option('home')) . 'paymentreturn/' . esc_attr($this->gateway)) . '">'; 
  380.  
  381. if($norepeat) { 
  382. $form .= '<input type="hidden" name="src" value="0">'; 
  383. } else { 
  384. $form .= '<input type="hidden" name="src" value="1">'; 
  385.  
  386. $form .= '<input type="hidden" name="modify" value="2">'; 
  387.  
  388. $button = get_option( $this->gateway . "_paypal_upgrade_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  389.  
  390. $form .= '<!-- Display the payment button. --> <input type="image" name="submit" border="0" src="' . $button . '" alt="PayPal - The safer, easier way to pay online">'; 
  391. $form .= '<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >'; 
  392. $form .= '</form>'; 
  393.  
  394. return $form; 
  395.  
  396.  
  397. function complex_upgrade_button($pricing, $subscription, $user_id, $fromsub_id = false) { 
  398.  
  399. global $M_options; 
  400.  
  401. if(empty($M_options['paymentcurrency'])) { 
  402. $M_options['paymentcurrency'] = 'USD'; 
  403.  
  404. $form = ''; 
  405.  
  406. if (get_option( $this->gateway . "_paypal_status" ) == 'live') { 
  407. $form .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">'; 
  408. } else { 
  409. $form .= '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">'; 
  410. $form .= '<input type="hidden" name="business" value="' . esc_attr(get_option( $this->gateway . "_paypal_email" )) . '">'; 
  411. $form .= '<input type="hidden" name="cmd" value="_xclick-subscriptions">'; 
  412. $form .= '<input type="hidden" name="item_name" value="' . $subscription->sub_name() . '">'; 
  413. $form .= '<input type="hidden" name="item_number" value="' . $subscription->sub_id() . '">'; 
  414. $form .= '<input type="hidden" name="currency_code" value="' . $M_options['paymentcurrency'] .'">'; 
  415.  
  416. // complex bits here 
  417. $count = 1; 
  418. $ff = array(); 
  419. foreach((array) $pricing as $key => $price) { 
  420.  
  421. switch($price['type']) { 
  422.  
  423. case 'finite': if(empty($price['amount'])) $price['amount'] = '0'; 
  424. if($count < 3) { 
  425. $ff['a' . $count] = apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($price['amount'], 2, '.' , '')); 
  426. $ff['p' . $count] = $price['period']; 
  427. $ff['t' . $count] = strtoupper($price['unit']); 
  428. } else { 
  429. // Or last finite is going to be the end of the subscription payments 
  430. $ff['a3'] = number_format($price['amount'], 2, '.' , ''); 
  431. $ff['p3'] = $price['period']; 
  432. $ff['t3'] = strtoupper($price['unit']); 
  433. $ff['src'] = '0'; 
  434. $count++; 
  435. break; 
  436.  
  437. case 'indefinite': 
  438. if(empty($price['amount'])) $price['amount'] = '0'; 
  439.  
  440. if($price['amount'] == '0') { 
  441. // The indefinite rule is free, we need to move any previous 
  442. // steps up to this one as we can't have a free a3 
  443. if( isset($ff['a2']) && $ff['a2'] != '0.00' ) { 
  444. // we have some other earlier rule so move it up 
  445. $ff['a3'] = $ff['a2']; 
  446. $ff['p3'] = $ff['p2']; 
  447. $ff['t3'] = $ff['t2']; 
  448. unset($ff['a2']); 
  449. unset($ff['p2']); 
  450. unset($ff['t2']); 
  451. $ff['src'] = '0'; 
  452. } elseif( isset($ff['a1']) && $ff['a1'] != '0.00' ) { 
  453. $ff['a3'] = $ff['a1']; 
  454. $ff['p3'] = $ff['p1']; 
  455. $ff['t3'] = $ff['t1']; 
  456. unset($ff['a1']); 
  457. unset($ff['p1']); 
  458. unset($ff['t1']); 
  459. $ff['src'] = '0'; 
  460. } else { 
  461. $ff['a3'] = apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($price['amount'], 2, '.' , '')); 
  462. $ff['p3'] = 1; 
  463. $ff['t3'] = 'Y'; 
  464. $ff['src'] = '0'; 
  465. break; 
  466. case 'serial': 
  467. if(empty($price['amount'])) $price['amount'] = '0'; 
  468.  
  469. if($price['amount'] == '0') { 
  470. // The serial rule is free, we need to move any previous 
  471. // steps up to this one as we can't have a free a3 
  472. if( isset($ff['a2']) && $ff['a2'] != '0.00' ) { 
  473. // we have some other earlier rule so move it up 
  474. $ff['a3'] = $ff['a2']; 
  475. $ff['p3'] = $ff['p2']; 
  476. $ff['t3'] = $ff['t2']; 
  477. unset($ff['a2']); 
  478. unset($ff['p2']); 
  479. unset($ff['t2']); 
  480. $ff['src'] = '1'; 
  481. } elseif( isset($ff['a1']) && $ff['a1'] != '0.00' ) { 
  482. $ff['a3'] = $ff['a1']; 
  483. $ff['p3'] = $ff['p1']; 
  484. $ff['t3'] = $ff['t1']; 
  485. unset($ff['a1']); 
  486. unset($ff['p1']); 
  487. unset($ff['t1']); 
  488. $ff['src'] = '1'; 
  489. } else { 
  490. $ff['a3'] = apply_filters('membership_amount_' . $M_options['paymentcurrency'], number_format($price['amount'], 2, '.' , '')); 
  491. $ff['p3'] = $price['period']; 
  492. $ff['t3'] = strtoupper($price['unit']); 
  493. $ff['src'] = '1'; 
  494.  
  495. break; 
  496.  
  497. if(!empty($ff)) { 
  498. foreach($ff as $key => $value) { 
  499. $form .= '<input type="hidden" name="' . $key . '" value="' . $value . '">'; 
  500.  
  501. $form .= '<input type="hidden" name="custom" value="' . $this->build_custom($user_id, $subscription->id, $ff['a3'], $fromsub_id) .'">'; 
  502.  
  503. // Remainder of the easy bits 
  504.  
  505. $form .= '<input type="hidden" name="return" value="' . apply_filters( 'membership_return_url_' . $this->gateway, M_get_returnurl_permalink()) . '">'; 
  506. $form .= '<input type="hidden" name="cancel_return" value="' . apply_filters( 'membership_cancel_url_' . $this->gateway, M_get_subscription_permalink()) . '">'; 
  507.  
  508.  
  509. $form .= '<input type="hidden" name="lc" value="' . esc_attr(get_option( $this->gateway . "_paypal_site" )) . '">'; 
  510. $form .= '<input type="hidden" name="notify_url" value="' . trailingslashit(get_option('home')) . 'paymentreturn/' . esc_attr($this->gateway) . '">'; 
  511.  
  512. $form .= '<input type="hidden" name="modify" value="2">'; 
  513.  
  514. $button = get_option( $this->gateway . "_paypal_upgrade_button", 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif' ); 
  515.  
  516. $form .= '<!-- Display the payment button. --> <input type="image" name="submit" border="0" src="' . $button . '" alt="PayPal - The safer, easier way to pay online">'; 
  517. $form .= '<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >'; 
  518. $form .= '</form>'; 
  519.  
  520. return $form; 
  521.  
  522.  
  523. function build_upgrade_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  524.  
  525. if(!empty($pricing)) { 
  526.  
  527. // check to make sure there is a price in the subscription 
  528. // we don't want to display free ones for a payment system 
  529. $free = true; 
  530. foreach($pricing as $key => $price) { 
  531. if(!empty($price['amount']) && $price['amount'] > 0 ) { 
  532. $free = false; 
  533.  
  534. if(!$free) { 
  535. if(count($pricing) == 1) { 
  536. // A basic price or a single subscription 
  537. if(in_array($pricing[0]['type'], array('indefinite', 'finite'))) { 
  538. // one-off payment 
  539. return $this->single_upgrade_button($pricing, $subscription, $user_id, true, $fromsub_id); 
  540. } else { 
  541. // simple subscription 
  542. return $this->single_upgrade_button($pricing, $subscription, $user_id, false, $fromsub_id); 
  543. } else { 
  544. // something much more complex 
  545. return $this->complex_upgrade_button($pricing, $subscription, $user_id, $fromsub_id); 
  546.  
  547.  
  548.  
  549.  
  550. function display_subscribe_button($subscription, $pricing, $user_id) { 
  551. echo $this->build_subscribe_button($subscription, $pricing, $user_id); 
  552.  
  553.  
  554. function display_upgrade_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  555. echo $this->build_upgrade_button($subscription, $pricing, $user_id, $fromsub_id); 
  556.  
  557. function display_cancel_button($subscription, $pricing, $user_id) { 
  558.  
  559. if($pricing[0]['amount'] < 1) { 
  560. // a free first level, so we can just cancel without having to go to paypal 
  561. echo '<form class="unsubbutton" action="" method="post">'; 
  562. wp_nonce_field('cancel-sub_' . $subscription->sub_id()); 
  563. echo "<input type='hidden' name='action' value='unsubscribe' />"; 
  564. echo "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  565. echo "<input type='hidden' name='subscription' value='" . $subscription->sub_id() . "' />"; 
  566. echo "<input type='hidden' name='user' value='" . $user_id . "' />"; 
  567. echo "<input type='submit' name='submit' value=' " . __('Unsubscribe', 'membership') . " ' class='button blue' />"; 
  568. echo "</form>"; 
  569. } else { 
  570. $form = ''; 
  571.  
  572. if (get_option( $this->gateway . "_paypal_status" ) == 'live') { 
  573. $form .= '<a class="unsubbutton" href="https://www.paypal.com/cgi-bin/webscr'; 
  574. } else { 
  575. $form .= '<a class="unsubbutton" href="https://www.sandbox.paypal.com/cgi-bin/webscr'; 
  576.  
  577. $form .= '?cmd=_subscr-find&alias=' . urlencode(esc_attr(get_option( $this->gateway . "_paypal_email" ))) . '">'; 
  578.  
  579. $button = get_option( $this->gateway . "_paypal_cancel_button", 'https://www.paypal.com/en_US/i/btn/btn_unsubscribe_LG.gif' ); 
  580. $form .= '<img border="0" src="' . esc_attr($button) . '">'; 
  581. $form .= '</a>'; 
  582.  
  583. echo $form; 
  584.  
  585.  
  586. function update() { 
  587.  
  588. if(isset($_POST['paypal_email'])) { 
  589. update_option( $this->gateway . "_paypal_email", $_POST[ 'paypal_email' ] ); 
  590. update_option( $this->gateway . "_paypal_site", $_POST[ 'paypal_site' ] ); 
  591. update_option( $this->gateway . "_currency", (isset($_POST[ 'currency' ])) ? $_POST[ 'currency' ] : 'USD' ); 
  592. update_option( $this->gateway . "_paypal_status", $_POST[ 'paypal_status' ] ); 
  593. update_option( $this->gateway . "_paypal_button", $_POST[ 'paypal_button' ] ); 
  594. update_option( $this->gateway . "_paypal_upgrade_button", $_POST[ '_paypal_upgrade_button' ] ); 
  595. update_option( $this->gateway . "_paypal_cancel_button", $_POST[ '_paypal_cancel_button' ] ); 
  596.  
  597. // default action is to return true 
  598. return true; 
  599.  
  600.  
  601. function display_free_upgrade_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  602.  
  603. echo '<form class="upgradebutton" action="' . M_get_subscription_permalink() . '" method="post">'; 
  604. wp_nonce_field('upgrade-sub_' . $subscription->sub_id()); 
  605. echo "<input type='hidden' name='action' value='upgradesolo' />"; 
  606. echo "<input type='hidden' name='gateway' value='" . $this->gateway . "' />"; 
  607. echo "<input type='hidden' name='subscription' value='" . $subscription->sub_id() . "' />"; 
  608. echo "<input type='hidden' name='user' value='" . $user_id . "' />"; 
  609. echo "<input type='hidden' name='fromsub_id' value='" . $fromsub_id . "' />"; 
  610. echo "<input type='submit' name='submit' value=' " . __('Upgrade', 'membership') . " ' class='button blue' />"; 
  611. echo "</form>"; 
  612.  
  613. function display_upgrade_from_free_button($subscription, $pricing, $user_id, $fromsub_id = false) { 
  614.  
  615. if(!empty($pricing)) { 
  616.  
  617. $free = true; 
  618. foreach($pricing as $key => $price) { 
  619. if(!empty($price['amount']) && $price['amount'] > 0 ) { 
  620. $free = false; 
  621.  
  622. if($free) { 
  623.  
  624. $this->display_free_upgrade_button($subscription, $pricing, $user_id, $fromsub_id); 
  625. } else { 
  626. $this->display_upgrade_button($subscription, $pricing, $user_id, $fromsub_id); 
  627.  
  628.  
  629.  
  630. // IPN stuff 
  631. function handle_paypal_return() { 
  632. // PayPal IPN handling code 
  633.  
  634. if ((isset($_POST['payment_status']) || isset($_POST['txn_type'])) && isset($_POST['custom'])) { 
  635.  
  636. if (get_option( $this->gateway . "_paypal_status" ) == 'live') { 
  637. $domain = 'https://www.paypal.com'; 
  638. } else { 
  639. $domain = 'https://www.sandbox.paypal.com'; 
  640.  
  641. $req = 'cmd=_notify-validate'; 
  642. if (!isset($_POST)) $_POST = $HTTP_POST_VARS; 
  643. foreach ($_POST as $k => $v) { 
  644. if (get_magic_quotes_gpc()) $v = stripslashes($v); 
  645. $req .= '&' . $k . '=' . $v; 
  646.  
  647. $header = 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n" 
  648. . 'Content-Type: application/x-www-form-urlencoded' . "\r\n" 
  649. . 'Content-Length: ' . strlen($req) . "\r\n" 
  650. . "\r\n"; 
  651.  
  652. @set_time_limit(60); 
  653. if ($conn = @fsockopen($domain, 80, $errno, $errstr, 30)) { 
  654. fputs($conn, $header . $req); 
  655. socket_set_timeout($conn, 30); 
  656.  
  657. $response = ''; 
  658. $close_connection = false; 
  659. while (true) { 
  660. if (feof($conn) || $close_connection) { 
  661. fclose($conn); 
  662. break; 
  663.  
  664. $st = @fgets($conn, 4096); 
  665. if ($st === false) { 
  666. $close_connection = true; 
  667. continue; 
  668.  
  669. $response .= $st; 
  670.  
  671. $error = ''; 
  672. $lines = explode("\n", str_replace("\r\n", "\n", $response)); 
  673. // looking for: HTTP/1.1 200 OK 
  674. if (count($lines) == 0) $error = 'Response Error: Header not found'; 
  675. else if (substr($lines[0], -7) != ' 200 OK') $error = 'Response Error: Unexpected HTTP response'; 
  676. else { 
  677. // remove HTTP header 
  678. while (count($lines) > 0 && trim($lines[0]) != '') array_shift($lines); 
  679.  
  680. // first line will be empty, second line will have the result 
  681. if (count($lines) < 2) $error = 'Response Error: No content found in transaction response'; 
  682. else if (strtoupper(trim($lines[1])) != 'VERIFIED') $error = 'Response Error: Unexpected transaction response'; 
  683.  
  684. if ($error != '') { 
  685. echo $error; 
  686. exit; 
  687.  
  688. // handle cases that the system must ignore 
  689. //if ($_POST['payment_status'] == 'In-Progress' || $_POST['payment_status'] == 'Partially-Refunded') exit; 
  690. $new_status = false; 
  691. // process PayPal response 
  692. switch ($_POST['payment_status']) { 
  693. case 'Partially-Refunded': 
  694. break; 
  695.  
  696. case 'In-Progress': 
  697. break; 
  698.  
  699. case 'Completed': 
  700. case 'Processed': 
  701. // case: successful payment 
  702. $amount = $_POST['mc_gross']; 
  703. $currency = $_POST['mc_currency']; 
  704. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  705.  
  706. $this->record_transaction($user_id, $sub_id, $amount, $currency, $timestamp, $_POST['txn_id'], $_POST['payment_status'], ''); 
  707.  
  708. // Added for affiliate system link 
  709. do_action('membership_payment_processed', $user_id, $sub_id, $amount, $currency, $_POST['txn_id']); 
  710. break; 
  711.  
  712. case 'Reversed': 
  713. // case: charge back 
  714. $note = __('Last transaction has been reversed. Reason: Payment has been reversed (charge back)', 'membership'); 
  715. $amount = $_POST['mc_gross']; 
  716. $currency = $_POST['mc_currency']; 
  717. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  718.  
  719. $this->record_transaction($user_id, $sub_id, $amount, $currency, $timestamp, $_POST['txn_id'], $_POST['payment_status'], $note); 
  720.  
  721. $member = new M_Membership($user_id); 
  722. if($member) { 
  723. $member->expire_subscription($sub_id); 
  724. if(defined('MEMBERSHIP_DEACTIVATE_USER_ON_CANCELATION') && MEMBERSHIP_DEACTIVATE_USER_ON_CANCELATION == true ) { 
  725. $member->deactivate(); 
  726.  
  727. do_action('membership_payment_reversed', $user_id, $sub_id, $amount, $currency, $_POST['txn_id']); 
  728. break; 
  729.  
  730. case 'Refunded': 
  731. // case: refund 
  732. $note = __('Last transaction has been reversed. Reason: Payment has been refunded', 'membership'); 
  733. $amount = $_POST['mc_gross']; 
  734. $currency = $_POST['mc_currency']; 
  735. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  736.  
  737. $this->record_transaction($user_id, $sub_id, $amount, $currency, $timestamp, $_POST['txn_id'], $_POST['payment_status'], $note); 
  738.  
  739. $member = new M_Membership($user_id); 
  740. if($member) { 
  741. $member->expire_subscription($sub_id); 
  742.  
  743. do_action('membership_payment_refunded', $user_id, $sub_id, $amount, $currency, $_POST['txn_id']); 
  744. break; 
  745.  
  746. case 'Denied': 
  747. // case: denied 
  748. $note = __('Last transaction has been reversed. Reason: Payment Denied', 'membership'); 
  749. $amount = $_POST['mc_gross']; 
  750. $currency = $_POST['mc_currency']; 
  751. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  752.  
  753. $this->record_transaction($user_id, $sub_id, $amount, $currency, $timestamp, $_POST['txn_id'], $_POST['payment_status'], $note); 
  754.  
  755. $member = new M_Membership($user_id); 
  756. if($member) { 
  757. $member->expire_subscription($sub_id); 
  758. if(defined('MEMBERSHIP_DEACTIVATE_USER_ON_CANCELATION') && MEMBERSHIP_DEACTIVATE_USER_ON_CANCELATION == true ) { 
  759. $member->deactivate(); 
  760.  
  761. do_action('membership_payment_denied', $user_id, $sub_id, $amount, $currency, $_POST['txn_id']); 
  762. break; 
  763.  
  764. case 'Pending': 
  765. // case: payment is pending 
  766. $pending_str = array( 
  767. 'address' => __('Customer did not include a confirmed shipping address', 'membership'),  
  768. 'authorization' => __('Funds not captured yet', 'membership'),  
  769. 'echeck' => __('eCheck that has not cleared yet', 'membership'),  
  770. 'intl' => __('Payment waiting for aproval by service provider', 'membership'),  
  771. 'multi-currency' => __('Payment waiting for service provider to handle multi-currency process', 'membership'),  
  772. 'unilateral' => __('Customer did not register or confirm his/her email yet', 'membership'),  
  773. 'upgrade' => __('Waiting for service provider to upgrade the PayPal account', 'membership'),  
  774. 'verify' => __('Waiting for service provider to verify his/her PayPal account', 'membership'),  
  775. '*' => '' 
  776. ); 
  777. $reason = @$_POST['pending_reason']; 
  778. $note = __('Last transaction is pending. Reason: ', 'membership') . (isset($pending_str[$reason]) ? $pending_str[$reason] : $pending_str['*']); 
  779. $amount = $_POST['mc_gross']; 
  780. $currency = $_POST['mc_currency']; 
  781. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  782.  
  783. $this->record_transaction($user_id, $sub_id, $amount, $currency, $timestamp, $_POST['txn_id'], $_POST['payment_status'], $note); 
  784.  
  785. do_action('membership_payment_pending', $user_id, $sub_id, $amount, $currency, $_POST['txn_id']); 
  786. break; 
  787.  
  788. default: 
  789. // case: various error cases 
  790.  
  791. //check for subscription details 
  792. switch ($_POST['txn_type']) { 
  793. case 'subscr_signup': 
  794. // start the subscription 
  795. $amount = $_POST['mc_amount3']; 
  796. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  797.  
  798. $newkey = md5('MEMBERSHIP' . $amount); 
  799. if($key != $newkey) { 
  800. $member = new M_Membership($user_id); 
  801. if($member) { 
  802. if(defined('MEMBERSHIP_DEACTIVATE_USER_ON_CANCELATION') && MEMBERSHIP_DEACTIVATE_USER_ON_CANCELATION == true ) { 
  803. $member->deactivate(); 
  804. } else { 
  805. // create_subscription 
  806. $member = new M_Membership($user_id); 
  807. if($member) { 
  808. $member->create_subscription($sub_id, $this->gateway); 
  809.  
  810. do_action('membership_payment_subscr_signup', $user_id, $sub_id); 
  811. break; 
  812.  
  813. case 'subscr_modify': 
  814. // modify the subscription 
  815. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  816.  
  817. // create_subscription 
  818. $member = new M_Membership($user_id); 
  819. if($member) { 
  820. // Remove the old subscription 
  821. $member->drop_subscription($sub_id); 
  822. // Join the new subscription 
  823. $member->create_subscription((int) $_POST['item_number'], $this->gateway); 
  824. // Timestamp the update 
  825. update_user_meta( $user_id, '_membership_last_upgraded', time()); 
  826.  
  827. do_action('membership_payment_subscr_signup', $user_id, $sub_id); 
  828. break; 
  829.  
  830. case 'subscr_cancel': 
  831. // mark for removal 
  832. list($timestamp, $user_id, $sub_id, $key) = explode(':', $_POST['custom']); 
  833.  
  834. $member = new M_Membership($user_id); 
  835. if($member) { 
  836. $member->mark_for_expire($sub_id); 
  837.  
  838. do_action('membership_payment_subscr_cancel', $user_id, $sub_id); 
  839. break; 
  840.  
  841. case 'new_case': 
  842. // a dispute 
  843. if($_POST['case_type'] == 'dispute') { 
  844. // immediately suspend the account 
  845. $member = new M_Membership($user_id); 
  846. if($member) { 
  847. $member->deactivate(); 
  848.  
  849. do_action('membership_payment_new_case', $user_id, $sub_id, $_POST['case_type']); 
  850. break; 
  851.  
  852. } else { 
  853. // Did not find expected POST variables. Possible access attempt from a non PayPal site. 
  854. header('Status: 404 Not Found'); 
  855. echo 'Error: Missing POST variables. Identification is not possible.'; 
  856. exit; 
  857.