Files
2025-03-12 17:06:23 +01:00

243 lines
8.5 KiB
PHP

<?php
class appBlueMediaFrontendActions extends stActions
{
public function executeGatewayList()
{
$gateways = array();
$this->smarty = new stSmarty($this->getModuleName());
$config = stConfig::getInstance('appBlueMedia');
$available = $config->get('gateways');
$totalAmount = $this->getUser()->getBasket()->getTotalAmount(true, false);
foreach (appBlueMedia::getInstance()->getGatewayList() as $id => $gateway)
{
if (isset($available[$id]) && ($id != 700 || $totalAmount > 200))
{
$gateways[$id] = $gateway;
}
}
$this->smarty->assign('gateways', $gateways);
}
public function executeBlik()
{
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
$code = $this->getRequestParameter('code');
$this->getUser()->setAttribute('code', $code, 'soteshop/appBlueMediaPlugin');
sfLoader::loadHelpers(array('Helper', 'stUrl'));
return $this->redirect('@stPaymentPay?id='.$this->getRequestParameter('order_id').'&hash_code='.$this->getRequestParameter('hash'));
}
return $this->forward404();
}
public function executeReturn()
{
if (!$this->hasRequestParameter('blik'))
{
$api = appBlueMedia::getInstance();
$params = array(
'ServiceID' => $this->getRequestParameter('ServiceID'),
'OrderID' => $this->getRequestParameter('OrderID'),
'Hash' => $this->getRequestParameter('Hash'),
);
$this->log(array(
'Client returned from payment service with payload',
$params,
));
if ($api->verifyHash($params['Hash'], $params))
{
$order = OrderPeer::retrieveByPK($params['OrderID']);
if (!$order)
{
$this->log("Order {$params['OrderID']} does not exist", stPayment::LOG_FATAL);
return $this->forward('appBlueMediaFrontend', 'returnFail');
}
$this->smarty = new stSmarty($this->getModuleName());
}
else
{
$this->log("Security hash verification failed", stPayment::LOG_ERROR);
return $this->forward('appBlueMediaFrontend', 'returnFail');
}
}
$this->smarty = new stSmarty($this->getModuleName());
}
public function executeItn()
{
$data = base64_decode($this->getRequestParameter('transactions'));
$api = appBlueMedia::getInstance();
$this->log(array(
"ITN with payload",
$data,
));
if ($this->getRequestParameter('hash') != appBlueMedia::getPostSecureHash())
{
$this->log(array("Security hash verification failed", $this->getRequestParameter('hash'), "!=", appBlueMedia::getPostSecureHash()), stPayment::LOG_ERROR);
}
elseif ($data)
{
$ok = true;
$transaction = $api->readNotifyRequest($data);
if ($transaction)
{
if ($api->verifyHash($transaction['Hash'], $transaction))
{
if ($transaction['PaymentStatus'] == 'SUCCESS')
{
$order = OrderPeer::retrieveByPK($transaction['OrderID']);
if ($order)
{
$payment = $order->getOrderPayment();
if ($payment)
{
$payment->setStatus(true);
$payment->save();
$this->log("OrderID {$transaction['OrderID']} has been paid successfully");
}
else
{
$this->log("Payment for OrderID {$transaction['OrderID']} does not exist", stPayment::LOG_FATAL);
$ok = false;
}
}
else
{
$this->log("OrderID {$transaction['OrderID']} does not exist", stPayment::LOG_FATAL);
$ok = false;
}
}
elseif ($transaction['paymentStatus'] == 'FAILURE')
{
$this->log("OrderID {$transaction['OrderID']} payment failure: ".$transaction['paymentStatusDetails'], stPayment::LOG_ERROR);
$ok = false;
}
}
else
{
$this->log("Transaction hash verification failure", stPayment::LOG_ERROR);
$ok = false;
}
}
else
{
$this->log("Couldn't parse the request", stPayment::LOG_FATAL);
$ok = false;
}
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setContentType('application/xml');
$response = array(
'ServiceID' => $transaction['ServiceID'],
'OrderID' => $transaction['OrderID'],
'Status' => $ok ? 'CONFIRMED' : 'NOTCONFIRMED',
);
return $this->renderText($api->returnNotifyStatus($response));
}
return $this->renderText('OK');
}
public function executeProcessPayment()
{
$this->smarty = new stSmarty($this->getModuleName());
$api = new appBlueMedia();
$order = OrderPeer::retrieveByIdAndHashCode($this->getRequestParameter('id'), $this->getRequestParameter('hash'));
sfLoader::loadHelpers(array('Helper', 'stUrl'));
try
{
$this->log('Creating payment');
$response = $api->createPayment($order, $api->isBlik($order) ? array('AuthorizationCode' => $this->getUser()->getAttribute('code', null, 'soteshop/appBlueMediaPlugin')) : array());
if (isset($response['confirmation']))
{
if ($response['confirmation'] == 'NOTCONFIRMED' || $response['paymentStatus'] == 'FAILURE')
{
$this->log(array('Failed with response', $response), stPayment::LOG_ERROR);
return $this->renderJSON(array('redirect' => st_url_for('@appBlueMediaFrontend?action=returnFail&blik='.$api->isBlik($order).'&order_id='.$order->getId().'&hash='.$order->getHashCode())));
}
$this->getUser()->setAttribute('code', null, 'soteshop/appBlueMediaPlugin');
$this->log(array('Success with response', $response));
return $this->renderJSON(array('redirect' => st_url_for('@appBlueMediaFrontend?action=return&blik='.$api->isBlik($order).'&order_id='.$order->getId().'&hash='.$order->getHashCode())));
}
elseif (isset($response['redirecturl']))
{
$this->log(array('Success redirecting to', $response['redirecturl']));
return $this->renderJSON(array('redirect' => $response['redirecturl']));
}
else
{
$this->log(array('Failed with response', $response), stPayment::LOG_ERROR);
}
}
catch (Exception $e)
{
}
return $this->renderJSON(array('redirect' => st_url_for('@appBlueMediaFrontend?action=returnFail')));
}
/**
* Negatywny powrót z płatności
*/
public function executeReturnFail()
{
$this->smarty = new stSmarty($this->getModuleName());
$webpage = WebpagePeer::retrieveByState('CONTACT');
if ($webpage)
{
sfLoader::loadHelpers(array('Helper', 'stUrl'));
$this->smarty->assign('contact_url', st_url_for('stWebpageFrontend/index?url='.$webpage->getFriendlyUrl()));
}
$this->smarty->assign('blik', $this->getRequestParameter('blik') ? array(
'url' => st_url_for('@appBlueMediaFrontend?action=blik&order_id='.$this->getRequestParameter('order_id').'&hash='.$this->getRequestParameter('hash')),
'code' => $this->getUser()->getAttribute('code', null, 'soteshop/appBlueMediaPlugin'),
) : false);
}
public function log($message, $type = stPayment::LOG_INFO)
{
stPayment::log("bluemedia", $message, $type);
}
}