Advertisement
businessdad

EU VAT Assistant - Validate VAT number and update order data

Jun 26th, 2020
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.11 KB | None | 0 0
  1. /**
  2.  * Aelia EU VAT Assistant for WooCommerce - Validate VAT number and store VAT information against
  3.  * an existing order.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * This code shows how to use the EU VAT Assistant to call the VIES service to validate a EU VAT number
  7.  * and how to store or update the VAT information collected during that process. You can wrap this code
  8.  * in a plugin, and wrap it into a function, then call it on Ajax events such as "wp_ajax_woocommerce_calc_line_taxes"
  9.  * or "wp_ajax_collect_order_vat_info".
  10.  * Note: when linking the execution of the code to the Ajax event, you should use a high priority (e.g. 1), so that
  11.  * your custom code runs before the "standard" one. This is important because, normally, Ajax handler terminate the
  12.  * script after running, and that would prevent your code from being executed.
  13.  *
  14.  * @see WC_AJAX::calc_line_taxes()
  15.  * @see Aelia\WC\EU_VAT_Assistant\WC_Aelia_EU_VAT_Assistant::wp_ajax_collect_order_vat_info()
  16.  *
  17.  * IMPORTANT
  18.  * This code will not remove VAT from an existing order that had taxes added to it, nor will it refund
  19.  * VAT that has already been paid. If a customer placed an order and paid VAT, e.g. because they forgot,
  20.  * to enter their VAT number, or because their number, you will have to refund such VAT manually. We recommend
  21.  * to consult your tax advisor to discuss the best way to handle this process, in compliance with your local
  22.  * tax laws.
  23.  *
  24.  * DISCLAIMER
  25.  * THE USE OF THIS CODE IS AT YOUR OWN RISK. You remain fully liable for compliance with tax laws.
  26.  * This code is offered free of charge and there is no warranty for it, to the extent permitted by applicable law.
  27.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  28.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  29.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  30.  * is with you. Should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  31.  *
  32.  * The code is provided as an example and it's not covered by our support service. We won't be able to offer free support
  33.  * in relation to it. Should you need a consultation, or assistance to customise this code, you can contact us to avail
  34.  * of our paid consultation services: https://aelia.co/hire_us
  35.  */
  36.  
  37. // Replace "123" with the ID of an actual order
  38. $order = new  Aelia\WC\EU_VAT_Assistant\Order(123);
  39.  
  40. // Call the VAT validation function. This call assumes that the order
  41. // already has a VAT number stored in its "vat_number" meta
  42. $raw_vies_response = apply_filters('wc_aelia_eu_vat_assistant_validate_vat_number', null, $order->get_billing_country(),    $order->get_meta('vat_number'), true);
  43.  
  44. if(!empty($raw_vies_response)) {
  45.     // Extract the VAT validation
  46.     $vat_number_validated = $raw_vies_response['euva_validation_result'];
  47.     $order->update_meta_data('_vat_number_validated', $vat_number_validated);
  48.  
  49.     // Store the VIES response against the order
  50.     $order->update_meta_data('vies_response', $raw_vies_response);
  51.  
  52.     if(is_array($raw_vies_response) && isset($raw_vies_response['raw_response']) && is_array($raw_vies_response['raw_response'])) {
  53.         // Change all the keys in the response to lower-case. This is to avoid issues with keys being
  54.         // returned like "requestIdentifier", "RequestIdentifier", "requestidentifier" by different versions
  55.         // of the VIES service
  56.         $raw_vies_response['raw_response'] = array_change_key_case($raw_vies_response['raw_response'], CASE_LOWER);
  57.     }
  58.  
  59.     // Store the consultation number, if present
  60.     if(!empty($raw_vies_response['raw_response']['requestidentifier'])) {
  61.         $order->update_meta_data('vies_consultation_number', $raw_vies_response['raw_response']['requestidentifier']);
  62.     }
  63.     else {
  64.         $order->update_meta_data('vies_consultation_number', __('Not returned', 'text-domain'));
  65.     }
  66.     $order->save_meta_data();
  67.  
  68.     // Generate and store details about order VAT
  69.     $order->update_vat_data();
  70.     // Save EU VAT compliance evidence
  71.     $order->store_vat_evidence();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement