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

- New Api v2 #109

Open
wants to merge 6 commits into
base: master-release
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
36 changes: 33 additions & 3 deletions src/admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ function validateAppKey()
}
$app_id = isset($_REQUEST['app_id']) ? sanitize_text_field($_REQUEST['app_id']) : '';
$secret_key = isset($_REQUEST['secret_key']) ? sanitize_text_field($_REQUEST['secret_key']) : '';
$old_app_id = $this->getApiKey();
if(!empty($old_app_id) && !empty($app_id) && $old_app_id != $app_id){
$this->disconnectApi();
}
$options_data = array(
RNOC_PLUGIN_PREFIX . 'is_retainful_connected' => '0',
RNOC_PLUGIN_PREFIX . 'retainful_app_id' => $app_id,
Expand All @@ -129,16 +133,20 @@ function validateAppKey()
wp_send_json($response);
}


/**
* disconnect the app
*/
function disconnectLicense()
{
WcFunctions::checkSecuritykey('rnoc_disconnect_license');
$license_details = get_option($this->slug . '_license', array());
$license_details[RNOC_PLUGIN_PREFIX . 'is_retainful_connected'] = 0;
update_option($this->slug . '_license', $license_details);
wp_send_json_success(__('App disconnected successfully!', RNOC_TEXT_DOMAIN));
if($this->disconnectApi()){
$license_details[RNOC_PLUGIN_PREFIX . 'is_retainful_connected'] = 0;
update_option($this->slug . '_license', $license_details);
wp_send_json_success(__('App disconnected successfully!', RNOC_TEXT_DOMAIN));
}
wp_send_json_error(__('App disconnected failed!', RNOC_TEXT_DOMAIN));
}

/**
Expand Down Expand Up @@ -1689,6 +1697,28 @@ function isApiEnabled($api_key = "", $secret_key = NULL, $store_data = NULL)
}
}

function disconnectApi($api_key = "", $secret_key = NULL)
{
if (empty($api_key)) {
$api_key = $this->getApiKey();
}
if (empty($secret_key)) {
$secret_key = $this->getSecretKey();
}
if (empty($store_data)) {
$store_data = $this->storeDetails($api_key, $secret_key);
}
if (!empty($api_key)) {
if ($details = $this->api->validateApi($api_key, $store_data,'disable')) {
if (!empty($details) && !is_string($details)) {
$this->updateUserAsFreeUser();
return true;
}
}
}
return false;
}

/**
* update user as Free user
*/
Expand Down
51 changes: 33 additions & 18 deletions src/library/RetainfulApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class RetainfulApi
{
public $app_url = "https://app.retainful.com/";
public $domain = "https://api.retainful.com/v1/";
public $domain = "https://api.retainful.com/v2/";
public $abandoned_cart_api_url = "https://api.retainful.com/v1/woocommerce/";

/**
Expand All @@ -31,32 +31,46 @@ function getAbandonedCartApiUrl()
return apply_filters('retainful_abandoned_cart_api_url', $this->abandoned_cart_api_url);
}

/**
* Validate API Key
* @param $api_key
* @param $body
* @return bool|array
*/
function validateApi($api_key, $body)
function validateApi($api_key, $params, $connection_type = 'enable')
{
$url = $this->getDomain() . 'app/' . $api_key;
$body = array(
'shop' => $body
);
if (is_array($body) || is_object($body)) {
$body = json_encode($body);
if(!is_string($api_key) || empty($api_key) || !is_array($params) || !is_string($connection_type) || !in_array($connection_type,array('enable','disable'))){
return null;
}
$url = trim($this->getDomain(),'/').'/app/'.$api_key.'/'.$connection_type;
if(!empty($params)){
$scheme = wc_site_is_https() ? 'https' : 'http';
$default_params = array(
'id' => null,
'secret_key' => '',
'zip' => get_option('woocommerce_store_postcode', NULL),
'city' => get_option('woocommerce_store_city', NULL),
'name' => get_option('blogname'),
'email' => get_option('admin_email'),
'domain' => get_home_url(null, null, $scheme),
'country' => null,
'address1' => get_option('woocommerce_store_address', NULL),
'address2' => get_option('woocommerce_store_address_2', NULL),
'currency' => '',
'timezone' => !empty(get_option('timezone_string')) ? get_option('timezone_string') : get_option('gmt_offset'),
'force_ssl' => (get_option('woocommerce_force_ssl_checkout', 'no') == 'yes'),
'weight_unit' => get_option('woocommerce_weight_unit'),
'country_code' => '',
'province_code' => '',
'primary_locale' => '',
'woocommerce_app_id' => '',
'enabled_presentment_currencies' => ''
);
$params = array_merge($default_params, $params);
}
$headers = array(
'app_id' => $api_key,
'Content-Type' => 'application/json'
);
$response = $this->request($url, array(), 'post', $body, $headers);
//$response = $this->request($this->domain . 'app/' . $api_key);
$response = $this->request($url, array(), 'post', json_encode($params), $headers);
if (isset($response->success) && $response->success) {
return $this->getPlanDetails($response);
} else {
return isset($response->message) ? $response->message : NULL;
}
return isset($response->message) ? $response->message : NULL;
}

/**
Expand Down Expand Up @@ -114,6 +128,7 @@ function request($url, $fields = array(), $method = 'get', $body = '', $headers
'headers' => $headers
);
$result = wp_remote_post($url, $args);

} else {
$result = \Requests::post($url, $headers, $body);
}
Expand Down