MP_Addon_Netsuite_REST
Class MP_Addon_Netsuite
Source Source
File: calculator-netsuite-add-on/inc/MP_Addon_Netsuite_REST.php
class MP_Addon_Netsuite_REST {
/**
* Netsuite get or update token URL.
*
* @access private
* @since 1.0.0
*
* @return string $url
*/
private function token_url() {
$account_id = get_option( MortgagePlatform::$prefix . 'netsuite' )['netsuite_account_id'];
if(!empty($account_id)){
$url = 'https://'.$account_id.'/suitetalk.api.netsuite.com/services/rest/auth/oauth2/v1/token';
return $url;
}
}
/**
* Get client ID.
* @access private
* @since 1.0.0
* @return mixed client ID
*/
private function get_netsuite_consumer_key() {
$microsoft_sheet_client_id = get_option( MortgagePlatform::$prefix . 'netsuite' )['netsuite_consumer_key'];
return $microsoft_sheet_client_id;
}
/**
* Get client secret.
* @access private
* @since 1.0.0
* @return mixed client secret
*/
private function get_netsuite_consumer_secret() {
$microsoft_sheet_client_secret = get_option( MortgagePlatform::$prefix . 'netsuite' )['netsuite_consumer_secret'];
return $microsoft_sheet_client_secret;
}
/**
* Get refresh token.
* @access private
* @since 1.0.0
* @return mixed refresh token
*/
private function get_netsuite_refresh_token() {
$netsuite_refresh_token = get_option( MortgagePlatform::$prefix . 'netsuite_refresh_token' );
return $netsuite_refresh_token;
}
/**
* Set refresh token.
* @access private
* @since 1.0.0
*
* @param string $refresh Token Netsuite refresh token
*/
private function set_refresh_token( $refreshToken ) {
$refreshToken = sanitize_text_field($refreshToken);
update_option( MortgagePlatform::$prefix . 'netsuite_refresh_token', $refreshToken );
}
/**
* Get Netsuite access token.
* @access private
* @since 1.0.0
* @return mixed Value set for the option. A value of any type may be returned, including
* array, boolean, float, integer, null, object, and string.
*/
private function get_netsuite_access_token() {
$netsuite_refresh_token = get_option( MortgagePlatform::$prefix . 'netsuite_access_token' );
return $netsuite_refresh_token;
}
/**
* Set Netsuite access token.
* @access private
* @since 1.0.0
*
* @param array $token access token.
*/
private function set_netsuite_access_token( $token ) {
update_option( MortgagePlatform::$prefix . 'netsuite_access_token', $token );
}
/**
* Get instance url.
*
* @access private
* @since 1.0.0
*
* @return string
*/
private function get_instance_url() {
$netsuite_instance_url = get_option( MortgagePlatform::$prefix . 'netsuite_access_token' )['instance_url'];
return $netsuite_instance_url;
}
/**
* Get redirect URI.
* @access private
* @since 1.0.0
* @return string|void redirect uri
*/
private function get_redirect_uri() {
$redirect_url = get_option( MortgagePlatform::$prefix . 'netsuite' )['netsuite_redirect_uri'];
return $redirect_url;
}
/**
* Callback function revoke token.
* @access public
* @since 1.0.0
*/
public function revoke_token() {
update_option( MortgagePlatform::$prefix . 'netsuite_refresh_token', "" );
update_option( MortgagePlatform::$prefix . 'netsuite_access_token', "" );
wp_die();
}
/**
* Set Lead data to Netsuite.
* @access public
* @since 1.0.0
*
* @param array $lead_data lead data
*/
public function set_lead_data_to_netsuite( $lead_data ) {
$data = $this->prepare_lead_data( $lead_data );
$this->netsuite_request( $data , $lead_data['lead']);
}
/**
* Get refresh code from GET params.
* @access public
* @since 1.0.0
* @return string
*/
public function get_refresh_code() {
$token_value = $this->get_netsuite_refresh_token();
$code = '';
if ( isset( $_GET['code'] ) && ! empty( $_GET['code'] ) ) {
$code = sanitize_text_field( wp_unslash($_GET['code']) );
$this->set_refresh_token( $code );
wp_redirect( get_admin_url() . 'admin.php?page=mortgage_platform&tab=netsuite' );
}
$value = $token_value ? $token_value : $code;
return $value;
}
/**
* Decode state.
* Decodes Data from $state query arg and adds them to $_REQUEST. This must happen
* before admin-ajax.php checks for the 'action' value
* @access public
* @since 1.0.0
*/
public function decode_state() {
if ( ! is_admin() || ! is_user_logged_in() ) {
return;
}
if ( ! isset( $_REQUEST['page'] ) || ! is_string( $_REQUEST['page'] ) ) {
return;
}
if ( ! isset( $_REQUEST['tab'] ) || ! is_string( $_REQUEST['tab'] ) ) {
return;
}
if( isset( $_GET['page'] ) && 'mortgage_platform' === $_GET['page'] && isset( $_GET['tab'] ) && 'netsuite' === $_GET['tab']) {
$this->get_refresh_code();
}
}
/**
* Get authorize url.
*
* @access public
* @since 1.0.0
*
* @return string
*/
public function get_url() {
//https://system.netsuite.com/app/login/oauth2/authorize.nl If the Account ID is not known, the generic endpoint can be used:
$account_id = get_option( MortgagePlatform::$prefix . 'netsuite' )['netsuite_account_id'];
$consumer_key = $this->get_netsuite_consumer_key();
$redirect_uri = $this->get_redirect_uri();
$url = 'https://'.$account_id.'.app.netsuite.com/app/login/oauth2/authorize.nl?
scope=rest_webservices&
redirect_uri='.$redirect_uri.'&
response_type=code&
client_id='.$consumer_key;
return $url;
}
/**
* Get access token.
*
* @access private
* @since 1.0.0
*
* @param string $code authorization code
*/
private function get_token($code){
$body = array(
'code' => $code,
'grant_type' => 'authorization_code',
'client_id' => $this->get_netsuite_consumer_key(),
'client_secret' => $this->get_netsuite_consumer_secret(),
'redirect_uri' => $this->get_redirect_uri(),
);
if(is_array($body) && count($body)>0) {
$body=http_build_query($body);
}
$response = wp_remote_post( $this->token_url(), array( 'body' => $body ) );
$token_request_data = json_decode($response['body'], true);
if (empty($token_request_data))
die("Couldn't decode '$token_request_data' as a JSON object");
if (!isset($token_request_data['access_token']))
die("Missing expected data from ".print_r($token_request_data, true));
$this->set_netsuite_access_token($token_request_data);
$refresh_token = $token_request_data['refresh_token'];
$this->set_refresh_token($refresh_token);
}
/**
* Update access token token.
*
* @access private
* @since 1.0.0
*/
private function update_token(){
$body = array(
'grant_type' => 'refresh_token',
'client_id' => $this->get_netsuite_consumer_key(),
'client_secret' => $this->get_netsuite_consumer_secret(),
'refresh_token' => $this->get_netsuite_refresh_token(),
);
if(is_array($body) && count($body)>0) {
$body=http_build_query($body);
}
$response = wp_remote_post( $this->token_url(), array( 'body' => $body ) );
$token_request_data = json_decode($response['body'], true);
if (empty($token_request_data))
die("Couldn't decode '$token_request_data' as a JSON object");
if (!isset($token_request_data['access_token']))
die("Missing expected data from ".print_r($token_request_data, true));
$this->set_netsuite_access_token($token_request_data);
}
/**
* Netsuite Oauth client.
* @access public
* @since 1.0.0
*/
public function get_client() {
if(empty(get_option( MortgagePlatform::$prefix . 'netsuite_access_token'))){
$this->get_token($this->get_netsuite_refresh_token());
} else {
$expires_in = $this->get_netsuite_access_token()['expires_in'];
$now = (time() + 300)*1000;
if ($expires_in <= $now) {
$this->update_token();
}
}
}
/**
* Prepare lead data to JSON.
* @access private
* @since 1.0.0
*
* @param array $lead_data lead data
*
* @return false|string $lead_data {
* Lead data in JSON.
* @type string 'firstname' lead first name
* @type string 'secondname' lead second name
* @type string 'email' lead email
* @type string 'phone' lead phone
* @type string 'zipcode' lead zip code
* @type string 'yearofbirth' lead year of birth
* @type string 'investmentamount' investment amount
* }
*/
private function prepare_lead_data( $lead_data ) {
$data = array(
"firstName" => isset( $lead_data['firstname'] ) ? sanitize_text_field( $lead_data['firstname'] ) : "",
"lastName" => isset( $lead_data['secondname'] ) ? sanitize_text_field( $lead_data['secondname'] ) : "",
"companyname" => MortgagePlatform::$plugin_name,
"externalId" => 'MPID_'.$lead_data['lead'],
"phone" => isset( $lead_data['phone'] ) ? sanitize_text_field( $lead_data['phone'] ) : "",
"email" => isset( $lead_data['email'] ) ? sanitize_text_field( $lead_data['email'] ) : "",
);
/**
* Action Netsuite data.
* @since 1.0.0
*
* @param array $data {
* Lead data.
*
* @type string 'firstName' lead first name
* @type string 'lastName' lead last name
* @type string 'companyname' company (Mortgage Platform)
* @type string 'phone' lead phone
* @type string 'email' lead email
* }
*/
do_action( 'netsuite_data', $data );
return $data;
}
/**
* Add lead data to Netsuite.
* @access private
* @since 1.0.0
*
* @param array $data $lead_data {
* Lead data.
* @type string 'firstName' lead first name
* @type string 'lastName' lead last name (required)
* @type string 'companyname' company (required)
* @type string 'phone' lead phone
* @type string 'email' lead email
* }
*
*
* @param string|int $lead lead id
*/
private function add_lead_data_to_netsuite($data, $lead) {
$account_id = get_option( MortgagePlatform::$prefix . 'netsuite' )['netsuite_account_id'];
//'https://'.$account_id.'.suitetalk.api.netsuite.com/services/rest/record/v1/metadata-catalog/?select=customer';
//'https://'.$account_id.'suitetalk.api.netsuite.com/services/rest/query/v1/dataset/'
//https://1157106-sb1.app.netsuite.com/app/help/helpcenter.nl?fid=section_1546938065.html
//https://system.netsuite.com/help/helpcenter/en_US/APIs/REST_API_Browser/record/v1/2020.2/index.html#tag-customer
$url = 'https://'.$account_id.'.suitetalk.api.netsuite.com/services/rest/record/v1/lead/eid:'.$data['externalId'];
$header=array("Authorization"=>' Bearer ' . get_option(MortgagePlatform::$prefix . 'netsuite_access_token')['access_token'],'content-type'=>'application/json');
$response = wp_remote_post( $url, array(
'method' => 'PUT',
'headers' => $header,
'body' => json_encode($data)
)
);
$token_request_data = json_decode($response['body'], true);
if (empty($token_request_data))
die("Couldn't decode '$token_request_data' as a JSON object");
$success_status = 'Success: ';
$error_status = 'Error: ';
if ( isset($token_request_data['success'])){
$success_status .= isset($token_request_data['id']) ? 'id '. $token_request_data['id'] : '';
} else {
$error_status .= isset($token_request_data['0']) ? $token_request_data['0']['message'] . ' ' . $token_request_data['0']['errorCode'] : '' ;
}
$status = isset($token_request_data['success']) && $token_request_data['success'] ? $success_status : $error_status;
MP_Logs::set_logs($lead, 'netsuite',$status );
}
/**
* Netsuite request.
* @access private
* @since 1.0.0
*
* @param string $data lead data
* @param string|int $lead lead id
*/
private function netsuite_request( $data, $lead ) {
$this->get_client();
$this->add_lead_data_to_netsuite($data, $lead);
}
/**
* Callback function to send lead data from the lead page.
*
* @access public
* @since 1.0.0
*/
public function netsuite_send_from_lead_page() {
MP_Callback::check_nonce($_REQUEST['nonce'], MortgagePlatform::$prefix . 'netsuite');
$post_ID = sanitize_text_field($_POST['id']);
$lead = array(
'lead' => $post_ID,
'firstname' => get_post_meta($post_ID, MortgagePlatform::$prefix . 'lead_firstname', true),
'secondname' => get_post_meta($post_ID,MortgagePlatform::$prefix . 'lead_secondname', true),
'phone' => get_post_meta($post_ID,MortgagePlatform::$prefix . 'lead_phone', true),
'email' => get_post_meta($post_ID,MortgagePlatform::$prefix . 'lead_email', true),
);
$this->set_lead_data_to_netsuite($lead);
wp_die();
}
/**
* Send multiple sending to Netsuite.
* @access public
* @since 1.0.0
*
* @param array $data $_POST
*/
public function send_netsuite_manually($data) {
if ('netsuite' === $data['connector']) {
$this->send_netsuite($data);
}
}
/**
* Send multiple sending to Netsuite.
* @access private
* @since 1.0.0
*
* @param array $data lead IDs
*/
private function send_netsuite($data){
unset( $data['connector'] );
if ( ! empty( $data ) ) {
$lead = array();
foreach ( $data as $key => $value ) {
$data = array(
'firstName' => get_post_meta($value, MortgagePlatform::$prefix . 'lead_firstname', true),
'lastName' => get_post_meta($value,MortgagePlatform::$prefix . 'lead_secondname', true),
"companyname" => MortgagePlatform::$plugin_name,
'phone' => get_post_meta($value,MortgagePlatform::$prefix . 'lead_phone', true),
'email' => get_post_meta($value,MortgagePlatform::$prefix . 'lead_email', true),
);
array_push($lead, $data);
}
/**
* Action lead data to Netsuite.
* @since 1.0.0
*
* @param array $data {
* Lead data.
*
* @type string 'firstName' lead first name
* @type string 'lastName' lead second name
* @type string 'companyname' Mortgage Platform
* @type string 'email' lead email
* @type string 'phone' lead phone
*
* }
*/
do_action('lead_to_netsuite',$lead);
//MortgagePlatform::debug($lead);
$this->get_client();
$account_id = get_option( MortgagePlatform::$prefix . 'netsuite' )['netsuite_account_id'];
$url = 'https://'.$account_id.'.suitetalk.api.netsuite.com/services/rest/record/v1/lead';
$header=array("Authorization"=>' Bearer ' . get_option(MortgagePlatform::$prefix . 'netsuite_access_token')['access_token'],'content-type'=>'application/json');
$response = wp_remote_post( $url, array(
'headers' => $header,
'body' => json_encode($lead)
)
);
$token_request_data = json_decode($response['body'], true);
MortgagePlatform::debug($token_request_data);
//$this->netsuite_request( $lead );
echo esc_html__( 'Ready', 'CalculatorsNetsuiteAddon' );
} else {
echo esc_html__( 'No lead selected', 'CalculatorsNetsuiteAddon' );
}
}
}
Methods Methods
- add_lead_data_to_netsuite — Add lead data to Netsuite.
- decode_state — Decode state.
- get_client — Netsuite Oauth client.
- get_instance_url — Get instance url.
- get_netsuite_access_token — Get Netsuite access token.
- get_netsuite_consumer_key — Get client ID.
- get_netsuite_consumer_secret — Get client secret.
- get_netsuite_refresh_token — Get refresh token.
- get_redirect_uri — Get redirect URI.
- get_refresh_code — Get refresh code from GET params.
- get_token — Get access token.
- get_url — Get authorize url.
- netsuite_request — Netsuite request.
- netsuite_send_from_lead_page — Callback function to send lead data from the lead page.
- prepare_lead_data — Prepare lead data to JSON.
- revoke_token — Callback function revoke token.
- send_netsuite — Send multiple sending to Netsuite.
- send_netsuite_manually — Send multiple sending to Netsuite.
- set_lead_data_to_netsuite — Set Lead data to Netsuite.
- set_netsuite_access_token — Set Netsuite access token.
- set_refresh_token — Set refresh token.
- token_url — Netsuite get or update token URL.
- update_token — Update access token token.
Changelog Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |