0

I try to connect to Netsuite API from Magento2. throws Invalid login attempt from Netsuite. my code is below please help me .

public function createPostRequest($type, $params, $page_constant){
    try{

        if(!trim($this->NETSUITE_ACCOUNT)){
            $this->reloadNetsuiteConfigData();
        }

        $data_string = json_encode($params);

        $deploy = $page_constant['deploy'];
        $script = $page_constant['script'];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->NETSUITE_URL . '?&script=' . $script . '&deploy=' . $deploy . '&realm=' . $this->NETSUITE_ACCOUNT);
        curl_setopt($ch, CURLOPT_POST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Authorization: ' . $this->PostHeaderGenerate( $params, $page_constant),
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        ]);

        $response = curl_exec($ch);
        curl_close($ch);

        $this->addLogs->add_info('createPostRequest - response', ['type' => $type, 'response' => $response], 'netsuite-curl');
        return $response;


    } catch (\Exception $exception){
        $this->addLogs->add_error('createPostRequest', ['type' => $type, $exception->getMessage()], 'netsuite');
    }
}

public function PostHeaderGenerate( $params, $page_constant)
{

    if(!trim($this->NETSUITE_ACCOUNT)){
        $this->reloadNetsuiteConfigData();
    }

    $deploy = $page_constant['deploy'];
    $script = $page_constant['script'];
    $data_string = json_encode($params);
    $oauth_nonce = md5(mt_rand());
    $oauth_timestamp = time();
    $oauth_signature_method = 'HMAC-SHA256';
    $oauth_version = "1.0";

    $base_string =
        "POST&" . urlencode($this->NETSUITE_URL) . "&" .
        urlencode(
            "deploy=" . $deploy
            . "&oauth_consumer_key=" . $this->NETSUITE_CONSUMER_KEY
            . "&oauth_nonce=" . $oauth_nonce
            . "&oauth_signature_method=" . $oauth_signature_method
            . "&oauth_timestamp=" . $oauth_timestamp
            . "&oauth_token=" . $this->NETSUITE_TOKEN_ID
            . "&oauth_version=" . $oauth_version
            . "&realm=" . $this->NETSUITE_ACCOUNT
            . "&script=" . $script
        );

    $sig_string = urlencode($this->NETSUITE_CONSUMER_SECRET) . '&' . urlencode($this->NETSUITE_TOKEN_SECRET);
    $signature = base64_encode(hash_hmac("sha256", $base_string, $sig_string, true));

    $header = "OAuth "
        . 'oauth_signature="' . rawurlencode($signature) . '", '
        . 'oauth_version="' . rawurlencode($oauth_version) . '", '
        . 'oauth_nonce="' . rawurlencode($oauth_nonce) . '", '
        . 'oauth_signature_method="' . rawurlencode($oauth_signature_method) . '", '
        . 'oauth_consumer_key="' . rawurlencode($this->NETSUITE_CONSUMER_KEY) . '", '
        . 'oauth_token="' . rawurlencode($this->NETSUITE_TOKEN_ID) . '", '
        . 'oauth_timestamp="' . rawurlencode($oauth_timestamp) . '", '
        . 'realm="' . rawurlencode($this->NETSUITE_ACCOUNT) .'"';

    $this->addLogs->add_info('header', [$header], 'netsuite-params');
    return  $header;
}
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.