Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- (Improvement)Support of short link added #22

Open
wants to merge 2 commits into
base: referral-program-api
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions src/Api/AbandonedCart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,19 @@ function recoverUserCart()
}
}

/**
* Recover user cart
*/
function recoverCartWithShortLink()
{
if (isset($_REQUEST['rnoc_cart']) && !empty($_REQUEST['rnoc_cart'])) {
$cart_id = sanitize_text_field($_REQUEST['rnoc_cart']);
if ($this->isPrefixMatch($cart_id, 'crt_')) {
$this->recoverCart();
}
}
}

/**
* Add abandon cart coupon automatically
*/
Expand Down Expand Up @@ -762,6 +775,26 @@ function printRefreshFragmentScript()
}
}

/**
* get the cart token
* @param bool $data_only
* @return array|string|null
*/
function getCartTokenFromRequest($data_only = false)
{
if (isset($_REQUEST['rnoc_cart']) && !empty($_REQUEST['rnoc_cart'])) {
return wc_clean(rawurldecode($_REQUEST['rnoc_cart']));
} else {
$request_data = wc_clean(rawurldecode($_REQUEST['token']));
if ($data_only) {
return $request_data;
}
$data = json_decode(base64_decode($request_data));
// readability
return isset($data->cart_token) ? $data->cart_token : NULL;
}
}

/**
* Recreate cart
* @return bool
Expand All @@ -770,19 +803,24 @@ function printRefreshFragmentScript()
function reCreateCart()
{
global $retainful;
$data = wc_clean(rawurldecode($_REQUEST['token']));
$data = $this->getCartTokenFromRequest(true);
$hash = wc_clean($_REQUEST['hash']);
if ($this->isHashMatches($hash, $data)) {
// decode
$data = json_decode(base64_decode($data));
// readability
$cart_token = isset($data->cart_token) ? $data->cart_token : NULL;
$is_short_link_request = (isset($_REQUEST['rnoc_cart']) && !empty($_REQUEST['rnoc_cart']));
if ($this->isHashMatches($hash, $data) || $is_short_link_request) {
$cart_token = $this->getCartTokenFromRequest();
if (!empty($cart_token)) {
if (empty($cart_token)) {
throw new Exception('Cart token missed');
}
$app_id = $retainful::$plugin_admin->getApiKey();
$data = $retainful::$api->retrieveCartDetails($app_id, $cart_token);
if (isset($_REQUEST['rnoc_cart']) && !empty($_REQUEST['rnoc_cart'])) {
$id_hash = $this->hashTheData($cart_token);
$ip = $this->getClientIp();
$format_ip = $this->formatUserIP($ip);
echo $data = $retainful::$api->retrieveShortLinkCartDetails($app_id, $cart_token, $id_hash, $format_ip);die;
} else {
$data = $retainful::$api->retrieveCartDetails($app_id, $cart_token);
}
//When the cart details from API was empty, then we no need to proceed further
if (empty($data)) {
return false;
Expand Down
12 changes: 12 additions & 0 deletions src/Api/AbandonedCart/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class RestApi
/** The HMAC hash algorithm to use to sign the encrypted cart data */
const HMAC_ALGORITHM = 'sha256';

/**
* check is prefix match
* @param $text
* @param $prefix
* @return bool
*/
function isPrefixMatch($text, $prefix)
{
$len = strlen($prefix);
return (substr($text, 0, $len) === $prefix);
}

/**
* Set the cart token for the session
* @param $cart_token
Expand Down
1 change: 1 addition & 0 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function initClassesAndHooks()
//add_action('wp_login', array($cart, 'userLoggedIn'));
add_action('woocommerce_api_retainful', array(self::$abandoned_cart, 'recoverUserCart'));
add_action('wp_loaded', array(self::$abandoned_cart, 'applyAbandonedCartCoupon'));
add_action('wp_loaded', array(self::$abandoned_cart, 'recoverCartWithShortLink'));
//Add tracking message
if (is_user_logged_in()) {
add_action('woocommerce_after_add_to_cart_button', array(self::$abandoned_cart, 'userGdprMessage'), 10);
Expand Down
25 changes: 25 additions & 0 deletions src/library/RetainfulApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,29 @@ function retrieveCartDetails($app_id, $cart_token)
}
return NULL;
}

/**
* Sync the cart details to server
* @param $app_id
* @param string $cart_token
* @param string $cart_hash
* @param string $ip
* @return array|bool|mixed|object|string
*/
function retrieveShortLinkCartDetails($app_id, $cart_token, $cart_hash, $ip)
{
$url = rtrim($this->domain, '/');
$url .= '/webhook/woocommerce/cart';
$request_url = add_query_arg(array('app_id' => $app_id, 'rnoc_cart' => $cart_token, 'rnoc_hash' => $cart_hash), $url);
$headers = array(
'app_id' => $app_id,
'x-client-referrer-ip' => $ip,
'x-retainful-version' => RNOC_VERSION
);
$response = $this->request($request_url, array(), 'get', '', $headers);
if (isset($response->success) && $response->success) {
return isset($response->data) ? $response->data : NULL;
}
return NULL;
}
}