Current Path : /storage/v11800/testtest/public_html/wp-content/plugins/advatix-shipstation/includes/functions/ |
Linux v11800 5.3.0-1023-aws #25~18.04.1-Ubuntu SMP Fri Jun 5 15:19:18 UTC 2020 aarch64
|
Current File : /storage/v11800/testtest/public_html/wp-content/plugins/advatix-shipstation/includes/functions/ajax-functions.php |
<?php
/**
* Generate Label - Order Ajax
*/
add_action('wp_ajax_adv_ship_generate_label_order', 'adv_ship_generate_label_order_callback');
add_action('wp_ajax_nopriv_adv_ship_generate_label_order', 'adv_ship_generate_label_order_callback');
function adv_ship_generate_label_order_callback()
{
error_reporting(0);
global $wpdb;
$order_id = sanitize_text_field($_POST['order_id']);
$order = wc_get_order( $order_id );
if ( !$order->has_status('on-hold') && !$order->has_status('cancelled') ) {
$base_url = SHIP_STATION_BASE_URL;
$table_name = $wpdb->base_prefix . 'shipstation_api_access';
$q = $wpdb->get_results('SELECT * FROM '.$table_name.' LIMIT 1');
$access_token = $q[0]->access_token;
$headers = array(
'Content-Type' => 'application/json',
'Device-Type' => 'Web',
'Ver' => '1.0',
'Authorization' => 'Bearer '.$access_token
);
$data = advatix_ship_order_data( $order_id );
$api_url = $base_url.'/shipments/createShipmentLabel';
$args = array(
'headers' => $headers,
'timeout' => 300000,
'body' => wp_json_encode($data),
'method' => 'POST'
);
$res = wp_remote_request($api_url, $args );
if(!is_wp_error($res)){
// if(simplexml_load_string($res['body'])){
// $result_jd = json_decode(json_encode((array)simplexml_load_string($res['body'])));
// }else{
$result_jd = json_decode($res['body']);
// }
// echo "<pre>";
// print_r($result_jd);
// echo "</pre>";
$status = $result_jd->responseStatusCode;
if($result_jd->responseObject[0]->labelData != ''){
$msg = 'Label Already Created';
}else{
if(isset($result_jd->responseObject)){
$msg = json_encode($result_jd->responseObject);
}else{
$msg = $result_jd->message;
$status = $result_jd->status;
}
}
// echo $msg;
$table_name = $wpdb->base_prefix . 'shipstation_create_labels';
$wpdb->insert(
$table_name,
array(
'requestJson' => wp_json_encode($data),
'order_id' => $order_id,
'responseMessage' => $msg,
'responseStatus' => $result_jd->responseStatus,
'responseStatusCode' => $status,
'responseObject' => json_encode($result_jd->responseObject),
'labelData' => $result_jd->responseObject[0]->labelData,
'time' => current_time( 'mysql' ),
)
);
$array = array(
'responseCode' => $status,
'responseMessage' => $msg
);
//Save Tracking ID
$table_name = $wpdb->base_prefix . 'shipstation_shipment_tracking';
$q = $wpdb->get_results('SELECT * FROM '.$table_name.' WHERE order_id="'.$order_id.'" AND trackingNumber="'.$result_jd->responseObject[0]->trackingNumber.'"');
if ( empty( $q ) ) {
$table_name = $wpdb->base_prefix . 'shipstation_shipment_tracking';
$wpdb->insert(
$table_name,
array(
'order_id' => $order_id,
'orderItems' => '',
'trackingNumber' => $result_jd->responseObject[0]->trackingNumber,
'trackingUrl' => '',
'carrierName' => $result_jd->responseObject[0]->carrier,
'time' => current_time( 'mysql' ),
)
);
}
if(in_array('wc-shipped',array_keys(wc_get_order_statuses()))){
$order->update_status('shipped');
}
}else{
$table_name = $wpdb->base_prefix . 'shipstation_create_labels';
$wpdb->insert(
$table_name,
array(
'requestJson' => wp_json_encode($data),
'order_id' => $order_id,
'responseMessage' => $res->get_error_message(),
'responseStatus' => $res->get_error_code(),
'responseStatusCode' => $res->get_error_code(),
'responseObject' => "",
'labelData' => '',
'time' => current_time( 'mysql' ),
)
);
$array = array(
'responseCode' => $res->get_error_code(),
'responseMessage' => $res->get_error_message()
);
}
}else{
$array = array(
'responseCode' => '201',
'responseMessage' => 'Order is either on-hold or cancelled'
);
}
// echo wp_json_encode($res['body']);
echo wp_json_encode($array);
die();
}
/**
* Generate Bulk Labels - Order Ajax
*/
add_action('wp_ajax_adv_sync_ship_all_order_details', 'adv_sync_ship_all_order_details_callback');
add_action('wp_ajax_nopriv_adv_sync_ship_all_order_details', 'adv_sync_ship_all_order_details_callback');
function adv_sync_ship_all_order_details_callback()
{
error_reporting(0);
global $wpdb;
$orders = $_POST['order_ids'];
foreach($orders as $order_id){
$order = wc_get_order( $order_id );
if ( !$order->has_status('on-hold') && !$order->has_status('cancelled') ) {
$base_url = SHIP_STATION_BASE_URL;
$table_name = $wpdb->base_prefix . 'shipstation_api_access';
$q = $wpdb->get_results('SELECT * FROM '.$table_name.' LIMIT 1');
$access_token = $q[0]->access_token;
$headers = array(
'Content-Type' => 'application/json',
'Device-Type' => 'Web',
'Ver' => '1.0',
'Authorization' => 'Bearer '.$access_token
);
$data = advatix_ship_order_data( $order_id );
$api_url = $base_url.'/shipments/createShipmentLabel';
$args = array(
'headers' => $headers,
'timeout' => 300000,
'body' => wp_json_encode($data),
'method' => 'POST'
);
$res = wp_remote_request($api_url, $args );
if(!is_wp_error($res)){
// if(simplexml_load_string($res['body'])){
// $result_jd = json_decode(json_encode((array)simplexml_load_string($res['body'])));
// }else{
$result_jd = json_decode($res['body']);
// }
// echo "<pre>";
// print_r($result_jd);
// echo "</pre>";
$status = $result_jd->responseStatusCode;
if($result_jd->responseObject[0]->labelData != ''){
$msg = 'Label Already Created';
}else{
if(isset($result_jd->responseObject)){
$msg = json_encode($result_jd->responseObject);
}else{
$msg = $result_jd->message;
$status = $result_jd->status;
}
}
// echo $msg;
$table_name = $wpdb->base_prefix . 'shipstation_create_labels';
$wpdb->insert(
$table_name,
array(
'requestJson' => wp_json_encode($data),
'order_id' => $order_id,
'responseMessage' => $msg,
'responseStatus' => $result_jd->responseStatus,
'responseStatusCode' => $status,
'responseObject' => json_encode($result_jd->responseObject),
'labelData' => $result_jd->responseObject[0]->labelData,
'time' => current_time( 'mysql' ),
)
);
$array[] = array(
'orderId' => $order_id,
'responseCode' => $status,
'responseMessage' => $msg
);
//Save Tracking ID
$table_name = $wpdb->base_prefix . 'shipstation_shipment_tracking';
$q = $wpdb->get_results('SELECT * FROM '.$table_name.' WHERE order_id="'.$order_id.'" AND trackingNumber="'.$result_jd->responseObject[0]->trackingNumber.'"');
if ( empty( $q ) ) {
$table_name = $wpdb->base_prefix . 'shipstation_shipment_tracking';
$wpdb->insert(
$table_name,
array(
'order_id' => $order_id,
'orderItems' => '',
'trackingNumber' => $result_jd->responseObject[0]->trackingNumber,
'trackingUrl' => '',
'carrierName' => $result_jd->responseObject[0]->carrier,
'time' => current_time( 'mysql' ),
)
);
}
if(in_array('wc-shipped',array_keys(wc_get_order_statuses()))){
$order->update_status('shipped');
}
}else{
$table_name = $wpdb->base_prefix . 'shipstation_create_labels';
$wpdb->insert(
$table_name,
array(
'requestJson' => wp_json_encode($data),
'order_id' => $order_id,
'responseMessage' => $res->get_error_message(),
'responseStatus' => $res->get_error_code(),
'responseStatusCode' => $res->get_error_code(),
'responseObject' => "",
'labelData' => '',
'time' => current_time( 'mysql' ),
)
);
$array[] = array(
'orderId' => $order_id,
'responseCode' => $res->get_error_code(),
'responseMessage' => $res->get_error_message()
);
}
}else{
$array[] = array(
'orderId' => $order_id,
'responseCode' => '201',
'responseMessage' => 'Order is either on-hold or cancelled'
);
}
// echo wp_json_encode($res['body']);
}
echo wp_json_encode($array);
die();
}
/**
* Fetch Order Generate Label API response Ajax
*/
add_action('wp_ajax_adv_ship_get_api_details', 'adv_ship_get_api_details_callback');
add_action('wp_ajax_nopriv_adv_ship_get_api_details', 'adv_ship_get_api_details_callback');
function adv_ship_get_api_details_callback()
{
error_reporting(0);
global $wpdb;
$table_name = $wpdb->base_prefix . 'shipstation_create_labels';
$q = $wpdb->get_results('SELECT * FROM '.$table_name.' WHERE order_id="'.sanitize_text_field($_POST['order_id']).'" ORDER BY id DESC LIMIT 10');
if(!empty($q)){
foreach($q as $k=>$v){
echo "<tr>";
echo "<td>".date('M d, Y - H:iA', strtotime($v->time))."</td>";
echo "<td>".$v->responseStatusCode." (<a href='javascript:void(0)' class='copy-request' data-request='".$v->requestJson."'>Copy Request</a>)</td>";
if($v->responseMessage==''){
echo "<td>-</td>";
}else{
echo "<td>".$v->responseMessage."</td>";
}
if($v->labelData==''){
echo "<td>-</td>";
}else{
echo "<td><a href='".$v->labelData."' target='_blank'>Print Label</a></td>";
}
echo "</tr>";
}
}else{
echo "<tr>";
echo "<td colspan='4'>No data found.</td>";
echo "</tr>";
}
die();
}
/**
* Validate Shipper Code API
*/
add_action('wp_ajax_adv_ship_validate_shipper_details', 'adv_ship_validate_shipper_details_callback');
add_action('wp_ajax_nopriv_adv_ship_validate_shipper_details', 'adv_ship_validate_shipper_details_callback');
function adv_ship_validate_shipper_details_callback()
{
error_reporting(0);
global $wpdb;
$base_url = SHIP_STATION_BASE_URL;
$table_name = $wpdb->base_prefix . 'shipstation_api_access';
$q = $wpdb->get_results('SELECT * FROM '.$table_name.' LIMIT 1');
$access_token = $q[0]->access_token;
if(empty($access_token)){
$ship_api_user = $_POST['ship_api_user'];
$ship_api_password = $_POST['ship_api_password'];
if(!empty($ship_api_user) && !empty($ship_api_password)){
$api_url = SHIP_STATION_BASE_URL.'/auth/login';
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer 12345',
);
$data = array(
'user' => $ship_api_user,
'credentials' => $ship_api_password
);
$args = array(
'headers' => $headers,
'timeout' => 300000,
'body' => wp_json_encode($data)
);
$res = wp_remote_post($api_url, $args );
// if(simplexml_load_string($res['body'])){
// $result_jd = json_decode(json_encode((array)simplexml_load_string($res['body'])));
// }else{
$result_jd = json_decode($res['body']);
// }
$access_token = $result_jd->accessToken;
$table_name = $wpdb->base_prefix . 'shipstation_api_access';
$q = $wpdb->get_results('SELECT * FROM '.$table_name.' LIMIT 1');
if($result_jd){
if(empty($q)){
$wpdb->insert(
$table_name,
array(
'access_token' => $result_jd->accessToken,
'last_modified' => date('Y-m-d h:i:s'),
)
);
}else{
$wpdb->update(
$table_name,
array(
'access_token' => $result_jd->accessToken,
'last_modified' => date('Y-m-d h:i:s'),
),
array('id' => $q[0]->id)
);
}
}
}
}
$headers = array(
'Device-Type' => 'Web',
'Ver' => '1.0',
'Authorization' => 'Bearer '.$access_token
);
$api_url = $base_url.'/user/validateShipperLocation?warehouseId='.$_POST['ship_shipperCode'];
$args = array(
'headers' => $headers,
'timeout' => 300000,
'method' => 'GET'
);
$res = wp_remote_request($api_url, $args );
if(!is_wp_error($res)){
// if(simplexml_load_string($res['body'])){
// $result_jd = json_decode(json_encode((array)simplexml_load_string($res['body'])));
// }else{
// $result_jd = json_decode($res['body']);
// }
$result_jd = json_decode($res['body']);
// echo "<pre>";
// print_r($res);
// echo "</pre>";
// echo "<pre>";
// print_r($result_jd);
// echo "</pre>";
if($result_jd->responseStatusCode == '200'){
$array = array(
'status' => 'success',
'data' => $result_jd->responseObject
);
}else{
$array = array(
'status' => 'fail',
'data' => ''
);
}
}else{
$array = array(
'status' => 'fail',
'data' => ''
);
}
echo json_encode($array);
die();
}