bebeboutik/modules/trustedshopsbbb/lib/TrustedShopsAPI.php

173 lines
6.2 KiB
PHP
Raw Normal View History

2017-02-02 13:00:59 +01:00
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6626 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class TrustedShopsAPI
{
/**
* Saved errors messages.
* @var array
*/
public $errors = array();
/**
* Saved logs messages.
* @var array
*/
public $logs = array();
/**
* API Response.
* @var array
*/
public $response;
const TRUSTED_SHOP_STATUS_OK = "SUCCESS";
const TRUSTED_SHOP_API_URL = "api.trustedshops.com/rest";
const TRUSTED_SHOP_REVIEW_COLLECTOR_PATH = "/restricted/v2/shops/{tsId}/reviewcollector";
const TRUSTED_SHOP_TOKEN = "{tsId}";
const TRUSTED_SHOP_CODE_OK = 200;
const TS_CALL_REVIEW_COLLECTOR = 'review_collector';
private $url = "";
private $tsid;
private $tsuser;
private $tspassword;
/**
* DOC : https://api.trustedshops.com/documentation/restricted/#!/review_collector/submitReviewCollectorRequest
* POST /restricted/v2/shops/{tsId}/reviewcollector
*/
public function __construct($options=array())
{
if(count($options)!=3){
$this->errors[] = 'Missing parameters';
$this->errors[] = 'Expecting $opt = array(';
$this->errors[] = ' \'tsid\' => "ID",';
$this->errors[] = ' \'tsuser\' => "USER",';
$this->errors[] = ' \'tspassword\' => "PASSWORD",';
$this->errors[] = ');';
}
$this->url = self::TRUSTED_SHOP_API_URL;
$options_keys = array_keys($options);
if(in_array('tsid',$options_keys)){
$this->tsid = $options['tsid'];
}else{
$this->errors[] = 'Missing parameter tsid';
}
if(in_array('tsuser',$options_keys)){
$this->tsuser = $options['tsuser'];
}else{
$this->errors[] = 'Missing parameter tsuser';
}
if(in_array('tspassword',$options_keys)){
$this->tspassword = $options['tspassword'];
}else{
$this->errors[] = 'Missing parameter tspassword';
}
$this->response = FALSE;
}
public function requestAPI($call, $order, $id_lang)
{
$this->response = FALSE;
if ($call == self::TS_CALL_REVIEW_COLLECTOR) {
$this->url .= str_replace(self::TRUSTED_SHOP_TOKEN, $this->tsid, self::TRUSTED_SHOP_REVIEW_COLLECTOR_PATH);
$reviewCollectorReview = new ReviewCollectorReview($order, $id_lang);
$this->response = $this->createGetRequest($reviewCollectorReview->generateRequest());
$this->response = json_decode($this->response)->response;
} else {
$this->errors[] = 'Connect failed with CURL method';
}
if(count($this->errors) > 0){
if(is_array($order) && count($order) > 0) {
Logger::AddLog("file TrustedShopsAPI.php - " .implode("-", $this->errors), 4, '0000001', 'Order', implode("-", $order));
}else{
Logger::AddLog("file TrustedShopsAPI.php - " .implode("-", $this->errors), 4, '0000001', 'Order', '0');
}
}else{
if (is_object($this->response)
&& $this->response->status == self::TRUSTED_SHOP_STATUS_OK
&& $this->response->code == self::TRUSTED_SHOP_CODE_OK )
{
return true;
}else{
if(is_array($order) && count($order) > 0) {
Logger::AddLog("file TrustedShopsAPI.php - " .implode("-", $this->errors), 4, '0000001', 'Order', implode("-", $order));
}else{
Logger::AddLog("file TrustedShopsAPI.php - " .implode("-", $this->errors), 4, '0000001', 'Order', '0');
}
}
}
}
private function createGetRequest($json_content)
{
$ch = @curl_init();
if (!$ch) {
$this->errors[] = 'Connect failed with CURL method';
} else {
$this->logs[] = 'Connect with CURL method successful';
$this->logs[] = '<b>' . 'Sending this params:' . '</b>';
$this->logs[] = $json_content;
$this->logs[] = $this->url;
@curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
@curl_setopt($ch, CURLOPT_URL, 'https://' . $this->url);
@curl_setopt($ch, CURLOPT_POST, TRUE);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $json_content);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
@curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
@curl_setopt($ch, CURLOPT_HEADER, FALSE);
@curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
@curl_setopt($ch, CURLOPT_TIMEOUT, 60);
@curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
@curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
@curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
@curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
@curl_setopt($ch, CURLOPT_USERPWD, $this->tsuser.":".$this->tspassword);
$result = @curl_exec($ch);
if (!$result) {
$this->errors[] = 'Send with CURL method failed ! Error:' . ' ' . curl_error($ch);
} else {
$this->logs[] = 'Send with CURL method successful';
}
@curl_close($ch);
return $result;
}
}
}