MS_Addon_Taxamo

Add-On controller for: Taxamo.

Defined (1)

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

/app/addon/taxamo/class-ms-addon-taxamo.php  
  1. class MS_Addon_Taxamo extends MS_Addon { 
  2.  
  3. /** 
  4. * The Add-on ID 
  5. * @since 1.0.0 
  6. */ 
  7. const ID = 'addon_taxamo'; 
  8.  
  9. // Ajax Actions 
  10. const AJAX_SAVE_SETTING = 'addon_taxamo_save'; 
  11. const AJAX_SAVE_USERPROFILE = 'addon_taxamo_profile'; 
  12.  
  13. /** 
  14. * HTML code that is output in the page footer when tax-editor dialog is 
  15. * available. 
  16. * @since 1.0.0 
  17. * @var string 
  18. */ 
  19. static protected $footer_html = ''; 
  20.  
  21. /** 
  22. * Checks if the current Add-on is enabled 
  23. * @since 1.0.0 
  24. * @return bool 
  25. */ 
  26. static public function is_active() { 
  27. return MS_Model_Addon::is_enabled( self::ID ); 
  28.  
  29. /** 
  30. * Returns the Add-on ID (self::ID). 
  31. * @since 1.0.1.0 
  32. * @return string 
  33. */ 
  34. public function get_id() { 
  35. return self::ID; 
  36.  
  37. /** 
  38. * Initializes the Add-on. Always executed. 
  39. * @since 1.0.0 
  40. */ 
  41. public function init() { 
  42. if ( self::is_active() ) { 
  43. $this->add_action( 
  44. 'ms_tax_editor',  
  45. 'tax_editor' 
  46. ); 
  47.  
  48. // Add new settings tab 
  49. $this->add_filter( 
  50. 'ms_controller_settings_get_tabs',  
  51. 'settings_tabs',  
  52. 10, 2 
  53. ); 
  54.  
  55. $this->add_filter( 
  56. 'ms_view_settings_edit_render_callback',  
  57. 'manage_render_callback',  
  58. 10, 3 
  59. ); 
  60.  
  61. // Save settings via ajax 
  62. $this->add_ajax_action( 
  63. self::AJAX_SAVE_SETTING,  
  64. 'ajax_save_setting' 
  65. ); 
  66.  
  67. // Save settings via ajax 
  68. $this->add_ajax_action( 
  69. self::AJAX_SAVE_USERPROFILE,  
  70. 'ajax_save_userprofile',  
  71. true, true 
  72. ); 
  73.  
  74. // Confirm payments with Taxamo 
  75. $this->add_action( 
  76. 'ms_gateway_paypalsingle_payment_processed_' . MS_Model_Invoice::STATUS_PAID,  
  77. 'confirm_payment' 
  78. ); 
  79. $this->add_action( 
  80. 'ms_gateway_paypalstandard_payment_processed_' . MS_Model_Invoice::STATUS_PAID,  
  81. 'confirm_payment' 
  82. ); 
  83.  
  84. // Track the transaction in taxamo 
  85. $this->add_action( 
  86. 'ms_invoice_paid',  
  87. 'invoice_paid' 
  88. ); 
  89.  
  90. // Add taxes to the price, based on users country. 
  91. $this->add_filter( 
  92. 'ms_apply_taxes',  
  93. 'apply_taxes' 
  94. ); 
  95.  
  96. // Set tax-details on a new invoice. 
  97. $this->add_filter( 
  98. 'ms_invoice_tax_rate',  
  99. 'invoice_tax_rate' 
  100. ); 
  101.  
  102. $this->add_filter( 
  103. 'ms_invoice_tax_name',  
  104. 'invoice_tax_name' 
  105. ); 
  106.  
  107. $this->add_filter( 
  108. 'ms_model_invoice_create_before_save',  
  109. 'invoice_tax_profile' 
  110. ); 
  111.  
  112. $this->add_filter( 
  113. 'ms_gateway_stripe_credit_card_saved',  
  114. 'stripe_card_profile' 
  115. ); 
  116.  
  117. /** 
  118. * Registers the Add-On 
  119. * @since 1.0.0 
  120. * @param array $list The Add-Ons list. 
  121. * @return array The updated Add-Ons list. 
  122. */ 
  123. public function register( $list ) { 
  124. $list[ self::ID ] = (object) array( 
  125. 'name' => __( 'Taxamo', 'membership2' ),  
  126. 'description' => __( 'Addresses EU VAT regulations.', 'membership2' ),  
  127. 'icon' => 'wpmui-fa wpmui-fa-euro',  
  128. 'details' => array( 
  129. array( 
  130. 'type' => MS_Helper_Html::TYPE_HTML_TEXT,  
  131. 'title' => __( 'Settings', 'membership2' ),  
  132. 'desc' => __( 'When this Add-on is enabled you will see a new section in the "Settings" page with additional options.', 'membership2' ),  
  133. ),  
  134. ),  
  135. ); 
  136.  
  137. return $list; 
  138.  
  139. /** 
  140. * Returns the Taxamo-Settings model. 
  141. * @since 1.0.0 
  142. * @return MS_Addon_Taxamo_Model 
  143. */ 
  144. static public function model() { 
  145. static $Model = null; 
  146.  
  147. if ( null === $Model ) { 
  148. $Model = MS_Factory::load( 'MS_Addon_Taxamo_Model' ); 
  149.  
  150. return $Model; 
  151.  
  152. /** 
  153. * Add taxamo settings tab in settings page. 
  154. * @since 1.0.0 
  155. * @param array $tabs The current tabs. 
  156. * @return array The filtered tabs. 
  157. */ 
  158. public function settings_tabs( $tabs ) { 
  159. $tabs[ self::ID ] = array( 
  160. 'title' => __( 'Taxamo', 'membership2' ),  
  161. 'url' => MS_Controller_Plugin::get_admin_url( 
  162. 'settings',  
  163. array( 'tab' => self::ID ) 
  164. ),  
  165. ); 
  166.  
  167. return $tabs; 
  168.  
  169. /** 
  170. * Add taxamo settings-view callback. 
  171. * @since 1.0.0 
  172. * @param array $callback The current function callback. 
  173. * @param string $tab The current membership rule tab. 
  174. * @param array $data The data shared to the view. 
  175. * @return array The filtered callback. 
  176. */ 
  177. public function manage_render_callback( $callback, $tab, $data ) { 
  178. if ( self::ID == $tab ) { 
  179. $view = MS_Factory::load( 'MS_Addon_Taxamo_View' ); 
  180. $callback = array( $view, 'render_tab' ); 
  181.  
  182. return $callback; 
  183.  
  184. /** 
  185. * Handle Ajax update custom setting action. 
  186. * @since 1.0.0 
  187. */ 
  188. public function ajax_save_setting() { 
  189. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  190.  
  191. $isset = array( 'field', 'value' ); 
  192. if ( $this->verify_nonce() 
  193. && self::validate_required( $isset, 'POST', false ) 
  194. && $this->is_admin_user() 
  195. ) { 
  196. $model = self::model(); 
  197. lib3()->array->strip_slashes( $_POST, 'value' ); 
  198.  
  199. $model->set( $_POST['field'], $_POST['value'] ); 
  200. $model->save(); 
  201. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  202.  
  203. wp_die( $msg ); 
  204.  
  205. /** 
  206. * Adds taxes to the net-amount. 
  207. * @since 1.0.0 
  208. * @param numeric $net_value Net value 
  209. * @return numeric Gross value 
  210. */ 
  211. public function apply_taxes( $net_value ) { 
  212. $gross_value = 0; 
  213.  
  214. if ( is_numeric( $net_value ) ) { 
  215. $tax = MS_Addon_Taxamo_Api::tax_info( $net_value ); 
  216. $gross_value = $net_value + $tax->amount; 
  217.  
  218. return $gross_value; 
  219.  
  220. /** 
  221. * Return the tax-rate for the users country. 
  222. * @since 1.0.0 
  223. * @param numeric $rate Default rate (0) 
  224. * @return numeric Tax rate to apply (e.g. 20 for 20%) 
  225. */ 
  226. public function invoice_tax_rate( $rate ) { 
  227. $tax = MS_Addon_Taxamo_Api::tax_info(); 
  228.  
  229. return $tax->rate; 
  230.  
  231. /** 
  232. * Return the tax-name for the users country. 
  233. * @since 1.0.0 
  234. * @param string $name Default name (empty string) 
  235. * @return string Tax display-name (e.g. 'EU Standard Tax (20 %)') 
  236. */ 
  237. public function invoice_tax_name( $rate ) { 
  238. $tax = MS_Addon_Taxamo_Api::tax_info(); 
  239.  
  240. return $tax->rate . '% ' . $tax->name; 
  241.  
  242. /** 
  243. * Saves tax-profile to the invoice. 
  244. * @since 1.0.0 
  245. * @param MS_Model_Invoice $invoice The invoice object. 
  246. * @return MS_Model_Invoice 
  247. */ 
  248. public function invoice_tax_profile( $invoice ) { 
  249. $invoice->set_custom_data( 'tax_profile', MS_Addon_Taxamo_Api::get_tax_profile() ); 
  250.  
  251. return $invoice; 
  252.  
  253. /** 
  254. * When a payment was made via stripe we can use the card details as an 
  255. * evidence for the tax-country. 
  256. * @since 1.0.0 
  257. * @param Stripe_Card $card The card details. 
  258. */ 
  259. public function stripe_card_profile( $card ) { 
  260. $card_info = $card->brand . ' Last4: ' . $card->last4 . ' (Stripe ID ' . $card->id . ')'; 
  261.  
  262. $card_country = (object) array( 
  263. 'code' => $card->country,  
  264. ); 
  265.  
  266. MS_Addon_Taxamo_Api::set_tax_profile( 'card_info', $card_info ); 
  267. MS_Addon_Taxamo_Api::set_tax_profile( 'card_country', $card_country ); 
  268.  
  269. /** 
  270. * When an invoice was paid we need to notify taxamo of the transaction. 
  271. * @since 1.0.0 
  272. * @param MS_Model_Invoice $invoice The processed invoice. 
  273. */ 
  274. public function invoice_paid( $invoice ) { 
  275. if ( ! $invoice->is_paid() ) { return; } 
  276. if ( 0 == $invoice->total ) { return; } 
  277.  
  278. $membership = $invoice->get_membership(); 
  279. $member = $invoice->get_member(); 
  280.  
  281. MS_Addon_Taxamo_Api::register_payment( 
  282. $invoice->total, // Transaction amount 
  283. $membership->name, // Transaction title 
  284. $invoice->tax_rate, // Tax-rate 
  285. $invoice->get_invoice_number(), // Internal Transaction ID = Invoice Number 
  286. $member->full_name, // Buyer name 
  287. $member->email, // Buyer email 
  288. $invoice->gateway_id, // Payment gateway 
  289. $invoice->currency, // Currency of invoice 
  290. $invoice->checkout_ip // IP of the user 
  291. ); 
  292.  
  293. /** 
  294. * Load taxamo scripts. 
  295. * @since 1.0.0 
  296. * @param MS_Model_Invoice $invoice Optional. The invoice to edit. 
  297. */ 
  298. public function tax_editor( $invoice ) { 
  299. static $Done = false; 
  300.  
  301. // Only one tax-editor is possible per page. 
  302. if ( $Done ) { return; } 
  303. $Done = true; 
  304.  
  305. $this->add_action( 
  306. 'wp_footer',  
  307. 'tax_editor_footer' 
  308. ); 
  309.  
  310. $plugin_url = MS_Plugin::instance()->url; 
  311.  
  312. wp_enqueue_style( 
  313. 'ms-addon-taxamo',  
  314. $plugin_url . '/app/addon/taxamo/assets/css/taxamo.css' 
  315. ); 
  316.  
  317. wp_enqueue_script( 
  318. 'ms-addon-taxamo',  
  319. $plugin_url . '/app/addon/taxamo/assets/js/taxamo.js',  
  320. array( 'jquery' ) 
  321. ); 
  322.  
  323. $view = MS_Factory::load( 'MS_Addon_Taxamo_Userprofile' ); 
  324. $view->data = apply_filters( 
  325. 'ms_addon_taxamo_editor_data',  
  326. array( 'invoice' => $invoice ),  
  327. $this 
  328. ); 
  329. self::$footer_html .= '<div id="ms-taxamo-wrapper">' . $view->to_html() . '</div>'; 
  330.  
  331. do_action( 'ms_addon_taxamo_enqueue_scripts', $this ); 
  332.  
  333. /** 
  334. * Outputs tax-editor code in the page footer. 
  335. * @since 1.0.0 
  336. */ 
  337. public function tax_editor_footer() { 
  338. echo self::$footer_html; 
  339. ?> 
  340. <script> 
  341. window.ms_taxamo = { 
  342. "ajax_url" : "<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>" 
  343. }; 
  344. </script> 
  345. <?php 
  346.  
  347. /** 
  348. * Handle Ajax action to update a user tax-profile field. 
  349. * @since 1.0.0 
  350. */ 
  351. public function ajax_save_userprofile() { 
  352. $response = ''; 
  353.  
  354. $isset = array( 
  355. 'country_choice',  
  356. 'declared_country',  
  357. 'vat_number',  
  358. ); 
  359.  
  360. if ( $this->verify_nonce() 
  361. && self::validate_required( $isset, 'POST', false ) 
  362. ) { 
  363. $invoice_id = intval( $_POST['invoice_id'] ); 
  364. $invoice = MS_Factory::load( 'MS_Model_Invoice', $invoice_id ); 
  365. $data = $_POST; 
  366. unset( $data['invoice_id'] ); 
  367. unset( $data['action'] ); 
  368. unset( $data['_wpnonce'] ); 
  369.  
  370. $data['declared_country'] = (object) array( 
  371. 'code' => $data['declared_country'],  
  372. ); 
  373.  
  374. foreach ( $data as $field => $value ) { 
  375. $value = apply_filters( 
  376. 'ms_addon_taxamo_userprofile_value',  
  377. $value,  
  378. $field,  
  379. $invoice_id,  
  380. $this 
  381. ); 
  382.  
  383. MS_Addon_Taxamo_Api::set_tax_profile( $field, $value ); 
  384.  
  385. // User profile updated. Now update the tax-rate in the invoice. 
  386. if ( $invoice->is_valid() ) { 
  387. $profile = MS_Addon_Taxamo_Api::get_tax_profile(); 
  388. $invoice->set_custom_data( 'tax_profile', $profile ); 
  389. $invoice->total_amount_changed(); 
  390. $invoice->save(); 
  391.  
  392. $view = MS_Factory::load( 'MS_Addon_Taxamo_Userprofile' ); 
  393. $view->data = apply_filters( 
  394. 'ms_addon_taxamo_editor_data',  
  395. array( 'invoice' => $invoice ),  
  396. $this 
  397. ); 
  398. $response = trim( $view->to_html() ); 
  399.  
  400. wp_die( $response ); 
  401.