Files
interblue.pl/modules/imoje/controllers/front/paymentblik.php
2024-10-25 14:16:28 +02:00

119 lines
3.3 KiB
PHP

<?php
use Imoje\Payment\Util;
/**
* Class IMojePaymentblikModuleFrontController
*
* @property bool display_column_left
* @property bool display_column_right
*/
class IMojePaymentblikModuleFrontController extends ModuleFrontController
{
/**
* Initialize controller
*
* @see FrontController::init()
*/
public function init()
{
$this->display_column_left = false;
$this->display_column_right = false;
parent::init();
}
/**
* @throws PrestaShopException
* @throws Exception
*/
public function initContent()
{
parent::initContent();
$cart = $this->context->cart;
if(!$cart->date_upd) {
Tools::redirect('/');
return;
}
$curlInit = curl_init('https://api.imoje.pl/v1/merchant/' . Configuration::get('IMOJE_MERCHANT_ID') . '/transaction');
curl_setopt($curlInit, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curlInit, CURLOPT_POSTFIELDS, $this->getDataForRequestToApi($cart, 'blik', 'blik'));
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlInit, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curlInit, CURLOPT_TIMEOUT, 10);
curl_setopt($curlInit, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer " . Configuration::get('IMOJE_TOKEN'),
]);
$resultCurl = json_decode(curl_exec($curlInit), true);
if((curl_getinfo($curlInit, CURLINFO_HTTP_CODE) !== 200) || !$resultCurl) {
Tools::redirect($this->context->link->getPageLink('history', true));
return;
}
$resActionContentRaw = [];
parse_str($resultCurl['action']['contentBodyRaw'], $resActionContentRaw);
$this->context->smarty->assign([
'form' => Util::createOrderForm($resActionContentRaw, $this->module->l('Continue', 'payment'), $resultCurl['action']['url']),
'blik_msg' => true,
'checkout_link' => $this->context->link->getPageLink(Configuration::get('PS_ORDER_PROCESS_TYPE')
? 'order-opc'
: 'order'),
'text_return_to_checkout' => $this->module->l('Please wait, you will be returned to checkout.', 'payment'),
]);
$this->setTemplate(IMoje::buildTemplatePath('pay', 'front'));
return;
}
/**
* Prepares data for form that is send to imoje.
*
* @param Cart $cart
*
* @param string $paymentMethod
* @param string $paymentMethodCode
*
* @return false|string
* @throws Exception
*/
private function getDataForRequestToApi($cart, $paymentMethod, $paymentMethodCode)
{
$cartId = $cart->id;
$customer = new Customer(intval($cart->id_customer));
$currencyInfo = Currency::getCurrency($cart->id_currency);
return json_encode([
'type' => 'sale',
'serviceId' => Configuration::get('IMOJE_SERVICE_ID'),
'amount' => Util::convertAmountToFractional(Context::getContext()->cart->getOrderTotal()),
'currency' => $currencyInfo['iso_code'],
'orderId' => (string) $cartId,
'paymentMethod' => $paymentMethod,
'paymentMethodCode' => $paymentMethodCode,
'successReturnUrl' => $this->context->link->getModuleLink('imoje', 'success'),
'failureReturnUrl' => $this->context->link->getModuleLink('imoje', 'failure'),
'clientIp' => $_SERVER['REMOTE_ADDR'],
'customer' => [
'firstName' => $customer->firstname,
'lastName' => $customer->lastname,
'email' => $customer->email,
],
]);
}
}