Handle order project and refactor api

This commit is contained in:
Almira Krdzic
2018-09-24 21:51:55 +02:00
parent 8cc2a7c8bc
commit 11c26aeee1
32 changed files with 1408 additions and 587 deletions

View File

@@ -6,348 +6,224 @@ class Wiaas_Cart_API {
*
* @var string
*/
private static $namespace = 'wiaas/cart';
private static $namespace = 'wiaas';
private static $rest_base = 'cart';
public static function register_routes() {
register_rest_route( self::$namespace, 'count', array(
'methods' => 'GET',
register_rest_route( self::$namespace, '/' . self::$rest_base . '/count', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array(__CLASS__, 'get_cart_count'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
'permission_callback' => 'is_user_logged_in',
) );
register_rest_route( self::$namespace, 'items', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_cart_items'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
register_rest_route( self::$namespace, '/' . self::$rest_base . '/items', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array(__CLASS__, 'get_cart_items'),
'permission_callback' => 'is_user_logged_in',
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array(__CLASS__, 'add_package_to_cart'),
'permission_callback' => 'is_user_logged_in',
'args' => array(
'package_id' => array(
'description' => __( 'Wiaas package ID.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
'price_id' => array(
'description' => __( 'Selected price ID for Wiaas package.', 'wiaas' ),
'type' => 'string',
'enum' => array_keys(Wiaas_Package_Pricing::get_available_pay_types()),
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
),
'options_ids' => array(
'description' => __( 'Wiaas package options IDs.', 'wiaas' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
)
),
'addons_ids' => array(
'description' => __( 'Wiaas package addons IDs.', 'wiaas' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
)
)
)
),
) );
register_rest_route( self::$namespace, 'documents', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_cart_documents'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
register_rest_route( self::$namespace, '/' . self::$rest_base . '/items/(?P<key>[\w-]+)', array(
'args' => array(
'key' => array(
'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array(__CLASS__, 'remove_package_from_cart'),
'permission_callback' => 'is_user_logged_in',
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array(__CLASS__, 'update_package_quantity'),
'permission_callback' => 'is_user_logged_in',
'args' => array(
'quantity' => array(
'description' => __( 'New quantity cart package item.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
)
)
)
) );
register_rest_route( self::$namespace, 'add', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'add_package_to_cart'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
register_rest_route( self::$namespace, '/' . self::$rest_base . '/checkout', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array(__CLASS__, 'checkout'),
'permission_callback' => 'is_user_logged_in',
'args' => array(
'vat' => array(
'description' => __( 'Vat Code for new order.', 'wiaas' ),
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_key',
),
'company' => array(
'description' => __( 'Company name for new order.', 'wiaas' ),
'type' => 'string',
'required' => true,
'sanitize_callback' => 'sanitize_key',
),
'reference' => array(
'description' => __( 'Location details for new order.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
),
'tender' => array(
'description' => __( 'Invoice reference for new order.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
),
'project_id' => array(
'description' => __( 'Order project ID for new order.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
),
'delivery_address_id' => array(
'description' => __( 'ID of delivery address for new order.', 'wiaas' ),
'type' => 'integer',
'required' => true,
'sanitize_callback' => 'absint',
),
'billing_address_id' => array(
'description' => __( 'ID of billing address for new order.', 'wiaas' ),
'type' => 'integer',
'required' => true,
'sanitize_callback' => 'absint',
),
)
) );
register_rest_route( self::$namespace, 'remove', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'remove_package_from_cart'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'update-quantity', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'update_package_quantity'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'place-order', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'place_order'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
}
public static function permission_check() {
if (!is_user_logged_in()) {
return new WP_Error( 'wiaas_rest_authorization_required',
__( 'Sorry, only authorized users can access!', 'wiaas' ),
array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get cart count
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
*/
public static function get_cart_count() {
$items = WC()->cart->get_cart_contents();
$count = 0;
foreach ($items as $key => $item) {
if (isset($item['_wiaas_standard_package'])) {
$count++;
}
}
return new WP_REST_Response(array(
'count' => $count,
return rest_ensure_response(array(
'count' => Wiaas_Cart::get_cart_packages_count(),
));
}
/**
* Get cart items
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
*
* @return WP_REST_Response
*/
public static function get_cart_items() {
$items = WC()->cart->get_cart_contents();
$result = array();
foreach ($items as $key => $item) {
if (!isset($item['_wiaas_standard_package'])) {
continue;
}
$package = wc_get_product($item['product_id']);
// Retrieve package addons
$addon_cart_items = wiaas_get_cart_item_addons($item);
$additional_packages = array();
foreach ($addon_cart_items as $addon_cart_item) {
$additional_package = wc_get_product($addon_cart_item['product_id']);
$additional_packages[] = array(
'idAdditionalPackage' => $additional_package->get_id(),
'packageName' => $additional_package->get_title(),
'prices' => array(
'fixedExtra' => $addon_cart_item['_wiaas_payment']['fixed_extra'],
'recurrentExtra' => $addon_cart_item['_wiaas_payment']['recurrent_extra'],
'servicesExtra' => $addon_cart_item['_wiaas_payment']['services_extra'],
)
);
}
// Retrieve package options
$option_cart_items = wiaas_get_cart_item_options($item);
$package_options = array();
foreach ($option_cart_items as $option_cart_item) {
$option_package = wc_get_product($option_cart_item['product_id']);
$package_options[] = array(
'idOptionPackage' => $option_package->get_id(),
'groupName' => $option_cart_item['_wiaas_option_group_name'],
'packageName' => $option_package->get_title(),
'prices' => array(
'fixedExtra' => $option_cart_item['_wiaas_payment']['fixed_extra'],
'recurrentExtra' => $option_cart_item['_wiaas_payment']['recurrent_extra'],
'servicesExtra' => $option_cart_item['_wiaas_payment']['services_extra'],
)
);
}
$totalPrices = Wiaas_Cart::get_cart_item_total($item);
$country = Wiaas_Countries::get_package_country($package);
return rest_ensure_response(array(
'items' => Wiaas_Cart::get_cart_packages(),
));
}
$result[] = array(
'idPackage' => $item['product_id'],
'key' => $item['key'],
'packageName' => $package->get_title(),
'additionalPackages' => $additional_packages,
'areAdditionalAvailable' => true,
'areOptionsAvailable' => true,
'bids' => array(),
'commercialLead' => 'Coor Service Management',
'country' => array(
'currency' => $country['currency']
),
'options' => $package_options,
'quantity' => $item['quantity'],
/**
* @param WP_REST_Request $request Request data.
*
* @return WP_REST_Response
*/
public static function add_package_to_cart($request) {
'idPayType' => $item['_wiaas_payment']['id'],
'payType' => $item['_wiaas_payment']['payment_type'],
'periodUnit' => $item['_wiaas_payment']['period_unit'],
'idPrice' => $item['_wiaas_payment']['id'],
'fixedPrice' => $item['_wiaas_payment']['fixed_extra'],
'recurrentPrice' => $item['_wiaas_payment']['recurrent_extra'],
'servicesPrice' => $item['_wiaas_payment']['services_extra'],
'totalPrices' => array(
'fixedPrice' => $totalPrices['fixed_extra'],
'recurrentPrice' => $totalPrices['recurrent_extra'],
'servicesPrice' => $totalPrices['services_extra'],
),
'status' => 'available',
'idCommercialLead' => 14,
);
}
return new WP_REST_Response(
array(
'cartItems' => $result,
'items' => $items,
'totalPrice' => ''
)
$success = Wiaas_Cart::add_package_to_cart(
$request['package_id'],
$request['price_id'],
$request['addons_ids'],
$request['options_ids']
);
if ($success) {
return wiaas_api_notice('PACKAGE_ADDED', 'success');
}
return wiaas_api_notice('PACKAGE_ALREADY_IN_CART', 'error');
}
/**
* Get cart documents
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @param WP_REST_Request $request Request data.
*
* @return WP_REST_Response
*/
public static function get_cart_documents() {
return new WP_REST_Response(array(
'areFilesUploaded' => true,
'templates' => array(),
'uploaded' => array()
));
}
public static function remove_package_from_cart($request) {
/**
* Add package to cart
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
* @throws Exception
*/
public static function add_package_to_cart() {
$package_id = $_POST['package_id'];
$success = WC()->cart->add_to_cart($package_id, 1, 0, array(), array(
'_wiaas_standard_package' => true
));
$success = Wiaas_Cart::remove_package_from_cart($request['key']);
if ($success) {
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'PACKAGE_ADDED'
)
)
));
return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', 'success');
}
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'error',
'message' => 'PACKAGE_ALREADY_IN_CART'
)
)
));
}
/*
* Remove package from cart
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
*/
public static function remove_package_from_cart() {
$package_cart_key = $_POST['package_item_key'];
$success = WC()->cart->remove_cart_item($package_cart_key);
if ($success) {
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'PACKAGE_REMOVED_FROM_CART'
)
)
));
}
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'error',
'message' => 'INVALID_PACKAGE_FOR_REMOVE'
)
)
));
return wiaas_api_notice('INVALID_PACKAGE_FOR_REMOVE', 'error');
}
/**
* Update package quantity in cart
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
*/
public static function update_package_quantity() {
$package_item_key = $_POST['package_item_key'];
$new_quantity = $_POST['quantity'];
public static function update_package_quantity($request) {
$success = WC()->cart->set_quantity($package_item_key, $new_quantity, true);
$success = Wiaas_Cart::update_package_quantity($request['key'], $request['quantity']);
if ($success) {
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'QUANTITY_UPDATED'
)
)
));
return wiaas_api_notice('QUANTITY_UPDATED', 'success');
}
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'error',
'message' => 'QUANTITY_NOT_UPDATED'
)
)
));
return wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error');
}
/**
* Placing order as part of checkout process
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @param WP_REST_Request $request Request data.
*
* @return WP_REST_Response
* @throws Exception
*/
public static function place_order() {
public static function checkout($request) {
$details = $_POST['details'];
$vat_code = $_POST['vat'];
$company_name = $_POST['companyName'];
$delivery_address = $_POST['delivery'];
$billing_address = $_POST['billing'];
Wiaas_Checkout::process_checkout($request->get_body_params());
$order_id = WC()->checkout()->create_order(array());
$order = wc_get_order( $order_id );
// set order currency
$line_items = $order->get_items();
foreach ($line_items as $line_item) {
if (isset($line_item['wiaas_currency'])) {
$order->set_currency($line_item['wiaas_currency']);
break;
}
}
$order->set_shipping_city($delivery_address['city']);
$order->set_shipping_country($delivery_address['countryName']);
$order->set_shipping_address_1($delivery_address['detailedAddress']);
$order->set_shipping_postcode($delivery_address['zipCode']);
$order->set_billing_city($billing_address['city']);
$order->set_billing_country($billing_address['countryName']);
$order->set_billing_address_1($billing_address['detailedAddress']);
$order->set_billing_postcode($billing_address['zipCode']);
$order->set_billing_first_name($billing_address['firstName']);
$order->set_billing_last_name($billing_address['lastName']);
$order->payment_complete();
add_post_meta($order_id, '_wiaas_vat_code', $vat_code);
add_post_meta($order_id, '_wiaas_company_name', $company_name);
add_post_meta($order_id, '_wiaas_project_id', $details['idProject']);
add_post_meta($order_id, '_wiaas_reference', $details['reference']);
add_post_meta($order_id, '_wiaas_tender', $details['tender']);
// $order->get_li
WC()->cart->empty_cart( true );
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'ORDER_PLACED'
)
),
'details' => $details
));
return wiaas_api_notice('ORDER_PLACED', 'success');
}
}

View File

@@ -0,0 +1,83 @@
<?php
class Wiaas_Order_Projects_API {
/**
* Endpoint namespace.
*
* @var string
*/
private static $namespace = 'wiaas';
private static $rest_base = 'order-projects';
public static function register_routes() {
register_rest_route( self::$namespace, '/' . self::$rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( __CLASS__, 'get_order_projects' ),
'permission_callback' => 'is_user_logged_in',
'args' => array(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( __CLASS__, 'create_order_project' ),
'permission_callback' => 'is_user_logged_in',
'args' => array(
'name' => array(
'type' => 'string',
'description' => __( 'Order project name.', 'wiaas' ),
'required' => true,
'sanitize_callback' => 'sanitize_title',
'validate_callback' => function($param, $request, $key) {
if ($param === '') {
return new WP_Error(
'rest_invalid_param',
'Order project name cannot be empty!'
);
}
if (strlen($param) > 100) {
return new WP_Error(
'rest_invalid_param',
'Order project name cannot be longer than 100 characters!'
);
}
},
),
),
)
) );
}
/**
* Retrieves available order projects
*
* @return WP_REST_Response
*/
public static function get_order_projects() {
$projects = Wiaas_Order_Project::get_available_order_projects();
return rest_ensure_response($projects);
}
/**
* Creates new available order project
*
* @param WP_REST_Request $request Request data.
*
* @return WP_REST_Response
*/
public static function create_order_project($request) {
$name = $request['name'];
$success = Wiaas_Order_Project::add_order_project($name);
if ($success) {
return wiaas_api_notice('PROJECT_ADDED', 'success');
}
return wiaas_api_notice('INVALID_DATA', 'error');
}
}

View File

@@ -62,10 +62,10 @@ class Wiaas_REST_Customer_API {
$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 wiaas_api_notice('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));
return wiaas_api_notice('PROFILE_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
}
public static function delete_customer_profile_address(WP_REST_Request $request){
@@ -73,10 +73,10 @@ class Wiaas_REST_Customer_API {
$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 wiaas_api_notice('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));
return wiaas_api_notice('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
}
public static function update_customer_billing_addresses(WP_REST_Request $request){
@@ -85,10 +85,10 @@ class Wiaas_REST_Customer_API {
$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 wiaas_api_notice('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));
return wiaas_api_notice('BILLING_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
}
@@ -97,10 +97,10 @@ class Wiaas_REST_Customer_API {
$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 wiaas_api_notice('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));
return wiaas_api_notice('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
}
public static function update_customer_personal_info(WP_REST_Request $request){
@@ -113,18 +113,18 @@ class Wiaas_REST_Customer_API {
$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));
return wiaas_api_notice('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));
return wiaas_api_notice('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 wiaas_api_notice('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));
return wiaas_api_notice('PROFILE_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
}
public static function update_customer_company_info(WP_REST_Request $request){
@@ -136,38 +136,18 @@ class Wiaas_REST_Customer_API {
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));
return wiaas_api_notice('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));
return wiaas_api_notice('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 wiaas_api_notice('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));
return wiaas_api_notice('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);
}
}

View File

@@ -93,15 +93,13 @@ class Wiass_REST_Delivery_Process_API {
);
}
$response = new WP_REST_Response( $data );
return $response;
return rest_ensure_response($data);
}
public static function get_customer_acceptance(WP_REST_Request $request){
$entry = GFAPI::get_entry($request['entry_id']);
if (is_wp_error($entry)){
return self::generate_error('Customer acceptance entry not found', 404);
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
}
$acceptance_documents = array();
@@ -130,27 +128,25 @@ class Wiass_REST_Delivery_Process_API {
$acceptance_status = ($entry[self::ACCEPTANCE_STATUS_FIELD_ID] === 'accept') ? 1 : -1;
}
$result = array(
return rest_ensure_response(array(
'documents' => $acceptance_documents,
'expiration' => $entry[self::EXPIRATION_DATE_FIELD_ID],
'status' => $acceptance_status,
'decline_reason' => $entry[self::DECLINE_REASON_FIELD_ID]
);
return new WP_REST_Response($result);
));
}
public static function submit_customer_acceptance(WP_REST_Request $request){
$entry = GFAPI::get_entry($request['entry_id']);
if (is_wp_error($entry)){
return self::generate_error('Customer acceptance entry not found', 404);
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
}
$status = $request['actionType'];
$reason = $request['declineReason'];
if (!in_array($status, self::ACCEPTABLE_STATUS)){
return self::generate_wiaas_response('ACCEPTANCE_STATUS_MISSING', 'error');
return wiaas_api_notice('ACCEPTANCE_STATUS_MISSING', 'error');
}
$installation_declined = ($status === self::DECLINE_STATUS_LABEL);
@@ -158,25 +154,25 @@ class Wiass_REST_Delivery_Process_API {
$uploaded_files = json_decode($entry[self::UPLOADED_FILES_FIELD_ID]);
if ($installation_declined && $reason === ''){
return self::generate_wiaas_response('DECLINE_REASON_EMPTY', 'error');
return wiaas_api_notice('DECLINE_REASON_EMPTY', 'error');
}
if (!$installation_declined && (count($uploaded_files)===0)){
return self::generate_wiaas_response('ACCEPTANCE_NOT_UPLOADED', 'error');
return wiaas_api_notice('ACCEPTANCE_NOT_UPLOADED', 'error');
}
$entry[self::DECLINE_REASON_FIELD_ID] = $reason;
$entry[self::ACCEPTANCE_STATUS_FIELD_ID] = $status;
if (!GFAPI::update_entry( $entry )){
return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error');
return wiaas_api_notice('INTERNAL_SERVER_ERROR', 'error');
}
//Check if step is already completed, to not submit again
$gf_api = new Gravity_Flow_API($entry['form_id']);
$current_step = $gf_api->get_current_step($entry);
if ($current_step->get_name() !== self::USER_INPUT_STEP_NAME){
return self::generate_wiaas_response('ACCEPTANCE_STATUS_UPDATED', 'success');
return wiaas_api_notice('ACCEPTANCE_STATUS_UPDATED', 'success');
}
if ( $current_step ) {
@@ -195,20 +191,20 @@ class Wiass_REST_Delivery_Process_API {
if ($installation_declined){
return self::generate_wiaas_response('INSTALLATION_DECLINED', 'success');
return wiaas_api_notice('INSTALLATION_DECLINED', 'success');
}
return self::generate_wiaas_response('INSTALLATION_ACCEPTED', 'success');
return wiaas_api_notice('INSTALLATION_ACCEPTED', 'success');
}
public static function upload_file(WP_REST_Request $request){
$files = $request->get_file_params();
if (!$files[self::FILE_KEY_NAME]){
return self::generate_wiaas_response('NO_FILES_UPLOADED', 'error');
return wiaas_api_notice('NO_FILES_UPLOADED', 'error');
}
$entry = GFAPI::get_entry($request['entry_id']);
if (is_wp_error($entry)){
return self::generate_error('Customer acceptance entry not found', 404);
return wiaas_api_generate_error('Customer acceptance entry not found', 404);
}
$form = GFAPI::get_form($entry['form_id']);
@@ -223,12 +219,12 @@ class Wiass_REST_Delivery_Process_API {
$file_path_details = pathinfo($file_name);
if ( GFCommon::file_name_has_disallowed_extension( $file_name ) ) {
return self::generate_wiaas_response('INVALID_FILE_ACCEPTANCE', 'error');
return wiaas_api_notice('INVALID_FILE_ACCEPTANCE', 'error');
}
$allowed_extensions = ! empty( $upload_file_field->allowedExtensions ) ? GFCommon::clean_extensions( explode( ',', strtolower( $upload_file_field->allowedExtensions ) ) ) : array();
if ( ! empty( $allowed_extensions ) ) {
if ( ! GFCommon::match_file_extension( $file_name, $allowed_extensions ) ) {
return self::generate_wiaas_response('INVALID_FILE_ACCEPTANCE', 'error');
return wiaas_api_notice('INVALID_FILE_ACCEPTANCE', 'error');
}
}
@@ -236,13 +232,13 @@ class Wiass_REST_Delivery_Process_API {
// Bypasses security checks when running unit tests.
if ( defined( 'WP_TEST_IN_PROGRESS' ) && WP_TEST_IN_PROGRESS ) {
return self::generate_wiaas_response('FILE_UPLOADED', 'success');
return wiaas_api_notice('FILE_UPLOADED', 'success');
}
if ( move_uploaded_file($files[self::FILE_KEY_NAME]['tmp_name'], $target_path . $new_file_name ) ) {
GFFormsModel::set_permissions( $target_path . $new_file_name );
} else {
return self::generate_wiaas_response('INTERNAL_SERVER_ERROR', 'error');
return wiaas_api_notice('INTERNAL_SERVER_ERROR', 'error');
}
//Extract path relative to the root
@@ -269,35 +265,9 @@ class Wiass_REST_Delivery_Process_API {
$entry[self::UPLOADED_FILES_FIELD_ID] = json_encode($uploaded_files);
if (GFAPI::update_entry( $entry )) {
return self::generate_wiaas_response('FILE_UPLOADED','success');
return wiaas_api_notice('FILE_UPLOADED','success');
}
return self::generate_wiaas_response('NOT_UPLOADED', 'error');
}
//Helper function
private static function generate_error($message, $code = 500){
$error = array(
'status' => $code,
'message' => $message,
);
$result = new WP_REST_Response($error);
$result->set_status($code);
return $result;
}
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);
return wiaas_api_notice('NOT_UPLOADED', 'error');
}
}

View File

@@ -31,7 +31,7 @@ class Wiass_REST_User_API {
public static function validate_token() {
$user = wp_get_current_user();
return new WP_REST_Response(array(
return rest_ensure_response(array(
'userInfo' => array(
'wiaas_id_user' => $user->ID,
'wiaas_is_company_admin' => 1, //TODO: don't hardcode this
@@ -45,6 +45,6 @@ class Wiass_REST_User_API {
public static function get_countries(){
$countries = Wiaas_Countries::get_list_of_countries();
return new WP_REST_Response($countries);
return rest_ensure_response($countries);
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* Generates REST API notice response
*
* @param string $message
* @param string $code
* @param array|null $data
*
* @return WP_REST_Response
*/
function wiaas_api_notice($message, $code, $data = null) {
return rest_ensure_response(array(
'messages' => [
array(
'code' => $code,
'message' => $message
)
],
'data' => $data
));
}
/**
* Generate REST API error
*
* @param string $message
* @param int $code
*
* @return WP_REST_Response
*/
function wiaas_api_generate_error($message, $code = 500) {
$response = rest_ensure_response(array(
'status' => $code,
'message' => $message,
));
$response->set_status($code);
return $response;
}