Files
2021-09-17 12:41:45 +02:00

148 lines
4.2 KiB
PHP

<?php
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Api\WebProfile;
use PayPal\Api\Presentation;
use PayPal\Api\InputFields;
class App_PayPal_PayPalApi
{
const LOGO_URL_IMAGE = "";
const WEBPROFILE_NAME = '';
//Parametri test dell'account SANDBOX info-facilitator@adriaticasistemi.it
const TEST_CLIENT_ID = '';
const TEST_CLIENT_SECRET = '';
//Parametri per l'account LIVE info@adriaticasistemi.it
const CLIENT_ID = '';
const CLIENT_SECRET = '';
protected $_configs;
protected $_ipManager;
protected $_apiContext;
protected $_apiPayPalTestAccount;
//Parametri di Default per la chiamata LIVE
protected $_apiMode = "live";
protected $_clientId = self::CLIENT_ID;
protected $_clientSecret = self::CLIENT_SECRET;
//Parametri per il LOG di Default per la chiamata LIVE
protected $_logEnabled = false;
protected $_logFileName = APPLICATION_PATH . '/../log/PayPal.log';
protected $_logLevel = "INFO";
public function __construct()
{
$this->_configs = new App_Service_ConfigManager();
$this->_ipManager = new App_Service_IpManager();
//La modalita Test o Sandbox viene impostata dal parametro delle configurazioni e solo se la chiamata avviene da un IP abilitato
$this->_apiPayPalTestAccount = false;
if($this->_ipManager->allowedDevMode()){
$this->_apiPayPalTestAccount = ($this->_configs->get('interrutore_paypal_test') == 'on') ? 1:0;
}
if($this->_apiPayPalTestAccount){
$this->_apiMode = "sandbox";
$this->_logLevel = "DEBUG";
$this->_clientId = self::TEST_CLIENT_ID;
$this->_clientSecret = self::TEST_CLIENT_SECRET;
}
//Controlla se il log è abilitato che esista anche il file su cui salvare altrimenti lo disabilita
if($this->_logEnabled)
$this->_logEnabled = file_exists($this->_logFileName);
$this->_apiContext = $this->setApiContext($this->_clientId, $this->_clientSecret);
}
public function getPayPalContext()
{
return $this->_apiContext;
}
public function getWebProfile(){
$webProfile = new WebProfile();
try {
$webProfileList = $webProfile->get_list($this->_apiContext);
} catch (Exception $ex) {
return null;
}
//Controlla se gia esiste un web profile con il nome assegnato
if($webProfileList){
foreach($webProfileList as $profile){
if($profile->name == self::WEBPROFILE_NAME){
$webProfile = $profile;
return $webProfile;
}
}
}
//Altrimenti lo creo con questi parametri specifici
$webProfile->setName(self::WEBPROFILE_NAME);
$webProfile->setTemporary(false);
$presentation = new Presentation();
$presentation->setLogoImage(self::LOGO_URL_IMAGE);
$webProfile->setPresentation($presentation);
$inputFields = new InputFields();
$inputFields->setNoShipping(1);
$webProfile->setInputFields($inputFields);
try {
$webProfile = $webProfile->create($this->_apiContext);
} catch (Exception $ex) {
return null;
}
return $webProfile;
}
public function deleteWebProfile(){
$webProfile = $this->getWebProfile();
//Elimina questo web profile
try {
$webProfileRes = $webProfile->delete($this->_apiContext);
} catch (Exception $ex) {
exit(1);
}
ResultPrinter::printResult("", "WebProfile", "", $webProfile, $webProfileRes);
return null;
}
private function setApiContext($clientId, $clientSecret)
{
$apiContext = new ApiContext(
new OAuthTokenCredential(
$clientId,
$clientSecret
)
);
$apiContext->setConfig(
array(
'mode' => $this->_apiMode,
'log.LogEnabled' => $this->_logEnabled,
'log.FileName' => $this->_logFileName,
'log.LogLevel' => $this->_logLevel,
'cache.enabled' => true
)
);
return $apiContext;
}
}