Changeset 3156908
- Timestamp:
- 09/24/2024 12:19:22 PM (17 months ago)
- Location:
- integration-of-pathao-for-woocommerce/trunk
- Files:
-
- 6 added
- 1 deleted
- 14 edited
-
assets/js/admin.js (modified) (8 diffs)
-
includes/Admin.php (modified) (2 diffs)
-
includes/Admin/Notice.php (added)
-
includes/Admin/Order.php (modified) (3 diffs)
-
includes/Admin/Settings.php (modified) (2 diffs)
-
includes/Admin/views/no-token.php (added)
-
includes/Admin/views/pathao-shipping.php (modified) (3 diffs)
-
includes/Ajax.php (modified) (6 diffs)
-
includes/Api.php (modified) (1 diff)
-
includes/Facades (added)
-
includes/Facades/PathaoAPI.php (added)
-
includes/Frontend.php (modified) (1 diff)
-
includes/Illuminate/Cron.php (modified) (3 diffs)
-
includes/Illuminate/class-pathao-method.php (modified) (4 diffs)
-
includes/Services (added)
-
includes/Services/PathaoApiService.php (added)
-
includes/Traits (deleted)
-
includes/functions.php (modified) (2 diffs)
-
integration-of-pathao-for-woocommerce.php (modified) (2 diffs)
-
readme.txt (modified) (1 diff)
-
vendor/composer/installed.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
integration-of-pathao-for-woocommerce/trunk/assets/js/admin.js
r3106287 r3156908 1 1 jQuery(document).ready(function ($) { 2 2 const nonce = $('#pathao_send_order_nonce').val(); 3 4 $('#pathao_city').selectWoo(); 5 $('#pathao_zone').selectWoo(); 6 $('#pathao_area').selectWoo(); 7 getCities(); 3 8 4 9 $('#pathao-setup').on('submit', async function (e) { … … 35 40 }); 36 41 }); 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); 39 44 window.location.reload(); 40 45 } else { 41 46 $('.pathao-notice').after( 42 47 '<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>' 45 50 ); 46 51 $('#dismiss-message').click(function (event) { … … 76 81 const zone = $(this).val(); 77 82 78 if ( zone == '') {83 if (!zone || '' == zone) { 79 84 $('#pathao_area_select').hide(); 80 85 return false; … … 101 106 $('#pathao_area').append( 102 107 '<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>' 109 114 ); 110 115 }); … … 173 178 } 174 179 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 == '') { 186 181 $.toast({ 187 182 position: 'bottom-center', … … 270 265 $('.pathao-shipping-spinner').removeClass('is-active'); 271 266 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 }280 267 $.each(errors, function (key, value) { 281 268 $.toast({ 282 269 position: 'bottom-center', 283 text: value [0],270 text: value, 284 271 icon: 'error', 285 272 hideAfter: 6000, … … 318 305 }, 319 306 success: function (res) { 320 $('#pathao_zone').html(''); 307 $('#pathao_zone').html( 308 '<option value="">No Zone Selected</option>' 309 ); 321 310 $.each(res.zones, function (key, value) { 322 311 $('#pathao_zone').append( 323 312 '<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>' 330 319 ); 331 320 }); … … 343 332 }); 344 333 } 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 } 345 375 }); 346 376 -
integration-of-pathao-for-woocommerce/trunk/includes/Admin.php
r3013075 r3156908 9 9 10 10 use SpringDevs\Pathao\Admin\Links; 11 use SpringDevs\Pathao\Admin\Notice; 11 12 use SpringDevs\Pathao\Admin\Settings; 12 13 … … 24 25 */ 25 26 public function __construct() { 27 new Notice(); 26 28 new Links(); 27 29 new Illuminate(); -
integration-of-pathao-for-woocommerce/trunk/includes/Admin/Order.php
r3131987 r3156908 16 16 add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) ); 17 17 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; 18 76 } 19 77 … … 33 91 'order_status' => $res->order_status, 34 92 'order_status_slug' => $res->order_status, 35 'reason' => __( 'Pathao Order created & it\'s pending.', 'sdevs_pathao' ),93 'reason' => 'Pathao Order created & it\'s pending.', 36 94 'updated_at' => current_time( 'mysql' ), 37 95 ) … … 112 170 wp_enqueue_script( 'pathao_admin_script' ); 113 171 114 $cities = sdevs_get_pathao_data( 'aladdin/api/v1/countries/1/city-list' );115 $cities = $cities && 'success' === $cities->type ? $cities->data->data : array();116 117 172 $amount = is_sdevs_pathao_pro_activated() && $order->has_status( substr( sdevs_pathao_settings( 'paid_order_status', 'wc-paid' ), 3 ) ) ? 0 : $order->get_total(); 118 173 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; 129 177 $status = $order->get_meta( '_pathao_order_status' ); 130 $item_description = implode( ', ', $item_description );131 178 132 179 include 'views/pathao-shipping.php'; -
integration-of-pathao-for-woocommerce/trunk/includes/Admin/Settings.php
r2985736 r3156908 7 7 */ 8 8 class Settings { 9 10 9 11 10 /** … … 27 26 * @return array 28 27 */ 29 public function update_settings_on_free( $settings ){28 public function update_settings_on_free( array $settings ): array { 30 29 if ( ! is_sdevs_pathao_pro_activated() ) { 31 30 $option = get_option( 'woocommerce_pathao_settings' ); -
integration-of-pathao-for-woocommerce/trunk/includes/Admin/views/pathao-shipping.php
r3131987 r3156908 7 7 <input type="hidden" value="<?php echo esc_html( $order_id ); ?>" id="pathao_order_id"> 8 8 <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> 10 13 <select style="width: 100%;" name="pathao_delivery_type" id="pathao_delivery_type"> 11 14 <option value="48">Normal Delivery</option> … … 14 17 </p> 15 18 <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> 17 23 <select style="width: 100%;" name="pathao_item_type" id="pathao_item_type"> 18 24 <option value="2">Parcel</option> … … 22 28 <p class="form-field"> 23 29 <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"> 25 31 <option value="">Select City</option> 26 <?php27 foreach ( $cities as $city ) :28 ?>29 <option30 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; ?>32 32 </select> 33 33 </p> 34 34 <p class="form-field" id="pathao_zone_select" style="display: none;"> 35 35 <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"> 37 37 </select> 38 38 </p> 39 39 <p class="form-field" id="pathao_area_select" style="display: none;"> 40 40 <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"> 42 42 </select> 43 43 </p> 44 44 <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> 46 49 <input type="text" value="<?php echo is_sdevs_pathao_pro_activated() ? esc_html( $total_weight ) : ''; ?>" 47 50 id="pathao_weight" name="pathao_weight" /> 48 51 </p> 49 52 <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> 51 57 <input type="text" value="<?php echo esc_html( round( $amount ) ); ?>" id="pathao_amount" name="pathao_amount" /> 52 58 </p> 53 59 <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> 55 63 <textarea style="width: 100%;" id="pathao_item_description" name="pathao_item_description"><?php echo esc_html( trim( $item_description ) ); ?></textarea> 56 64 </p> -
integration-of-pathao-for-woocommerce/trunk/includes/Ajax.php
r3034808 r3156908 3 3 namespace SpringDevs\Pathao; 4 4 5 use stdClass;5 use SpringDevs\Pathao\Facades\PathaoAPI; 6 6 7 7 /** … … 18 18 public function __construct() { 19 19 add_action( 'wp_ajax_setup_pathao', array( $this, 'setup_pathao' ) ); 20 add_action( 'wp_ajax_get_cities', array( $this, 'get_cities' ) ); 20 21 add_action( 'wp_ajax_get_city_zones', array( $this, 'get_city_zones' ) ); 21 22 add_action( 'wp_ajax_get_zone_areas', array( $this, 'get_zone_areas' ) ); 22 23 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 ); 23 45 } 24 46 … … 40 62 'username' => sanitize_email( wp_unslash( $_POST['client_username'] ) ), 41 63 'password' => sanitize_text_field( wp_unslash( $_POST['client_password'] ) ), 42 'grant_type' => 'password',43 64 ); 44 65 45 66 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 ) { 60 71 update_option( 'pathao_client_id', $client_id ); 61 72 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 ); 69 85 } 70 86 … … 81 97 $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) ); 82 98 $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(); 85 102 86 103 wp_send_json( … … 104 121 $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) ); 105 122 $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(); 108 125 109 126 wp_send_json( … … 119 136 */ 120 137 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' ) ) { 193 143 wp_send_json( 194 144 array( 195 145 'success' => false, 196 ' message' => 'Please generate access token to use pathao plugin !!',146 'errors' => array( __( 'Invalid nonce', 'sdevs_pathao' ) ), 197 147 ) 198 148 ); 199 149 } 200 150 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 ) 248 194 ); 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 ); 253 211 } 254 212 } -
integration-of-pathao-for-woocommerce/trunk/includes/Api.php
r3013075 r3156908 14 14 * API Class 15 15 */ 16 class A PI{16 class Api { 17 17 18 18 -
integration-of-pathao-for-woocommerce/trunk/includes/Frontend.php
r2985736 r3156908 1 1 <?php 2 3 2 /** 4 3 * Frontend handler class -
integration-of-pathao-for-woocommerce/trunk/includes/Illuminate/Cron.php
r2985736 r3156908 2 2 3 3 namespace SpringDevs\Pathao\Illuminate; 4 5 use SpringDevs\Pathao\Facades\PathaoAPI; 4 6 5 7 /** … … 10 12 class Cron { 11 13 12 13 14 /** 14 15 * Initialize the class. … … 18 19 } 19 20 21 /** 22 * Refresh tokens. 23 * 24 * @return void 25 */ 20 26 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(); 24 28 25 if ( ! $ client_id || ! $client_secret || ! $refresh_token) {29 if ( ! $res->success ) { 26 30 return; 27 31 } 28 32 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 ); 51 36 } 52 37 } -
integration-of-pathao-for-woocommerce/trunk/includes/Illuminate/class-pathao-method.php
r3131987 r3156908 1 1 <?php 2 3 use SpringDevs\Pathao\Facades\PathaoAPI; 4 2 5 if ( ! defined( 'ABSPATH' ) ) { 3 6 exit; … … 64 67 */ 65 68 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(); 68 71 $dropdown_stores = array(); 69 72 foreach ( $stores as $store ) { 70 $dropdown_stores[ $store-> store_id ] = $store->store_name;73 $dropdown_stores[ $store->id ] = $store->name; 71 74 } 72 75 … … 80 83 $this->update_option( 'title', 'Pathao' ); 81 84 $this->update_option( 'store', array_key_first( $dropdown_stores ) ); 82 $this->update_option( 'replace_checkout_fields', 'yes' );83 85 $this->update_option( 'area_field', 'display_required' ); 84 86 $this->update_option( 'delivery_type', 48 ); … … 223 225 * @access public 224 226 * 225 * @param Array $package Package.227 * @param array $package Package. 226 228 * 227 229 * @return void -
integration-of-pathao-for-woocommerce/trunk/includes/functions.php
r3131987 r3156908 9 9 10 10 use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; 11 12 /**13 * Get filename extension.14 *15 * @param string $file_name File name.16 *17 * @return false|string18 * @since 1.0.019 */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 string30 */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 }120 11 121 12 /** … … 167 58 168 59 /** 60 * Get total weight, quantity, description from order. 61 * 62 * @param \WC_Order $order Order Object. 63 * 64 * @return object 65 */ 66 function 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 /** 169 89 * Check if HPOS enabled. 170 90 */ -
integration-of-pathao-for-woocommerce/trunk/integration-of-pathao-for-woocommerce.php
r3131987 r3156908 4 4 * Plugin URI: https://springdevs.com/plugin/pathao 5 5 * Description: Pathao integration for WooCommerce 6 * Version: 1. 0.96 * Version: 1.1 7 7 * Author: SpringDevs 8 8 * Author URI: https://springdevs.com … … 63 63 * @since 1.0.0 64 64 */ 65 const VERSION = '1. 0.9';65 const VERSION = '1.1.0'; 66 66 67 67 /** -
integration-of-pathao-for-woocommerce/trunk/readme.txt
r3131987 r3156908 81 81 == Changelog == 82 82 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 83 89 = 1.0.9 = 84 90 * **Update:** Autofill item description field. -
integration-of-pathao-for-woocommerce/trunk/vendor/composer/installed.php
r3131987 r3156908 4 4 'pretty_version' => 'dev-next', 5 5 'version' => 'dev-next', 6 'reference' => ' c1f9d944740d1ac866733730aa20d6872256f955',6 'reference' => '493eca2e56dc181d2a891c542389b937b9a1e2a7', 7 7 'type' => 'wordpress-plugin', 8 8 'install_path' => __DIR__ . '/../../', … … 14 14 'pretty_version' => 'dev-next', 15 15 'version' => 'dev-next', 16 'reference' => ' c1f9d944740d1ac866733730aa20d6872256f955',16 'reference' => '493eca2e56dc181d2a891c542389b937b9a1e2a7', 17 17 'type' => 'wordpress-plugin', 18 18 'install_path' => __DIR__ . '/../../',
Note: See TracChangeset
for help on using the changeset viewer.