Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Send outbound request to Parcel Ninja
- * The CURL response will be the tracking code which will be saved
- * into a custom field on the order
- */
- add_action( 'woocommerce_order_status_processing', 'PARCEL_NINJA_create_new_outbound_request', 10, 1 );
- function PARCEL_NINJA_create_new_outbound_request( $order_id ) {
- // get the order
- $order = new WC_Order( $order_id );
- // get the order information
- $this_order = $order->get_data();
- // get order items
- foreach( $order->get_items() as $item_id => $item_product ){
- $product = $item_product->get_product();
- $sku = $product->get_sku();
- // get the quantity
- $item_quantity = $item_product->get_quantity();
- $items[] = [
- 'itemNo' => $sku,
- 'qty' => $item_quantity
- ];
- }
- // build required JSON object
- $outbound_order = [
- 'clientId' => $this_order['id'],
- 'typeId' => 2,
- 'deliveryInfo' => [
- 'customer' => $this_order['shipping']['first_name'],
- 'companyName' => $this_order['shipping']['company'],
- 'contactNo' => $this_order['billing']['phone'],
- 'addressLine1' => $this_order['shipping']['address_1'],
- 'addressLine2' => $this_order['shipping']['address_2'],
- 'suburb' => $this_order['shipping']['city'],
- 'postalCode' => $this_order['shipping']['postcode'],
- 'deliveryOption' => [
- 'deliveryQuoteId' => 0,
- ],
- 'forCollection' => true,
- ],
- 'items' => $items
- ];
- // run the curl request
- $username = '###';
- $password = '###';
- $ch = curl_init();
- curl_setopt( $ch, CURLOPT_URL, "https://storeapi.parcelninja.com/api/v1/outbounds" );
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
- curl_setopt( $ch, CURLOPT_HEADER, TRUE );
- curl_setopt( $ch, CURLOPT_POST, TRUE );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $outbound_order ) );
- curl_setopt( $ch, CURLOPT_HTTPHEADER, [
- "Content-Type: application/json",
- "Accept: application/json",
- "Authorization: Basic " . base64_encode( $username . ':' . $password )
- ] );
- $response = curl_exec( $ch );
- curl_close( $ch );
- // handle headers to get parcel ninja reply
- $headers = [];
- $data = explode( "\n", $response );
- foreach( $data as $part ) {
- $middle = explode( ":", $part );
- $headers[ trim( $middle[0] ) ] = trim( $middle[1] );
- }
- // save response to custom field
- $field_update = update_field( 'field_5b42f473f809b', $headers['x-parcelninja-outbound-id'], $order_id );
- }
- /**
- * Parcel Ninja outbound codes
- */
- function PARCEL_NINJA_return_status_code_description( $id ) {
- $PARCEL_NINJA_outbound_codes = [
- 240 => 'Awaiting stock',
- 290 => 'In picking queue',
- 241 => 'Ready to be picked',
- 291 => 'In packing queue',
- 242 => 'Ready to be packed',
- 243 => 'Awaiting courier pickup',
- 244 => 'Dispatched with courier',
- 300 => 'Scheduled for delivery with courier',
- 245 => 'Delivered',
- 246 => 'Awaiting collection',
- 247 => 'Collected',
- 280 => 'Unable to deliver',
- 282 => 'Order on hold',
- 390 => 'Unsuccessful'
- ];
- return $PARCEL_NINJA_outbound_codes[$id];
- }
- /**
- * Hook for Parcel Ninja to update delivery details
- */
- function PARCEL_NINJA_WEBHOOK() {
- $payload = file_get_contents( 'php://input' ); // receive the webhook payload
- $response = json_decode( $payload, true ); // create json response
- // $response = json_decode( get_field('pn_payload_test', 'option'), true ); // <-- for testing
- // echo $response['clientId'];
- // echo $response['events'][0]['description'];
- // echo $response['events'][0]['code'];
- // update the order
- $field_update = update_field( 'field_5b4360aba3cac', $payload, $response['clientId'] );
- }
- add_action( 'wp_ajax_PARCEL_NINJA_WEBHOOK', 'PARCEL_NINJA_WEBHOOK' );
- add_action( 'wp_ajax_nopriv_PARCEL_NINJA_WEBHOOK', 'PARCEL_NINJA_WEBHOOK' );
- /** Retrieve the current webhook URL */
- function getWebHookURL() {
- $username = '###';
- $password = '###';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, "https://storeapi.parcelninja.com/api/v1/hooks/getCallback");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_HEADER, TRUE);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- "Content-Type: application/json",
- "Accept: application/json",
- "Authorization: Basic " . base64_encode( $username . ':' . $password )
- ));
- $response = curl_exec($ch);
- curl_close($ch);
- echo '<pre>';print_r($response);echo '</pre>';
- }
- // getWebHookURL();
- /** Set/Update the webhook URL */
- function setWebHookURL() {
- $username = '###';
- $password = '###';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, "https://storeapi.parcelninja.com/api/v1/hooks/postCallback");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_HEADER, TRUE);
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, "{
- \"hookUrl\": \"http://vidaecaffe.wpengine.com/wp-admin/admin-ajax.php?action=PARCEL_NINJA_WEBHOOK\"
- }");
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- "Content-Type: application/json",
- "Accept: application/json",
- "Authorization: Basic " . base64_encode( $username . ':' . $password )
- ));
- $response = curl_exec($ch);
- curl_close($ch);
- // echo '<pre>';print_r($response);echo '</pre>';
- }
- // setWebHookURL();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement