[A-Za-z0-9]{6,32})'; $this->route( '/portal/sign-in-link', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_link' ), 'permission' => $public, 'args' => array( 'email' => array( 'type' => 'string', 'required' => true, ), 'redirect' => array( 'type' => 'string' ), ), ), ) ); $this->route( '/portal/sign-in', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'sign_in' ), 'permission' => $public, ), ) ); $this->route( '/portal/sign-out', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'sign_out' ), 'permission' => $public, ), ) ); $this->route( '/portal/bookings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'list' ), 'permission' => $public, ), ) ); $this->route( $uid, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'show' ), 'permission' => $public, ), ) ); foreach ( array( 'cancel', 'reschedule', 'pay', 'return' ) as $action ) { $this->route( $uid . '/' . $action, array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, $action . '_booking' ), 'permission' => $public, ), ) ); } } /** * Email a sign-in link. Always says it was sent. * * @param WP_REST_Request $request Request. * @return array */ public function send_link( WP_REST_Request $request ): array { self::throttle( 'sign-in-link', 5, 10 * MINUTE_IN_SECONDS ); Services::portal()->request_sign_in( (string) $request['email'], (string) ( $request['redirect'] ?? '' ) ); return array( 'sent' => true ); } /** * Sign in. * * @param WP_REST_Request $request Request. * @return array */ public function sign_in( WP_REST_Request $request ): array { self::throttle( 'sign-in', 10, 15 * MINUTE_IN_SECONDS ); $body = self::body( $request ); Services::portal()->sign_in( (string) ( $body['token'] ?? '' ), (string) ( $body['email'] ?? '' ), (string) ( $body['code'] ?? '' ) ); return array( 'bookings' => Services::portal()->bookings() ); } /** * Sign out. * * @return array */ public function sign_out(): array { Services::portal()->sign_out(); return array( 'signed_out' => true ); } /** * The customer's bookings. * * @return array */ public function list(): array { return array( 'bookings' => Services::portal()->bookings() ); } /** * One booking. * * @param WP_REST_Request $request Request. * @return array */ public function show( WP_REST_Request $request ): array { self::throttle( 'show', 60 ); return Services::portal()->present( $this->booking( $request ) ); } /** * Cancel a booking as the customer. * * @param WP_REST_Request $request Request. * @return array */ public function cancel_booking( WP_REST_Request $request ): array { $booking = $this->booking( $request ); $reason = sanitize_textarea_field( (string) ( self::body( $request )['reason'] ?? '' ) ); $booking = Services::checkout()->cancel( $booking['id'], 'customer', null, true, mb_substr( $reason, 0, 500 ) ); return Services::portal()->present( $booking ) + array( 'refund_note' => $booking['refund_note'] ); } /** * Move a booking as the customer. * * @param WP_REST_Request $request Request. * @return array */ public function reschedule_booking( WP_REST_Request $request ): array { $booking = $this->booking( $request ); $change = self::body( $request ); unset( $change['notify'] ); return Services::portal()->present( Services::checkout()->reschedule( $booking['id'], $change, 'customer' ) ); } /** * Start paying online. * * @param WP_REST_Request $request Request. * @return array */ public function pay_booking( WP_REST_Request $request ): array { $booking = $this->booking( $request ); return array( 'url' => Services::payment_flow()->start( $booking, (string) ( self::body( $request )['method'] ?? '' ) ) ); } /** * Check a payment after the customer returns from the provider. * * @param WP_REST_Request $request Request. * @return array */ public function return_booking( WP_REST_Request $request ): array { self::throttle( 'return', 20 ); $booking = $this->booking( $request ); $params = array_map( 'strval', array_filter( self::body( $request ), 'is_scalar' ) ); return Services::portal()->present( Services::payment_flow()->returned( $booking, $params ) ); } /** * The booking in the path, if the visitor may see it. * * @param WP_REST_Request $request Request. * @return array * @throws BookingException When it is not theirs. */ private function booking( WP_REST_Request $request ): array { $key = (string) ( $request['key'] ?? ( self::body( $request )['key'] ?? '' ) ); return Services::portal()->booking( (string) $request['uid'], $key ); } }