Files
kupshop/class/payments/class.Quatro.php
2025-08-02 16:30:27 +02:00

176 lines
5.5 KiB
PHP

<?php
use Firebase\JWT\JWT;
use KupShop\KupShopBundle\Config;
/**
* Dependencies: `composer require firebase/php-jwt=^5.4.0`
* Class Quatro.
*/
class Quatro extends Payment
{
public static $name = 'Quatro';
public $class = 'Quatro';
protected $pay_method = Payment::METHOD_INSTALLMENTS;
protected $templateOrderView = 'payment.Quatro.orderView.tpl';
public function getCalcUrl(Decimal $price): ?string
{
$price = roundPrice($price, -1, 'DB', 0)->asInteger();
if (empty($this->config['seller']) || $price > 10000 || $price < 100) {
return null;
}
return "https://quatro.vub.sk/kalkulacka/{$this->config['seller']}?cenaTovaru={$price}";
}
public function getGatewayUrl(): ?string
{
if (empty($this->config['seller'])) {
return null;
}
return "https://quatroapi.vub.sk/stores/{$this->config['seller']}/create-application";
}
public function processStep_1()
{
}
public function processStep_2()
{
// hack protože natvrdo lepěj ? ke callbacku
$cn = str_replace('?cn=', '', getVal('h'));
$id = getVal('id');
$state = getVal('state');
$sign = getVal('hmacSign');
if (hash_hmac('sha1', "cn={$cn}&id={$id}&state={$state}", base64_decode($this->config['key'])) != strtolower($sign) && !isDevelopment()) {
throw new \KupShop\OrderingBundle\Exception\PaymentException('Chyba ověření podpisu');
}
$remainingPayment = roundPrice($this->order->getRemainingPayment())->asFloat();
if ($remainingPayment > 0.00) {
if (!$this->getPendingPayment()) {
$this->createPayment(
$id,
$remainingPayment,
['paymentClass' => self::class]
);
}
if (getVal('state') == 'signed') {
$paymentStatus = Payment::STATUS_FINISHED;
} elseif (getVal('state') == 'canceled') {
$paymentStatus = Payment::STATUS_STORNO;
} else {
$paymentStatus = Payment::STATUS_PENDING;
}
// change payment status
if (!$this->setStatus($paymentStatus, $id)) {
logError(__FILE__, __LINE__, 'Payment::updatePaymentStatus: setStatus failed!');
throw new \Exception('Set status failed');
}
}
}
protected function getSubject()
{
$subject = '';
foreach ($this->order->fetchItems() as $item) {
if (!$item['id_product']) {
continue;
}
/** @var Product $product */
$product = $item['product'];
$subject .= "{$product->fetchSections()[0]->getName()} - {$product->fetchProducer()['name']} - {$product->title},";
}
$subject = substr($subject, 0, -1);
if (strlen($subject) > 250) {
$subject = substr($subject, 0, 247).'...';
}
return $subject;
}
public function getPayload()
{
$payload = [
'application' => [
'orderNumber' => $this->order->order_no,
'applicant' => [
'firstName' => $this->order->invoice_name,
'lastName' => $this->order->invoice_surname,
'email' => $this->order->invoice_email,
'mobile' => $this->order->invoice_phone,
'permanentAddress' => [
'addressLine' => $this->order->invoice_street,
'city' => $this->order->invoice_city,
'zipCode' => $this->order->invoice_zip,
'country' => $this->order->invoice_country,
],
],
'subject' => $this->getSubject(),
'totalAmount' => $this->order->total_price->asFloat(),
'goodsAction' => null,
'callback' => $this->getGenericPaymentUrl(2, ['h' => '']),
],
'iat' => time(),
];
$jwt = JWT::encode($payload, base64_decode($this->config['key']), 'HS256');
return $jwt;
}
// https://www.kupshop.local/platby/Quatro/1/49698/?cf=2171114784fcbceb29f9b6bdc6f07e48&h=?cn=1000018425&id=0514186c-3eb6-4150-ac31-048eb330507d&state=canceled&hmacSign=95F512D8D7F14A02376A78CE94382A3F2301DA5E
public function accept($totalPrice, $freeDelivery)
{
$totalPrice = $totalPrice->getPriceWithVat()->asFloat();
if ($totalPrice <= 0 && $this->order) {
$totalPrice = $this->order->total_price;
}
return parent::accept($totalPrice, $freeDelivery) && $totalPrice >= 100 && $totalPrice <= 10000;
}
public static function getSettingsConfiguration(): array
{
return [
'fields' => [
'key' => [
'title' => 'Bezpečnostní klíč',
'type' => 'text',
],
'seller' => [
'title' => 'Kód prodejny',
'type' => 'text',
],
],
];
}
public function startPayment()
{
return false;
}
public function hasOnlinePayment()
{
return true;
}
public static function isEnabled($className)
{
$cfg = Config::get();
if (empty($cfg['Modules']['payments'][$className])) {
return false;
}
return true;
}
}