Advertisement
Guest User

Untitled

a guest
Aug 31st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.23 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Send outbound request to Parcel Ninja
  5.  * The CURL response will be the tracking code which will be saved
  6.  * into a custom field on the order
  7.  */
  8. add_action( 'woocommerce_order_status_processing', 'PARCEL_NINJA_create_new_outbound_request', 10, 1 );
  9. function PARCEL_NINJA_create_new_outbound_request( $order_id ) {
  10.  
  11.     // get the order
  12.     $order = new WC_Order( $order_id );
  13.  
  14.     // get the order information
  15.     $this_order = $order->get_data();
  16.  
  17.     // get order items
  18.     foreach( $order->get_items() as $item_id => $item_product ){
  19.         $product = $item_product->get_product();
  20.         $sku     = $product->get_sku();
  21.  
  22.         // get the quantity
  23.         $item_quantity = $item_product->get_quantity();
  24.  
  25.         $items[] = [
  26.             'itemNo' => $sku,
  27.             'qty'    => $item_quantity
  28.         ];
  29.     }
  30.  
  31.     // build required JSON object
  32.     $outbound_order = [
  33.         'clientId'     => $this_order['id'],
  34.         'typeId'       => 2,
  35.         'deliveryInfo' => [
  36.             'customer'       => $this_order['shipping']['first_name'],
  37.             'companyName'    => $this_order['shipping']['company'],
  38.             'contactNo'      => $this_order['billing']['phone'],
  39.             'addressLine1'   => $this_order['shipping']['address_1'],
  40.             'addressLine2'   => $this_order['shipping']['address_2'],
  41.             'suburb'         => $this_order['shipping']['city'],
  42.             'postalCode'     => $this_order['shipping']['postcode'],
  43.             'deliveryOption' => [
  44.                 'deliveryQuoteId' => 0,
  45.             ],
  46.             'forCollection' => true,
  47.         ],
  48.         'items' => $items
  49.     ];
  50.  
  51.     // run the curl request
  52.     $username = '###';
  53.     $password = '###';
  54.  
  55.     $ch = curl_init();
  56.     curl_setopt( $ch, CURLOPT_URL, "https://storeapi.parcelninja.com/api/v1/outbounds" );
  57.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  58.     curl_setopt( $ch, CURLOPT_HEADER, TRUE );
  59.     curl_setopt( $ch, CURLOPT_POST, TRUE );
  60.     curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $outbound_order ) );
  61.     curl_setopt( $ch, CURLOPT_HTTPHEADER, [
  62.         "Content-Type: application/json",
  63.         "Accept: application/json",
  64.         "Authorization: Basic " . base64_encode( $username . ':' . $password )
  65.     ] );
  66.     $response = curl_exec( $ch );
  67.     curl_close( $ch );
  68.  
  69.     // handle headers to get parcel ninja reply
  70.     $headers = [];
  71.     $data = explode( "\n", $response );
  72.     foreach( $data as $part ) {
  73.         $middle = explode( ":", $part );
  74.         $headers[ trim( $middle[0] ) ] = trim( $middle[1] );
  75.     }
  76.  
  77.     // save response to custom field
  78.     $field_update = update_field( 'field_5b42f473f809b', $headers['x-parcelninja-outbound-id'], $order_id );
  79.  
  80. }
  81.  
  82. /**
  83.  * Parcel Ninja outbound codes
  84.  */
  85. function PARCEL_NINJA_return_status_code_description( $id ) {
  86.     $PARCEL_NINJA_outbound_codes = [
  87.         240 => 'Awaiting stock',
  88.         290 => 'In picking queue',
  89.         241 => 'Ready to be picked',
  90.         291 => 'In packing queue',
  91.         242 => 'Ready to be packed',
  92.         243 => 'Awaiting courier pickup',
  93.         244 => 'Dispatched with courier',
  94.         300 => 'Scheduled for delivery with courier',
  95.         245 => 'Delivered',
  96.         246 => 'Awaiting collection',
  97.         247 => 'Collected',
  98.         280 => 'Unable to deliver',
  99.         282 => 'Order on hold',
  100.         390 => 'Unsuccessful'
  101.     ];
  102.  
  103.     return $PARCEL_NINJA_outbound_codes[$id];
  104. }
  105.  
  106.  
  107. /**
  108.  * Hook for Parcel Ninja to update delivery details
  109.  */
  110. function PARCEL_NINJA_WEBHOOK() {
  111.     $payload = file_get_contents( 'php://input' ); // receive the webhook payload
  112.     $response = json_decode( $payload, true ); // create json response
  113.  
  114.     // $response = json_decode( get_field('pn_payload_test', 'option'), true ); // <-- for testing
  115.  
  116.     // echo $response['clientId'];
  117.     // echo $response['events'][0]['description'];
  118.     // echo $response['events'][0]['code'];
  119.  
  120.     // update the order
  121.     $field_update = update_field( 'field_5b4360aba3cac', $payload, $response['clientId'] );
  122. }
  123. add_action( 'wp_ajax_PARCEL_NINJA_WEBHOOK', 'PARCEL_NINJA_WEBHOOK' );
  124. add_action( 'wp_ajax_nopriv_PARCEL_NINJA_WEBHOOK', 'PARCEL_NINJA_WEBHOOK' );
  125.  
  126.  
  127.  
  128.  
  129. /** Retrieve the current webhook URL */
  130. function getWebHookURL() {
  131.     $username = '###';
  132.     $password = '###';
  133.  
  134.     $ch = curl_init();
  135.     curl_setopt($ch, CURLOPT_URL, "https://storeapi.parcelninja.com/api/v1/hooks/getCallback");
  136.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  137.     curl_setopt($ch, CURLOPT_HEADER, TRUE);
  138.  
  139.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  140.         "Content-Type: application/json",
  141.         "Accept: application/json",
  142.         "Authorization: Basic " . base64_encode( $username . ':' . $password )
  143.     ));
  144.  
  145.     $response = curl_exec($ch);
  146.     curl_close($ch);
  147.  
  148.     echo '<pre>';print_r($response);echo '</pre>';
  149. }
  150. // getWebHookURL();
  151.  
  152.  
  153. /** Set/Update the webhook URL */
  154. function setWebHookURL() {
  155.     $username = '###';
  156.     $password = '###';
  157.  
  158.     $ch = curl_init();
  159.  
  160.     curl_setopt($ch, CURLOPT_URL, "https://storeapi.parcelninja.com/api/v1/hooks/postCallback");
  161.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  162.     curl_setopt($ch, CURLOPT_HEADER, TRUE);
  163.  
  164.     curl_setopt($ch, CURLOPT_POST, TRUE);
  165.  
  166.     curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  167.         \"hookUrl\": \"http://vidaecaffe.wpengine.com/wp-admin/admin-ajax.php?action=PARCEL_NINJA_WEBHOOK\"
  168.     }");
  169.  
  170.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  171.         "Content-Type: application/json",
  172.         "Accept: application/json",
  173.         "Authorization: Basic " . base64_encode( $username . ':' . $password )
  174.     ));
  175.  
  176.     $response = curl_exec($ch);
  177.     curl_close($ch);
  178.  
  179.     // echo '<pre>';print_r($response);echo '</pre>';
  180. }
  181. // setWebHookURL();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement