'PICKED_UP', 'shipment.out_for_delivery' => 'OUT_FOR_DELIVERY', 'shipment.delivered' => 'DELIVERED', 'shipment.returned_to_shipper' => 'RETURNED_TO_SHIPPER', ); /** * Hooks into the REST API. * * @return void */ public static function register() { add_action( 'rest_api_init', array( __CLASS__, 'routes' ) ); } /** * Registers the route. * * @return void */ public static function routes() { register_rest_route( self::NAMESPACE_V1, self::ROUTE, array( array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'challenge' ), // Public by design: answers only the registration challenge, reveals nothing. 'permission_callback' => '__return_true', ), array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'receive' ), // Public by design: every request is authenticated by its HMAC signature in receive(). 'permission_callback' => '__return_true', ), ) ); } /** * Echoes the challenge token ABS sends when the webhook is registered. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public static function challenge( \WP_REST_Request $request ) { $token = (string) $request->get_param( 'challenge' ); if ( ! preg_match( '/^[A-Za-z0-9_-]{1,128}$/', $token ) ) { return new \WP_REST_Response( array( 'ok' => true ), 200 ); } return new \WP_REST_Response( array( 'challenge' => $token ), 200 ); } /** * Receives an event. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public static function receive( \WP_REST_Request $request ) { $body = (string) $request->get_body(); if ( strlen( $body ) > self::MAX_BODY ) { return self::response( 413, 'payload_too_large' ); } if ( ! self::signature_is_valid( $body, (string) $request->get_header( 'x-webhook-signature' ), WebhookRegistrar::saved()['secret'] ) ) { return self::response( 401, 'invalid_signature' ); } $event = json_decode( $body, true ); if ( ! is_array( $event ) || ! isset( $event['event'], $event['eventId'], $event['occurredOn'] ) || ! is_string( $event['event'] ) || ! is_scalar( $event['eventId'] ) || ! is_string( $event['occurredOn'] ) || false === strtotime( $event['occurredOn'] ) ) { // occurredOn is required: it is what stops a replayed old event from changing the status. return self::response( 400, 'invalid_payload' ); } update_option( self::LAST_OPTION, time(), false ); $data = isset( $event['data'] ) && is_array( $event['data'] ) ? $event['data'] : array(); $awb = isset( $data['awb'] ) && is_scalar( $data['awb'] ) ? sanitize_text_field( (string) $data['awb'] ) : ''; $status = isset( $data['status'] ) && is_string( $data['status'] ) ? strtoupper( $data['status'] ) : ''; if ( ! Statuses::is_known( $status ) ) { $status = isset( self::EVENT_STATUS[ $event['event'] ] ) ? self::EVENT_STATUS[ $event['event'] ] : ''; } if ( '' === $awb || '' === $status ) { // Valid but not actionable (e.g. an event type the plugin does not use). return self::response( 200, 'ignored' ); } $dedupe = 'abs_courier_evt_' . md5( (string) $event['eventId'] ); if ( get_transient( $dedupe ) ) { return self::response( 200, 'duplicate' ); } set_transient( $dedupe, 1, self::DEDUPE_TTL ); Queue::now( StatusSync::HOOK_APPLY, array( $awb, $status, $event['occurredOn'] ) ); return self::response( 200, 'accepted' ); } /** * Verifies `X-Webhook-Signature: sha256=` = HMAC-SHA256(secret, raw body). * * @param string $body Raw body. * @param string $header Signature header. * @param string $secret Signing secret ('' when not registered). * @return bool */ public static function signature_is_valid( $body, $header, $secret ) { if ( '' === $secret || 0 !== stripos( $header, 'sha256=' ) ) { return false; } return hash_equals( hash_hmac( 'sha256', $body, $secret ), strtolower( substr( $header, 7 ) ) ); } /** * JSON response. * * @param int $status HTTP status. * @param string $result Short result code. * @return \WP_REST_Response */ private static function response( $status, $result ) { return new \WP_REST_Response( array( 'result' => $result ), $status ); } }