Initial Commit
This commit is contained in:
130
local/modules/Paypal/Classes/API/PaypalApiCredentials.php
Normal file
130
local/modules/Paypal/Classes/API/PaypalApiCredentials.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Paypal\Classes\API;
|
||||
|
||||
use Paypal\Paypal;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
|
||||
class PaypalApiCredentials
|
||||
{
|
||||
|
||||
/** @var string PayPal API username */
|
||||
protected $apiUsername = null;
|
||||
|
||||
/** @var string PayPal API password */
|
||||
protected $apiPassword = null;
|
||||
|
||||
/** @var string PayPal API signature (Three Token Authentication) */
|
||||
protected $apiSignature = null;
|
||||
|
||||
/**
|
||||
* Create a NVP Credentials
|
||||
*
|
||||
* @param string $user PayPal API username
|
||||
* @param string $password PayPal API password
|
||||
* @param string $signature PayPal API signature (3T)
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct($user = null, $password = null, $signature = null)
|
||||
{
|
||||
if ($user === null && $password === null && $signature === null) {
|
||||
$this->setDefaultCredentials();
|
||||
} else {
|
||||
if (empty($user) || empty($password) || empty($signature)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'PaypalApiCredentials : Missing Argument'
|
||||
);
|
||||
}
|
||||
$this->apiPassword = $password;
|
||||
$this->apiSignature = $signature;
|
||||
$this->apiUsername = $user;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set credentials from database according to SandBox Mode
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function setDefaultCredentials()
|
||||
{
|
||||
$paypalApiManager = new PaypalApiManager();
|
||||
|
||||
if ($paypalApiManager->isModeSandbox()) {
|
||||
$username = Paypal::getConfigValue('sandbox_login', '');
|
||||
$password = Paypal::getConfigValue('sandbox_password', '');
|
||||
$signature = Paypal::getConfigValue('sandbox_signature', '');
|
||||
} else {
|
||||
$username = Paypal::getConfigValue('login', '');
|
||||
$password = Paypal::getConfigValue('password', '');
|
||||
$signature = Paypal::getConfigValue('signature', '');
|
||||
}
|
||||
|
||||
if (empty($username)) {
|
||||
throw new \InvalidArgumentException(Translator::getInstance()->trans('The username option must be set.'));
|
||||
}
|
||||
if (empty($password)) {
|
||||
throw new \InvalidArgumentException(Translator::getInstance()->trans('The password option must be set.'));
|
||||
}
|
||||
if (empty($signature)) {
|
||||
throw new \InvalidArgumentException(Translator::getInstance()->trans('The signature option must be set.'));
|
||||
}
|
||||
|
||||
$this->apiUsername = $username;
|
||||
$this->apiPassword = $password;
|
||||
$this->apiSignature = $signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return API password
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getApiPassword()
|
||||
{
|
||||
return $this->apiPassword;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return API signature
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getApiSignature()
|
||||
{
|
||||
return $this->apiSignature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return API username
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getApiUsername()
|
||||
{
|
||||
return $this->apiUsername;
|
||||
}
|
||||
}
|
||||
92
local/modules/Paypal/Classes/API/PaypalApiLogManager.php
Normal file
92
local/modules/Paypal/Classes/API/PaypalApiLogManager.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Paypal\Classes\API;
|
||||
|
||||
use Thelia\Log\Tlog;
|
||||
|
||||
/**
|
||||
* Class PaypalApiLogManager
|
||||
* This class is the Paypal logger
|
||||
* Logged actions: transaction
|
||||
*/
|
||||
class PaypalApiLogManager
|
||||
{
|
||||
/** @var Tlog $log */
|
||||
protected static $logger;
|
||||
|
||||
/**
|
||||
* Parse and log the return of the Paypal NVP API
|
||||
*
|
||||
* @param string $transaction A special string returned by the NVP API
|
||||
*/
|
||||
public function logTransaction($parsedTransaction)
|
||||
{
|
||||
if ($parsedTransaction) {
|
||||
/*
|
||||
* Then write
|
||||
*/
|
||||
$logLine = '';
|
||||
$date = new \DateTime($parsedTransaction['TIMESTAMP']);
|
||||
|
||||
$logLine .= $date->format('Y-m-d H:i:s') . ' ';
|
||||
$logLine .= 'Transaction ' . $parsedTransaction['ACK'] . ' ';
|
||||
$logLine .= 'correlationId: ' . $parsedTransaction['CORRELATIONID'] . ' ';
|
||||
|
||||
if ($parsedTransaction !== null && array_key_exists('L_ERRORCODE0', $parsedTransaction)) {
|
||||
$logLine .= 'error: ';
|
||||
$logLine .= '[' . $parsedTransaction['L_ERRORCODE0'] . '] ';
|
||||
$logLine .= '<' . $parsedTransaction['L_SHORTMESSAGE0'] . '> ';
|
||||
$logLine .= $parsedTransaction['L_LONGMESSAGE0'] . ' ';
|
||||
}
|
||||
|
||||
$this->getLogger()->info($logLine);
|
||||
} else {
|
||||
$this->getLogger()->info('No transaction was created.');
|
||||
}
|
||||
}
|
||||
|
||||
public static function getLogFilePath()
|
||||
{
|
||||
return THELIA_LOG_DIR . DS . "log-paypal.txt";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Tlog
|
||||
*/
|
||||
public function getLogger()
|
||||
{
|
||||
if (self::$logger == null) {
|
||||
self::$logger = Tlog::getNewInstance();
|
||||
|
||||
$logFilePath = self::getLogFilePath();
|
||||
|
||||
self::$logger->setPrefix("#LEVEL: #DATE #HOUR: ");
|
||||
self::$logger->setDestinations("\\Thelia\\Log\\Destination\\TlogDestinationRotatingFile");
|
||||
self::$logger->setConfig("\\Thelia\\Log\\Destination\\TlogDestinationRotatingFile", 0, $logFilePath);
|
||||
self::$logger->setLevel(Tlog::INFO);
|
||||
}
|
||||
|
||||
return self::$logger;
|
||||
}
|
||||
}
|
||||
199
local/modules/Paypal/Classes/API/PaypalApiManager.php
Normal file
199
local/modules/Paypal/Classes/API/PaypalApiManager.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Paypal\Classes\API;
|
||||
|
||||
use Paypal\Classes\PaypalResources;
|
||||
use Paypal\Classes\vendor\MobileDetect\MobileDetect;
|
||||
use Paypal\Paypal;
|
||||
|
||||
/**
|
||||
* Class PaypalApiManager
|
||||
* Assist in helping managing API
|
||||
*/
|
||||
class PaypalApiManager
|
||||
{
|
||||
/** Live API */
|
||||
const DEFAULT_NVP_3T_API_URL_LIVE = 'https://api-3t.paypal.com/nvp';
|
||||
|
||||
/** SandBox API */
|
||||
const DEFAULT_NVP_3T_API_URL_SANDBOX = 'https://api-3t.sandbox.paypal.com/nvp';
|
||||
|
||||
/** Button Source - Identification code for third-party applications */
|
||||
const BUTTON_SOURCE = 'Thelia_Cart';
|
||||
|
||||
/** API Version */
|
||||
const API_VERSION = '108.0';
|
||||
|
||||
const PAYMENT_TYPE_ORDER = 'Order';
|
||||
const PAYMENT_TYPE_SALE = 'Sale';
|
||||
const PAYMENT_TYPE_AUTHORIZATION = 'Authorization';
|
||||
|
||||
/** @var bool if SandBox mode is enabled or not */
|
||||
protected $isModeSandbox = true;
|
||||
|
||||
protected $config=null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->isModeSandbox = Paypal::isSandboxMode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if SandBox is enabled or not
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isModeSandbox()
|
||||
{
|
||||
return $this->isModeSandbox;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert NVP string to array
|
||||
*
|
||||
* @param string $nvpstr NVP string
|
||||
*
|
||||
* @return array parameters
|
||||
*/
|
||||
public static function nvpToArray($nvpstr)
|
||||
{
|
||||
$paypalResponse = array();
|
||||
parse_str($nvpstr, $paypalResponse);
|
||||
|
||||
$cleanedArray = array();
|
||||
$previousKey = reset($paypalResponse);
|
||||
foreach ($paypalResponse as $key => $value) {
|
||||
if (1 === preg_match('#^([A-Z0-9_]+)$#', $key)) {
|
||||
$cleanedArray[$key] = $value;
|
||||
$previousKey = $key;
|
||||
} else {
|
||||
$cleanedArray[$previousKey] .= '&' . $key . '=' . $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $cleanedArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert array to NVP string
|
||||
*
|
||||
* @param $data
|
||||
* @param \stdClass|null $ret
|
||||
* @param null $construct_scheme
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function arrayToNvp($data, \stdClass $ret = null, $construct_scheme = null)
|
||||
{
|
||||
if ($ret===null) {
|
||||
$ret = new \stdClass();
|
||||
$ret->value="";
|
||||
}
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $key => $value) {
|
||||
self::arrayToNvp($value, $ret, $construct_scheme===null?$key:$construct_scheme."_".$key);
|
||||
}
|
||||
} else {
|
||||
$ret->value .= $construct_scheme."=".$data."&";
|
||||
}
|
||||
|
||||
return substr($ret->value, 0, strlen($ret->value)-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if user is using a mobile
|
||||
* Used in Express Checkout Mobile
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMobile()
|
||||
{
|
||||
$detect = new MobileDetect();
|
||||
|
||||
return $detect->isMobile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return Express checkout URL
|
||||
* Check itself if mobile or not
|
||||
*
|
||||
* @param string $token Paypal API token
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getExpressCheckoutUrl($token)
|
||||
{
|
||||
if ($this->isMobile()) {
|
||||
$cmd = PaypalResources::CMD_EXPRESS_CHECKOUT_MOBILE_KEY;
|
||||
} else {
|
||||
$cmd = PaypalResources::CMD_EXPRESS_CHECKOUT_KEY;
|
||||
}
|
||||
|
||||
return $this->getPaypalUrl() .'?cmd=' . $cmd . '&token=' . $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return relevant PayPal redirect URL
|
||||
* According to SandBox Mode on or not
|
||||
*
|
||||
* @return string URL
|
||||
*/
|
||||
public function getPaypalUrl()
|
||||
{
|
||||
$url = PaypalResources::PAYPAL_REDIRECT_SANDBOX_URL;
|
||||
|
||||
if (!$this->isModeSandbox()) {
|
||||
$url = PaypalResources::PAYPAL_REDIRECT_NORMAL_URL;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert float into NVP number
|
||||
*
|
||||
* @param string $number number
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function convertFloatToNvpFormat($number)
|
||||
{
|
||||
return number_format($number, 2, '.', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return API Url (sandbox or live)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getApiUrl()
|
||||
{
|
||||
$url = self::DEFAULT_NVP_3T_API_URL_SANDBOX;
|
||||
if (!$this->isModeSandbox) {
|
||||
$url = self::DEFAULT_NVP_3T_API_URL_LIVE;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user