I tried for Opencart V2 but gave up and went with PayPal, you're welcome to the attempted code.
Code: Select all
<?php
require_once 'netbanx.php';
class ControllerPaymentPerpetualPayments extends Controller {
public function index() {
$this->load->language('payment/perpetual_payments');
$data['text_wait'] = $this->language->get('text_wait');
$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
$nbx_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$nbx_uri = 'https://api.netbanx.com';
$nbx_payment_amount = (int) ($order_info['total'] * 100);
$nbx_currency_code = 'GBP';
$nbx_merchant_reference = $order_info['order_id'];
$nbx = new NetbanxAPI(array(
"key" => $nbx_key,
"uri" => $nbx_uri
)
);
$order = $nbx->create_order(
array(
'total_amount' => $nbx_payment_amount,
'currency_code' => $nbx_currency_code,
'customerNotificationEmail' => $order_info['email'],
'merchant_notification_email' => $this->config->get('config_email'),
'merchant_ref_num' => $nbx_merchant_reference,
'link' =>
array(
array(
'rel' => 'return_url',
'uri' => "https://www.website.com/index.php?route=checkout/checkout"
),
array(
'rel' => 'cancel_url',
'uri' => "https://www.website.com/index.php?route=checkout/checkout"
)
),
'callback' => // cgi callback
array(
array(
'format' => 'form-urlencoded',
'rel' => 'on_success',
'synchronous' => true,
'retries' => 1,
'returnKeys' =>
array(
'id',
'transaction.merchantRefNum',
'transaction.errorMessage',
'transaction.amount',
'transaction.status'
),
'uri' => "https://www.website.com/index.php?route=payment/perpetual_payments/callback"
),
array(
'format' => 'form-urlencoded',
'rel' => 'on_hold',
'synchronous' => true,
'retries' => 1,
'returnKeys' =>
array(
'transaction.merchantRefNum',
'id',
'transaction.errorMessage',
'transaction.amount',
'transaction.status',
),
'uri' => "https://www.website.com/index.php?route=payment/perpetual_payments/callback"
),
array(
'format' => 'form-urlencoded',
'rel' => 'on_decline',
'synchronous' => true,
'retries' => 1,
'returnKeys' =>
array(
'transaction.merchantRefNum',
'id',
'transaction.errorMessage',
'transaction.amount',
'transaction.status',
),
'uri' => "https://www.website.com/index.php?route=payment/perpetual_payments/callback"
)
),
)
);
$data['button_continue_action'] = $order->link[0]->uri;
return $this->load->view('payment/perpetual_payments', $data);
$this->model_checkout_order->updatenetbanx($nbx_merchant_reference, $order->id);
}
public function callback() {
$this->load->language('payment/perpetual_payments');
$this->load->model('checkout/order');
// Check Post for Shop Order Number | Assign Order number to variable
// Load OpenCart Order | Compare Netbanx Hash
if (isset($_POST['transaction_merchantRefNum'])) {
$netbanxreply = $_POST['transaction_merchantRefNum'];
$order_info = $this->model_checkout_order->getOrder($netbanxreply);
if ($order_info['invoice_prefix'] === $_POST['id']) {
// Log Transaction Details into the Order Notes Field
$message = "NETBANX RESPONSE VALIDATED" . " | ";
if (isset($_POST['transaction_merchantRefNum'])) {
$message .= 'ORDER NUMBER : ' . $_POST['transaction_merchantRefNum'] . " | ";
}
if (isset($_POST['transaction_amount'])) {
$message .= 'AMOUNT : ' . $_POST['transaction_amount'] . " | ";
}
if (isset($_POST['id'])) {
$message .= 'HASH : ' . $_POST['id'] . " | ";
}
// Successful order - Confirm and complete
if (($_POST['transaction_status']) == "success") {
$this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('perpetual_payments_order_status_id'), $message, false);
$this->response->setOutput($this->load->view('checkout/success', $data));
unset($_POST);
} else {
// Failed Order - Set to pending for further attempts
$message .= ("THIS TRANSACTION FAILED WITH NETBANX - " . $order_info['invoice_prefix'] ."\n");
if (isset($_POST['transaction.errorMessage'])) {
$message .= 'Netbanx Transaction Errors: ' . $_POST['transaction.errorMessage'] . "\n";
$this->redirect('checkout/checkout');
}
}
$this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('perpetual_payments_order_status_id'), $message, false);
}
}
unset($_POST);
}
}
?>