Changeset 2430169
- Timestamp:
- 12/02/2020 11:22:36 AM (5 years ago)
- Location:
- posturinn
- Files:
-
- 2 added
- 3 edited
- 9 copied
-
tags/1.0.9 (copied) (copied from posturinn/trunk)
-
tags/1.0.9/inc/admin.class.php (copied) (copied from posturinn/trunk/inc/admin.class.php) (11 diffs)
-
tags/1.0.9/inc/admin.php (copied) (copied from posturinn/trunk/inc/admin.php)
-
tags/1.0.9/inc/api.class.php (added)
-
tags/1.0.9/inc/functions.php (copied) (copied from posturinn/trunk/inc/functions.php)
-
tags/1.0.9/languages/postis-is_IS.mo (copied) (copied from posturinn/trunk/languages/postis-is_IS.mo)
-
tags/1.0.9/languages/postis-is_IS.po (copied) (copied from posturinn/trunk/languages/postis-is_IS.po)
-
tags/1.0.9/languages/postis.pot (copied) (copied from posturinn/trunk/languages/postis.pot)
-
tags/1.0.9/readme.txt (copied) (copied from posturinn/trunk/readme.txt) (1 diff)
-
tags/1.0.9/wc-posturinn-shipping-api.php (copied) (copied from posturinn/trunk/wc-posturinn-shipping-api.php) (2 diffs)
-
trunk/inc/admin.class.php (modified) (11 diffs)
-
trunk/inc/api.class.php (added)
-
trunk/readme.txt (modified) (1 diff)
-
trunk/wc-posturinn-shipping-api.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
posturinn/tags/1.0.9/inc/admin.class.php
r2415877 r2430169 14 14 15 15 /* 16 * this var use to get domain name16 * this var use to get API requests 17 17 */ 18 var $domain; 19 20 /* 21 * this var use to get domain name 22 */ 23 var $apikey; 24 25 /* 26 * this var use to get tracking shipment endpoint 27 */ 28 var $tracking_ep; 29 30 /* 31 * this var use to get create shipment endpoint 32 */ 33 var $create_shipment_ep; 34 35 /* 36 * this var use to get postbox endpoint 37 */ 38 var $postboxes_ep; 39 40 /* 41 * this var use to get pdf endpoint 42 */ 43 var $pdf_ep; 44 45 /* 46 * this var use to get print shipment endpoint 47 */ 48 var $print_shipment_ep; 49 18 var $api; 50 19 51 20 function __construct() { 52 21 53 $this ->domain = $_SERVER['HTTP_HOST']; 54 55 $this ->apikey = postis_get_settings( 'apikey' ); 56 57 $this ->tracking_ep = postis_get_endpoints( 'tracking' ); 58 59 $this ->create_shipment_ep = postis_get_endpoints( 'create_shipment' ); 60 61 $this ->postboxes_ep = postis_get_endpoints( 'postboxes' ); 62 63 $this ->pdf_ep = postis_get_endpoints( 'pdf' ); 64 65 $this ->print_shipment_ep = postis_get_endpoints( 'print' ); 22 $this->api = new POSTIS_API(); 66 23 67 24 add_filter( 'manage_edit-shop_order_columns', array($this, 'create_order_column'), 20); … … 78 35 add_action('wp_ajax_postis_shipment_options', array($this, 'shipment_options') ); 79 36 80 add_action( 'admin_post_postis_pdf_action', array($this, ' print_shipment_pdf') );37 add_action( 'admin_post_postis_pdf_action', array($this, 'get_shipment_pdf') ); 81 38 82 39 add_action( 'admin_init', array($this, 'create_metabox') ); … … 239 196 $order_id = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0; 240 197 241 $numberofitems = isset($_REQUEST['numberofitems']) ? intval($_REQUEST['numberofitems']) : ''; 242 $shipmnet_desc = isset($_REQUEST['description']) ? sanitize_text_field($_REQUEST['description']) : ''; 243 244 // Get Dynamic Options 245 $dynamic_opt_control = array(); 246 if(isset($_REQUEST['optional_services']) && is_array($_REQUEST['optional_services'])){ 247 $dynamic_shipment_options = array_map('sanitize_text_field', $_REQUEST['optional_services']); 248 } 249 250 foreach($dynamic_shipment_options as $opt_name => $opt_val){ 251 if ( isset($dynamic_shipment_options[$opt_name]) && $dynamic_shipment_options[$opt_name] == 'on' ) { 252 $dynamic_opt_control[$opt_name] = true; 253 }else{ 254 $dynamic_opt_control[$opt_name] = $opt_val; 255 } 256 } 257 258 // international shipment content meta 259 $new_contents_meta = array(); 260 if(isset($_REQUEST['contents']) && is_array($_REQUEST['contents'])){ 261 foreach ($_REQUEST['contents'] as $item_id => $contents_meta) { 262 263 $new_contents_meta[$item_id] = array_map('sanitize_text_field', $contents_meta); 264 } 265 } 266 267 // Get order meta 268 $order = wc_get_order($order_id); 269 270 // User's Shipping data 271 $ship_methods = $order->get_shipping_methods(); 272 $shipping_customerName = $order->get_formatted_shipping_full_name(); 273 $shipping_address1 = $order->get_shipping_address_1(); 274 $shipping_postcode = $order->get_shipping_postcode(); 275 $country_code = $order->get_shipping_country(); 276 $city = $order->get_shipping_city(); 277 $customer_email = $order->get_billing_email(); 278 $customer_phone = $order->get_billing_phone(); 279 280 $valid_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $customer_phone; 281 282 if ( isset($dynamic_opt_control['cod']) && $dynamic_opt_control['cod'] ) { 283 $order_total = $order->get_total(); 284 $dynamic_opt_control['codAmount'] = $order_total; 285 } 286 287 $shipmnet_desc = sprintf(__("W: %s", "postis"), $shipmnet_desc); 288 289 // Get Shipping Method 290 $shipping_service = get_post_meta( $order_id, 'postis_shipping_method', true ); 291 292 // If service is DPO add postboxes address 293 if ($shipping_service == 'DPO') { 294 295 // Get All Postbox List From API 296 $req_headers = array("headers" => array('x-api-key' => $this ->apikey, 'domain_name'=> $this ->domain)); 297 $postbox_response = wp_remote_get( $this ->postboxes_ep, $req_headers ); 298 $postbox_list = json_decode( wp_remote_retrieve_body($postbox_response), true); 299 300 $postbox_id = get_post_meta( $order_id, 'postis_dpo_postbox', true ); 301 $postbox_phonenumber = get_post_meta( $order_id, 'postis_dpo_phonenumber', true ); 302 303 $postbox_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $postbox_phonenumber; 304 305 if( ! is_wp_error($postbox_response) && $postbox_response['response']['code'] == 200 ) { 306 foreach ($postbox_list['postboxes'] as $index => $postboxes) { 307 308 if ($postboxes['postboxId'] == $postbox_id) { 309 310 $postboxes_address = sprintf(__("Póstbox %s", "postis"), $postboxes['address']); 311 312 $recipeint = array( 'name' => $shipping_customerName, 313 'addressLine1' => $postboxes_address, 314 "postcode" => $postboxes['postcode'], 315 "countryCode" => $country_code, 316 "mobilePhone" => $postbox_phonenumber, 317 ); 318 } 319 } 320 } 198 $response = $this ->api->create_shipment_api_request($order_id, $_REQUEST, false); 199 200 $get_shipment_meta = json_decode( $response['body'], true ); 201 202 if( $response['response']['code'] != 400 && $response['response']['code'] != 401 ) { 203 204 update_post_meta( $order_id, 'postis_shipment_meta', $get_shipment_meta ); 205 206 $ajax_response = array('status' => 'success', 'message' => __('Shipment Created Successfully', 'postis')); 321 207 322 208 }else{ 323 324 $recipeint = array( 'name' => $shipping_customerName, 325 'addressLine1' => $shipping_address1, 326 "postcode" => $shipping_postcode, 327 "countryCode" => $country_code, 328 "mobilePhone" => $valid_phonenumber, 329 ); 330 } 331 332 // If shipment is international 333 $international_shipment = postis_is_shipment_international($country_code); 334 335 if ($international_shipment) { 336 $recipeint['town'] = $city; 337 } 338 $recipeint['email'] = $customer_email; 339 340 $options = array( 'deliveryServiceId' => $shipping_service, 341 'numberOfItems' => floatval($numberofitems), 342 'description' => $shipmnet_desc 343 ); 344 $option_array = array_merge($options, $dynamic_opt_control); 345 346 // Request params. 347 if ($international_shipment) { 348 349 $params = array('recipient' => $recipeint, 350 'options' => $option_array, 351 'contents' => $new_contents_meta, 352 ); 353 }else{ 354 $params = array('recipient' => $recipeint, 355 'options' => $option_array, 356 ); 357 } 358 359 $params = array_filter( $params ); 360 361 // Set Header 362 $headers = array( 363 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 364 'x-api-key' => $this ->apikey, 365 'domain_name' => $this ->domain 366 ), 367 'body' => json_encode($params), 368 'method' => 'POST', 369 'data_format' => 'body', 370 ); 371 372 // Run the query 373 $response = wp_remote_post( $this ->create_shipment_ep, $headers ); 374 $get_shipment_meta = json_decode( $response['body'], true ); 375 // postis_pa($get_shipment_meta); exit; 376 377 if( $response['response']['code'] != 400 && $response['response']['code'] != 401 ) { 378 379 update_post_meta( $order_id, 'postis_shipment_meta', $get_shipment_meta ); 380 381 $ajax_response = array('status' => 'success', 'message' => __('Shipment Created Successfully', 'postis')); 382 383 }else{ 209 384 210 $resp_message = sprintf( __("Shipment Not Created.\nServer Response: %s", "postis"), $get_shipment_meta['message'] ); 385 211 … … 401 227 402 228 $shipmentId = postis_get_shipment_data(intval( $_REQUEST['order_id'] ), 'shipmentId'); 403 404 $resp = $this ->tracking_shipment_api_request($shipmentId);229 230 $resp = $this ->api->tracking_shipment_api_request($shipmentId); 405 231 406 232 $tracking_info = json_decode( $resp['body'], true ); … … 450 276 **========== Create Shipment PDF =========== 451 277 */ 452 function print_shipment_pdf() {278 function get_shipment_pdf() { 453 279 454 280 $order_id = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0; … … 536 362 $actions['mark_as_printed'] = __( 'Mark as printed', 'postis' ); 537 363 $actions['mark_as_not_printed'] = __( 'Mark as not printed', 'postis' ); 364 538 365 return $actions; 539 366 } … … 562 389 if ($shipment_ready) { 563 390 564 $response = $this-> create_shipment_api_request($order_id);391 $response = $this->api->create_shipment_api_request($order_id, $postmeta = array(), true); 565 392 566 393 $get_shipment_meta = json_decode( $response['body'], true ); … … 599 426 $postis_changed++; 600 427 } 601 602 // postis_pa($pdf_files); exit;603 428 604 429 if ($postis_changed) { … … 753 578 754 579 /* 755 **======== Create shipment API Request =========756 */757 function create_shipment_api_request($order_id){758 759 $order = wc_get_order( $order_id );760 761 $ship_methods = $order->get_shipping_methods();762 $shipping_customerName = $order->get_formatted_shipping_full_name();763 $shipping_address1 = $order->get_shipping_address_1();764 $shipping_postcode = $order->get_shipping_postcode();765 $country_code = $order->get_shipping_country();766 $city = $order->get_shipping_city();767 $customer_email = $order->get_billing_email();768 $customer_phone = $order->get_billing_phone();769 770 $shipping_service = get_post_meta( $order_id, 'postis_shipping_method', true );771 772 $valid_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $customer_phone;773 774 /* ######################### */775 $postis_apikey = postis_get_settings( 'apikey' );776 $domain = $_SERVER['HTTP_HOST'];777 // If Service is DPO add postboxes address778 if ($shipping_service == 'DPO') {779 780 781 // Get All Postbox List From API782 $postbox_endpoint_url = postis_get_endpoints( 'postboxes' );783 $req_headers = array("headers" => array('x-api-key'=>$postis_apikey,'domain_name'=> $domain));784 $postbox_response = wp_remote_get( $postbox_endpoint_url, $req_headers );785 $postbox_list = json_decode( wp_remote_retrieve_body($postbox_response), true);786 787 $postbox_id = get_post_meta( $order_id, 'postis_dpo_postbox', true );788 $postbox_phonenumber = get_post_meta( $order_id, 'postis_dpo_phonenumber', true );789 790 $postbox_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $postbox_phonenumber;791 792 // postis_pa($postbox_list); exit;793 if( ! is_wp_error($postbox_response) && $postbox_response['response']['code'] == 200 ) {794 foreach ($postbox_list['postboxes'] as $index => $postboxes) {795 796 if ($postboxes['postboxId'] == $postbox_id) {797 798 $postboxes_address = sprintf(__("Póstbox %s", "postis"), $postboxes['address']);799 800 $recipeint = array( 'name' => $shipping_customerName,801 'addressLine1' => $postboxes_address,802 "postcode" => $postboxes['postcode'],803 "countryCode" => $country_code,804 "mobilePhone" => $postbox_phonenumber,805 );806 }807 }808 }809 810 }else{811 if ($shipping_service == 'DPP' OR $shipping_service == 'DPH') {812 if (substr($customer_phone, 0, 1) === '6' || substr($customer_phone, 0, 1) === '7' || substr($customer_phone, 0, 1) === '8') {813 // phoneNumber is valid814 } else {815 $valid_phonenumber = '';816 }817 }818 819 $recipeint = array( 'name' => $shipping_customerName,820 'addressLine1' => $shipping_address1,821 "postcode" => $shipping_postcode,822 "countryCode" => $country_code,823 "mobilePhone" => $valid_phonenumber,824 );825 }826 827 828 /* ########################## */829 /*830 $recipeint = array( 'name' => $shipping_customerName,831 'addressLine1' => $shipping_address1,832 "postcode" => $shipping_postcode,833 "countryCode" => $country_code,834 "mobilePhone" => $customer_phone,835 );836 */837 $recipeint['email'] = $customer_email;838 839 $options = array('deliveryServiceId' => $shipping_service);840 $params = array(841 'recipient' => $recipeint,842 'options' => $options,843 );844 845 $params = array_filter( $params );846 847 $headers = array(848 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8',849 'x-api-key' => $this ->apikey,850 'domain_name' => $this ->domain851 ),852 'body' => json_encode($params),853 'method' => 'POST',854 'data_format' => 'body',855 );856 //postis_pa($headers); exit;857 return wp_remote_post( $this ->create_shipment_ep, $headers );858 }859 860 861 /*862 **======== API Request For Tracking Shipment =========863 */864 function pdf_shipment_api_request($order_id, $allow_printing = true){865 866 $customerId = postis_get_shipment_data($order_id, 'customerId');867 $registrationKey = postis_get_shipment_data($order_id, 'registrationKey');868 $shipmentId = postis_get_shipment_data($order_id, 'shipmentId');869 870 // PDF Endpoint871 $pdf_endpoint = $this ->pdf_ep.$shipmentId.'/pdf';872 873 $automatic_printing = postis_get_settings( 'pdf_print' );874 if ($automatic_printing == 'yes' && $allow_printing) {875 876 $print_resp = $this->print_shipment_api_request($shipmentId);877 878 $print_resp_code = wp_remote_retrieve_response_code( $print_resp );879 }880 881 $params = array(882 'registrationKey' => $registrationKey,883 'customerId' => $customerId,884 'inline' => 'true'885 );886 $params = array_filter( $params );887 888 // Query format parameters.889 $query = add_query_arg( $params, $pdf_endpoint );890 891 $pdf_resp = wp_remote_get( $query ,892 array(893 'timeout' => 10,894 'headers' => array(895 'accept' => 'application/pdf',896 'content-type' => 'application/pdf',897 'x-api-key' => $this ->apikey,898 'domain_name' => $this ->domain899 )900 )901 );902 903 return $pdf_resp;904 }905 906 907 /*908 **======== API Request For Tracking Shipment =========909 */910 function tracking_shipment_api_request($shipmentId){911 912 $params = array(913 'trackingNumber' => $shipmentId,914 'language' => 'IS',915 );916 917 $params = array_filter( $params );918 $query = add_query_arg( $params, $this ->tracking_ep );919 920 $req_headers = array("headers" => array('domain_name' => $this ->domain));921 922 return wp_remote_get( $query, $req_headers );923 }924 925 926 /*927 **======== API Request For Print Shipment PDF =========928 */929 function print_shipment_api_request($shipmentId){930 931 $params = array(932 'shipmentId' => $shipmentId,933 'outputFormat' => 'poststod'934 );935 936 $params = array_filter( $params );937 938 $query = add_query_arg( $params, $this ->print_shipment_ep );939 940 $header = array(941 'headers' => array('x-api-key' => $this ->apikey, 'domain_name' => $this ->domain),942 'method' => 'POST',943 );944 945 return wp_remote_post( $query, $header );946 }947 948 949 /*950 580 **======== Single PDF Generator ========= 951 581 */ … … 970 600 $pdf_biggerbarcode = postis_get_settings( 'pdf_biggerbarcode' ); 971 601 972 $pdf_request = $this-> pdf_shipment_api_request($order_id, $allow_printing = true);602 $pdf_request = $this->api->pdf_shipment_api_request($order_id, $allow_printing = true); 973 603 974 604 if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) { … … 1032 662 $path_pdf = postis_files_setup_get_directory('pdf')."/{$shipmentId}.pdf"; 1033 663 1034 $pdf_request = $this-> pdf_shipment_api_request($order_id, $allow_printing = false);664 $pdf_request = $this->api->pdf_shipment_api_request($order_id, $allow_printing = true); 1035 665 1036 666 if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) { -
posturinn/tags/1.0.9/readme.txt
r2415872 r2430169 26 26 27 27 == Changelog == 28 = 1.0.9 = 29 fixed issue for missing town 30 fixed issue for too long name 31 fixed issue for too long town 32 fixed issue for too long address 33 add api.class 34 28 35 = 1.0.8 = 29 36 Add bulk actions for createShipment -
posturinn/tags/1.0.9/wc-posturinn-shipping-api.php
r2415872 r2430169 3 3 * Plugin Name: Pósturinn's Shipping with WooCommerce 4 4 * Description: This plugin is an WooCommerce extension to extend shipping method with Pósturinn's - Iceland 5 * Version: 1.0. 85 * Version: 1.0.9 6 6 * Author: Pósturinn 7 7 * Author URI: https://postur.is … … 34 34 include plugin_dir_path(__FILE__).'inc/functions.php'; 35 35 include plugin_dir_path(__FILE__).'inc/callbacks.php'; 36 include plugin_dir_path(__FILE__).'inc/api.class.php'; 36 37 include plugin_dir_path(__FILE__).'inc/admin.class.php'; 37 38 // include plugin_dir_path(__FILE__).'inc/admin.php'; -
posturinn/trunk/inc/admin.class.php
r2415877 r2430169 14 14 15 15 /* 16 * this var use to get domain name16 * this var use to get API requests 17 17 */ 18 var $domain; 19 20 /* 21 * this var use to get domain name 22 */ 23 var $apikey; 24 25 /* 26 * this var use to get tracking shipment endpoint 27 */ 28 var $tracking_ep; 29 30 /* 31 * this var use to get create shipment endpoint 32 */ 33 var $create_shipment_ep; 34 35 /* 36 * this var use to get postbox endpoint 37 */ 38 var $postboxes_ep; 39 40 /* 41 * this var use to get pdf endpoint 42 */ 43 var $pdf_ep; 44 45 /* 46 * this var use to get print shipment endpoint 47 */ 48 var $print_shipment_ep; 49 18 var $api; 50 19 51 20 function __construct() { 52 21 53 $this ->domain = $_SERVER['HTTP_HOST']; 54 55 $this ->apikey = postis_get_settings( 'apikey' ); 56 57 $this ->tracking_ep = postis_get_endpoints( 'tracking' ); 58 59 $this ->create_shipment_ep = postis_get_endpoints( 'create_shipment' ); 60 61 $this ->postboxes_ep = postis_get_endpoints( 'postboxes' ); 62 63 $this ->pdf_ep = postis_get_endpoints( 'pdf' ); 64 65 $this ->print_shipment_ep = postis_get_endpoints( 'print' ); 22 $this->api = new POSTIS_API(); 66 23 67 24 add_filter( 'manage_edit-shop_order_columns', array($this, 'create_order_column'), 20); … … 78 35 add_action('wp_ajax_postis_shipment_options', array($this, 'shipment_options') ); 79 36 80 add_action( 'admin_post_postis_pdf_action', array($this, ' print_shipment_pdf') );37 add_action( 'admin_post_postis_pdf_action', array($this, 'get_shipment_pdf') ); 81 38 82 39 add_action( 'admin_init', array($this, 'create_metabox') ); … … 239 196 $order_id = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0; 240 197 241 $numberofitems = isset($_REQUEST['numberofitems']) ? intval($_REQUEST['numberofitems']) : ''; 242 $shipmnet_desc = isset($_REQUEST['description']) ? sanitize_text_field($_REQUEST['description']) : ''; 243 244 // Get Dynamic Options 245 $dynamic_opt_control = array(); 246 if(isset($_REQUEST['optional_services']) && is_array($_REQUEST['optional_services'])){ 247 $dynamic_shipment_options = array_map('sanitize_text_field', $_REQUEST['optional_services']); 248 } 249 250 foreach($dynamic_shipment_options as $opt_name => $opt_val){ 251 if ( isset($dynamic_shipment_options[$opt_name]) && $dynamic_shipment_options[$opt_name] == 'on' ) { 252 $dynamic_opt_control[$opt_name] = true; 253 }else{ 254 $dynamic_opt_control[$opt_name] = $opt_val; 255 } 256 } 257 258 // international shipment content meta 259 $new_contents_meta = array(); 260 if(isset($_REQUEST['contents']) && is_array($_REQUEST['contents'])){ 261 foreach ($_REQUEST['contents'] as $item_id => $contents_meta) { 262 263 $new_contents_meta[$item_id] = array_map('sanitize_text_field', $contents_meta); 264 } 265 } 266 267 // Get order meta 268 $order = wc_get_order($order_id); 269 270 // User's Shipping data 271 $ship_methods = $order->get_shipping_methods(); 272 $shipping_customerName = $order->get_formatted_shipping_full_name(); 273 $shipping_address1 = $order->get_shipping_address_1(); 274 $shipping_postcode = $order->get_shipping_postcode(); 275 $country_code = $order->get_shipping_country(); 276 $city = $order->get_shipping_city(); 277 $customer_email = $order->get_billing_email(); 278 $customer_phone = $order->get_billing_phone(); 279 280 $valid_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $customer_phone; 281 282 if ( isset($dynamic_opt_control['cod']) && $dynamic_opt_control['cod'] ) { 283 $order_total = $order->get_total(); 284 $dynamic_opt_control['codAmount'] = $order_total; 285 } 286 287 $shipmnet_desc = sprintf(__("W: %s", "postis"), $shipmnet_desc); 288 289 // Get Shipping Method 290 $shipping_service = get_post_meta( $order_id, 'postis_shipping_method', true ); 291 292 // If service is DPO add postboxes address 293 if ($shipping_service == 'DPO') { 294 295 // Get All Postbox List From API 296 $req_headers = array("headers" => array('x-api-key' => $this ->apikey, 'domain_name'=> $this ->domain)); 297 $postbox_response = wp_remote_get( $this ->postboxes_ep, $req_headers ); 298 $postbox_list = json_decode( wp_remote_retrieve_body($postbox_response), true); 299 300 $postbox_id = get_post_meta( $order_id, 'postis_dpo_postbox', true ); 301 $postbox_phonenumber = get_post_meta( $order_id, 'postis_dpo_phonenumber', true ); 302 303 $postbox_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $postbox_phonenumber; 304 305 if( ! is_wp_error($postbox_response) && $postbox_response['response']['code'] == 200 ) { 306 foreach ($postbox_list['postboxes'] as $index => $postboxes) { 307 308 if ($postboxes['postboxId'] == $postbox_id) { 309 310 $postboxes_address = sprintf(__("Póstbox %s", "postis"), $postboxes['address']); 311 312 $recipeint = array( 'name' => $shipping_customerName, 313 'addressLine1' => $postboxes_address, 314 "postcode" => $postboxes['postcode'], 315 "countryCode" => $country_code, 316 "mobilePhone" => $postbox_phonenumber, 317 ); 318 } 319 } 320 } 198 $response = $this ->api->create_shipment_api_request($order_id, $_REQUEST, false); 199 200 $get_shipment_meta = json_decode( $response['body'], true ); 201 202 if( $response['response']['code'] != 400 && $response['response']['code'] != 401 ) { 203 204 update_post_meta( $order_id, 'postis_shipment_meta', $get_shipment_meta ); 205 206 $ajax_response = array('status' => 'success', 'message' => __('Shipment Created Successfully', 'postis')); 321 207 322 208 }else{ 323 324 $recipeint = array( 'name' => $shipping_customerName, 325 'addressLine1' => $shipping_address1, 326 "postcode" => $shipping_postcode, 327 "countryCode" => $country_code, 328 "mobilePhone" => $valid_phonenumber, 329 ); 330 } 331 332 // If shipment is international 333 $international_shipment = postis_is_shipment_international($country_code); 334 335 if ($international_shipment) { 336 $recipeint['town'] = $city; 337 } 338 $recipeint['email'] = $customer_email; 339 340 $options = array( 'deliveryServiceId' => $shipping_service, 341 'numberOfItems' => floatval($numberofitems), 342 'description' => $shipmnet_desc 343 ); 344 $option_array = array_merge($options, $dynamic_opt_control); 345 346 // Request params. 347 if ($international_shipment) { 348 349 $params = array('recipient' => $recipeint, 350 'options' => $option_array, 351 'contents' => $new_contents_meta, 352 ); 353 }else{ 354 $params = array('recipient' => $recipeint, 355 'options' => $option_array, 356 ); 357 } 358 359 $params = array_filter( $params ); 360 361 // Set Header 362 $headers = array( 363 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 364 'x-api-key' => $this ->apikey, 365 'domain_name' => $this ->domain 366 ), 367 'body' => json_encode($params), 368 'method' => 'POST', 369 'data_format' => 'body', 370 ); 371 372 // Run the query 373 $response = wp_remote_post( $this ->create_shipment_ep, $headers ); 374 $get_shipment_meta = json_decode( $response['body'], true ); 375 // postis_pa($get_shipment_meta); exit; 376 377 if( $response['response']['code'] != 400 && $response['response']['code'] != 401 ) { 378 379 update_post_meta( $order_id, 'postis_shipment_meta', $get_shipment_meta ); 380 381 $ajax_response = array('status' => 'success', 'message' => __('Shipment Created Successfully', 'postis')); 382 383 }else{ 209 384 210 $resp_message = sprintf( __("Shipment Not Created.\nServer Response: %s", "postis"), $get_shipment_meta['message'] ); 385 211 … … 401 227 402 228 $shipmentId = postis_get_shipment_data(intval( $_REQUEST['order_id'] ), 'shipmentId'); 403 404 $resp = $this ->tracking_shipment_api_request($shipmentId);229 230 $resp = $this ->api->tracking_shipment_api_request($shipmentId); 405 231 406 232 $tracking_info = json_decode( $resp['body'], true ); … … 450 276 **========== Create Shipment PDF =========== 451 277 */ 452 function print_shipment_pdf() {278 function get_shipment_pdf() { 453 279 454 280 $order_id = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0; … … 536 362 $actions['mark_as_printed'] = __( 'Mark as printed', 'postis' ); 537 363 $actions['mark_as_not_printed'] = __( 'Mark as not printed', 'postis' ); 364 538 365 return $actions; 539 366 } … … 562 389 if ($shipment_ready) { 563 390 564 $response = $this-> create_shipment_api_request($order_id);391 $response = $this->api->create_shipment_api_request($order_id, $postmeta = array(), true); 565 392 566 393 $get_shipment_meta = json_decode( $response['body'], true ); … … 599 426 $postis_changed++; 600 427 } 601 602 // postis_pa($pdf_files); exit;603 428 604 429 if ($postis_changed) { … … 753 578 754 579 /* 755 **======== Create shipment API Request =========756 */757 function create_shipment_api_request($order_id){758 759 $order = wc_get_order( $order_id );760 761 $ship_methods = $order->get_shipping_methods();762 $shipping_customerName = $order->get_formatted_shipping_full_name();763 $shipping_address1 = $order->get_shipping_address_1();764 $shipping_postcode = $order->get_shipping_postcode();765 $country_code = $order->get_shipping_country();766 $city = $order->get_shipping_city();767 $customer_email = $order->get_billing_email();768 $customer_phone = $order->get_billing_phone();769 770 $shipping_service = get_post_meta( $order_id, 'postis_shipping_method', true );771 772 $valid_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $customer_phone;773 774 /* ######################### */775 $postis_apikey = postis_get_settings( 'apikey' );776 $domain = $_SERVER['HTTP_HOST'];777 // If Service is DPO add postboxes address778 if ($shipping_service == 'DPO') {779 780 781 // Get All Postbox List From API782 $postbox_endpoint_url = postis_get_endpoints( 'postboxes' );783 $req_headers = array("headers" => array('x-api-key'=>$postis_apikey,'domain_name'=> $domain));784 $postbox_response = wp_remote_get( $postbox_endpoint_url, $req_headers );785 $postbox_list = json_decode( wp_remote_retrieve_body($postbox_response), true);786 787 $postbox_id = get_post_meta( $order_id, 'postis_dpo_postbox', true );788 $postbox_phonenumber = get_post_meta( $order_id, 'postis_dpo_phonenumber', true );789 790 $postbox_phonenumber = isset($_REQUEST['phonenumber']) && $_REQUEST['phonenumber'] != '' ? wc_sanitize_phone_number( $_REQUEST['phonenumber'] ): $postbox_phonenumber;791 792 // postis_pa($postbox_list); exit;793 if( ! is_wp_error($postbox_response) && $postbox_response['response']['code'] == 200 ) {794 foreach ($postbox_list['postboxes'] as $index => $postboxes) {795 796 if ($postboxes['postboxId'] == $postbox_id) {797 798 $postboxes_address = sprintf(__("Póstbox %s", "postis"), $postboxes['address']);799 800 $recipeint = array( 'name' => $shipping_customerName,801 'addressLine1' => $postboxes_address,802 "postcode" => $postboxes['postcode'],803 "countryCode" => $country_code,804 "mobilePhone" => $postbox_phonenumber,805 );806 }807 }808 }809 810 }else{811 if ($shipping_service == 'DPP' OR $shipping_service == 'DPH') {812 if (substr($customer_phone, 0, 1) === '6' || substr($customer_phone, 0, 1) === '7' || substr($customer_phone, 0, 1) === '8') {813 // phoneNumber is valid814 } else {815 $valid_phonenumber = '';816 }817 }818 819 $recipeint = array( 'name' => $shipping_customerName,820 'addressLine1' => $shipping_address1,821 "postcode" => $shipping_postcode,822 "countryCode" => $country_code,823 "mobilePhone" => $valid_phonenumber,824 );825 }826 827 828 /* ########################## */829 /*830 $recipeint = array( 'name' => $shipping_customerName,831 'addressLine1' => $shipping_address1,832 "postcode" => $shipping_postcode,833 "countryCode" => $country_code,834 "mobilePhone" => $customer_phone,835 );836 */837 $recipeint['email'] = $customer_email;838 839 $options = array('deliveryServiceId' => $shipping_service);840 $params = array(841 'recipient' => $recipeint,842 'options' => $options,843 );844 845 $params = array_filter( $params );846 847 $headers = array(848 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8',849 'x-api-key' => $this ->apikey,850 'domain_name' => $this ->domain851 ),852 'body' => json_encode($params),853 'method' => 'POST',854 'data_format' => 'body',855 );856 //postis_pa($headers); exit;857 return wp_remote_post( $this ->create_shipment_ep, $headers );858 }859 860 861 /*862 **======== API Request For Tracking Shipment =========863 */864 function pdf_shipment_api_request($order_id, $allow_printing = true){865 866 $customerId = postis_get_shipment_data($order_id, 'customerId');867 $registrationKey = postis_get_shipment_data($order_id, 'registrationKey');868 $shipmentId = postis_get_shipment_data($order_id, 'shipmentId');869 870 // PDF Endpoint871 $pdf_endpoint = $this ->pdf_ep.$shipmentId.'/pdf';872 873 $automatic_printing = postis_get_settings( 'pdf_print' );874 if ($automatic_printing == 'yes' && $allow_printing) {875 876 $print_resp = $this->print_shipment_api_request($shipmentId);877 878 $print_resp_code = wp_remote_retrieve_response_code( $print_resp );879 }880 881 $params = array(882 'registrationKey' => $registrationKey,883 'customerId' => $customerId,884 'inline' => 'true'885 );886 $params = array_filter( $params );887 888 // Query format parameters.889 $query = add_query_arg( $params, $pdf_endpoint );890 891 $pdf_resp = wp_remote_get( $query ,892 array(893 'timeout' => 10,894 'headers' => array(895 'accept' => 'application/pdf',896 'content-type' => 'application/pdf',897 'x-api-key' => $this ->apikey,898 'domain_name' => $this ->domain899 )900 )901 );902 903 return $pdf_resp;904 }905 906 907 /*908 **======== API Request For Tracking Shipment =========909 */910 function tracking_shipment_api_request($shipmentId){911 912 $params = array(913 'trackingNumber' => $shipmentId,914 'language' => 'IS',915 );916 917 $params = array_filter( $params );918 $query = add_query_arg( $params, $this ->tracking_ep );919 920 $req_headers = array("headers" => array('domain_name' => $this ->domain));921 922 return wp_remote_get( $query, $req_headers );923 }924 925 926 /*927 **======== API Request For Print Shipment PDF =========928 */929 function print_shipment_api_request($shipmentId){930 931 $params = array(932 'shipmentId' => $shipmentId,933 'outputFormat' => 'poststod'934 );935 936 $params = array_filter( $params );937 938 $query = add_query_arg( $params, $this ->print_shipment_ep );939 940 $header = array(941 'headers' => array('x-api-key' => $this ->apikey, 'domain_name' => $this ->domain),942 'method' => 'POST',943 );944 945 return wp_remote_post( $query, $header );946 }947 948 949 /*950 580 **======== Single PDF Generator ========= 951 581 */ … … 970 600 $pdf_biggerbarcode = postis_get_settings( 'pdf_biggerbarcode' ); 971 601 972 $pdf_request = $this-> pdf_shipment_api_request($order_id, $allow_printing = true);602 $pdf_request = $this->api->pdf_shipment_api_request($order_id, $allow_printing = true); 973 603 974 604 if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) { … … 1032 662 $path_pdf = postis_files_setup_get_directory('pdf')."/{$shipmentId}.pdf"; 1033 663 1034 $pdf_request = $this-> pdf_shipment_api_request($order_id, $allow_printing = false);664 $pdf_request = $this->api->pdf_shipment_api_request($order_id, $allow_printing = true); 1035 665 1036 666 if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) { -
posturinn/trunk/readme.txt
r2415872 r2430169 26 26 27 27 == Changelog == 28 = 1.0.9 = 29 fixed issue for missing town 30 fixed issue for too long name 31 fixed issue for too long town 32 fixed issue for too long address 33 add api.class 34 28 35 = 1.0.8 = 29 36 Add bulk actions for createShipment -
posturinn/trunk/wc-posturinn-shipping-api.php
r2415872 r2430169 3 3 * Plugin Name: Pósturinn's Shipping with WooCommerce 4 4 * Description: This plugin is an WooCommerce extension to extend shipping method with Pósturinn's - Iceland 5 * Version: 1.0. 85 * Version: 1.0.9 6 6 * Author: Pósturinn 7 7 * Author URI: https://postur.is … … 34 34 include plugin_dir_path(__FILE__).'inc/functions.php'; 35 35 include plugin_dir_path(__FILE__).'inc/callbacks.php'; 36 include plugin_dir_path(__FILE__).'inc/api.class.php'; 36 37 include plugin_dir_path(__FILE__).'inc/admin.class.php'; 37 38 // include plugin_dir_path(__FILE__).'inc/admin.php';
Note: See TracChangeset
for help on using the changeset viewer.