Plugin Directory

Changeset 3156908


Ignore:
Timestamp:
09/24/2024 12:19:22 PM (17 months ago)
Author:
ok9xnirab
Message:

v1.1 released.

Location:
integration-of-pathao-for-woocommerce/trunk
Files:
6 added
1 deleted
14 edited

Legend:

Unmodified
Added
Removed
  • integration-of-pathao-for-woocommerce/trunk/assets/js/admin.js

    r3106287 r3156908  
    11jQuery(document).ready(function ($) {
    22    const nonce = $('#pathao_send_order_nonce').val();
     3
     4    $('#pathao_city').selectWoo();
     5    $('#pathao_zone').selectWoo();
     6    $('#pathao_area').selectWoo();
     7    getCities();
    38
    49    $('#pathao-setup').on('submit', async function (e) {
     
    3540                        });
    3641                    });
    37                     $('#pathao_access_token').val(res.data.access_token);
    38                     $('#pathao_refresh_token').val(res.data.refresh_token);
     42                    $('#pathao_access_token').val(res.access_token);
     43                    $('#pathao_refresh_token').val(res.refresh_token);
    3944                    window.location.reload();
    4045                } else {
    4146                    $('.pathao-notice').after(
    4247                        '<div class="notice notice-error is-dismissible"><p><b>' +
    43                             res.data.message +
    44                             '</b></p><button id="dismiss-message" class="notice-dismiss" type="button"><span class="screen-reader-text">Dismiss this notice.</span></button></div>'
     48                        res.messages[0] +
     49                        '</b></p><button id="dismiss-message" class="notice-dismiss" type="button"><span class="screen-reader-text">Dismiss this notice.</span></button></div>'
    4550                    );
    4651                    $('#dismiss-message').click(function (event) {
     
    7681        const zone = $(this).val();
    7782
    78         if (zone == '') {
     83        if (!zone || '' == zone) {
    7984            $('#pathao_area_select').hide();
    8085            return false;
     
    101106                    $('#pathao_area').append(
    102107                        '<option value="' +
    103                             value.area_id +
    104                             '"' +
    105                             `${value.area_id == res.value ? ' selected' : ''}` +
    106                             '>' +
    107                             value.area_name +
    108                             '</option>'
     108                        value.id +
     109                        '"' +
     110                        `${value.id == res.value ? ' selected' : ''}` +
     111                        '>' +
     112                        value.name +
     113                        '</option>'
    109114                    );
    110115                });
     
    173178        }
    174179
    175         if (city == '') {
    176             $.toast({
    177                 position: 'bottom-center',
    178                 text: 'Please select city',
    179                 icon: 'error',
    180                 hideAfter: 6000,
    181             });
    182             return false;
    183         }
    184 
    185         if (zone == '') {
     180        if (city != '' && zone == '') {
    186181            $.toast({
    187182                position: 'bottom-center',
     
    270265                    $('.pathao-shipping-spinner').removeClass('is-active');
    271266                    const errors = res.errors;
    272                     if (!errors && res.message) {
    273                         $.toast({
    274                             position: 'bottom-center',
    275                             text: res.message,
    276                             icon: 'error',
    277                             hideAfter: 6000,
    278                         });
    279                     }
    280267                    $.each(errors, function (key, value) {
    281268                        $.toast({
    282269                            position: 'bottom-center',
    283                             text: value[0],
     270                            text: value,
    284271                            icon: 'error',
    285272                            hideAfter: 6000,
     
    318305            },
    319306            success: function (res) {
    320                 $('#pathao_zone').html('');
     307                $('#pathao_zone').html(
     308                    '<option value="">No Zone Selected</option>'
     309                );
    321310                $.each(res.zones, function (key, value) {
    322311                    $('#pathao_zone').append(
    323312                        '<option value="' +
    324                             value.zone_id +
    325                             '"' +
    326                             `${value.zone_id == res.value ? ' selected' : ''}` +
    327                             '>' +
    328                             value.zone_name +
    329                             '</option>'
     313                        value.id +
     314                        '"' +
     315                        `${value.id == res.value ? ' selected' : ''}` +
     316                        '>' +
     317                        value.name +
     318                        '</option>'
    330319                    );
    331320                });
     
    343332        });
    344333    }
     334
     335    function getCities() {
     336        $('#pathao_submit_shipping').prop('disabled', true);
     337        $('.pathao-shipping-spinner').addClass('is-active');
     338
     339        $.ajax({
     340            type: 'post',
     341            dataType: 'json',
     342            url: pathao_admin_obj.ajax_url,
     343            data: {
     344                action: 'get_cities',
     345                nonce,
     346                order_id: pathao_admin_obj.order_id,
     347            },
     348            success: function (res) {
     349                $('#pathao_city').html(
     350                    '<option value="">No City Selected</option>'
     351                );
     352                $.each(res.cities, function (key, value) {
     353                    $('#pathao_city').append(
     354                        '<option value="' +
     355                        value.id +
     356                        '"' +
     357                        `${value.id == res.value ? ' selected' : ''}` +
     358                        '>' +
     359                        value.name +
     360                        '</option>'
     361                    );
     362                });
     363                if (res.value) {
     364                    getZones(res.value);
     365                }
     366
     367                $('#pathao_submit_shipping').prop('disabled', false);
     368                $('.pathao-shipping-spinner').removeClass('is-active');
     369            },
     370            error: function (error) {
     371                console.log(error);
     372            },
     373        });
     374    }
    345375});
    346376
  • integration-of-pathao-for-woocommerce/trunk/includes/Admin.php

    r3013075 r3156908  
    99
    1010use SpringDevs\Pathao\Admin\Links;
     11use SpringDevs\Pathao\Admin\Notice;
    1112use SpringDevs\Pathao\Admin\Settings;
    1213
     
    2425     */
    2526    public function __construct() {
     27        new Notice();
    2628        new Links();
    2729        new Illuminate();
  • integration-of-pathao-for-woocommerce/trunk/includes/Admin/Order.php

    r3131987 r3156908  
    1616        add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );
    1717        add_action( 'pathao_order_created', array( $this, 'store_log_after_creation' ) );
     18
     19        // order columns.
     20        add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_custom_columns' ) );
     21        add_filter( 'woocommerce_shop_order_list_table_columns', array( $this, 'add_custom_columns' ) );
     22        add_action( 'manage_shop_order_posts_custom_column', array( $this, 'add_custom_columns_data' ), 10, 2 );
     23        add_action( 'init', array( $this, 'load_hpos_hooks' ) );
     24    }
     25
     26    /**
     27     * Load HPOS hooks here else `wc_get_page_screen_id` isn't available.
     28     */
     29    public function load_hpos_hooks() {
     30        add_action( 'manage_' . wc_get_page_screen_id( 'shop_order' ) . '_custom_column', array( $this, 'add_custom_columns_data' ), 10, 2 );
     31    }
     32
     33    /**
     34     * Add Custom Column to Orders Table.
     35     *
     36     * @param array $columns Columns.
     37     *
     38     * @return array
     39     */
     40    public function add_custom_columns( $columns ) {
     41        $columns['sdevs_pathao_order_column'] = __( 'Pathao', 'sdevs_pathao' );
     42        return $columns;
     43    }
     44
     45    /**
     46     * Add Custom Column Data.
     47     *
     48     * @param string        $column Column ID.
     49     * @param int|\WC_Order $post_id post_id or Order Obj.
     50     */
     51    public function add_custom_columns_data( $column, $post_id ) {
     52        if ( 'sdevs_pathao_order_column' === $column ) :
     53            // check if post_id is order object.
     54            $order = $post_id;
     55            if ( 'object' !== gettype( $post_id ) ) {
     56                $order = wc_get_order( $post_id );
     57            }
     58            $consignment_id = $order->get_meta( '_pathao_consignment_id' );
     59            $status         = $order->get_meta( '_pathao_order_status' );
     60
     61            ?>
     62            <div>
     63                <?php if ( $consignment_id ) : ?>
     64                    <p>
     65                        <code>
     66                            <?php echo esc_html( $consignment_id ); ?>
     67                        </code>
     68                    </p>
     69                    <p><b>(<?php echo esc_html( $status ); ?>)</b></p>
     70                <?php else : ?>
     71                    <p>-</p>
     72                <?php endif; ?>
     73            </div>
     74            <?php
     75        endif;
    1876    }
    1977
     
    3391                'order_status'      => $res->order_status,
    3492                'order_status_slug' => $res->order_status,
    35                 'reason'            => __( 'Pathao Order created & it\'s pending.', 'sdevs_pathao' ),
     93                'reason'            => 'Pathao Order created & it\'s pending.',
    3694                'updated_at'        => current_time( 'mysql' ),
    3795            )
     
    112170        wp_enqueue_script( 'pathao_admin_script' );
    113171
    114         $cities = sdevs_get_pathao_data( 'aladdin/api/v1/countries/1/city-list' );
    115         $cities = $cities && 'success' === $cities->type ? $cities->data->data : array();
    116 
    117172        $amount = is_sdevs_pathao_pro_activated() && $order->has_status( substr( sdevs_pathao_settings( 'paid_order_status', 'wc-paid' ), 3 ) ) ? 0 : $order->get_total();
    118173
    119         $total_weight     = 0;
    120         $item_description = array();
    121         foreach ( $order->get_items() as $order_item ) {
    122             $product = $order_item->get_product();
    123             if ( ! $product->is_virtual() ) {
    124                 array_push( $item_description, "{$order_item->get_name()}(x{$order_item->get_quantity()})" );
    125                 $total_weight += empty( $product->get_weight() ) ? 0 : intval( $product->get_weight() ) * $order_item['quantity'];
    126             }
    127         }
    128         $total_weight     = floatval( max( $total_weight, 0.5 ) );
     174        $all_totals       = sdevs_pathao_get_totals_from_items( $order );
     175        $total_weight     = $all_totals->weight;
     176        $item_description = $all_totals->item_description;
    129177        $status           = $order->get_meta( '_pathao_order_status' );
    130         $item_description = implode( ', ', $item_description );
    131178
    132179        include 'views/pathao-shipping.php';
  • integration-of-pathao-for-woocommerce/trunk/includes/Admin/Settings.php

    r2985736 r3156908  
    77 */
    88class Settings {
    9 
    109
    1110    /**
     
    2726     * @return array
    2827     */
    29     public function update_settings_on_free( $settings ) {
     28    public function update_settings_on_free( array $settings ): array {
    3029        if ( ! is_sdevs_pathao_pro_activated() ) {
    3130            $option = get_option( 'woocommerce_pathao_settings' );
  • integration-of-pathao-for-woocommerce/trunk/includes/Admin/views/pathao-shipping.php

    r3131987 r3156908  
    77    <input type="hidden" value="<?php echo esc_html( $order_id ); ?>" id="pathao_order_id">
    88    <p class="form-field">
    9         <label for="pathao_delivery_type"><b>Delivery Type</b></label>
     9        <label for="pathao_delivery_type">
     10            <b>Delivery Type</b>
     11            <abbr class="required" title="required">*</abbr>
     12        </label>
    1013        <select style="width: 100%;" name="pathao_delivery_type" id="pathao_delivery_type">
    1114            <option value="48">Normal Delivery</option>
     
    1417    </p>
    1518    <p class="form-field">
    16         <label for="pathao_item_type"><b>Item Type</b></label>
     19        <label for="pathao_item_type">
     20            <b>Item Type</b>
     21            <abbr class="required" title="required">*</abbr>
     22        </label>
    1723        <select style="width: 100%;" name="pathao_item_type" id="pathao_item_type">
    1824            <option value="2">Parcel</option>
     
    2228    <p class="form-field">
    2329        <label for="pathao_city"><b>City</b></label>
    24         <select style="width: 100%;" id="pathao_city" name="pathao_city">
     30        <select style="width: 95%;" id="pathao_city" name="pathao_city">
    2531            <option value="">Select City</option>
    26             <?php
    27             foreach ( $cities as $city ) :
    28                 ?>
    29                 <option
    30                     value="<?php echo esc_attr( $city->city_id ); ?>" <?php is_sdevs_pathao_pro_activated() ? selected( $city->city_id, $order->get_meta( '_shipping_pathao_city_id' ) ) : ''; ?>><?php echo esc_html( $city->city_name ); ?></option>
    31             <?php endforeach; ?>
    3232        </select>
    3333    </p>
    3434    <p class="form-field" id="pathao_zone_select" style="display: none;">
    3535        <label for="pathao_zone"><b>Zone</b></label>
    36         <select style="width: 100%;" id="pathao_zone" name="pathao_zone">
     36        <select style="width: 95%;" id="pathao_zone" name="pathao_zone">
    3737        </select>
    3838    </p>
    3939    <p class="form-field" id="pathao_area_select" style="display: none;">
    4040        <label for="pathao_area"><b>Area</b></label>
    41         <select style="width: 100%;" id="pathao_area" name="pathao_area">
     41        <select style="width: 95%;" id="pathao_area" name="pathao_area">
    4242        </select>
    4343    </p>
    4444    <p class="form-field">
    45         <label for="pathao_weight"><b>Total weight (kg)</b></label>
     45        <label for="pathao_weight">
     46            <b>Total weight (kg)</b>
     47            <abbr class="required" title="required">*</abbr>   
     48        </label>
    4649        <input type="text" value="<?php echo is_sdevs_pathao_pro_activated() ? esc_html( $total_weight ) : ''; ?>"
    4750                id="pathao_weight" name="pathao_weight" />
    4851    </p>
    4952    <p class="form-field">
    50         <label for="pathao_amount"><b>Amount to Collect</b></label>
     53        <label for="pathao_amount">
     54            <b>Amount to Collect</b>
     55            <abbr class="required" title="required">*</abbr>
     56        </label>
    5157        <input type="text" value="<?php echo esc_html( round( $amount ) ); ?>" id="pathao_amount" name="pathao_amount" />
    5258    </p>
    5359    <p class="form-field">
    54         <label for="pathao_item_description"><b>Item Description</b></label>
     60        <label for="pathao_item_description">
     61            <b>Item Description</b>
     62        </label>
    5563        <textarea style="width: 100%;" id="pathao_item_description" name="pathao_item_description"><?php echo esc_html( trim( $item_description ) ); ?></textarea>
    5664    </p>
  • integration-of-pathao-for-woocommerce/trunk/includes/Ajax.php

    r3034808 r3156908  
    33namespace SpringDevs\Pathao;
    44
    5 use stdClass;
     5use SpringDevs\Pathao\Facades\PathaoAPI;
    66
    77/**
     
    1818    public function __construct() {
    1919        add_action( 'wp_ajax_setup_pathao', array( $this, 'setup_pathao' ) );
     20        add_action( 'wp_ajax_get_cities', array( $this, 'get_cities' ) );
    2021        add_action( 'wp_ajax_get_city_zones', array( $this, 'get_city_zones' ) );
    2122        add_action( 'wp_ajax_get_zone_areas', array( $this, 'get_zone_areas' ) );
    2223        add_action( 'wp_ajax_send_order_to_pathao', array( $this, 'send_order_to_pathao' ) );
     24    }
     25
     26    /**
     27     * Get cities.
     28     *
     29     * @return void
     30     */
     31    public function get_cities() {
     32        if ( ! isset( $_POST['nonce'], $_POST['order_id'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'pathao_send_order' ) ) {
     33            return;
     34        }
     35
     36        $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
     37        $cities   = PathaoAPI::get_cities();
     38
     39        wp_send_json(
     40            array(
     41                'cities' => $cities->data,
     42                'value'  => apply_filters( 'pathao_selected_order_city_value', null, $order_id ),
     43            )
     44        );
    2345    }
    2446
     
    4062            'username'      => sanitize_email( wp_unslash( $_POST['client_username'] ) ),
    4163            'password'      => sanitize_text_field( wp_unslash( $_POST['client_password'] ) ),
    42             'grant_type'    => 'password',
    4364        );
    4465
    4566        update_option( 'pathao_sandbox_mode', 'true' === $_POST['sandbox_mode'] ? true : false );
    46         $base_url = sdevs_pathao_base_url();
    47 
    48         $res      = wp_remote_post(
    49             $base_url . 'aladdin/api/v1/issue-token',
    50             array(
    51                 'body' => $data,
    52             )
    53         );
    54         $res_code = wp_remote_retrieve_response_code( $res );
    55         $data     = wp_remote_retrieve_body( $res );
    56         $data     = wp_remote_retrieve_body( $res );
    57         $data     = json_decode( $data );
    58 
    59         if ( 200 === $res_code ) {
     67
     68        $res = PathaoAPI::generate_tokens( $data );
     69
     70        if ( $res->success ) {
    6071            update_option( 'pathao_client_id', $client_id );
    6172            update_option( 'pathao_client_secret', $client_secret );
    62             update_option( 'pathao_access_token', $data->access_token );
    63             update_option( 'pathao_refresh_token', $data->refresh_token );
    64             wp_send_json_success( $data );
    65         }
    66 
    67         wp_send_json_error( $data );
    68         die();
     73            update_option( 'pathao_access_token', $res->data->access_token );
     74            update_option( 'pathao_refresh_token', $res->data->refresh_token );
     75            wp_send_json(
     76                array(
     77                    'success'       => true,
     78                    'access_token'  => $res->data->access_token,
     79                    'refresh_token' => $res->data->refresh_token,
     80                )
     81            );
     82        }
     83
     84        wp_send_json( $res );
    6985    }
    7086
     
    8197        $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
    8298        $city     = sanitize_text_field( wp_unslash( $_POST['city'] ) );
    83         $zones    = sdevs_get_pathao_data( "aladdin/api/v1/cities/$city/zone-list" );
    84         $zones    = 'success' === $zones->type ? $zones->data->data : array();
     99
     100        $zones = PathaoAPI::get_zones( $city );
     101        $zones = $zones->success ? $zones->data : array();
    85102
    86103        wp_send_json(
     
    104121        $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
    105122        $zone     = sanitize_text_field( wp_unslash( $_POST['zone'] ) );
    106         $areas    = sdevs_get_pathao_data( "aladdin/api/v1/zones/$zone/area-list" );
    107         $areas    = 'success' === $areas->type ? $areas->data->data : array();
     123        $areas    = PathaoAPI::get_areas( $zone );
     124        $areas    = $areas->success ? $areas->data : array();
    108125
    109126        wp_send_json(
     
    119136     */
    120137    public function send_order_to_pathao() {
    121         if ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'pathao_send_order' ) && isset( $_POST['order_id'] ) && isset( $_POST['city'] ) && isset( $_POST['zone'] ) && isset( $_POST['area'] ) && isset( $_POST['special_instruction'], $_POST['item_description'] ) && isset( $_POST['delivery_type'] ) && isset( $_POST['item_type'] ) && isset( $_POST['amount'] ) && isset( $_POST['item_weight'] ) ) {
    122             $order_id            = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
    123             $store               = sdevs_pathao_store_id();
    124             $city                = sanitize_text_field( wp_unslash( $_POST['city'] ) );
    125             $zone                = sanitize_text_field( wp_unslash( $_POST['zone'] ) );
    126             $area                = sanitize_text_field( wp_unslash( $_POST['area'] ) );
    127             $item_description    = trim( sanitize_text_field( wp_unslash( $_POST['item_description'] ) ) );
    128             $special_instruction = trim( sanitize_text_field( wp_unslash( $_POST['special_instruction'] ) ) );
    129             $delivery_type       = sanitize_text_field( wp_unslash( $_POST['delivery_type'] ) );
    130             $item_type           = sanitize_text_field( wp_unslash( $_POST['item_type'] ) );
    131             $amount              = sanitize_text_field( wp_unslash( $_POST['amount'] ) );
    132             $item_weight         = sanitize_text_field( wp_unslash( $_POST['item_weight'] ) );
    133 
    134             $res = $this->send_order( $order_id, $store, $city, $zone, $area, $special_instruction, $item_description, $delivery_type, $item_type, $amount, $item_weight );
    135 
    136             if ( 'error' === $res->type ) {
    137                 wp_send_json(
    138                     array(
    139                         'success' => false,
    140                         'message' => $res->message,
    141                         'errors'  => $res->errors,
    142                     )
    143                 );
    144             }
    145 
    146             $res_data = $res->data;
    147             $order    = wc_get_order( $order_id );
    148             $order->update_meta_data( '_pathao_consignment_id', $res_data->consignment_id );
    149             $order->update_meta_data( '_pathao_delivery_fee', $res_data->delivery_fee );
    150             $order->update_meta_data( '_pathao_order_status', $res_data->order_status );
    151             $order->save();
    152 
    153             do_action( 'pathao_order_created', $res_data );
    154 
    155             wp_send_json(
    156                 array(
    157                     'success' => true,
    158                     'message' => 'Order sent to Pathao successfull.',
    159                 )
    160             );
    161         }
    162 
    163         wp_send_json(
    164             array(
    165                 'success' => false,
    166                 'message' => 'Invalid nonce',
    167             )
    168         );
    169     }
    170 
    171     /**
    172      * Send order to pathao server.
    173      *
    174      * @param int        $order_id Order Id.
    175      * @param int        $store Pathao Store Id.
    176      * @param int        $city City Id.
    177      * @param int        $zone Zone Id.
    178      * @param string|int $area Area Id.
    179      * @param string     $special_instruction instructions.
    180      * @param string     $item_description item instructions.
    181      * @param string     $delivery_type Normal or On-Demand.
    182      * @param string     $item_type document or parcel.
    183      * @param float      $amount amount.
    184      * @param float      $item_weight total order weight.
    185      *
    186      * @return mixed
    187      */
    188     public function send_order( $order_id, $store, $city, $zone, $area, $special_instruction, $item_description, $delivery_type, $item_type, $amount, $item_weight ) {
    189         $base_url     = sdevs_pathao_base_url();
    190         $access_token = get_option( 'pathao_access_token' );
    191 
    192         if ( ! $access_token ) {
     138        if ( ! isset( $_POST['nonce'], $_POST['order_id'], $_POST['item_type'], $_POST['delivery_type'], $_POST['amount'], $_POST['item_weight'] ) ) {
     139            return;
     140        }
     141
     142        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'pathao_send_order' ) ) {
    193143            wp_send_json(
    194144                array(
    195145                    'success' => false,
    196                     'message' => 'Please generate access token to use pathao plugin !!',
     146                    'errors'  => array( __( 'Invalid nonce', 'sdevs_pathao' ) ),
    197147                )
    198148            );
    199149        }
    200150
    201         $order             = wc_get_order( $order_id );
    202         $recipient_name    = $order->get_formatted_shipping_full_name() !== ' ' ? $order->get_formatted_shipping_full_name() : $order->get_formatted_billing_full_name();
    203         $recipient_phone   = $order->get_shipping_phone() !== '' ? $order->get_shipping_phone() : $order->get_billing_phone();
    204         $recipient_phone   = substr( $recipient_phone, 0, 3 ) === '+88' ? str_replace( '+88', '', $recipient_phone ) : $recipient_phone;
    205         $recipient_address = $order->get_formatted_shipping_address() !== '' ? $order->get_formatted_shipping_address() : $order->get_formatted_billing_address();
    206 
    207         $body = array(
    208             'store_id'            => $store,
    209             'merchant_order_id'   => $order_id,
    210             'recipient_name'      => $recipient_name,
    211             'recipient_phone'     => $recipient_phone,
    212             'recipient_address'   => $recipient_address,
    213             'recipient_city'      => $city,
    214             'recipient_zone'      => $zone,
    215             'delivery_type'       => $delivery_type,
    216             'item_type'           => $item_type,
    217             'special_instruction' => $special_instruction,
    218             'item_description'    => $item_description,
    219             'item_quantity'       => $order->get_item_count(),
    220             'item_weight'         => $item_weight,
    221             'amount_to_collect'   => $amount,
    222         );
    223 
    224         if ( '' !== $area ) {
    225             $body['recipient_area'] = $area;
    226         }
    227 
    228         $res = wp_remote_post(
    229             $base_url . 'aladdin/api/v1/orders',
    230             array(
    231                 'headers' => array(
    232                     'Authorization' => 'Bearer ' . $access_token,
    233                 ),
    234                 'body'    => $body,
    235             )
    236         );
    237 
    238         $body        = wp_remote_retrieve_body( $res );
    239         $status_code = wp_remote_retrieve_response_code( $res );
    240         $data        = json_decode( $body );
    241 
    242         if ( 401 === $status_code ) {
    243             $err_res          = new stdClass();
    244             $err_res->type    = 'error';
    245             $err_res->message = __( 'Please generate your token !!', 'sdevs_pathao' );
    246             $err_res->errors  = array(
    247                 'auth' => array( __( 'Please generate your token !!', 'sdevs_pathao' ) ),
     151        $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
     152        $body     = array();
     153
     154        if ( ! empty( $_POST['city'] ) && ! empty( $_POST['zone'] ) ) {
     155            $body['recipient_city'] = sanitize_text_field( wp_unslash( $_POST['city'] ) );
     156            $body['recipient_zone'] = sanitize_text_field( wp_unslash( $_POST['zone'] ) );
     157            if ( ! empty( $_POST['area'] ) ) {
     158                $body['recipient_area'] = sanitize_text_field( wp_unslash( $_POST['area'] ) );
     159            }
     160        }
     161
     162        if ( ! empty( $_POST['item_description'] ) ) {
     163            $body['item_description'] = trim( sanitize_text_field( wp_unslash( $_POST['item_description'] ) ) );
     164        }
     165
     166        if ( ! empty( $_POST['special_instruction'] ) ) {
     167            $body['special_instruction'] = trim( sanitize_text_field( wp_unslash( $_POST['special_instruction'] ) ) );
     168        }
     169
     170        if ( ! empty( $_POST['delivery_type'] ) ) {
     171            $body['delivery_type'] = sanitize_text_field( wp_unslash( $_POST['delivery_type'] ) );
     172        }
     173
     174        if ( ! empty( $_POST['item_type'] ) ) {
     175            $body['item_type'] = sanitize_text_field( wp_unslash( $_POST['item_type'] ) );
     176        }
     177
     178        if ( ! empty( $_POST['item_weight'] ) ) {
     179            $body['item_weight'] = sanitize_text_field( wp_unslash( $_POST['item_weight'] ) );
     180        }
     181
     182        if ( ! empty( $_POST['amount'] ) ) {
     183            $body['amount_to_collect'] = sanitize_text_field( wp_unslash( $_POST['amount'] ) );
     184        }
     185
     186        $res_data = PathaoAPI::send_order( $order_id, $body );
     187
     188        if ( ! $res_data->success ) {
     189            wp_send_json(
     190                array(
     191                    'success' => false,
     192                    'errors'  => $res_data->messages,
     193                )
    248194            );
    249             return $err_res;
    250         }
    251 
    252         return $data;
     195        }
     196
     197        $order = wc_get_order( $order_id );
     198        $order->update_meta_data( '_pathao_consignment_id', $res_data->data->consignment_id );
     199        $order->update_meta_data( '_pathao_delivery_fee', $res_data->data->delivery_fee );
     200        $order->update_meta_data( '_pathao_order_status', $res_data->data->order_status );
     201        $order->save();
     202
     203        do_action( 'pathao_order_created', $res_data->data );
     204
     205        wp_send_json(
     206            array(
     207                'success' => true,
     208                'message' => 'Order sent to Pathao successfull.',
     209            )
     210        );
    253211    }
    254212}
  • integration-of-pathao-for-woocommerce/trunk/includes/Api.php

    r3013075 r3156908  
    1414 * API Class
    1515 */
    16 class API {
     16class Api {
    1717
    1818
  • integration-of-pathao-for-woocommerce/trunk/includes/Frontend.php

    r2985736 r3156908  
    11<?php
    2 
    32/**
    43 * Frontend handler class
  • integration-of-pathao-for-woocommerce/trunk/includes/Illuminate/Cron.php

    r2985736 r3156908  
    22
    33namespace SpringDevs\Pathao\Illuminate;
     4
     5use SpringDevs\Pathao\Facades\PathaoAPI;
    46
    57/**
     
    1012class Cron {
    1113
    12 
    1314    /**
    1415     * Initialize the class.
     
    1819    }
    1920
     21    /**
     22     * Refresh tokens.
     23     *
     24     * @return void
     25     */
    2026    public function update_token() {
    21         $client_id     = get_option( 'pathao_client_id' );
    22         $client_secret = get_option( 'pathao_client_secret' );
    23         $refresh_token = get_option( 'pathao_refresh_token' );
     27        $res = PathaoAPI::refresh_tokens();
    2428
    25         if ( ! $client_id || ! $client_secret || ! $refresh_token ) {
     29        if ( ! $res->success ) {
    2630            return;
    2731        }
    2832
    29         $base_url = sdevs_pathao_base_url();
    30         $data     = array(
    31             'client_id'     => $client_id,
    32             'client_secret' => $client_secret,
    33             'refresh_token' => $refresh_token,
    34             'grant_type'    => 'refresh_token',
    35         );
    36 
    37         $res      = wp_remote_post(
    38             $base_url . 'aladdin/api/v1/issue-token',
    39             array(
    40                 'body' => $data,
    41             )
    42         );
    43         $res_code = wp_remote_retrieve_response_code( $res );
    44 
    45         if ( $res_code == 200 ) {
    46             $data = wp_remote_retrieve_body( $res );
    47             $data = json_decode( $data );
    48             update_option( 'pathao_access_token', $data->access_token );
    49             update_option( 'pathao_refresh_token', $data->refresh_token );
    50         }
     33        $data = $res->data;
     34        update_option( 'pathao_access_token', $data->access_token );
     35        update_option( 'pathao_refresh_token', $data->refresh_token );
    5136    }
    5237}
  • integration-of-pathao-for-woocommerce/trunk/includes/Illuminate/class-pathao-method.php

    r3131987 r3156908  
    11<?php
     2
     3use SpringDevs\Pathao\Facades\PathaoAPI;
     4
    25if ( ! defined( 'ABSPATH' ) ) {
    36    exit;
     
    6467             */
    6568            public function init_form_fields() {
    66                 $stores          = sdevs_get_pathao_data( 'aladdin/api/v1/stores' );
    67                 $stores          = $stores && 'success' === $stores->type ? $stores->data->data : array();
     69                $stores          = PathaoAPI::get_stores();
     70                $stores          = $stores->success ? $stores->data : array();
    6871                $dropdown_stores = array();
    6972                foreach ( $stores as $store ) {
    70                     $dropdown_stores[ $store->store_id ] = $store->store_name;
     73                    $dropdown_stores[ $store->id ] = $store->name;
    7174                }
    7275
     
    8083                    $this->update_option( 'title', 'Pathao' );
    8184                    $this->update_option( 'store', array_key_first( $dropdown_stores ) );
    82                     $this->update_option( 'replace_checkout_fields', 'yes' );
    8385                    $this->update_option( 'area_field', 'display_required' );
    8486                    $this->update_option( 'delivery_type', 48 );
     
    223225             * @access public
    224226             *
    225              * @param Array $package Package.
     227             * @param array $package Package.
    226228             *
    227229             * @return void
  • integration-of-pathao-for-woocommerce/trunk/includes/functions.php

    r3131987 r3156908  
    99
    1010use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
    11 
    12 /**
    13  * Get filename extension.
    14  *
    15  * @param string $file_name File name.
    16  *
    17  * @return false|string
    18  * @since 1.0.0
    19  */
    20 function sdevs_get_pathao_get_extension( $file_name ) {
    21     $n = strrpos( $file_name, '.' );
    22 
    23     return ( false === $n ) ? '' : substr( $file_name, $n + 1 );
    24 }
    25 
    26 /**
    27  * Get pathao base URL.
    28  *
    29  * @return string
    30  */
    31 function sdevs_pathao_base_url(): string {
    32     return get_option( 'pathao_sandbox_mode' ) ? 'https://courier-api-sandbox.pathao.com/' : 'https://api-hermes.pathao.com/';
    33 }
    34 
    35 /**
    36  * Get data from pathao server.
    37  *
    38  * @param string $endpoint Endpoint.
    39  *
    40  * @return [object]
    41  */
    42 function sdevs_get_pathao_data( string $endpoint ) {
    43     $base_url     = sdevs_pathao_base_url();
    44     $access_token = get_option( 'pathao_access_token' );
    45 
    46     if ( ! $access_token ) {
    47         return (object) array(
    48             'type'  => 'failed',
    49             'error' => 'Please generate access token to use pathao plugin !!',
    50         );
    51     }
    52 
    53     $res = wp_remote_get(
    54         $base_url . $endpoint,
    55         array(
    56             'headers' => array(
    57                 'Authorization' => 'Bearer ' . $access_token,
    58                 'Accept'        => 'application/json',
    59             ),
    60         )
    61     );
    62 
    63     $data     = json_decode( wp_remote_retrieve_body( $res ) );
    64     $res_code = wp_remote_retrieve_response_code( $res );
    65 
    66     if ( 200 === $res_code ) {
    67         return $data;
    68     }
    69 
    70     return (object) array(
    71         'type'     => 'failed',
    72         'res_code' => $res_code,
    73         'body'     => $data,
    74     );
    75 }
    76 
    77 /**
    78  * Send request on pathao server.
    79  *
    80  * @param string $endpoint Endpoint.
    81  * @param array  $body Body.
    82  *
    83  * @return mixed|object [object]
    84  */
    85 function sdevs_send_pathao_data( string $endpoint, array $body ) {
    86     $base_url     = sdevs_pathao_base_url();
    87     $access_token = get_option( 'pathao_access_token' );
    88 
    89     if ( ! $access_token ) {
    90         return (object) array(
    91             'type'  => 'failed',
    92             'error' => 'Please generate access token to use pathao plugin !!',
    93         );
    94     }
    95 
    96     $res = wp_remote_post(
    97         $base_url . $endpoint,
    98         array(
    99             'headers' => array(
    100                 'Authorization' => 'Bearer ' . $access_token,
    101                 'Accept'        => 'application/json',
    102             ),
    103             'body'    => $body,
    104         )
    105     );
    106 
    107     $data     = json_decode( wp_remote_retrieve_body( $res ) );
    108     $res_code = wp_remote_retrieve_response_code( $res );
    109 
    110     if ( 200 === $res_code ) {
    111         return $data;
    112     }
    113 
    114     return (object) array(
    115         'type'     => 'failed',
    116         'res_code' => $res_code,
    117         'body'     => $data,
    118     );
    119 }
    12011
    12112/**
     
    16758
    16859/**
     60 * Get total weight, quantity, description from order.
     61 *
     62 * @param \WC_Order $order Order Object.
     63 *
     64 * @return object
     65 */
     66function sdevs_pathao_get_totals_from_items( \WC_Order $order ) {
     67    $total_weight     = 0;
     68    $quantity         = 0;
     69    $item_description = array();
     70    foreach ( $order->get_items() as $order_item ) {
     71        $product = $order_item->get_product();
     72        if ( ! $product->is_virtual() ) {
     73            array_push( $item_description, "{$order_item->get_name()}(x{$order_item->get_quantity()})" );
     74            $quantity     += $order_item['quantity'];
     75            $total_weight += empty( $product->get_weight() ) ? 0 : intval( $product->get_weight() ) * $order_item['quantity'];
     76        }
     77    }
     78    $total_weight     = floatval( max( $total_weight, 0.5 ) );
     79    $item_description = implode( ', ', $item_description );
     80
     81    return (object) array(
     82        'weight'           => 0 === $total_weight ? apply_filters( 'sdevs_pathao_default_weight', 0.5 ) : $total_weight,
     83        'item_description' => $item_description,
     84        'quantity'         => $quantity,
     85    );
     86}
     87
     88/**
    16989 * Check if HPOS enabled.
    17090 */
  • integration-of-pathao-for-woocommerce/trunk/integration-of-pathao-for-woocommerce.php

    r3131987 r3156908  
    44 * Plugin URI: https://springdevs.com/plugin/pathao
    55 * Description: Pathao integration for WooCommerce
    6  * Version: 1.0.9
     6 * Version: 1.1
    77 * Author: SpringDevs
    88 * Author URI: https://springdevs.com
     
    6363     * @since 1.0.0
    6464     */
    65     const VERSION = '1.0.9';
     65    const VERSION = '1.1.0';
    6666
    6767    /**
  • integration-of-pathao-for-woocommerce/trunk/readme.txt

    r3131987 r3156908  
    8181== Changelog ==
    8282
     83= 1.1 =
     84* **New:** Pathao column on order list.
     85* **Update:** City and Zone fields are totally optional now.
     86* **Update:** Optimize perfromence.
     87* **Update:** Rebuild the API part.
     88
    8389= 1.0.9 =
    8490* **Update:** Autofill item description field.
  • integration-of-pathao-for-woocommerce/trunk/vendor/composer/installed.php

    r3131987 r3156908  
    44        'pretty_version' => 'dev-next',
    55        'version' => 'dev-next',
    6         'reference' => 'c1f9d944740d1ac866733730aa20d6872256f955',
     6        'reference' => '493eca2e56dc181d2a891c542389b937b9a1e2a7',
    77        'type' => 'wordpress-plugin',
    88        'install_path' => __DIR__ . '/../../',
     
    1414            'pretty_version' => 'dev-next',
    1515            'version' => 'dev-next',
    16             'reference' => 'c1f9d944740d1ac866733730aa20d6872256f955',
     16            'reference' => '493eca2e56dc181d2a891c542389b937b9a1e2a7',
    1717            'type' => 'wordpress-plugin',
    1818            'install_path' => __DIR__ . '/../../',
Note: See TracChangeset for help on using the changeset viewer.