public function hookBackOfficeHeader() { if (!isset($_GET['vieworder']) || !isset($_GET['id_order'])) { return; } //Capture start if (Tools::isSubmit('Submit_beGateway_Capture') && isset($_POST['id_auth'])) { $transaction_details = Db::getInstance()->getRow('SELECT * FROM ' . _DB_PREFIX_ . 'begateway_transaction WHERE id_begateway_transaction = ' . (int) $_POST['id_auth'] . ' AND type = \'authorization\' AND status = \'successful\''); if (isset($transaction_details['uid'])) { $capture = new \beGateway\Capture(); $capture->setParentUid($transaction_details['uid']); $capture->money->setCurrency($transaction_details['currency']); $capture->money->setAmount($transaction_details['amount']); $capture_response = $capture->submit(); if ($capture_response->isSuccess()) { Db::getInstance()->getRow('UPDATE ' . _DB_PREFIX_ . 'begateway_transaction SET type = \'payment\', au_uid = \'' . $transaction_details['uid'] . '\' , uid = \'' . $capture_response->getUid() . '\' WHERE id_begateway_transaction = ' . (int) $_POST['id_auth']); } } } //Capture end if (Tools::isSubmit('Submit_beGateway_Refund') && isset($_POST['begateway_amount_to_refund']) && isset($_POST['id_refund'])) { $transaction_details = Db::getInstance()->getRow('SELECT * FROM ' . _DB_PREFIX_ . 'begateway_transaction WHERE id_begateway_transaction = ' . (int) $_POST['id_refund'] . ' AND type = \'payment\' AND status = \'successful\''); if (isset($transaction_details['uid'])) { $already_refunded = Db::getInstance()->getValue('SELECT SUM(amount) FROM ' . _DB_PREFIX_ . 'begateway_transaction WHERE id_order = ' . (int) $_GET['id_order'] . ' AND type = \'refund\' AND status = \'successful\''); if ($_POST['begateway_amount_to_refund'] <= number_format($transaction_details['amount'] - $already_refunded, 2, '.', '')) { $refund = new \beGateway\Refund(); $refund->setParentUid($transaction_details['uid']); $refund->money->setCurrency($transaction_details['currency']); $refund->money->setAmount($_POST['begateway_amount_to_refund']); $refund->setReason($this->l('Order Refund :') . $_GET['id_order']); $refund_response = $refund->submit(); if ($refund_response->isSuccess()) { Db::getInstance()->Execute(' INSERT INTO ' . _DB_PREFIX_ . 'begateway_transaction (type, id_begateway_customer, id_cart, id_order, uid, amount, status, currency, date_add) VALUES (\'refund\', ' . (int) $transaction_details['id_begateway_customer'] . ', ' . (int) $transaction_details['id_cart'] . ', ' . (int) $_GET['id_order'] . ', \'' . $refund_response->getUid() . '\', \'' . (double) $_POST['begateway_amount_to_refund'] . '\', \'' . 'successful' . '\', \'' . $transaction_details['currency'] . '\', NOW())'); } } else { $this->_errors['refund_error'] = $this->l('You cannot refund more than') . ' ' . Tools::displayPrice($transaction_details['amount'] - $already_refunded) . ' ' . $this->l('on this order'); } } } /* Check if the order was paid with beGateway and display the transaction details */ if (Db::getInstance()->getValue('SELECT module FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = ' . (int) $_GET['id_order']) == $this->name) { /* Get the transaction details */ $id_cart = Db::getInstance()->getValue('SELECT id_cart FROM ' . _DB_PREFIX_ . 'orders WHERE id_order = ' . (int) $_GET['id_order']); $transaction_details = Db::getInstance()->getRow('SELECT * FROM ' . _DB_PREFIX_ . 'begateway_transaction WHERE id_cart = ' . (int) $id_cart . ' AND ((type = \'payment\') or (type = \'authorization\')) AND status = \'successful\''); $refunded = 0; $output_refund = ''; $refund_details = Db::getInstance()->ExecuteS('SELECT amount, status, date_add, uid FROM ' . _DB_PREFIX_ . 'begateway_transaction WHERE id_order = ' . (int) $_GET['id_order'] . ' AND type = \'refund\' ORDER BY date_add DESC'); foreach ($refund_details as $refund_detail) { $refunded += $refund_detail['status'] == 'successful' ? $refund_detail['amount'] : 0; $output_refund .= '<tr' . ($refund_detail['status'] != 'successful' ? ' style="background: #FFBBAA;"' : '') . '><td>' . Tools::safeOutput($refund_detail['date_add']) . '</td><td style="text-align: right;">' . Tools::displayPrice($refund_detail['amount']) . '</td><td>' . ($refund_detail['status'] == 'successful' ? $this->l('Processed') : $this->l('Error')) . '</td><td>' . Tools::safeOutput($refund_detail['uid']) . '</td></tr>'; } $output = ' <script type="text/javascript"> $(document).ready(function() { $(\'' . (_PS_VERSION_ < 1.6 ? '' : '<div class="row"><div class="col-lg-12">') . '<fieldset' . (_PS_VERSION_ < 1.5 ? ' style="width: 400px;"' : '') . '><legend><img src="../img/admin/money.gif" alt="" />' . $this->l('Payment Details') . '</legend>'; if (isset($transaction_details['uid'])) { if ($transaction_details['type'] == 'authorization') { $stat_str = $this->l('Status:') . ' <span style="font-weight: bold; color: ' . ($transaction_details['status'] == 'successful' ? 'green;">' . $this->l('Authorized') : '#CC0000;">' . $this->l('Unauthorized')) . '</span>'; $stat_str .= '<form action="" method="post"><input type="hidden" name="id_auth" value="' . Tools::safeOutput($transaction_details['id_begateway_transaction']) . '" /> <input type="submit" class="btn btn-primary" onclick="return confirm(\\\'' . addslashes($this->l('Do you want to capture this transaction?')) . '\\\');" name="Submit_beGateway_Capture" value="' . $this->l('Capture this transaction') . '" /></form>'; } else { $stat_str = $this->l('Status:') . ' <span style="font-weight: bold; color: ' . ($transaction_details['status'] == 'successful' ? 'green;">' . $this->l('Paid') : '#CC0000;">' . $this->l('Unpaid')) . '</span><br />'; } $output .= $this->l('Transaction UID:') . ' ' . Tools::safeOutput($transaction_details['uid']) . '<br /><br />' . $stat_str . $this->l('Amount:') . ' ' . Tools::displayPrice($transaction_details['amount']) . '<br />' . $this->l('Processed on:') . ' ' . Tools::safeOutput($transaction_details['date_add']) . '<br />'; } else { $output .= '<b style="color: #CC0000;">' . $this->l('Warning:') . '</b> ' . $this->l('The customer paid and an error occured (check details at the bottom of this page)'); } $output .= '</fieldset><br />'; if ($transaction_details['status'] == 'successful' && $transaction_details['type'] == 'payment') { $output .= '<fieldset' . (_PS_VERSION_ < 1.5 ? ' style="width: 400px;"' : '') . '><legend><img src="../img/admin/money.gif" alt="" />' . $this->l('Proceed to a full or partial refund') . '</legend>' . (empty($this->_errors['refund_error']) && isset($_POST['uid_refund']) ? '<div class="conf confirmation">' . $this->l('Your refund was successfully processed') . '</div>' : '') . (!empty($this->_errors['refund_error']) ? '<span style="color: #CC0000; font-weight: bold;">' . $this->l('Error:') . ' ' . Tools::safeOutput($this->_errors['refund_error']) . '</span><br /><br />' : '') . $this->l('Already refunded:') . ' <b>' . Tools::displayPrice($refunded) . '</b><br /><br />' . ($refunded ? '<table class="table" cellpadding="0" cellspacing="0" style="font-size: 12px;"><tr><th>' . $this->l('Date') . '</th><th>' . $this->l('Amount refunded') . '</th><th>' . $this->l('Status') . '</th><th>' . $this->l('UID') . '</th></tr>' . $output_refund . '</table><br />' : '') . ($transaction_details['amount'] > $refunded ? '<form action="" method="post">' . $this->l('Refund:') . ' <input type="text" value="' . number_format($transaction_details['amount'] - $refunded, 2, '.', '') . '" name="begateway_amount_to_refund" style="text-align: right; width: 100px;" /> <input type="hidden" name="id_refund" value="' . Tools::safeOutput($transaction_details['id_begateway_transaction']) . '" /><input type="submit" class="btn btn-primary" onclick="return confirm(\\\'' . addslashes($this->l('Do you want to proceed to this refund?')) . '\\\');" name="Submit_beGateway_Refund" value="' . $this->l('Process Refund') . '" /></form>' : '') . '</fieldset><br />'; } if (_PS_VERSION_ < 1.6) { $output .= '\').insertBefore($(\'select[name=id_order_state]\').parent().parent().find(\'fieldset\').first());'; } else { $output .= '</div></div>\').insertBefore($(\'select[name=id_order_state]\').closest(\'.row\'));'; } $output .= '}); </script>'; return $output; } }
$transaction->card->setCardExpYear(2030); $transaction->card->setCardCvc('123'); $transaction->customer->setFirstName('John'); $transaction->customer->setLastName('Doe'); $transaction->customer->setCountry('LV'); $transaction->customer->setAddress('Demo str 12'); $transaction->customer->setCity('Riga'); $transaction->customer->setZip('LV-1082'); $transaction->customer->setIp('127.0.0.1'); $transaction->customer->setEmail('*****@*****.**'); $response = $transaction->submit(); print "Transaction message: " . $response->getMessage() . PHP_EOL; print "Transaction status: " . $response->getStatus() . PHP_EOL; if ($response->isSuccess()) { print "Transaction UID: " . $response->getUid() . PHP_EOL; print "Trying to Refund transaction " . $response->getUid() . PHP_EOL; $refund = new \beGateway\Refund(); $refund->setParentUid($response->getUid()); $refund->money->setAmount($transaction->money->getAmount()); $refund->setReason('customer request'); $refund_response = $refund->submit(); if ($refund_response->isSuccess()) { print "Refund successfuly. Refund transaction UID " . $refund_response->getUid() . PHP_EOL; } else { print "Problem to refund" . PHP_EOL; print "Refund message: " . $refund_response->getMessage() . PHP_EOL; } } ?>