* @copyright PayPal * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ namespace PaypalAddons\classes\InstallmentBanner; use \Context; use \Module; use PaypalAddons\classes\AbstractMethodPaypal; use \Configuration; use PaypalAddons\classes\InstallmentBanner\ConfigurationMap; class Banner { /** @var \PayPal*/ protected $module; /** @var string*/ protected $placement; /** @var string*/ protected $layout; /** @var float*/ protected $amount; /** @var string*/ protected $template; /** @var array*/ protected $jsVars; /** @var array*/ protected $tplVars; /** @var AbstractMethodPaypal*/ protected $method; /** @var string*/ protected $pageTypeAttribute; public function __construct() { $this->module = Module::getInstanceByName('paypal'); $this->setTemplate(_PS_MODULE_DIR_ . $this->module->name . '/views/templates/installmentBanner/banner.tpl'); $this->method = AbstractMethodPaypal::load(); } public function render() { return Context::getContext()->smarty ->assign('JSvars', $this->getJsVars()) ->assign($this->getTplVars()) ->assign('JSscripts', $this->getJS()) ->fetch($this->getTemplate()); } protected function getJsVars() { $vars = []; if ((int)Configuration::get(ConfigurationMap::ADVANCED_OPTIONS_INSTALLMENT)) { $vars['color'] = Configuration::get(ConfigurationMap::COLOR); } else { $vars['color'] = ConfigurationMap::COLOR_GRAY; } $vars['placement'] = $this->getPlacement(); $vars['layout'] = $this->getLayout(); if ($this->getAmount()) { $vars['amount'] = $this->getAmount(); } if (empty($this->jsVars) === false) { foreach ($this->jsVars as $name => $value) { $vars[$name] = $value; } } return $vars; } protected function getJS() { $js = [ 'tot-paypal-sdk-messages' => [ 'src' => $this->getPaypalSdkLib(), 'data-namespace' => 'totPaypalSdk', 'data-page-type' => $this->getPageTypeAttribute(), 'enable-funding' => 'paylater' ] ]; if (false === defined('_PS_ADMIN_DIR_')) { $js['tot-paypal-sdk-messages']['data-partner-attribution-id'] = $this->getPartnerId(); } return $js; } /** * @return string */ public function getPlacement() { return $this->placement ? $this->placement : 'home'; } /** * @return self */ public function setPlacement($placement) { $this->placement = (string)$placement; return $this; } /** * @return float */ public function getAmount() { return (float)$this->amount; } /** * @param float $amount * @return Banner */ public function setAmount($amount) { $this->amount = (float)$amount; return $this; } /** * @return string */ public function getLayout() { return $this->layout ? $this->layout : 'flex'; } /** * @param string $layout * @return Banner */ public function setLayout($layout) { $this->layout = (string)$layout ; return $this; } /** * @return string */ public function getTemplate() { return $this->template; } /** * @param string $template * @return Banner */ public function setTemplate($template) { $this->template = $template; return $this; } /** * @param string $name * @param mixed $value * @return Banner */ public function addJsVar($name, $value) { if (is_array($this->jsVars) === false) { $this->jsVars = []; } $this->jsVars[$name] = $value; return $this; } /** * @return array */ protected function getTplVars() { if (is_array($this->tplVars)) { return $this->tplVars; } return []; } /** * @param string $name * @param mixed $value * @return Banner */ public function addTplVar($name, $value) { if (is_array($this->tplVars) === false) { $this->tplVars = []; } $this->tplVars[$name] = $value; return $this; } /** * @return string */ protected function getPaypalSdkLib() { $params = [ 'client-id' => $this->method->getClientId(), 'components' => 'messages' ]; return 'https://www.paypal.com/sdk/js?' . http_build_query($params); } /** * @return string */ public function getPageTypeAttribute() { return (string) $this->pageTypeAttribute; } /** * @return Banner */ public function setPageTypeAttribute($pageTypeAttribute) { if (is_string($pageTypeAttribute)) { $this->pageTypeAttribute = $pageTypeAttribute; } return $this; } public function getPartnerId() { return 'PRESTASHOP_Cart_SPB'; } }