Skip to content

Instantly share code, notes, and snippets.

@surferxo3
Created May 2, 2020 12:55
Show Gist options
  • Save surferxo3/db8db0de4bd8a941d127e1a9ec57d2ab to your computer and use it in GitHub Desktop.
Save surferxo3/db8db0de4bd8a941d127e1a9ec57d2ab to your computer and use it in GitHub Desktop.
LiveOrder POC for `Login` and `Add to Cart with Promocode`
<?php
/*#############################
* Developer: Mohammad Sharaf Ali
* Designation: Sr. SE
* Version: 1.0
*/#############################
class SuiteCommerce
{
protected $cookie;
public function __construct() {
$this->cookie = '';
}
public function initCurlRequest($reqType, $reqURL, $reqBody = '', $headers = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $reqURL);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $reqType);
curl_setopt($ch, CURLOPT_POSTFIELDS, $reqBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, true);
$body = curl_exec($ch);
// extract header
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($body, 0, $headerSize);
$header = $this->getHeaders($header);
// extract body
$body = substr($body, $headerSize);
curl_close($ch);
return [$header, $body];
}
public function getHeaders($respHeaders) {
$headers = [];
$headerText = substr($respHeaders, 0, strpos($respHeaders, "\r\n\r\n"));
foreach (explode("\r\n", $headerText) as $i => $line) {
if ($i === 0) {
$headers['http_code'] = $line;
} else {
list($key, $value) = explode(': ', $line);
// prevent duplicate key(s)
if (!isset($headers[$key])) {
$headers[$key] = $value;
}
}
}
return $headers;
}
public function doLogin() {
$reqBody = [
'email' => 'your_ns_email',
'password' => 'your_ns_pass',
'redirect' => 'false'
];
$headers = [
'Content-Type: application/json;charset=utf-8',
];
list($header) = $this->initCurlRequest(
'POST',
'https://checkout.na0.netsuite.com/c.your_ns_acc/scs/services/Account.Login.Service.ss?n=2&c=your_ns_acc',
json_encode($reqBody),
$headers
);
$this->cookie = explode(';', trim($header['Set-Cookie']))[0];
}
public function doLogout() {;
$reqBody = '';
$headers = array(
'Cookie: ' . $this->cookie
);
$this->initCurlRequest(
'GET',
'https://checkout.na0.netsuite.com/c.your_ns_acc/scs/logOut.ssp?n=2&logoff=T',
$reqBody,
$headers
);
}
public function doAddToCart() {
$reqBody = [
'item' => [
'internalid' => 61068,
'type' => 'Assembly',
],
'quantity' => 1,
'options' => [],
'location' => '',
'fulfillmentChoice' => 'ship',
'freeGift' => false
];
$headers = [
'Content-Type: application/json;charset=utf-8',
'Cookie: ' . $this->cookie
];
$this->initCurlRequest(
'POST',
'http://your_domain/scs/services/LiveOrder.Line.Service.ss?c=your_ns_acc&n=2',
json_encode($reqBody),
$headers
);
}
}
$suiteCommerce = new SuiteCommerce();
$suiteCommerce->doLogin();
$suiteCommerce->doAddToCart();
$suiteCommerce->doLogout();
echo 'Script Execution Completed!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment