MS_Controller_Gateway

Gateway controller.

Defined (1)

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

/app/controller/class-ms-controller-gateway.php  
  1. class MS_Controller_Gateway extends MS_Controller { 
  2.  
  3. /** 
  4. * AJAX action constants. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const AJAX_ACTION_TOGGLE_GATEWAY = 'toggle_gateway'; 
  9. const AJAX_ACTION_UPDATE_GATEWAY = 'update_gateway'; 
  10.  
  11. /** 
  12. * Allowed actions to execute in template_redirect hook. 
  13. * @since 1.0.0 
  14. * @var string 
  15. */ 
  16. private $allowed_actions = array( 'update_card', 'purchase_button' ); 
  17.  
  18. /** 
  19. * Prepare the gateway controller. 
  20. * @since 1.0.0 
  21. */ 
  22. public function __construct() { 
  23. parent::__construct(); 
  24.  
  25. $this->add_action( 'template_redirect', 'process_actions', 1 ); 
  26.  
  27. $this->add_action( 'ms_controller_gateway_settings_render_view', 'gateway_settings_edit' ); 
  28.  
  29. $this->add_action( 'ms_view_shortcode_invoice_purchase_button', 'purchase_button', 10, 2 ); 
  30. $this->add_action( 'ms_view_frontend_payment_purchase_button', 'purchase_button', 10, 2 ); 
  31.  
  32. $this->add_action( 'ms_controller_frontend_signup_gateway_form', 'gateway_form_mgr', 1 ); 
  33. $this->add_action( 'ms_controller_frontend_signup_process_purchase', 'process_purchase', 1 ); 
  34. $this->add_filter( 'ms_view_shortcode_membershipsignup_cancel_button', 'cancel_button', 10, 2 ); 
  35.  
  36. $this->add_action( 'ms_view_shortcode_account_card_info', 'card_info' ); 
  37.  
  38. $this->add_action( 'pre_get_posts', 'handle_payment_return', 1 ); 
  39. $this->add_action( 'ms_gateway_transaction_log', 'log_transaction', 10, 8 ); 
  40.  
  41. $this->add_action( 'ms_controller_frontend_enqueue_scripts', 'enqueue_scripts' ); 
  42.  
  43. $this->add_ajax_action( self::AJAX_ACTION_TOGGLE_GATEWAY, 'toggle_ajax_action' ); 
  44. $this->add_ajax_action( self::AJAX_ACTION_UPDATE_GATEWAY, 'ajax_action_update_gateway' ); 
  45.  
  46. /** 
  47. * Handle URI actions for registration. 
  48. * Matches returned 'action' to method to execute. 
  49. * Related action hooks: 
  50. * - template_redirect 
  51. * @since 1.0.0 
  52. */ 
  53. public function process_actions() { 
  54. $action = $this->get_action(); 
  55.  
  56. /** 
  57. * If $action is set, then call relevant method. 
  58. * Methods: 
  59. * @see $allowed_actions property 
  60. */ 
  61. if ( ! empty( $action ) 
  62. && method_exists( $this, $action ) 
  63. && in_array( $action, $this->allowed_actions ) 
  64. ) { 
  65. $this->$action(); 
  66.  
  67. /** 
  68. * Handle Ajax toggle action. 
  69. * Related action hooks: 
  70. * - wp_ajax_toggle_gateway 
  71. * @since 1.0.0 
  72. */ 
  73. public function toggle_ajax_action() { 
  74. $msg = 0; 
  75.  
  76. $fields = array( 'gateway_id' ); 
  77. if ( $this->verify_nonce() 
  78. && self::validate_required( $fields ) 
  79. && $this->is_admin_user() 
  80. ) { 
  81. $msg = $this->gateway_list_do_action( 
  82. 'toggle_activation',  
  83. array( $_POST['gateway_id'] ) 
  84. ); 
  85.  
  86. wp_die( $msg ); 
  87.  
  88. /** 
  89. * Handle Ajax update gateway action. 
  90. * Related action hooks: 
  91. * - wp_ajax_update_gateway 
  92. * @since 1.0.0 
  93. */ 
  94. public function ajax_action_update_gateway() { 
  95. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  96.  
  97. $fields = array( 'action', 'gateway_id', 'field', 'value' ); 
  98. if ( $this->verify_nonce() 
  99. && self::validate_required( $fields ) 
  100. && $this->is_admin_user() 
  101. ) { 
  102. lib3()->array->strip_slashes( $_POST, 'value' ); 
  103.  
  104. $msg = $this->gateway_list_do_action( 
  105. $_POST['action'],  
  106. array( $_POST['gateway_id'] ),  
  107. array( $_POST['field'] => $_POST['value'] ) 
  108. ); 
  109.  
  110. wp_die( $msg ); 
  111.  
  112. /** 
  113. * Show gateway settings page. 
  114. * Related action hooks: 
  115. * - ms_controller_gateway_settings_render_view 
  116. * @since 1.0.0 
  117. */ 
  118. public function gateway_settings_edit( $gateway_id ) { 
  119. if ( ! empty( $gateway_id ) 
  120. && MS_Model_Gateway::is_valid_gateway( $gateway_id ) 
  121. ) { 
  122. switch ( $gateway_id ) { 
  123. case MS_Gateway_Manual::ID: 
  124. $view = MS_Factory::create( 'MS_Gateway_Manual_View_Settings' ); 
  125. break; 
  126.  
  127. case MS_Gateway_Paypalsingle::ID: 
  128. $view = MS_Factory::create( 'MS_Gateway_Paypalsingle_View_Settings' ); 
  129. break; 
  130.  
  131. case MS_Gateway_Paypalstandard::ID: 
  132. $view = MS_Factory::create( 'MS_Gateway_Paypalstandard_View_Settings' ); 
  133. break; 
  134.  
  135. case MS_Gateway_Authorize::ID: 
  136. $view = MS_Factory::create( 'MS_Gateway_Authorize_View_Settings' ); 
  137. break; 
  138.  
  139. case MS_Gateway_Stripe::ID: 
  140. $view = MS_Factory::create( 'MS_Gateway_Stripe_View_Settings' ); 
  141. break; 
  142.  
  143. default: 
  144. // Empty form... 
  145. $view = MS_Factory::create( 'MS_View' ); 
  146. break; 
  147.  
  148. $data = array( 
  149. 'model' => MS_Model_Gateway::factory( $gateway_id ),  
  150. 'action' => 'edit',  
  151. ); 
  152.  
  153. $view->data = apply_filters( 
  154. 'ms_gateway_view_settings_edit_data',  
  155. $data 
  156. ); 
  157. $view = apply_filters( 
  158. 'ms_gateway_view_settings_edit',  
  159. $view,  
  160. $gateway_id 
  161. ); 
  162.  
  163. $view->render(); 
  164.  
  165. /** 
  166. * Handle Payment Gateway list actions. 
  167. * @since 1.0.0 
  168. * @param string $action The action to execute. 
  169. * @param int[] $gateways The gateways IDs to process. 
  170. * @param mixed[] $fields The data to process. 
  171. */ 
  172. public function gateway_list_do_action( $action, $gateways, $fields = null ) { 
  173. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  174. if ( ! $this->is_admin_user() ) { 
  175. return $msg; 
  176.  
  177. foreach ( $gateways as $gateway_id ) { 
  178. $gateway = MS_Model_Gateway::factory( $gateway_id ); 
  179.  
  180. switch ( $action ) { 
  181. case 'toggle_activation': 
  182. $gateway->active = ! $gateway->active; 
  183. $gateway->save(); 
  184. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  185.  
  186. /** 
  187. * Hook called after a gateway-status was toggled. 
  188. * @since 1.0.0 
  189. */ 
  190. do_action( 'ms_gateway_toggle_' . $gateway_id, $gateway ); 
  191. break; 
  192.  
  193. case 'edit': 
  194. case 'update_gateway': 
  195. foreach ( $fields as $field => $value ) { 
  196. $gateway->$field = trim( $value ); 
  197. $gateway->save(); 
  198.  
  199. /** 
  200. * $settings->is_global_payments_set is used to hide global 
  201. * payment settings in the membership setup payment step 
  202. */ 
  203. if ( $gateway->is_configured() ) { 
  204. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  205. $settings->is_global_payments_set = true; 
  206. $settings->save(); 
  207. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  208. } else { 
  209. $msg = MS_Helper_Settings::SETTINGS_MSG_UNCONFIGURED; 
  210.  
  211. /** 
  212. * Hook called after a gateway-settings were modified. 
  213. * @since 1.0.0 
  214. */ 
  215. do_action( 'ms_gateway_changed_' . $gateway_id, $gateway ); 
  216. break; 
  217.  
  218. return apply_filters( 
  219. 'ms_controller_gateway_gateway_list_do_action',  
  220. $msg,  
  221. $action,  
  222. $gateways,  
  223. $fields,  
  224. $this 
  225. ); 
  226.  
  227. /** 
  228. * Show gateway purchase button. 
  229. * Related action hooks: 
  230. * - ms_view_frontend_payment_purchase_button 
  231. * - ms_view_shortcode_invoice_purchase_button 
  232. * @since 1.0.0 
  233. */ 
  234. public function purchase_button( $subscription, $invoice ) { 
  235. // Get only active gateways 
  236. $gateways = MS_Model_Gateway::get_gateways( true ); 
  237. $data = array(); 
  238.  
  239. $membership = $subscription->get_membership(); 
  240. $is_free = false; 
  241. if ( $membership->is_free() ) { $is_free = true; } 
  242. elseif ( 0 == $invoice->total ) { $is_free = true; } 
  243. elseif ( $invoice->uses_trial ) { $is_free = true; } 
  244.  
  245. // show gateway purchase button for every active gateway 
  246. foreach ( $gateways as $gateway ) { 
  247. $view = null; 
  248.  
  249. // Skip gateways that are not configured. 
  250. if ( ! $gateway->is_configured() ) { continue; } 
  251. if ( ! $membership->can_use_gateway( $gateway->id ) ) { continue; } 
  252.  
  253. $data['ms_relationship'] = $subscription; 
  254. $data['gateway'] = $gateway; 
  255. $data['step'] = MS_Controller_Frontend::STEP_PROCESS_PURCHASE; 
  256.  
  257. // Free membership, show only free gateway 
  258. if ( $is_free ) { 
  259. if ( MS_Gateway_Free::ID !== $gateway->id ) { 
  260. continue; 
  261. // Skip free gateway 
  262. elseif ( MS_Gateway_Free::ID === $gateway->id ) { 
  263. continue; 
  264.  
  265. $view_class = get_class( $gateway ) . '_View_Button'; 
  266. $view = MS_Factory::create( $view_class ); 
  267.  
  268. if ( MS_Gateway_Authorize::ID == $gateway->id ) { 
  269. /** 
  270. * set additional step for authorize.net (gateway specific form) 
  271. * @todo change to use popup, instead of another step (like stripe) 
  272. */ 
  273. $data['step'] = 'gateway_form'; 
  274.  
  275. if ( ! empty( $view ) ) { 
  276. $view = apply_filters( 
  277. 'ms_gateway_view_button',  
  278. $view,  
  279. $gateway->id 
  280. ); 
  281.  
  282. $view->data = apply_filters( 
  283. 'ms_gateway_view_button_data',  
  284. $data,  
  285. $gateway->id 
  286. ); 
  287.  
  288. $html = apply_filters( 
  289. 'ms_controller_gateway_purchase_button_'. $gateway->id,  
  290. $view->to_html(),  
  291. $subscription,  
  292. $this 
  293. ); 
  294.  
  295. echo $html; 
  296.  
  297.  
  298. /** 
  299. * Show gateway purchase button. 
  300. * Related action hooks: 
  301. * - ms_view_shortcode_membershipsignup_cancel_button 
  302. * @since 1.0.0 
  303. */ 
  304. public function cancel_button( $button, $subscription ) { 
  305. $view = null; 
  306. $data = array(); 
  307. $data['ms_relationship'] = $subscription; 
  308. $new_button = null; 
  309.  
  310. switch ( $subscription->gateway_id ) { 
  311. case MS_Gateway_Paypalstandard::ID: 
  312. $view = MS_Factory::create( 'MS_Gateway_Paypalstandard_View_Cancel' ); 
  313. $data['gateway'] = $subscription->get_gateway(); 
  314. break; 
  315.  
  316. case MS_Gateway_Authorize::ID: 
  317. case MS_Gateway_Paypalsingle::ID: 
  318. case MS_Gateway_Stripe::ID: 
  319. case MS_Gateway_Free::ID: 
  320. case MS_Gateway_Manual::ID: 
  321. default: 
  322. break; 
  323. $view = apply_filters( 'ms_gateway_view_cancel_button', $view ); 
  324.  
  325. if ( $view && is_a( $view, 'MS_View' ) ) { 
  326. $view->data = apply_filters( 
  327. 'ms_gateway_view_cancel_button_data',  
  328. $data 
  329. ); 
  330.  
  331. $new_button = $view->get_button(); 
  332.  
  333. if ( ! $new_button ) { 
  334. $new_button = $button; 
  335.  
  336. return apply_filters( 
  337. 'ms_controller_gateway_cancel_button',  
  338. $new_button,  
  339. $subscription,  
  340. $this 
  341. ); 
  342.  
  343. /** 
  344. * Set hook to handle gateway extra form to commit payments. 
  345. * Related action hooks: 
  346. * - ms_controller_frontend_signup_gateway_form 
  347. * @since 1.0.0 
  348. */ 
  349. public function gateway_form_mgr() { 
  350. // Display gateway form 
  351. $this->add_filter( 'the_content', 'gateway_form', 10 ); 
  352.  
  353. // Enqueue styles and scripts used 
  354. $this->add_action( 'wp_enqueue_scripts', 'enqueue_scripts' ); 
  355.  
  356. /** 
  357. * Handles gateway extra form to commit payments. 
  358. * Related filter hooks: 
  359. * - the_content 
  360. * @since 1.0.0 
  361. * @param string $content The page content to filter. 
  362. * @return string The filtered content. 
  363. */ 
  364. public function gateway_form( $content ) { 
  365. $data = array(); 
  366. $html = ''; 
  367.  
  368. // Do not parse the form when building the excerpt. 
  369. global $wp_current_filter; 
  370. if ( in_array( 'get_the_excerpt', $wp_current_filter ) ) { 
  371. return ''; 
  372.  
  373. $fields = array( 'gateway', 'ms_relationship_id' ); 
  374. if ( self::validate_required( $fields ) 
  375. && MS_Model_Gateway::is_valid_gateway( $_POST['gateway'] ) 
  376. ) { 
  377. $data['gateway'] = $_POST['gateway']; 
  378. $data['ms_relationship_id'] = $_POST['ms_relationship_id']; 
  379. $view = null; 
  380.  
  381. $subscription = MS_Factory::load( 
  382. 'MS_Model_Relationship',  
  383. $_POST['ms_relationship_id'] 
  384. ); 
  385.  
  386. switch ( $_POST['gateway'] ) { 
  387. case MS_Gateway_Authorize::ID: 
  388. $member = $subscription->get_member(); 
  389. $view = MS_Factory::create( 'MS_Gateway_Authorize_View_Form' ); 
  390. $gateway = MS_Model_Gateway::factory( MS_Gateway_Authorize::ID ); 
  391. $data['countries'] = $gateway->get_country_codes(); 
  392.  
  393. $data['action'] = $this->get_action(); 
  394.  
  395. if ( 'update_card' == $this->get_action() ) { 
  396. // Only new card option available on update card action. 
  397. $data['cim_profiles'] = array(); 
  398. } else { 
  399. // show existing credit card. 
  400. $data['cim_profiles'] = $gateway->get_cim_profile( $member ); 
  401.  
  402. lib3()->array->strip_slashes( $_POST, 'auth_error' ); 
  403.  
  404. $data['cim_payment_profile_id'] = $gateway->get_cim_payment_profile_id( $member ); 
  405. $data['auth_error'] = ! empty( $_POST['auth_error'] ) ? $_POST['auth_error'] : ''; 
  406. break; 
  407.  
  408. default: 
  409. break; 
  410.  
  411. $view = apply_filters( 
  412. 'ms_gateway_view_form',  
  413. $view,  
  414. $_POST['gateway'],  
  415. $subscription 
  416. ); 
  417.  
  418. if ( $view && is_a( $view, 'MS_View' ) ) { 
  419. $view->data = apply_filters( 
  420. 'ms_gateway_view_form_data',  
  421. $data 
  422. ); 
  423.  
  424. $html = $view->to_html(); 
  425.  
  426. return apply_filters( 
  427. 'ms_controller_gateway_form',  
  428. $html,  
  429. $this 
  430. ); 
  431.  
  432. /** 
  433. * Process purchase using gateway. 
  434. * Related Action Hooks: 
  435. * - ms_controller_frontend_signup_process_purchase 
  436. * @since 1.0.0 
  437. */ 
  438. public function process_purchase() { 
  439. $fields = array( 'gateway', 'ms_relationship_id' ); 
  440.  
  441. lib3()->array->equip_request( 'gateway', 'ms_relationship_id' ); 
  442.  
  443. $valid = true; 
  444. $nonce_name = $_REQUEST['gateway'] . '_' . $_REQUEST['ms_relationship_id']; 
  445.  
  446. if ( ! self::validate_required( $fields, 'any' ) ) { 
  447. $valid = false; 
  448. $err = 'GAT-01 (invalid fields)'; 
  449. } elseif ( ! MS_Model_Gateway::is_valid_gateway( $_REQUEST['gateway'] ) ) { 
  450. $valid = false; 
  451. $err = 'GAT-02 (invalid gateway)'; 
  452. } elseif ( ! $this->verify_nonce( $nonce_name, 'any' ) ) { 
  453. $valid = false; 
  454. $err = 'GAT-03 (invalid nonce)'; 
  455.  
  456. if ( $valid ) { 
  457. $subscription = MS_Factory::load( 
  458. 'MS_Model_Relationship',  
  459. $_REQUEST['ms_relationship_id'] 
  460. ); 
  461.  
  462. $gateway_id = $_REQUEST['gateway']; 
  463. $gateway = MS_Model_Gateway::factory( $gateway_id ); 
  464.  
  465. try { 
  466. $invoice = $gateway->process_purchase( $subscription ); 
  467.  
  468. // If invoice is successfully paid, redirect to welcome page. 
  469. if ( $invoice->is_paid() 
  470. || ( $invoice->uses_trial 
  471. && MS_Model_Invoice::STATUS_BILLED == $invoice->status 
  472. ) { 
  473. // Make sure to respect the single-membership rule 
  474. $this->validate_membership_states( $subscription ); 
  475.  
  476. // Redirect user to the Payment-Completed page. 
  477. if ( ! defined( 'IS_UNIT_TEST' ) ) { 
  478. MS_Model_Pages::redirect_to( 
  479. MS_Model_Pages::MS_PAGE_REG_COMPLETE,  
  480. array( 'ms_relationship_id' => $subscription->id ) 
  481. ); 
  482. } elseif ( MS_Gateway_Manual::ID == $gateway_id ) { 
  483. // For manual gateway payments. 
  484. $this->add_action( 'the_content', 'purchase_info_content' ); 
  485. } else { 
  486. // Something went wrong, the payment was not successful. 
  487. $this->add_action( 'the_content', 'purchase_error_content' ); 
  488. catch ( Exception $e ) { 
  489. MS_Helper_Debug::log( $e->getMessage() ); 
  490.  
  491. switch ( $gateway_id ) { 
  492. case MS_Gateway_Authorize::ID: 
  493. $_POST['auth_error'] = $e->getMessage(); 
  494. // call action to step back 
  495. do_action( 'ms_controller_frontend_signup_gateway_form' ); 
  496. break; 
  497.  
  498. case MS_Gateway_Stripe::ID: 
  499. // Stripeplan IS PRO ONLY! 
  500. $_POST['error'] = sprintf( 
  501. __( 'Error: %s', 'membership2' ),  
  502. $e->getMessage() 
  503. ); 
  504.  
  505. // Hack to send the error message back to the payment_table. 
  506. MS_Plugin::instance()->controller->controllers['frontend']->add_action( 
  507. 'the_content',  
  508. 'payment_table', 1 
  509. ); 
  510. break; 
  511.  
  512. default: 
  513. do_action( 'ms_controller_gateway_form_error', $e ); 
  514. $this->add_action( 'the_content', 'purchase_error_content' ); 
  515. break; 
  516. } else { 
  517. MS_Helper_Debug::log( 'Error Code ' . $err ); 
  518.  
  519. $this->add_action( 'the_content', 'purchase_error_content' ); 
  520.  
  521. // Hack to show signup page in case of errors 
  522. $ms_page = MS_Model_Pages::get_page( MS_Model_Pages::MS_PAGE_REGISTER ); 
  523.  
  524. if ( $ms_page ) { 
  525. // During unit-testing the $ms_page object might be empty. 
  526. global $wp_query; 
  527. $wp_query->query_vars['page_id'] = $ms_page->ID; 
  528. $wp_query->query_vars['post_type'] = 'page'; 
  529.  
  530. do_action( 
  531. 'ms_controller_gateway_process_purchase_after',  
  532. $this 
  533. ); 
  534.  
  535. /** 
  536. * Make sure that we respect the Single-Membership rule. 
  537. * This rule is active when the "Multiple-Memberships" Add-on is DISABLED. 
  538. * @since 1.0.0 
  539. * @param MS_Model_Relationship $new_relationship 
  540. */ 
  541. protected function validate_membership_states( $new_relationship ) { 
  542. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MULTI_MEMBERSHIPS ) ) { 
  543. // Multiple memberships allowed. No need to check anything. 
  544. return; 
  545.  
  546. $cancel_these = array( 
  547. MS_Model_Relationship::STATUS_TRIAL,  
  548. MS_Model_Relationship::STATUS_ACTIVE,  
  549. MS_Model_Relationship::STATUS_PENDING,  
  550. ); 
  551.  
  552. $member = $new_relationship->get_member(); 
  553. foreach ( $member->subscriptions as $subscription ) { 
  554. if ( $subscription->id === $new_relationship->id ) { continue; } 
  555. if ( in_array( $subscription->status, $cancel_these ) ) { 
  556. $subscription->cancel_membership(); 
  557.  
  558. /** 
  559. * Show signup page with custom content. 
  560. * This is used by manual gateway (overridden) to show payment info. 
  561. * Related action hooks: 
  562. * @since 1.0.0 
  563. * @param string $content The page content to filter. 
  564. * @return string The filtered content. 
  565. */ 
  566. public function purchase_info_content( $content ) { 
  567. return apply_filters( 
  568. 'ms_controller_gateway_purchase_info_content',  
  569. $content,  
  570. $this 
  571. ); 
  572.  
  573. /** 
  574. * Show error message in the signup page. 
  575. * Related action hooks: 
  576. * @since 1.0.0 
  577. */ 
  578. public function purchase_error_content( $content ) { 
  579. return apply_filters( 
  580. 'ms_controller_gateway_purchase_error_content',  
  581. __( 'Sorry, your signup request has failed. Try again.', 'membership2' ),  
  582. $content,  
  583. $this 
  584. ); 
  585.  
  586. /** 
  587. * Handle payment gateway return IPNs. 
  588. * Used by Paypal gateways. 
  589. * A redirection rule is set up in the main MS_Plugin object 
  590. * (protected_content.php): 
  591. * /ms-payment-return/XYZ becomes index.php?paymentgateway=XYZ 
  592. * Related action hooks: 
  593. * - pre_get_posts 
  594. * @todo Review how this works when we use OAuth API's with gateways. 
  595. * @since 1.0.0 
  596. * @param WP_Query $wp_query The WordPress query object 
  597. */ 
  598. public function handle_payment_return( $wp_query ) { 
  599. // Do not check custom loops. 
  600. if ( ! $wp_query->is_main_query() ) { return; } 
  601.  
  602. if ( ! empty( $wp_query->query_vars['paymentgateway'] ) ) { 
  603. $gateway = $wp_query->query_vars['paymentgateway']; 
  604.  
  605. /** 
  606. * In 1.1.0 the underscore in payment gateway names was removed. 
  607. * To compensate for this we need to continue listen to these old 
  608. * gateway-names. 
  609. */ 
  610. switch ( $gateway ) { 
  611. case 'paypal_single': $gateway = 'paypalsingle'; break; 
  612. case 'paypal_standard': $gateway = 'paypalstandard'; break; 
  613. case 'paypal-single': $gateway = 'paypalsingle'; break; 
  614. case 'paypal-standard': $gateway = 'paypalstandard'; break; 
  615. case 'paypalsolo': $gateway = 'paypalsingle'; break; // M1 
  616. case 'paypalexpress': $gateway = 'paypalstandard'; break; //M1 
  617.  
  618. do_action( 'lib2_debug_log', 'Incoming Payment Notification for "' . $gateway . '"' ); 
  619. do_action( 'lib2_debug_log', $_POST ); 
  620.  
  621. if ( MS_Model_Gateway::is_active( $gateway ) ) { 
  622. $action = 'ms_gateway_handle_payment_return_' . $gateway; 
  623. do_action( $action ); 
  624. } else { 
  625. // Log the payment attempt when the gateway is not active. 
  626. if ( MS_Model_Gateway::is_valid_gateway( $gateway ) ) { 
  627. $note = __( 'Gateway is inactive', 'membership2' ); 
  628. } else { 
  629. $note = sprintf( 
  630. __( 'Unknown Gateway: %s', 'membership2' ),  
  631. $gateway 
  632. ); 
  633.  
  634. do_action( 
  635. 'ms_gateway_transaction_log',  
  636. $gateway, // gateway ID 
  637. 'handle', // request|process|handle 
  638. false, // success flag 
  639. 0, // subscription ID 
  640. 0, // invoice ID 
  641. 0, // charged amount 
  642. $note, // Descriptive text 
  643. '' // External ID 
  644. ); 
  645.  
  646. /** 
  647. * Show gateway credit card information. 
  648. * If a card is used, show it in account's page. 
  649. * Related action hooks: 
  650. * - ms_view_shortcode_account_card_info 
  651. * @since 1.0.0 
  652. * @param mixed $data The data passed to hooked view. 
  653. */ 
  654. public function card_info( $data = null ) { 
  655. if ( ! empty( $data['gateway'] ) && is_array( $data['gateway'] ) ) { 
  656. $gateways = array(); 
  657.  
  658. foreach ( $data['gateway'] as $ms_relationship_id => $gateway ) { 
  659. // avoid duplicates 
  660. if ( ! in_array( $gateway->id, $gateways ) ) { 
  661. $gateways[] = $gateway->id; 
  662. } else { 
  663. continue; 
  664. $view = null; 
  665.  
  666. switch ( $gateway->id ) { 
  667. case MS_Gateway_Stripe::ID: 
  668. $member = MS_Model_Member::get_current_member(); 
  669. $data['stripe'] = $member->get_gateway_profile( 
  670. $gateway->id 
  671. ); 
  672.  
  673. if ( empty( $data['stripe']['card_exp'] ) ) { 
  674. continue 2; 
  675.  
  676. $view = MS_Factory::create( 'MS_Gateway_Stripe_View_Card' ); 
  677. $data['member'] = $member; 
  678. $data['publishable_key'] = $gateway->get_publishable_key(); 
  679. $data['ms_relationship_id'] = $ms_relationship_id; 
  680. $data['gateway'] = $gateway; 
  681. break; 
  682.  
  683. case MS_Gateway_Authorize::ID: 
  684. $member = MS_Model_Member::get_current_member(); 
  685. $data['authorize'] = $member->get_gateway_profile( 
  686. $gateway->id 
  687. ); 
  688.  
  689. if ( empty( $data['authorize']['card_exp'] ) ) { 
  690. continue 2; 
  691.  
  692. $view = MS_Factory::create( 'MS_Gateway_Authorize_View_Card' ); 
  693. $data['member'] = $member; 
  694. $data['ms_relationship_id'] = $ms_relationship_id; 
  695. $data['gateway'] = $gateway; 
  696. break; 
  697.  
  698. default: 
  699. break; 
  700.  
  701. if ( ! empty( $view ) ) { 
  702. $view = apply_filters( 
  703. 'ms_gateway_view_change_card',  
  704. $view,  
  705. $gateway->id 
  706. ); 
  707. $view->data = apply_filters( 
  708. 'ms_gateway_view_change_card_data',  
  709. $data,  
  710. $gateway->id 
  711. ); 
  712.  
  713. $html = $view->to_html(); 
  714. echo '' . $html; 
  715.  
  716. /** 
  717. * Handle update credit card information in gateway. 
  718. * Used to change credit card info in account's page. 
  719. * Related action hooks: 
  720. * - template_redirect 
  721. * @since 1.0.0 
  722. */ 
  723. public function update_card() { 
  724. if ( ! empty( $_POST['gateway'] ) ) { 
  725. $gateway = MS_Model_Gateway::factory( $_POST['gateway'] ); 
  726. $member = MS_Model_Member::get_current_member(); 
  727.  
  728. switch ( $gateway->id ) { 
  729. case MS_Gateway_Stripe::ID: 
  730. if ( ! empty( $_POST['stripeToken'] ) && $this->verify_nonce() ) { 
  731. lib3()->array->strip_slashes( $_POST, 'stripeToken' ); 
  732.  
  733. $gateway->add_card( $member, $_POST['stripeToken'] ); 
  734. if ( ! empty( $_POST['ms_relationship_id'] ) ) { 
  735. $ms_relationship = MS_Factory::load( 
  736. 'MS_Model_Relationship',  
  737. $_POST['ms_relationship_id'] 
  738. ); 
  739. MS_Model_Event::save_event( 
  740. MS_Model_Event::TYPE_UPDATED_INFO,  
  741. $ms_relationship 
  742. ); 
  743.  
  744. wp_safe_redirect( 
  745. esc_url_raw( add_query_arg( array( 'msg' => 1 ) ) ) 
  746. ); 
  747. exit; 
  748. break; 
  749.  
  750. case MS_Gateway_Authorize::ID: 
  751. if ( $this->verify_nonce() ) { 
  752. do_action( 
  753. 'ms_controller_frontend_signup_gateway_form',  
  754. $this 
  755. ); 
  756. } elseif ( ! empty( $_POST['ms_relationship_id'] ) 
  757. && $this->verify_nonce( $_POST['gateway'] .'_' . $_POST['ms_relationship_id'] ) 
  758. ) { 
  759. $gateway->update_cim_profile( $member ); 
  760. $gateway->save_card_info( $member ); 
  761. if ( ! empty( $_POST['ms_relationship_id'] ) ) { 
  762. $ms_relationship = MS_Factory::load( 
  763. 'MS_Model_Relationship',  
  764. $_POST['ms_relationship_id'] 
  765. ); 
  766. MS_Model_Event::save_event( 
  767. MS_Model_Event::TYPE_UPDATED_INFO,  
  768. $ms_relationship 
  769. ); 
  770.  
  771. wp_safe_redirect( 
  772. esc_url_raw( add_query_arg( array( 'msg' => 1 ) ) ) 
  773. ); 
  774. exit; 
  775. break; 
  776.  
  777. default: 
  778. break; 
  779.  
  780. do_action( 
  781. 'ms_controller_gateway_update_card',  
  782. $this 
  783. ); 
  784.  
  785. /** 
  786. * Saves transaction details to the database. The transaction logs can later 
  787. * be displayed in the Billings section. 
  788. * @since 1.0.0 
  789. * @internal Action handler for 'ms_gateway_transaction_log' 
  790. * @param string $gateway_id The gateway ID. 
  791. * @param string $method Following values: 
  792. * "handle": IPN response 
  793. * "process": Process order (i.e. user comes from Payment screen) 
  794. * "request": Automatically request recurring payment 
  795. * @param bool $success True means that the transaction was paid/successful. 
  796. * False indicates an error. 
  797. * NULL indicates a message that was intentionally skipped. 
  798. * @param int $subscription_id 
  799. * @param int $invoice_id 
  800. * @param float $amount Payment amount. 
  801. * @param string $notes Additional text to describe the transaction or error. 
  802. * @param string $external_id The gateways transaction ID. 
  803. */ 
  804. public function log_transaction( $gateway_id, $method, $success, $subscription_id, $invoice_id, $amount, $notes, $external_id ) { 
  805. $log = MS_Factory::create( 'MS_Model_Transactionlog' ); 
  806. $log->description = $notes; 
  807. $log->gateway_id = $gateway_id; 
  808. $log->method = $method; 
  809. $log->success = $success; 
  810. $log->subscription_id = $subscription_id; 
  811. $log->invoice_id = $invoice_id; 
  812. $log->amount = $amount; 
  813. $log->external_id = $external_id; 
  814. $log->save(); 
  815.  
  816. /** 
  817. * Adds CSS and javascript 
  818. * @since 1.0.0 
  819. */ 
  820. public function enqueue_scripts( $step = null ) { 
  821. if ( empty( $step ) && ! empty( $_POST['step'] ) ) { 
  822. $step = $_POST['step']; 
  823.  
  824. lib3()->array->equip_post( 'gateway' ); 
  825. $gateway_id = $_POST['gateway']; 
  826.  
  827. switch ( $step ) { 
  828. case MS_Controller_Frontend::STEP_GATEWAY_FORM: 
  829. case MS_Controller_Frontend::STEP_PROCESS_PURCHASE: 
  830. if ( MS_Gateway_Authorize::ID == $gateway_id ) { 
  831. wp_enqueue_script( 'jquery-validate' ); 
  832.  
  833. $data = array( 
  834. 'ms_init' => array( 'gateway_authorize' ),  
  835. ); 
  836.  
  837. lib3()->ui->add( 'core' ); 
  838. lib3()->ui->add( 'select' ); 
  839. lib3()->ui->data( 'ms_data', $data ); 
  840. wp_enqueue_script( 'ms-public' ); 
  841. break; 
  842.