PluginProbe
PostNL for WooCommerce / 5.9.12
PostNL for WooCommerce v5.9.12
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / src / Rest_API / Legacy / Checkout / Client.php

Client.php in PostNL for WooCommerce 5.9.12, at src/Rest_API/Legacy/Checkout/Client.php

168 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Rest_API\Checkout\Client file.
4 *
5 * @package PostNLWooCommerce\Rest_API\Legacy\Checkout
6 */
7
8 namespace PostNLWooCommerce\Rest_API\Legacy\Checkout;
9
10 use PostNLWooCommerce\Helper\Mapping;
11 use PostNLWooCommerce\Rest_API\Base;
12 use PostNLWooCommerce\Rest_API\Contracts\Timeframe_Service_Interface;
13 use PostNLWooCommerce\Rest_API\Contracts\Pickup_Location_Service_Interface;
14 use PostNLWooCommerce\Utils;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Class Client
22 *
23 * @package PostNLWooCommerce\Rest_API\Legacy\Checkout
24 */
25 class Client extends Base implements Timeframe_Service_Interface, Pickup_Location_Service_Interface {
26 /**
27 * API Endpoint.
28 *
29 * @var string
30 */
31 public $endpoint = '/shipment/v1/checkout';
32
33 /**
34 * Function for composing API request.
35 */
36 public function compose_body_request() {
37 return array(
38 'OrderDate' => $this->item_info->body['order_date'],
39 'ShippingDuration' => $this->item_info->body['shipping_duration'],
40 'CutOffTimes' => $this->get_cutoff_times(),
41 'HolidaySorting' => true,
42 'Options' => $this->get_checkout_options(),
43 /* Temporarily hardcoded in Settings::get_number_pickup_points(). */
44 'Locations' => $this->item_info->body['locations'],
45 'Days' => $this->item_info->body['days'],
46 'Addresses' => array(
47 array(
48 'AddressType' => '01',
49 'Street' => $this->item_info->receiver['address_1'],
50 'HouseNr' => $this->item_info->receiver['address_2'],
51 'HouseNrExt' => '',
52 'Zipcode' => $this->item_info->receiver['postcode'],
53 'City' => $this->item_info->receiver['city'],
54 'CountryCode' => $this->item_info->receiver['country'],
55 ),
56 array(
57 'AddressType' => '02',
58 'Street' => $this->item_info->shipper['address_1'],
59 'HouseNr' => $this->item_info->shipper['address_2'],
60 'Zipcode' => $this->item_info->shipper['postcode'],
61 'City' => $this->item_info->shipper['city'],
62 'CountryCode' => $this->item_info->shipper['country'],
63 ),
64 ),
65 );
66 }
67
68 /**
69 * Get cutoff times value from the settings.
70 *
71 * @return array
72 */
73 public function get_cutoff_times() {
74 $cutoff_time = $this->item_info->body['cut_off_time'];
75 $excl_dropoff_days = $this->item_info->body['excluded_dropoff_days'];
76 $cutoff = array(
77 array(
78 'Day' => '00',
79 'Available' => true,
80 'Type' => 'Regular',
81 'Time' => $cutoff_time,
82 ),
83 );
84
85 if ( empty( $excl_dropoff_days ) ) {
86 return $cutoff;
87 }
88
89 foreach ( $excl_dropoff_days as $day ) {
90 $cutoff[] = array(
91 'Day' => $day,
92 'Available' => false,
93 );
94 }
95
96 return $cutoff;
97 }
98
99 /**
100 * Get options value from the settings.
101 *
102 * @return array
103 */
104 public function get_checkout_options() {
105 $options = array();
106
107 // Required options.
108 if ( $this->item_info->body['delivery_days_enabled'] ) {
109 $options[] = 'Daytime';
110 }
111
112 if ( $this->item_info->body['pickup_points_enabled'] ) {
113 $options[] = 'Pickup';
114 }
115
116 // Optional options - Options available for specific countries only.
117 $checkout_options = Mapping::available_country_for_checkout_feature();
118
119 if ( ! isset( $checkout_options[ $this->item_info->shipper['country'] ][ $this->item_info->receiver['country'] ] ) ) {
120 return $options;
121 }
122
123 $available_checkout_options = $checkout_options[ $this->item_info->shipper['country'] ][ $this->item_info->receiver['country'] ];
124
125 if ( in_array( '08:00-12:00', $available_checkout_options ) && $this->item_info->body['morning_delivery_enabled'] ) {
126 $options[] = '08:00-12:00';
127 }
128
129 if ( in_array( 'evening_delivery', $available_checkout_options ) && $this->item_info->body['evening_delivery_enabled'] ) {
130 $options[] = 'Evening';
131 }
132
133 return $options;
134 }
135
136 /**
137 * Get delivery options for the given post data.
138 *
139 * @param array $post_data Post data for delivery options request.
140 *
141 * @return array
142 */
143 public function get_delivery_options( array $post_data ): array {
144 $item_info = new Item_Info( $post_data );
145 $client = new self( $item_info );
146
147 // send_request() can return null on an empty/non-JSON 200; normalize to honor the array return type.
148 $response = $client->send_request();
149 return is_array( $response ) ? $response : array();
150 }
151
152 /**
153 * Get pickup locations for the given post data.
154 *
155 * @param array $post_data Post data for pickup locations request.
156 *
157 * @return array
158 */
159 public function get_pickup_locations( array $post_data ): array {
160 $item_info = new Item_Info( $post_data );
161 $client = new self( $item_info );
162
163 // send_request() can return null on an empty/non-JSON 200; normalize to honor the array return type.
164 $response = $client->send_request();
165 return is_array( $response ) ? $response : array();
166 }
167 }
168