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

169 lines
5.5 KiB
PHP

<?php
use KupShop\KupShopBundle\Config;
/**
* Requires composer package "mattiabasone/pagonline: ^1.0".
*/
class PagOnline extends Payment
{
public static $name = 'PagOnline Imprese platební brána';
public $template = 'payment.OmnipayNestpay.tpl';
public $class = 'PagOnline';
protected $pay_method = Payment::METHOD_ONLINE;
public function getPaymentUrl($step = 1)
{
return createScriptURL([
's' => 'payment',
'IDo' => $this->order->id,
'cf' => $this->order->getSecurityCode(),
'step' => $step,
'class' => $this->class,
'paymentId' => $this->paymentId,
'absolute' => true,
]);
}
private function getPayment(bool $new = false)
{
$kupshopPayment = $this->getPendingPayment();
if ($kupshopPayment && $new) {
$status = Payment::STATUS_UNKNOWN;
sqlQuery('UPDATE '.getTableName('order_payments')." SET status={$status}, date=NOW() WHERE id={$kupshopPayment['id']}");
$kupshopPayment = false;
}
if ($kupshopPayment && !$new) {
return $kupshopPayment;
} else {
$this->createPayment(
null,
$amount = roundPrice($this->order->getRemainingPayment())->asFloat(),
['paymentClass' => self::class]
);
$paymentRow = $this->selectSQL('order_payments', ['id' => $this->paymentId])->fetch();
$json = json_decode($paymentRow['payment_data'], true);
$json['session'] = $this->paymentId;
$json['shopID'] = $this->order->id.'_'.$paymentRow['id'].'_'.uniqid();
$paymentRow['payment_data'] = json_encode($json);
$this->updateSQL('order_payments', $paymentRow, ['id' => $this->paymentId]);
return $this->getPendingPayment();
}
}
public function savePaymentData(array $data, array $payment)
{
$paymentRow = $this->selectSQL('order_payments', ['id' => $payment['id']])->fetch();
$json = json_decode($paymentRow['payment_data'], true);
$json = array_merge($json, $data);
$paymentRow['payment_data'] = json_encode($json);
$this->updateSQL('order_payments', $paymentRow, ['id' => $payment['id']]);
}
/* Payment steps */
public function processStep_1()
{
$merchangConfig = $this->getMerchantConfig();
$amount = roundPrice($this->order->getRemainingPayment())->asFloat();
if ($amount <= (float) 0) {
$this->step(-3, 'storno');
}
$payment = $this->getPayment(true);
// Initialize the payment page
// See https://github.com/mattiabasone/PagOnline
$init = new \PagOnline\Init\IgfsCgInit();
$init->serverURL = $merchangConfig['url'];
$init->tid = $merchangConfig['tid'];
$init->kSig = $merchangConfig['kSig'];
$init->shopID = $payment['decoded_data']->shopID;
$init->shopUserRef = $this->order->invoice_email;
$init->shopUserName = $this->order->invoice_surname.','.$this->order->invoice_name;
$init->trType = 'AUTH';
$init->currencyCode = 'EUR';
$init->amount = (int) ($amount * 100); // Amount without comma (500 = 5,00)
$init->langID = 'IT';
$init->notifyURL = $this->getPaymentUrl(2);
$init->errorURL = $this->getPaymentUrl(-3);
// $init->addInfo1 = 'myFirstAddintionalInfo';
if (!$init->execute()) {
// Something went wrong, save error
$this->savePaymentData([
'error' => $init->errorDesc,
'rc' => $init->rc,
], $payment);
$this->step(-3, 'storno');
} else {
// save PagOnline PaymentID
$this->savePaymentData(['session' => $init->paymentID], $payment);
// Redirect user to payment gateway
redirection($init->redirectURL);
}
}
public function processStep_2()
{
$merchangConfig = $this->getMerchantConfig();
$payment = $this->getPayment();
$verify = new \PagOnline\Init\IgfsCgVerify();
$verify->setRequestTimeout(15);
$verify->serverURL = $merchangConfig['url'];
$verify->tid = $merchangConfig['tid']; // per servizio MyBank usare UNI_MYBK
$verify->kSig = $merchangConfig['kSig'];
$verify->shopID = $payment['decoded_data']->shopID;
$verify->paymentID = $payment['decoded_data']->session;
if (!$verify->execute()) {
// save error
$this->savePaymentData([
'error' => $verify->errorDesc,
'rc' => $verify->rc,
], $payment);
$this->step(-3, 'storno');
return;
}
if (!$this->setStatus(Payment::STATUS_FINISHED, $verify->paymentID)) {
throw new Exception('PagOnline::updatePaymentStatus: setStatus failed!');
}
$this->success(translate('paymentSuccess', 'payment'));
}
public function hasOnlinePayment()
{
return true;
}
public static function isEnabled($className)
{
$cfg = Config::get();
if (empty($cfg['Modules']['payments'][$className])) {
return false;
}
return true;
}
protected function getMerchantConfig(): array
{
$defaultTestConfig = [
'url' => 'https://testeps.netswgroup.it/UNI_CG_SERVICES/services',
'tid' => 'UNI_ECOM',
'kSig' => 'UNI_TESTKEY',
];
return array_merge($defaultTestConfig, $this->config);
}
}