173 lines
6.0 KiB
PHP
173 lines
6.0 KiB
PHP
<?php
|
|
|
|
class Wiaas_REST_Customer_API {
|
|
/**
|
|
* Endpoint namespace.
|
|
*
|
|
* @var string
|
|
*/
|
|
private static $namespace = 'wiaas';
|
|
|
|
public function __construct() {
|
|
include_once dirname( __FILE__ ) . '/../user/class-wiaas-customer.php';
|
|
include_once dirname( __FILE__ ) . '/helper/class-rest-helper-functions.php';
|
|
}
|
|
|
|
|
|
public static function register_routes() {
|
|
|
|
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/profile-addresses', array(
|
|
'methods' => 'PUT',
|
|
'callback' => array(__CLASS__, 'update_customer_profile_addresses'),
|
|
'permission_callback' => 'is_user_logged_in'
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/profile-addresses/(?P<address_id>\d+)', array(
|
|
'methods' => 'DELETE',
|
|
'callback' => array(__CLASS__, 'delete_customer_profile_address'),
|
|
'permission_callback' => 'is_user_logged_in'
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/billing-addresses', array(
|
|
'methods' => 'PUT',
|
|
'callback' => array(__CLASS__, 'update_customer_billing_addresses'),
|
|
'permission_callback' => 'is_user_logged_in'
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/billing-addresses/(?P<address_id>\d+)', array(
|
|
'methods' => 'DELETE',
|
|
'callback' => array(__CLASS__, 'delete_customer_billing_addresses'),
|
|
'permission_callback' => 'is_user_logged_in'
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/personal-info', array(
|
|
'methods' => 'PUT',
|
|
'callback' => array(__CLASS__, 'update_customer_personal_info'),
|
|
'permission_callback' => 'is_user_logged_in'
|
|
) );
|
|
|
|
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/company-info', array(
|
|
'methods' => 'PUT',
|
|
'callback' => array(__CLASS__, 'update_customer_company_info'),
|
|
'permission_callback' => 'is_user_logged_in'
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function update_customer_profile_addresses(WP_REST_Request $request){
|
|
$customer_id = $request['id'];
|
|
$params = $request->get_body_params();
|
|
$new_address = json_decode($params['profile_address']);
|
|
|
|
if (!Wiaas_Customer::update_customer_profile_addresses($customer_id, $new_address)){
|
|
return self::generate_wiaas_response('PROFILE_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
return self::generate_wiaas_response('PROFILE_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
public static function delete_customer_profile_address(WP_REST_Request $request){
|
|
$customer_id = $request['id'];
|
|
$address_id = $request['address_id'];
|
|
|
|
if (!Wiaas_Customer::delete_customer_profile_address($customer_id, $address_id)){
|
|
return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
public static function update_customer_billing_addresses(WP_REST_Request $request){
|
|
$customer_id = $request['id'];
|
|
$params = $request->get_body_params();
|
|
$new_address = json_decode($params['billing_address']);
|
|
|
|
if (!Wiaas_Customer::update_customer_billing_addresses($customer_id, $new_address)){
|
|
return self::generate_wiaas_response('BILLING_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
return self::generate_wiaas_response('BILLING_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
|
|
|
}
|
|
|
|
public static function delete_customer_billing_addresses(WP_REST_Request $request){
|
|
$customer_id = $request['id'];
|
|
$address_id = $request['address_id'];
|
|
|
|
if (!Wiaas_Customer::delete_customer_billing_address($customer_id, $address_id)){
|
|
return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
public static function update_customer_personal_info(WP_REST_Request $request){
|
|
$customer_id = $request['id'];
|
|
$params = $request->get_body_params();
|
|
|
|
$first_name = $params['first_name'];
|
|
$last_name = $params['last_name'];
|
|
$phone = $params['phone'];
|
|
$name = $first_name . ' ' . $last_name;
|
|
|
|
if (!is_string($name) || strlen($name) === 1){
|
|
return self::generate_wiaas_response('ADD_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
if (!is_string($phone) || strlen($phone) < 1){
|
|
return self::generate_wiaas_response('ADD_PHONE_NUMBER', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
if (!Wiaas_Customer::update_customer_profile_info($customer_id, $first_name, $last_name, $phone)){
|
|
return self::generate_wiaas_response('PROFILE_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
return self::generate_wiaas_response('PROFILE_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
public static function update_customer_company_info(WP_REST_Request $request){
|
|
$customer_id = $request['id'];
|
|
$params = $request->get_body_params();
|
|
|
|
$company_name = $params['company_name'];
|
|
$vat_code = $params['vat_code'];
|
|
|
|
|
|
if (!is_string($company_name) || strlen($company_name) < 1){
|
|
return self::generate_wiaas_response('ADD_COMPANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
if (!is_string($vat_code) || strlen($vat_code) < 1){
|
|
return self::generate_wiaas_response('ADD_VAT', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
if (!Wiaas_Customer::update_customer_company_info($customer_id, $company_name, $vat_code)){
|
|
return self::generate_wiaas_response('COMPANY_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
|
}
|
|
|
|
return self::generate_wiaas_response('COMPANY_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function generate_wiaas_response($message, $code, $data = NULL){
|
|
$response = array(
|
|
'messages' => [
|
|
array(
|
|
'code' => $code,
|
|
'message' => $message
|
|
)
|
|
],
|
|
'data' => $data
|
|
);
|
|
|
|
return new WP_REST_Response($response);
|
|
}
|
|
|
|
|
|
} |