From a07c0e4584c3d69146eff7b5f0d6fbc12bba938d Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Fri, 16 Nov 2018 17:14:15 +0100 Subject: [PATCH 1/4] Added support for sending customer emails --- .../includes/api/class-wiaas-support-api.php | 65 +++++++++++++++++++ ...lass-wiaas-wc-package-api-integration.php} | 0 .../wiaas/includes/class-wiaas-api.php | 4 +- backend/config/application.php | 3 +- frontend/src/actions/orders/processActions.js | 5 +- 5 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php rename backend/app/plugins/wiaas/includes/api/{class-wiaas-wc- package-api-integration.php => class-wiaas-wc-package-api-integration.php} (100%) diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php new file mode 100644 index 0000000..93ec35f --- /dev/null +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php @@ -0,0 +1,65 @@ + 'POST', + 'callback' => array(__CLASS__, 'send_support_email'), + 'permission_callback' => 'is_user_logged_in', + //this.props.order, this.props.order.packages, this.props.supportText + 'args' => array( + 'id' => array( + 'description' => __('Order ID.', 'wiaas'), + 'type' => 'integer', + 'required' => true, + 'sanitize_callback' => 'absint', + ), + 'supportText' => array( + 'description' => __('Email text.', 'wiaas'), + 'type' => 'string', + 'required' => true + ) + ) + )); + } + + /** + * Send support email and save massage to order notes + * + * @param WP_REST_Request $request Request data. + * + * @return WP_REST_Response + */ + public static function send_support_email($request) { + + $order_id = $request['id']; + $message = $request['supportText']; + + $mailer = WC()->mailer(); + + $recipient = WIAAS_SUPPORT_EMAIL; + $subject = __("Customer comment for order id: ".$order_id); + + $headers = array(); + $success = $mailer->send( $recipient, $subject, $message, $headers ); + + if ($success) { + wc_create_order_note($message = $request['id'] , $message, true ); + return wiaas_api_notice('EMAIL_SENT', 'success'); + } + + return wiaas_api_notice('EMAIL_NOT_SENT', 'failed'); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-wc- package-api-integration.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-wc-package-api-integration.php similarity index 100% rename from backend/app/plugins/wiaas/includes/api/class-wiaas-wc- package-api-integration.php rename to backend/app/plugins/wiaas/includes/api/class-wiaas-wc-package-api-integration.php diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-api.php b/backend/app/plugins/wiaas/includes/class-wiaas-api.php index 8cb4cb5..6cf0d66 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-api.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-api.php @@ -43,7 +43,8 @@ class Wiaas_API { include_once dirname( __FILE__ ) . '/api/class-wiaas-order-projects-api.php'; - include_once dirname( __FILE__ ) . '/api/class-wiaas-wc- package-api-integration.php'; + include_once dirname( __FILE__ ) . '/api/class-wiaas-wc-package-api-integration.php'; + include_once dirname( __FILE__ ) . '/api/class-wiaas-support-api.php'; // API functions include_once dirname( __FILE__ ) . '/api/wiaas-api-functions.php'; @@ -58,6 +59,7 @@ class Wiaas_API { 'Wiass_REST_User_API', 'Wiaas_REST_Customer_API', 'Wiaas_Order_Projects_API', + 'Wiaas_Support_Api', ); foreach ( $controllers as $controller ) { diff --git a/backend/config/application.php b/backend/config/application.php index da02a01..32cbb01 100644 --- a/backend/config/application.php +++ b/backend/config/application.php @@ -76,6 +76,7 @@ define('SENDGRID_FROM_NAME', env('WP_SENDGRID_FROM_NAME' ?: 'Wiaas')); * Wiaas Env variables */ define('WIAAS_CUSTOMER_INTERFACE', env('WIAAS_CUSTOMER_INTERFACE') ?: WP_HOME); +define('WIAAS_SUPPORT_EMAIL', env('WIAAS_SUPPORT_EMAIL') ?: 'support@co-ideation.com'); /** * Custom Settings @@ -89,4 +90,4 @@ define('DISALLOW_FILE_EDIT', true); */ if (!defined('ABSPATH')) { define('ABSPATH', $webroot_dir . '/wp/'); -} \ No newline at end of file +} diff --git a/frontend/src/actions/orders/processActions.js b/frontend/src/actions/orders/processActions.js index 87bef48..b5eebf3 100644 --- a/frontend/src/actions/orders/processActions.js +++ b/frontend/src/actions/orders/processActions.js @@ -326,11 +326,12 @@ export const setSupportMessage = (message) => ({ }); export const sendSupportMail = (orderInfo, orderPackages, supportText) => { + let id = orderInfo.id; return dispatch => { return htmlClient.fetch({ - url: `${API_SERVER}/orders/api/sendSupportMail`, + url: `${API_SERVER}/wp-json/wiaas/support/sendSupportMail`, method: 'post', - data: {orderInfo, orderPackages, supportText} + data: {id, supportText} }) .then(response => { if (typeof response.data !== 'undefined' && 'messages' in response.data) { From 752df56d4a3dbc60072c858c76cc71c4472d31e9 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Mon, 19 Nov 2018 11:31:46 +0100 Subject: [PATCH 2/4] Fixed bug where order note text is saved as order ID --- .../app/plugins/wiaas/includes/api/class-wiaas-support-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php index 93ec35f..627e5eb 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php @@ -56,7 +56,7 @@ class Wiaas_Support_Api { $success = $mailer->send( $recipient, $subject, $message, $headers ); if ($success) { - wc_create_order_note($message = $request['id'] , $message, true ); + wc_create_order_note($request['id'] , $message, true ); return wiaas_api_notice('EMAIL_SENT', 'success'); } From f56377f606b36bbe1decfa9cb9cb2ccb245f5e7f Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Mon, 19 Nov 2018 12:35:04 +0100 Subject: [PATCH 3/4] Handled proper endpoint naming --- .../wiaas/includes/api/class-wiaas-support-api.php | 14 +++++++++----- backend/config/application.php | 3 +++ frontend/src/actions/orders/processActions.js | 6 +++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php index 627e5eb..475d0f2 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-support-api.php @@ -14,7 +14,7 @@ class Wiaas_Support_Api { public static function register_routes() { - register_rest_route(self::$namespace, self::$rest_base . '/sendSupportMail', array( + register_rest_route(self::$namespace, self::$rest_base . '/send-support-email', array( 'methods' => 'POST', 'callback' => array(__CLASS__, 'send_support_email'), 'permission_callback' => 'is_user_logged_in', @@ -26,7 +26,7 @@ class Wiaas_Support_Api { 'required' => true, 'sanitize_callback' => 'absint', ), - 'supportText' => array( + 'support_text' => array( 'description' => __('Email text.', 'wiaas'), 'type' => 'string', 'required' => true @@ -45,18 +45,22 @@ class Wiaas_Support_Api { public static function send_support_email($request) { $order_id = $request['id']; - $message = $request['supportText']; + $message = $request['support_text']; + $order = wc_get_order($order_id); + $customer_id = $order->get_customer_id(); + $customer = get_user_by('id', $customer_id); + $mailer = WC()->mailer(); $recipient = WIAAS_SUPPORT_EMAIL; - $subject = __("Customer comment for order id: ".$order_id); + $subject = __('Customer: '.$customer->get('first_name').', '.''.$customer->get('last_name').' needs support for order number: ' .$order->get_order_number()); $headers = array(); $success = $mailer->send( $recipient, $subject, $message, $headers ); if ($success) { - wc_create_order_note($request['id'] , $message, true ); + wc_create_order_note($order_id , $message, true ); return wiaas_api_notice('EMAIL_SENT', 'success'); } diff --git a/backend/config/application.php b/backend/config/application.php index 32cbb01..b61c083 100644 --- a/backend/config/application.php +++ b/backend/config/application.php @@ -48,6 +48,9 @@ define('DB_CHARSET', 'utf8mb4'); define('DB_COLLATE', ''); $table_prefix = env('DB_PREFIX') ?: 'wp_'; +define( 'WP_DEBUG', true ); +define( 'WP_DEBUG_LOG', true ); + /** * Authentication Unique Keys and Salts */ diff --git a/frontend/src/actions/orders/processActions.js b/frontend/src/actions/orders/processActions.js index b5eebf3..03b9117 100644 --- a/frontend/src/actions/orders/processActions.js +++ b/frontend/src/actions/orders/processActions.js @@ -325,13 +325,13 @@ export const setSupportMessage = (message) => ({ supportText: message }); -export const sendSupportMail = (orderInfo, orderPackages, supportText) => { +export const sendSupportMail = (orderInfo, orderPackages, support_text) => { let id = orderInfo.id; return dispatch => { return htmlClient.fetch({ - url: `${API_SERVER}/wp-json/wiaas/support/sendSupportMail`, + url: `${API_SERVER}/wp-json/wiaas/support/send-support-email`, method: 'post', - data: {id, supportText} + data: {id, support_text} }) .then(response => { if (typeof response.data !== 'undefined' && 'messages' in response.data) { From 35e670fa52a10208fd3cd8ae9e2d947f76d62840 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Mon, 19 Nov 2018 13:15:50 +0100 Subject: [PATCH 4/4] removved debug log --- backend/config/application.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/backend/config/application.php b/backend/config/application.php index b61c083..32cbb01 100644 --- a/backend/config/application.php +++ b/backend/config/application.php @@ -48,9 +48,6 @@ define('DB_CHARSET', 'utf8mb4'); define('DB_COLLATE', ''); $table_prefix = env('DB_PREFIX') ?: 'wp_'; -define( 'WP_DEBUG', true ); -define( 'WP_DEBUG_LOG', true ); - /** * Authentication Unique Keys and Salts */