<?php
namespace PHPFUI\PayPal;
class Checkout extends \PHPFUI\HTML5Element
{
use \PHPFUI\Traits\Page;
private array $functions = [];
private array $options = [];
private array $styles = [
'layout' => 'vertical',
'size' => 'responsive',
'shape' => 'pill',
'color' => 'gold',
'label' => 'checkout',
];
public function __construct(protected \PHPFUI\Interfaces\Page $page, string $clientId)
{
parent::__construct('div');
$this->options['client-id'] = $clientId;
}
public function addOption(string $option, string $value) : static
{
$this->options[$option] = $value;
return $this;
}
public function addStyle(string $style, ?string $value = null) : static
{
if (null === $value)
{
unset($this->styles[$style]);
}
else
{
$this->styles[$style] = $value;
}
return $this;
}
public function getStyle() : array
{
return $this->styles;
}
public function setFunctionJavaScript(string $function, string $js) : static
{
$this->functions[$function] = $js;
return $this;
}
public function setStyles(array $styles) : static
{
$this->styles = $styles;
return $this;
}
protected function getStart() : string
{
$this->page->addHeadScript('https://www.paypal.com/sdk/js?' . \http_build_query($this->options));
$id = $this->getId();
$js = 'paypal.Buttons({style:' . \PHPFUI\TextHelper::arrayToJS($this->styles, "'");
foreach ($this->functions as $function => $javaScript)
{
$js .= ",{$function}:function(data,actions){" . $javaScript . "}\n";
}
$js .= "\n}).render('#{$id}')";
$this->page->addJavaScript($js);
return parent::getStart();
}
}