Plugin Directory

Changeset 2430169


Ignore:
Timestamp:
12/02/2020 11:22:36 AM (5 years ago)
Author:
posturinn
Message:

Tagging version 1.0.9

Location:
posturinn
Files:
2 added
3 edited
9 copied

Legend:

Unmodified
Added
Removed
  • posturinn/tags/1.0.9/inc/admin.class.php

    r2415877 r2430169  
    1414   
    1515    /*
    16      * this var use to get domain name
     16     * this var use to get API requests
    1717    */
    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;
    5019   
    5120    function __construct() {
    5221       
    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();
    6623       
    6724        add_filter( 'manage_edit-shop_order_columns', array($this, 'create_order_column'), 20);
     
    7835        add_action('wp_ajax_postis_shipment_options', array($this, 'shipment_options') );
    7936       
    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') );
    8138       
    8239        add_action( 'admin_init', array($this, 'create_metabox') );
     
    239196        $order_id   = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0;
    240197       
    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'));
    321207           
    322208        }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           
    384210            $resp_message  = sprintf( __("Shipment Not Created.\nServer Response: %s", "postis"), $get_shipment_meta['message'] );
    385211           
     
    401227   
    402228        $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);
    405231       
    406232        $tracking_info = json_decode( $resp['body'], true );
     
    450276    **========== Create Shipment PDF ===========
    451277    */
    452     function print_shipment_pdf() {
     278    function get_shipment_pdf() {
    453279   
    454280        $order_id   = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0;
     
    536362        $actions['mark_as_printed']     = __( 'Mark as printed', 'postis' );
    537363        $actions['mark_as_not_printed'] = __( 'Mark as not printed', 'postis' );
     364       
    538365        return $actions;
    539366    }
     
    562389                    if ($shipment_ready) {
    563390                       
    564                         $response = $this->create_shipment_api_request($order_id);
     391                        $response = $this->api->create_shipment_api_request($order_id, $postmeta = array(), true);
    565392               
    566393                        $get_shipment_meta = json_decode( $response['body'], true );
     
    599426                    $postis_changed++;
    600427                }
    601                
    602                 // postis_pa($pdf_files); exit;
    603428               
    604429                if ($postis_changed) {
     
    753578   
    754579    /*
    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 address
    778         if ($shipping_service == 'DPO') {
    779            
    780            
    781             // Get All Postbox List From API
    782             $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 valid
    814                 } 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 ->domain
    851                                         ),
    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 Endpoint
    871         $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 ->domain
    899                                 )
    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     /*
    950580    **======== Single PDF Generator =========
    951581    */
     
    970600        $pdf_biggerbarcode = postis_get_settings( 'pdf_biggerbarcode' );
    971601       
    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);
    973603   
    974604        if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) {
     
    1032662        $path_pdf   = postis_files_setup_get_directory('pdf')."/{$shipmentId}.pdf";
    1033663       
    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);
    1035665   
    1036666        if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) {
  • posturinn/tags/1.0.9/readme.txt

    r2415872 r2430169  
    2626
    2727== Changelog ==
     28= 1.0.9 =
     29fixed issue for missing town
     30fixed issue for too long name
     31fixed issue for too long town
     32fixed issue for too long address
     33add api.class
     34
    2835= 1.0.8 =
    2936Add bulk actions for createShipment
  • posturinn/tags/1.0.9/wc-posturinn-shipping-api.php

    r2415872 r2430169  
    33 * Plugin Name: Pósturinn's Shipping with WooCommerce
    44 * Description: This plugin is an WooCommerce extension to extend shipping method with Pósturinn's - Iceland
    5  * Version: 1.0.8
     5 * Version: 1.0.9
    66 * Author: Pósturinn
    77 * Author URI: https://postur.is
     
    3434include plugin_dir_path(__FILE__).'inc/functions.php';
    3535include plugin_dir_path(__FILE__).'inc/callbacks.php';
     36include plugin_dir_path(__FILE__).'inc/api.class.php';
    3637include plugin_dir_path(__FILE__).'inc/admin.class.php';
    3738// include plugin_dir_path(__FILE__).'inc/admin.php';
  • posturinn/trunk/inc/admin.class.php

    r2415877 r2430169  
    1414   
    1515    /*
    16      * this var use to get domain name
     16     * this var use to get API requests
    1717    */
    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;
    5019   
    5120    function __construct() {
    5221       
    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();
    6623       
    6724        add_filter( 'manage_edit-shop_order_columns', array($this, 'create_order_column'), 20);
     
    7835        add_action('wp_ajax_postis_shipment_options', array($this, 'shipment_options') );
    7936       
    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') );
    8138       
    8239        add_action( 'admin_init', array($this, 'create_metabox') );
     
    239196        $order_id   = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0;
    240197       
    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'));
    321207           
    322208        }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           
    384210            $resp_message  = sprintf( __("Shipment Not Created.\nServer Response: %s", "postis"), $get_shipment_meta['message'] );
    385211           
     
    401227   
    402228        $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);
    405231       
    406232        $tracking_info = json_decode( $resp['body'], true );
     
    450276    **========== Create Shipment PDF ===========
    451277    */
    452     function print_shipment_pdf() {
     278    function get_shipment_pdf() {
    453279   
    454280        $order_id   = isset($_REQUEST['order_id']) ? intval($_REQUEST['order_id']) : 0;
     
    536362        $actions['mark_as_printed']     = __( 'Mark as printed', 'postis' );
    537363        $actions['mark_as_not_printed'] = __( 'Mark as not printed', 'postis' );
     364       
    538365        return $actions;
    539366    }
     
    562389                    if ($shipment_ready) {
    563390                       
    564                         $response = $this->create_shipment_api_request($order_id);
     391                        $response = $this->api->create_shipment_api_request($order_id, $postmeta = array(), true);
    565392               
    566393                        $get_shipment_meta = json_decode( $response['body'], true );
     
    599426                    $postis_changed++;
    600427                }
    601                
    602                 // postis_pa($pdf_files); exit;
    603428               
    604429                if ($postis_changed) {
     
    753578   
    754579    /*
    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 address
    778         if ($shipping_service == 'DPO') {
    779            
    780            
    781             // Get All Postbox List From API
    782             $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 valid
    814                 } 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 ->domain
    851                                         ),
    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 Endpoint
    871         $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 ->domain
    899                                 )
    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     /*
    950580    **======== Single PDF Generator =========
    951581    */
     
    970600        $pdf_biggerbarcode = postis_get_settings( 'pdf_biggerbarcode' );
    971601       
    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);
    973603   
    974604        if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) {
     
    1032662        $path_pdf   = postis_files_setup_get_directory('pdf')."/{$shipmentId}.pdf";
    1033663       
    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);
    1035665   
    1036666        if ( is_wp_error( $pdf_request ) || wp_remote_retrieve_response_code( $pdf_request ) != 200 ) {
  • posturinn/trunk/readme.txt

    r2415872 r2430169  
    2626
    2727== Changelog ==
     28= 1.0.9 =
     29fixed issue for missing town
     30fixed issue for too long name
     31fixed issue for too long town
     32fixed issue for too long address
     33add api.class
     34
    2835= 1.0.8 =
    2936Add bulk actions for createShipment
  • posturinn/trunk/wc-posturinn-shipping-api.php

    r2415872 r2430169  
    33 * Plugin Name: Pósturinn's Shipping with WooCommerce
    44 * Description: This plugin is an WooCommerce extension to extend shipping method with Pósturinn's - Iceland
    5  * Version: 1.0.8
     5 * Version: 1.0.9
    66 * Author: Pósturinn
    77 * Author URI: https://postur.is
     
    3434include plugin_dir_path(__FILE__).'inc/functions.php';
    3535include plugin_dir_path(__FILE__).'inc/callbacks.php';
     36include plugin_dir_path(__FILE__).'inc/api.class.php';
    3637include plugin_dir_path(__FILE__).'inc/admin.class.php';
    3738// include plugin_dir_path(__FILE__).'inc/admin.php';
Note: See TracChangeset for help on using the changeset viewer.