diff --git a/paynlpaymentmethods/controllers/front/ajax.php b/paynlpaymentmethods/controllers/front/ajax.php new file mode 100644 index 0000000..756f2d6 --- /dev/null +++ b/paynlpaymentmethods/controllers/front/ajax.php @@ -0,0 +1,94 @@ + +* @copyright 2007-2015 PrestaShop SA +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +/** + * @since 1.5.0 + */ +class PaynlPaymentMethodsAjaxModuleFrontController extends ModuleFrontController +{ + + public function initContent() + { + $prestaorderid = Tools::getValue('prestaorderid'); + $amount = Tools::getValue('amount'); + + /** + * @var $module PaynlPaymentMethods + */ + $module = $this->module; + + try { + $order = new Order($prestaorderid); + } catch (Exception $e) { + $module->payLog('Refund', 'Failed trying to refund ' . $amount . ' on ps-orderid ' . $prestaorderid . ' Order not found. Errormessage: ' . $e->getMessage()); + $this->returnResponse(false, 0, 'Could not find order'); + } + + $paymenyArr = $order->getOrderPayments(); + $orderPayment = reset($paymenyArr); + $transactionId = $orderPayment->transaction_id; + + $currencyId = $orderPayment->id_currency; + $currency = new Currency($currencyId); + $strCurrency = $currency->iso_code; + + $cartId = !empty($order->id_cart) ? $order->id_cart : null; + + $module->payLog('Refund', 'Trying to refund ' . $amount . ' ' . $strCurrency . ' on prestashop-orderid ' . $prestaorderid, $cartId, $transactionId); + + $arrRefundResult = $module->doRefund($transactionId, $amount, $strCurrency); + $refundResult = $arrRefundResult['data']; + + if ($arrRefundResult['result']) + { + $arrResult = $refundResult->getData(); + $amountRefunded = !empty($arrResult['amountRefunded']) ? $arrResult['amountRefunded'] : ''; + + $desc = !empty($arrResult['description']) ? $arrResult['description'] : 'empty'; + $module->payLog('Refund', 'Refund success, result message: ' . $desc, $cartId, $transactionId); + + $this->returnResponse(true, $amountRefunded, 'succesfully_refunded ' . $strCurrency . ' ' . $amount); + } else { + $module->payLog('Refund', 'Refund failed: ' . $refundResult, $cartId, $transactionId); + $this->returnResponse(false, 0, 'could_not_process_refund'); + } + + } + + private function returnResponse($result, $amountRefunded = '', $message = '') + { + header('Content-Type: application/json;charset=UTF-8'); + + $returnarray = array( + 'success' => $result, + 'amountrefunded' => $amountRefunded, + 'message' => $message + ); + + die(json_encode($returnarray)); + } + +} diff --git a/paynlpaymentmethods/controllers/front/exchange.php b/paynlpaymentmethods/controllers/front/exchange.php index 0e38dea..35814d5 100644 --- a/paynlpaymentmethods/controllers/front/exchange.php +++ b/paynlpaymentmethods/controllers/front/exchange.php @@ -34,21 +34,22 @@ class PaynlPaymentMethodsExchangeModuleFrontController extends ModuleFrontContro */ public function postProcess() { - if($_REQUEST['action'] == 'pending'){ - die('TRUE| Ignoring pending'); - } + $transactionId = $_REQUEST['order_id']; # PAY. action $action = Tools::getValue('action'); + $cartid = Tools::getValue('extra1'); - $transactionId = $_REQUEST['order_id']; - - $module = $this->module; /** - * @var $module PaynlPaymentMethods - */ + * @var $module PaynlPaymentMethods + */ + $module = $this->module; + + $module->payLog('Exchange', 'Action: ' . $action, $cartid, $transactionId); - $module->payLog('Exchange. Action: ' . $action . '. TransactionId: ' . $transactionId); + if($_REQUEST['action'] == 'pending'){ + die('TRUE| Ignoring pending'); + } try { $message = ''; diff --git a/paynlpaymentmethods/controllers/front/finish.php b/paynlpaymentmethods/controllers/front/finish.php index a3418b4..1e593fc 100644 --- a/paynlpaymentmethods/controllers/front/finish.php +++ b/paynlpaymentmethods/controllers/front/finish.php @@ -49,14 +49,21 @@ public function postProcess() $this->orderStatusId = Tools::getValue('orderStatusId'); $this->paymentSessionId = Tools::getValue('paymentSessionId'); - $module = $this->module; - /** - * @var $module PaynlPaymentMethods - */ + /** + * @var $module PaynlPaymentMethods + */ + $module = $this->module; + try { $transaction = $module->getTransaction($transactionId); + } catch (Exception $e) { + $module->payLog('finishPostProcess', 'Could not retrieve transaction', null, $transactionId); + return; + } - if ($transaction->isPaid() || $transaction->isPending() || $transaction->isBeingVerified() || $transaction->isAuthorized()) { + $module->payLog('finishPostProcess', 'Returning to webshop', $transaction->getExtra1(), $transactionId); + + if ($transaction->isPaid() || $transaction->isPending() || $transaction->isBeingVerified() || $transaction->isAuthorized()) { // naar success /** * @var $cart CartCore @@ -87,13 +94,13 @@ public function postProcess() Tools::redirect('index.php?controller=order-confirmation'.$slow.'&id_cart=' . $cartId . '&id_module=' . $this->module->id . '&id_order=' . $orderId . '&key=' . $customer->secure_key); - } else { - # delete old payment fee - $this->context->cart->deleteProduct(Configuration::get('PAYNL_FEE_PRODUCT_ID'),0); + } else { + # Delete old payment fee + $this->context->cart->deleteProduct(Configuration::get('PAYNL_FEE_PRODUCT_ID'), 0); - // naar checkout - Tools::redirect('index.php?controller=order&step=1'); - } + # To checkout + Tools::redirect('index.php?controller=order&step=1'); + } } diff --git a/paynlpaymentmethods/controllers/front/startPayment.php b/paynlpaymentmethods/controllers/front/startPayment.php index 00c077e..23b2bec 100644 --- a/paynlpaymentmethods/controllers/front/startPayment.php +++ b/paynlpaymentmethods/controllers/front/startPayment.php @@ -54,7 +54,6 @@ public function postProcess() die($this->module->l('This payment method is not available.', 'validation')); } - $extra_data = array(); if(isset($_REQUEST['bank'])){ $extra_data['bank'] = $_REQUEST['bank']; @@ -63,8 +62,8 @@ public function postProcess() $redirectUrl = $this->module->startPayment($cart, $paymentOptionId, $extra_data); Tools::redirect($redirectUrl); } catch (Exception $e){ - - die('Error: '.$e->getMessage()); + $this->module->payLog('postProcess', 'Error startPayment: ' . $e->getMessage(), $cart->id); + die('Error: ' . $e->getMessage()); } diff --git a/paynlpaymentmethods/css/PAY.css b/paynlpaymentmethods/css/PAY.css new file mode 100644 index 0000000..3c71b0b --- /dev/null +++ b/paynlpaymentmethods/css/PAY.css @@ -0,0 +1,59 @@ +.PAY.panel #dialog-info-modal +{ + display:none; +} +.ui-dialog +{ + border:0; + background: none; + background-color: #fff; + color: white; +} +.ui-dialog * +{ + color: #333; +} +.ui-dialog-title +{ + color: #3b3b3b; +} +.ui-dialog-titlebar +{ + background:none; + background-color: #5eba6c; + border:0; + border-radius: 0; +} +.PAY.panel .payOption { + margin-right: 30px; + display: inline-block; + width: 350px; +} +.PAY.panel .payOption div.label { + display: inline-block; + color: #333; +} +.PAY.panel .payOption.disabled { + opacity: 0.3; + cursor: default; +} +.PAY.panel .payOption.disabled * { + cursor: default; +} +.PAY.panel div.payFields div.labelvalue { + display: inline-block; + padding: 4px 7px; + background-color: transparent; + border-radius: 4px; + color: #333; + margin-right: 30px; +} +.PAY.panel div.payFields div.label { + display: inline-block; + padding: 8px; + background-color: #eaeaea; + margin-right: 4px; + margin-left: 0; + border-radius: 4px; + color: #272727; +} \ No newline at end of file diff --git a/paynlpaymentmethods/nl.php b/paynlpaymentmethods/nl.php index ddd32c4..e04e87d 100644 --- a/paynlpaymentmethods/nl.php +++ b/paynlpaymentmethods/nl.php @@ -5,6 +5,22 @@ $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_14cb4d5344b0a3bf1e97aed5419679ba'] = 'PAY.'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_56250a5892b6e8c3deedac6525e1a6fa'] = 'PAY. betaalmethoden voor PrestaShop'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_a02758d758e8bec77a33d7f392eb3f8a'] = 'Er is voor deze module geen valuta ingesteld'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_f7aae2a74abc8a53ff0ddc6808182d2b'] = 'Weet u zeker dat u dit bedrag wil terugbetalen'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_1f7d859f8eacd7176e673032482543ee'] = 'Terugbetalen'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_729a51874fe901b092899e9e8b31c97a'] = 'Weet u het zeker?'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_2b04fe9fdd308d7d099e439e3fbcf528'] = 'Terugbetalen is niet mogelijk'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_4237c6f83fc2696b6ac87ca894607c03'] = 'Bedrag'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_643562a9ae7099c8aabfdc93478db117'] = 'Bezig met verwerken'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_386c339d37e737a436499d423a77df0c'] = 'Valuta'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_9a4c56a1ee5b7ee89b9a5f705c2fda6c'] = 'Orderbedrag'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_a9ced76f2dd6907220fa95b3a136b04a'] = 'Ongeldig bedrag'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_0767b61acf9e2b7c9c346788ff9d778d'] = 'Terugbetaling gelukt'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_27e600a9fa8e36c761aad9cb1ba5a156'] = 'Betaalmethode'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_9b24ac685f7ec44b2895b9d172d22ccc'] = 'Terugbetalen mislukt. Probeer het later nog eens of pas het bedrag aan.'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_76f0ed934de85cc7131910b32ede7714'] = 'Terugbetaling'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_26cd177f2b6765750334ed39a2f4b5b6'] = 'De orderstatus wordt alleen bij een volledige terugbetaling aangepast naar `Terugbetaald`. De voorraad wordt hierbij niet aangepast.'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_b2d37ae1cedf42ff874289b721860af2'] = 'Log-informatie'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_4f6cbea9f2eade99f783747f7cafb211'] = 'Ga hiervoor naar `Geavanceerde instellingen` en open `Logboeken` en filter bij (indien ingeschakeld bij de plugin instellingen) `Bericht` op `PAY.`'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_020c2091aec3019d02193cef080bcf97'] = 'Betaalmethode toeslag'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_9f06b28a40790c4c4df5739bce3c1eb0'] = 'Verzendkosten'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_68bf367e228f45ba83cb8831a5ee6447'] = 'Nederlands'; @@ -13,12 +29,11 @@ $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_4be8e06d27bca7e1828f2fa9a49ca985'] = 'Italiaans'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_ad225f707802ba118c22987186dd38e8'] = 'Frans'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_86bc3115eb4e9873ac96904a4a68e19e'] = 'Duits'; -$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_086247a9b57fde6eefee2a0c4752242d'] = 'Automatisch'; -$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_06224fd1af0863575df3ea430dd98848'] = 'Kan de Pay.nl SDK niet vinden, heb je de source geinstalleerd in plaats van het pakketbestand?'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_72a1a22dadebb68c2b328ce4acb74781'] = 'Taal van de webshop'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_1be25759cbc758a4cd49adfc7979c0e7'] = 'Taal van de browser'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_d262bca42a884db060f5a6667b21205d'] = 'APItoken is verplicht'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_eca1973b20e3b246847fd7f1648c2599'] = 'ServiceId is verplicht'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_c888438d14855d7d96a2724ee9c306bd'] = 'Instellingen opgeslagen'; -$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_8cfa0f8895de4a51e4055e0a4476e274'] = 'PAY. Account Instellingen. Plugin versie %s'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_0a0d3d18ca5a07d08e62a1c962f91946'] = 'APItoken'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_04542edeb8b2c5b3922716ca8ddd0032'] = 'Je kunt je APItoken vinden onderaan op https://admin.pay.nl/my_merchant'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_b1b404a85c213977a7cabb82b730639c'] = 'ServiceId'; @@ -26,16 +41,18 @@ $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_adc1e83ea8057c3b182f292b223612da'] = 'Transactieomschrijving voorvoegsel'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_2ab45b27dbaf728ece860448598238ae'] = 'Een tekst die voor de transactieomschrijving wordt gezet'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_b989ff7da93d9c8344a4c4e765cab502'] = 'Validatievertraging'; -$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_0dad032fbd1b79c430527f7622add693'] = 'Wanneer de betaling is geslaagd, wacht op PAY. om de betaling te valideren voordat de klant wordt doorgestuurd naar de succes pagina'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_00d23a76e43b46dae9ec7aa9dcbebb32'] = 'Ingeschakeld'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_b9f5c797ebbf55adccdd8539a65a0241'] = 'Uitgeschakeld'; +$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_56d40defec5b7682e494af89a7a4d2a9'] = 'Log communicatie en procesinformatie van PAY.'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_4245499695408b974322be6f01b0d17a'] = 'Test modus'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_61142607f22ea506073bcb1c1c74133e'] = 'Start de transacties in sandbox modus om te testen'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_4861c3d5c88af6037e2bfb0d5c309ce1'] = 'Weergave afbeeldingen'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_f7d5170bde3cbe04f1e4c3cb3459dc08'] = 'Laat de afbeeldingen van de betaalmethoden zien in de checkout.'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_3d8bf69f189462bae437c9b50d32f4e1'] = 'Betaalscherm taal'; -$_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_1efea134dc382b9d18ef9ed8a21e178f'] = 'Selecteer de taal waarin het betaalscherm wordt weergegeven, gebruikt automatisch de browser taal'; $_MODULE['<{paynlpaymentmethods}prestashop>paynlpaymentmethods_c9cc8cce247e49bae79f15173ce97354'] = 'Opslaan'; +$_MODULE['<{paynlpaymentmethods}prestashop>startpayment_e2b7dec8fa4b498156dfee6e4c84b156'] = 'Deze betaalmethode is niet beschikbaar'; +$_MODULE['<{paynlpaymentmethods}prestashop>payment_form_ideal_eb45bd2725cb8ac50d0795432a918f86'] = 'Bank'; +$_MODULE['<{paynlpaymentmethods}prestashop>payment_form_ideal_3ee7960dc804545cf3cc91a1fbc155ca'] = 'Kies uw bank a.u.b.'; $_MODULE['<{paynlpaymentmethods}prestashop>admin_paymentmethods_0da8d9a75492046bea7f314521e07cae'] = 'Betaalmethoden'; $_MODULE['<{paynlpaymentmethods}prestashop>admin_paymentmethods_49ee3087348e8d44e1feda1917443987'] = 'Naam'; $_MODULE['<{paynlpaymentmethods}prestashop>admin_paymentmethods_9444baf5ee361fb18f91039baab7df9c'] = 'De naam van de betaalmethode'; @@ -56,6 +73,3 @@ $_MODULE['<{paynlpaymentmethods}prestashop>admin_paymentmethods_6ddb1f21389093a6d6c014a18b680a22'] = 'Betaalkosten als percentage'; $_MODULE['<{paynlpaymentmethods}prestashop>admin_paymentmethods_15552aa94cf4b7239d3746c980512bf1'] = 'Het type toeslag voor deze betaalmethode'; $_MODULE['<{paynlpaymentmethods}prestashop>admin_paymentmethods_fff62f3f9fe367bba8d702ca5ef30c6c'] = 'Waarde van de toeslag'; -$_MODULE['<{paynlpaymentmethods}prestashop>payment_form_ideal_eb45bd2725cb8ac50d0795432a918f86'] = 'Bank'; -$_MODULE['<{paynlpaymentmethods}prestashop>payment_form_ideal_3ee7960dc804545cf3cc91a1fbc155ca'] = 'Kies uw bank a.u.b.'; -$_MODULE['<{paynlpaymentmethods}prestashop>startpayment_e2b7dec8fa4b498156dfee6e4c84b156'] = 'Deze betaalmethode is niet beschikbaar'; diff --git a/paynlpaymentmethods/paynlpaymentmethods.php b/paynlpaymentmethods/paynlpaymentmethods.php index 409ef97..738b2e3 100644 --- a/paynlpaymentmethods/paynlpaymentmethods.php +++ b/paynlpaymentmethods/paynlpaymentmethods.php @@ -31,6 +31,7 @@ } } +use Paynl\Result\Transaction\Refund; use PrestaShop\PrestaShop\Core\Payment\PaymentOption; if (!defined('_PS_VERSION_')) { @@ -52,9 +53,9 @@ public function __construct() { $this->name = 'paynlpaymentmethods'; $this->tab = 'payments_gateways'; - $this->version = '4.2.10'; + $this->version = '4.2.11'; - $this->payLogEnabled = false; + $this->payLogEnabled = null; $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); $this->author = 'PAY.'; $this->controllers = array('startPayment', 'finish', 'exchange'); @@ -77,6 +78,10 @@ public function __construct() $this->warning = $this->l('No currency has been set for this module.'); } + if (!$this->isRegisteredInHook('displayAdminOrder')) { + $this->registerHook('displayAdminOrder'); + } + } public function install() @@ -85,17 +90,139 @@ public function install() if (!parent::install() || !$this->registerHook('paymentOptions') || !$this->registerHook('paymentReturn') + || !$this->registerHook('displayAdminOrder') ) { return false; } - $this->createPaymentFeeProduct(); return true; } - public function createPaymentFeeProduct() + /** + * @param $params + * @return bool|string|void + */ + public function hookDisplayAdminOrder($params) + { + + try { + $cartId = Cart::getCartIdByOrderId((int)$params['id_order']); + $orderId = Order::getIdByCartId($cartId); + $order = new Order($orderId); + } catch (Exception $e) { + return; + } + + # Check if the order is processed by PAY. + if($order->module !== 'paynlpaymentmethods') { + return; + } + + $orderPayments = $order->getOrderPayments(); + $orderPayment = reset($orderPayments); + + $status = 'unavailable'; + $method = $order->payment; + $currency = new Currency($orderPayment->id_currency); + $transactionId = $orderPayment->transaction_id; + + try { + $transaction = $this->getTransaction($transactionId); + $arrTransactionDetails = $transaction->getData(); + $status = $arrTransactionDetails['paymentDetails']['stateName']; + $method = $arrTransactionDetails['paymentDetails']['paymentProfileName']; + $showRefundButton = $transaction->isPaid() || $transaction->isPartiallyRefunded(); + } catch (Exception $exception) { + $showRefundButton = false; + } + + $amountFormatted = number_format($order->total_paid, 2, ',','.'); + + $this->context->controller->addCss($this->_path . 'css/PAY.css'); + $this->context->controller->addJqueryUI('ui.dialog'); + $this->context->controller->addJs($this->_path . 'views/js/PAY.js'); + + $this->context->smarty->assign(array( + 'lang' => $this->getMultiLang(), + 'this_version' => $this->version, + 'PrestaOrderId' => $orderId, + 'amountFormatted' => $amountFormatted, + 'amount' => $order->total_paid, + 'currency' => $currency->iso_code, + 'pay_orderid' => $transactionId, + 'status' => $status, + 'method' => $method, + 'ajaxURL' => $this->context->link->getModuleLink($this->name, 'ajax', array(), true), + 'showRefundButton' => $showRefundButton, + )); + + return $this->display(__FILE__, 'payorder.tpl'); + } + + private function getMultiLang() + { + $lang['title'] = $this->l('PAY.'); + $lang['are_you_sure'] = $this->l('Are you sure want to refund this amount'); + $lang['refund_button'] = $this->l('REFUND'); + $lang['my_text'] = $this->l('Are you sure?'); + $lang['refund_not_possible'] = $this->l('Refund is not possible'); + $lang['amount_to_refund'] = $this->l('Amount to refund'); + $lang['refunding'] = $this->l('Processing'); + $lang['currency'] = $this->l('Currency'); + $lang['amount'] = $this->l('Orderamount'); + $lang['invalidamount'] = $this->l('Invalid amount'); + $lang['succesfully_refunded'] = $this->l('Succesfully refunded'); + $lang['paymentmethod'] = $this->l('Paymentmethod'); + $lang['could_not_process_refund'] = $this->l('Could not process refund. Refund might be too fast or amount is invalid'); + $lang['info_refund_title'] = $this->l('Refund'); + $lang['info_refund_text'] = $this->l('The orderstatus will only change to `Refunded` when the full amount is refunded. Stock wont by updated.'); + $lang['info_log_title'] = $this->l('Logs'); + $lang['info_log_text'] = $this->l('For log information see `Advanced settings` and then `Logs`. Then filter on `PAY.`.'); + + return $lang; + } + + /** + * @param $transactionId + * @param null $amount + * @param string|null $strCurrency + * @return array + */ + public function doRefund($transactionId, $amount = null, $strCurrency = null) + { + try { + $this->sdkLogin(); + $result = true; + $refundResult = \Paynl\Transaction::refund($transactionId, $amount, null, null, $strCurrency); + } catch (Exception $objException) { + $refundResult = $objException->getMessage(); + $result = false; + } + + return array('result' => $result, 'data' => $refundResult); + } + + /** + * Update order status + * + * @param $orderId + * @param $orderState + * @param string $cartId + * @param string $transactionId + */ + public function updateOrderHistory($orderId, $orderState, $cartId = '', $transactionId = '') + { + $this->payLog('updateOrderHistory', 'Update status. orderId: ' . $orderId . '. orderState: ' . $orderState, $cartId, $transactionId); + + $history = new OrderHistory(); + $history->id_order = $orderId; + $history->changeIdOrderState($orderState, $orderId, true); + $history->addWs(); + } + + public function createPaymentFeeProduct() { $id_product = Configuration::get('PAYNL_FEE_PRODUCT_ID'); $feeProduct = new Product(Configuration::get('PAYNL_FEE_PRODUCT_ID'), true); @@ -375,21 +502,22 @@ private function sdkLogin() public function processPayment($transactionId, &$message = null) { $transaction = $this->getTransaction($transactionId); + $arrPayData = $transaction->getData(); - $order_state = $this->statusPending; + $iOrderState = $this->statusPending; if ($transaction->isPaid() || $transaction->isAuthorized()) { - $order_state = $this->statusPaid; + $iOrderState = $this->statusPaid; } elseif ($transaction->isCanceled()) { - $order_state = $this->statusCanceled; + $iOrderState = $this->statusCanceled; } if ($transaction->isRefunded(false)) { - $order_state = $this->statusRefund; + $iOrderState = $this->statusRefund; } /** * @var $orderState OrderStateCore */ - $orderState = new OrderState($order_state); + $orderState = new OrderState($iOrderState); $orderStateName = $orderState->name; if (is_array($orderStateName)) { $orderStateName = array_pop($orderStateName); @@ -397,11 +525,6 @@ public function processPayment($transactionId, &$message = null) $cartId = $transaction->getExtra1(); - $cart = new Cart((int)$cartId); - - /** - * @var $cart CartCore - */ if (version_compare(_PS_VERSION_, '1.7.1.0', '>=')) { $orderId = Order::getIdByCartId($cartId); } else { @@ -409,12 +532,23 @@ public function processPayment($transactionId, &$message = null) $orderId = Order::getOrderByCartId($cartId); } - if ($orderId) { - $order = new Order($orderId); + if ($orderId) + { + $order = new Order($orderId); + + $this->payLog('processPayment', 'orderStateName:' . $orderStateName . '. iOrderState: ' . $iOrderState .'. ' . + 'orderRef:'. $order->reference .'. orderModule:'. $order->module, $cartId, $transactionId); /** * @var $order OrderCore */ + + # Check if the order is processed by PAY. + if($order->module !== 'paynlpaymentmethods') { + $message = 'Not a PAY. order. Customer seemed to used different provider. Not updating the order.'; + return $transaction; + } + if ($order->hasBeenPaid() && !$transaction->isRefunded(false)) { $message = 'Order is already paid | OrderReference: ' . $order->reference; @@ -437,7 +571,7 @@ public function processPayment($transactionId, &$message = null) $orderPayment->order_reference = $order->reference; } - $orderPayment->payment_method = $transaction->getData()['paymentDetails']['paymentProfileName']; + $orderPayment->payment_method = $arrPayData['paymentDetails']['paymentProfileName']; $orderPayment->amount = $transaction->getPaidCurrencyAmount(); @@ -451,44 +585,46 @@ public function processPayment($transactionId, &$message = null) $orderPayment->save(); # In case of banktransfer the total_paid_real isn't set, we're doing that now. - if ($order_state == $this->statusPaid && $order->total_paid_real == 0) { + if ($iOrderState == $this->statusPaid && $order->total_paid_real == 0) { $order->total_paid_real = $orderPayment->amount; $order->save(); } - $history = new OrderHistory(); - - $history->id_order = $order->id; - - $history->changeIdOrderState($order_state, $order->id, true); - $history->addWs(); + $this->updateOrderHistory($order->id, $iOrderState, $cartId, $transactionId); $message = "Updated order (" . $order->reference . ") to: " . $orderStateName; - $this->payLog($message . '. Transaction: ' . $transactionId , $cartId); - } else { - if ($transaction->isPaid() || $transaction->isAuthorized() || $transaction->isBeingVerified()) { + $iState = !empty($arrPayData['paymentDetails']['state']) ? $arrPayData['paymentDetails']['state'] : null; + if ($transaction->isPaid() || $transaction->isAuthorized() || $transaction->isBeingVerified()) + { + $this->payLog('processPayment', 'orderStateName:' . $orderStateName . '. iOrderState: ' . $iOrderState . '. iState:' . $iState, $cartId, $transactionId); + $amountPaid = $transaction->getPaidCurrencyAmount(); if($transaction->isAuthorized()){ $amountPaid = $transaction->getCurrencyAmount(); } try { - $profileId = $transaction->getData()['paymentDetails']['paymentOptionId']; - $paymentMethodName = $transaction->getData()['paymentDetails']['paymentProfileName']; + $profileId = $arrPayData['paymentDetails']['paymentOptionId']; + $paymentMethodName = $arrPayData['paymentDetails']['paymentProfileName']; # Profile 613 is for testing purposes if($profileId != 613) { $settings = $this->getPaymentMethodSettings($profileId); # Get the custom method name - $paymentMethodName = $settings->name; + $paymentMethodName = empty($settings->name) ? '' : $settings->name; } - $this->payLog('processPayment(). Transaction: ' . $transactionId . '. Creating ORDER for ppid ' . $profileId . '. Status: ' . $orderStateName, $cartId); + /** + * @var $cart CartCore + */ + $cart = new Cart((int)$cartId); + + $this->payLog('processPayment', 'Creating ORDER for ppid ' . $profileId . '. Status: ' . $orderStateName . '. Method: ' . $paymentMethodName, $cartId, $transactionId); - $this->validateOrder((int)$transaction->getExtra1(), $order_state, + $this->validateOrder((int)$transaction->getExtra1(), $iOrderState, $amountPaid, $paymentMethodName, null, array('transaction_id' => $transactionId), null, false, $cart->secure_key); /** @var OrderCore $orderId */ @@ -497,17 +633,27 @@ public function processPayment($transactionId, &$message = null) $message = "Validated order (" . $order->reference . ") with status: " . $orderStateName; } catch (Exception $ex) { - $this->payLog('processPayment(). Could not validate(create) order.', $cartId); + $this->payLog('processPayment', 'Could not validate(create) order.', $cartId, $transactionId); $message = "Could not validate order, error: " . $ex->getMessage(); Throw new Exception($message); } } + else { + $this->payLog('processPayment', 'orderStateName:' . $orderStateName . '. iOrderState: ' . $iOrderState .'. iState:'. $iState, $cartId, $transactionId); + } + } return $transaction; } + /** + * @param $transactionId + * @return \Paynl\Result\Transaction\Transaction + * @throws \Paynl\Error\Api + * @throws \Paynl\Error\Error + */ public function getTransaction($transactionId) { $this->sdkLogin(); @@ -517,14 +663,23 @@ public function getTransaction($transactionId) return $transaction; } - public function payLog($message, $cartid = null) + /** + * @param $method + * @param $message + * @param null $cartid + * @param null $transactionId + */ + public function payLog($method, $message, $cartid = null, $transactionId = null) { + if(is_null($this->payLogEnabled)) { + $this->payLogEnabled = Configuration::get('PAYNL_PAYLOGGER') == 1; + } + if($this->payLogEnabled) { - if (is_null($cartid)) { - PrestaShopLogger::addLog('PAY.: ' . $message); - } else { - PrestaShopLogger::addLog('PAY.: CartId:' . $cartid . '. ' . $message); - } + $strCartId = empty($cartid) ? '' : ' CartId: '. $cartid; + $strTransaction = empty($transactionId) ? '' : ' [ '.$transactionId .' ] '; + + PrestaShopLogger::addLog('PAY. - ' . $method . ' - '. $strTransaction . $strCartId . ': ' . $message); } } @@ -551,7 +706,7 @@ public function startPayment(Cart $cart, $payment_option_id, $extra_data = array $iPaymentFee = empty($iPaymentFee) ? 0 : $iPaymentFee; $cartId = $cart->id; - $this->payLog('Starting new payment with cart-total: ' . $cartTotal . '. Fee: ' . $iPaymentFee, $cartId); + $this->payLog('startPayment', 'Starting new payment with cart-total: ' . $cartTotal . '. Fee: ' . $iPaymentFee, $cartId); $this->addPaymentFee($cart, $iPaymentFee); @@ -586,25 +741,43 @@ public function startPayment(Cart $cart, $payment_option_id, $extra_data = array # Retrieve language $startData['language'] = $this->getLanguageForOrder($cart); - $result = \Paynl\Transaction::start($startData); + + /** + * @var $payTransaction Paynl\Result\Transaction\Start + */ + $payTransaction = \Paynl\Transaction::start($startData); + + $payTransactionData = $payTransaction->getData(); + $payTransactionId = !empty($payTransactionData['transaction']['transactionId']) ? $payTransactionData['transaction']['transactionId'] : ''; if ($this->shouldValidateOnStart($payment_option_id)) { - $this->payLog('Pre-Creating order for pp : ' . $payment_option_id, $cartId); + $this->payLog('startPayment', 'Pre-Creating order for pp : ' . $payment_option_id, $cartId, $payTransactionId); - // flush the package list, so the fee is added to it. + # Flush the package list, so the fee is added to it. $this->context->cart->getPackageList(true); $paymentMethodSettings = $this->getPaymentMethodSettings($payment_option_id); - $this->validateOrder($cart->id, $this->statusPending, 0, $paymentMethodSettings->name, - null, array(), null, false, $cart->secure_key); + $this->validateOrder($cart->id, $this->statusPending, 0, $paymentMethodSettings->name, null, array(), null, false, $cart->secure_key); + + $orderId = Order::getIdByCartId($cartId); + $order = new Order($orderId); + + $orderPayment = new OrderPayment(); + $orderPayment->order_reference = $order->reference; + $orderPayment->payment_method = 'PAY Overboeking'; + $orderPayment->amount = $startData['amount']; + $orderPayment->transaction_id = $payTransactionData['transaction']['transactionId']; + $orderPayment->id_currency = $cart->id_currency; + $orderPayment->save(); + } else { - $this->payLog('Not pre-creating the order, waiting for payment.', $cartId); + $this->payLog('startPayment', 'Not pre-creating the order, waiting for payment.', $cartId, $payTransactionId); } - return $result->getRedirectUrl(); + return $payTransaction->getRedirectUrl(); } /** @@ -980,6 +1153,7 @@ protected function _postProcess() Configuration::updateValue('PAYNL_SERVICE_ID', Tools::getValue('PAYNL_SERVICE_ID')); Configuration::updateValue('PAYNL_TEST_MODE', Tools::getValue('PAYNL_TEST_MODE')); Configuration::updateValue('PAYNL_VALIDATION_DELAY', Tools::getValue('PAYNL_VALIDATION_DELAY')); + Configuration::updateValue('PAYNL_PAYLOGGER', Tools::getValue('PAYNL_PAYLOGGER')); Configuration::updateValue('PAYNL_DESCRIPTION_PREFIX', Tools::getValue('PAYNL_DESCRIPTION_PREFIX')); Configuration::updateValue('PAYNL_PAYMENTMETHODS', Tools::getValue('PAYNL_PAYMENTMETHODS')); Configuration::updateValue('PAYNL_LANGUAGE', Tools::getValue('PAYNL_LANGUAGE')); @@ -1036,6 +1210,24 @@ public function renderAccountSettingsForm() 'label' => $this->l('Disabled') ) ), + ), + array( + 'type' => 'switch', + 'label' => $this->l('PAY Logging'), + 'name' => 'PAYNL_PAYLOGGER', + 'desc' => $this->l('Log internal PAY. processing information.'), + 'values' => array( + array( + 'id' => 'paylogger_on', + 'value' => 1, + 'label' => $this->l('Enabled') + ), + array( + 'id' => 'paylogger_off', + 'value' => 0, + 'label' => $this->l('Disabled') + ) + ), ), array( 'type' => 'switch', @@ -1134,6 +1326,7 @@ public function getConfigFieldsValues() 'PAYNL_SERVICE_ID' => Tools::getValue('PAYNL_SERVICE_ID', Configuration::get('PAYNL_SERVICE_ID')), 'PAYNL_TEST_MODE' => Tools::getValue('PAYNL_TEST_MODE', Configuration::get('PAYNL_TEST_MODE')), 'PAYNL_VALIDATION_DELAY' => Tools::getValue('PAYNL_VALIDATION_DELAY', Configuration::get('PAYNL_VALIDATION_DELAY')), + 'PAYNL_PAYLOGGER' => Tools::getValue('PAYNL_PAYLOGGER', Configuration::get('PAYNL_PAYLOGGER')), 'PAYNL_DESCRIPTION_PREFIX' => Tools::getValue('PAYNL_DESCRIPTION_PREFIX', Configuration::get('PAYNL_DESCRIPTION_PREFIX')), 'PAYNL_LANGUAGE' => Tools::getValue('PAYNL_LANGUAGE', Configuration::get('PAYNL_LANGUAGE')), 'PAYNL_SHOW_IMAGE' => Tools::getValue('PAYNL_SHOW_IMAGE', Configuration::get('PAYNL_SHOW_IMAGE')), diff --git a/paynlpaymentmethods/vendor/paynl/sdk/src/Api/Transaction/Refund.php b/paynlpaymentmethods/vendor/paynl/sdk/src/Api/Transaction/Refund.php index bc84092..e2d4bde 100644 --- a/paynlpaymentmethods/vendor/paynl/sdk/src/Api/Transaction/Refund.php +++ b/paynlpaymentmethods/vendor/paynl/sdk/src/Api/Transaction/Refund.php @@ -55,6 +55,12 @@ public function setTransactionId($transactionId) $this->transactionId = $transactionId; } + private $currency; + + public function setCurrency($strCurrency) + { + $this->currency = $strCurrency; + } /** * @param int $amount */ @@ -101,6 +107,10 @@ protected function getData() $this->data['processDate'] = $this->processDate->format('d-m-Y'); } + if(!empty($this->currency)) { + $this->data['currency'] = $this->currency; + } + return parent::getData(); } diff --git a/paynlpaymentmethods/vendor/paynl/sdk/src/Transaction.php b/paynlpaymentmethods/vendor/paynl/sdk/src/Transaction.php index e674767..930b825 100644 --- a/paynlpaymentmethods/vendor/paynl/sdk/src/Transaction.php +++ b/paynlpaymentmethods/vendor/paynl/sdk/src/Transaction.php @@ -309,6 +309,7 @@ public static function getForExchange() * @param int|float|null $amount * @param string|null $description * @param \DateTime $processDate + * @param string|null $strCurrency * * @return Result\Refund * @throws Error\Api @@ -318,7 +319,8 @@ public static function refund( $transactionId, $amount = null, $description = null, - \DateTime $processDate = null + \DateTime $processDate = null, + $strCurrency = null ) { $api = new Api\Refund(); @@ -333,6 +335,9 @@ public static function refund( if ($processDate !== null) { $api->setProcessDate($processDate); } + if(!empty($strCurrency)) { + $api->setCurrency($strCurrency); + } $result = $api->doRequest(); return new Result\Refund($result); diff --git a/paynlpaymentmethods/views/js/PAY.js b/paynlpaymentmethods/views/js/PAY.js new file mode 100644 index 0000000..f7fa588 --- /dev/null +++ b/paynlpaymentmethods/views/js/PAY.js @@ -0,0 +1,72 @@ +jQuery(document).ready(function () { + jQuery("#PAY-info-button").click(function () { + jQuery("#dialog-info-modal").dialog({ + modal: true, + closeOnEscape: false + }); + }); + + jQuery("#pay-refund-button").click(function () { + var amount = jQuery('#pay-refund-amount').val(); + var errorMessage = jQuery('#pay-lang-invalidamount').val(); + + if (!/^[0-9,]+$/.test(amount)) { + alert(errorMessage); + return; + } + + if (amount.indexOf(',') === -1) { + amount = amount / 100; + } else { + amount = parseFloat(amount.replace(',', '.').replace(' ', '')); + } + var transactionid = jQuery('#pay-transactionid').val(); + var PrestaOrderId = jQuery('#pay-prestaorderid').val(); + var ajaxurl = jQuery('#pay-ajaxurl').val(); + var presentationAmount = amount.toFixed(2); + var currency = jQuery('#pay-currency').text(); + var lang_areyoursure = jQuery('#pay-lang-areyoursure').val(); + var lang_refunding = jQuery('#pay-lang-refunding').val(); + var lang_sucrefund = jQuery("#pay-lang-succesfullyrefunded").val(); + var lang_refundbutton = jQuery("#pay-lang-refundbutton").val(); + var lang_couldnotprocess = jQuery("#pay-lang-couldnotprocess").val(); + + presentationAmount = presentationAmount.replace('.', ','); + + if (confirm(lang_areyoursure + ': ' + currency + ' ' + presentationAmount + ' ?')) { + + var data = {}; + jQuery.extend(data, {amount: amount}); + jQuery.extend(data, {orderid: transactionid}); + jQuery.extend(data, {prestaorderid: PrestaOrderId}); + + + var refundButton = jQuery(this); + var payOption = jQuery(this).parent(); + + jQuery(refundButton).text(lang_refunding); + + setTimeout(function () { + jQuery.ajax({ + url: ajaxurl, + type: 'POST', + data: data, + dataType: 'json', + success: function (data) { + if (data.success) { + jQuery('#pay-status').text(' - '); + jQuery(payOption).text(lang_sucrefund + ' ' + currency + ' ' + presentationAmount); + } else { + jQuery(refundButton).text(lang_refundbutton); + alert(lang_couldnotprocess); + } + }, + error: function () { + jQuery(refundButton).text(lang_refundbutton); + alert('Refund failed'); + } + }); + }, 750); + } + }); +}); diff --git a/paynlpaymentmethods/views/templates/hook/payorder.tpl b/paynlpaymentmethods/views/templates/hook/payorder.tpl new file mode 100644 index 0000000..f971ec7 --- /dev/null +++ b/paynlpaymentmethods/views/templates/hook/payorder.tpl @@ -0,0 +1,54 @@ +
{$lang.info_refund_title}
+ * {$lang.info_refund_text}
+
{$lang.info_log_title}
+ * {$lang.info_log_text}
+