The OpenCart payment module consists of three parts: the model, which decides whether to show the payment methodAn extension such as card or cash payment. More in the glossary → at checkout (getMethod() in 3.0, getMethods() in 4.1), the controllerCode that receives a request and prepares data. More in the glossary →, which renders the payment button or form at the confirmation step, and the response handler: confirm() for offline methods or callback() for online payment. In all cases the result is the same: a call to addOrderHistory() (in 4.1, addHistory()) with the status from the module settings.
Below is how this is organized in OpenCart 3.0 (3.0.4.1 and 3.0.5.1) and 4.1 (4.1.0.4), using core modules as examples, and which callback checks you must not skip. The examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ.
Which files make up a payment module?
The simplest example in both branches is the core module cod (cash on deliveryPayment upon receipt. It increases the risk of parcel refusals. More in the glossary →). For online payment, 3.0 also has modules with a callback, for example liqpay; in 4.1, the core still includes only bank_transfer, cheque, cod, and free_checkout.
| Part | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| Admin settings | admin/ | extension/ (methods index, save) |
| Storefront model | catalog/, method getMethod($address, $total) | extension/, method getMethods(array $address = []): array |
| Storefront controller | catalog/ | extension/ |
| Button template | catalog/ | extension/ |
| Callback route | extension/ | extension/ |
Settings are stored in the setting table with the payment_{code}_ prefix: payment_{code}_status, payment_{code}_sort_order, payment_{code}_geo_zone_id, payment_{code}_order_status_id, and your own keys (merchant, secret). How to build the form and save these values is described in the articles OpenCart extension anatomy and module settings form.

How does the model decide whether to show the payment method?
At the payment selection step, the core goes through all enabled payment extensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → (payment_{code}_status) and calls their model. An empty array means "this method is currently unavailable."
OpenCart 3.0: getMethod($address, $total) receives the payment address and the order total already calculated by the chain of total modules, and returns a single array with the keys code, title, terms, sort_order. If the cart contains items with recurring payments, the core additionally requires the model to have the method recurringPayments(), which returns true.
OpenCart 4.1: getMethods(array $address = []) does not receive the total; it returns the method with nested options under the key option. The option code has the form {code}.{підкод}, and this is exactly what the core then places into session.
Example: getMethods for online payment · OpenCart 4.1
namespace Opencart\Catalog\Model\Extension\Mypay\Payment;
class Mypay extends \Opencart\System\Engine\Model {
public function getMethods(array $address = []): array {
$this->load->language('extension/mypay/payment/mypay');
$status = true;
if ($this->cart->hasSubscription()) {
$status = false;
}
$min = (float)$this->config->get('payment_mypay_total');
if ($min > 0 && $this->cart->getTotal() < $min) {
$status = false;
}
if (!$status) {
return [];
}
return [
'code' => 'mypay',
'name' => $this->language->get('heading_title'),
'option' => [
'mypay' => [
'code' => 'mypay.mypay',
'name' => $this->language->get('text_card')
]
],
'sort_order' => $this->config->get('payment_mypay_sort_order')
];
}
}
Here cart->getTotal() is the sum of products with taxes, without shipping or discounts; if the threshold should account for totals, calculate them via the checkout/cart model. In 4.1, geo zone restrictions are handled via model_localisation_geo_zone, as in the cod module. Note that in 4.1 the payment address can be disabled (config_checkout_payment_address), in which case $address is empty.
Payment form at the confirmation step
When the checkout session is complete, checkout/confirm creates an order with status 0 and embeds the output of the selected payment module's controller: $this in 3.0, 'extension/ in 4.1. Your controller's index() method must return HTML, not output it. How the order is created, and why the number must be taken from the session at that exact moment, is covered in the article checkout in code.
There are two typical scenarios:
- Offline or "confirm and pay later" (cash on delivery, bank transfer): the button makes an AJAX request to
confirm(), which checks the session, calls the history method with the module status, and returnsredirecttocheckout/success. - Online payment:
index()takes the order fromsession, builds a request to the payment systemA service that accepts cards: LiqPay, WayForPay, Monobank, and others. The commission depends on the contract. More in the glossary → (amount, currency, number, return URL, and callback URL), and renders a form with a redirect or a widget. The status is not set by this page but by the callback.->data['order_id']
The cod module in 4.1 shows a full check in confirm(): the order exists, the payment method in the session is exactly cod.cod, and only then addHistory() with the status payment_cod_order_status_id. Checking the payment method code is mandatory: without it, anyone can call confirm of your module, choose a different method at checkout, and get an order in "paid" status. Format controller responses as JSON following the rules from AJAX in OpenCart.
Example: calculating the amount for the payment system · both branches
$order_info = $this->model_checkout_order->getOrder((int)$this->session->data['order_id']);
// Сума у валюті замовлення, без форматування
$amount = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
order.total is stored in the store's base currency, so before sending it you must convert it to the order currency using the rate fixed at checkout (currency_value). The fourth argument of false disables formatting and returns a number. If the payment system accepts only hryvnias and the store allows other currencies, convert explicitly to UAH and show the customer how much will be charged.
How to write a secure callback
A callback is a request from the payment system's server, not from the customer's browser. It has no customer session, so the order number is taken only from the request data, and that data can be trusted only after the signature is verified. Look at the core 3.0 module liqpay: its callback() verifies the signature and calls addOrderHistory() with the status config_order_status_id, but it does not check the payment status in the response or the amount. For your own module, this is not enough.
- Read the raw request body. The
Requestclass in both branches passes$_POSTthroughhtmlspecialchars(), so$thismay differ from what the payment system signed. For signature verification, take->request ->post file_get_contents('php://orinput') $_POSTdirectly. - Verify the signature using the secret from the settings and
hash_equals(), not==. - Find the order by the number from the request.
getOrder()returnsfalsein 3.0 and[]in 4.1 if the order does not exist. - Check the payment status in the response: success, refusal, pending, refund. These are different order statuses, or none at all.
- Match the amount and currency against the order (taking
currency_valueinto account), and match the order's payment method against your module. - Be idempotent. If the order is already in a paid status, change nothing. The callback often arrives more than once.
- Call the history method with the status from the module settings, and respond to the payment system as it requires (usually HTTP 200 with a short body).
Example: callback · OpenCart 4.1
public function callback(): void {
$raw = (string)file_get_contents('php://input');
$data = json_decode($raw, true) ?: [];
$secret = (string)$this->config->get('payment_mypay_secret');
$signature = (string)($this->request->server['HTTP_X_SIGNATURE'] ?? '');
if (!$secret || !hash_equals(hash_hmac('sha256', $raw, $secret), $signature)) {
http_response_code(403);
return;
}
$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder((int)($data['order_id'] ?? 0));
if (!$order_info || ($order_info['payment_method']['code'] ?? '') != 'mypay.mypay') {
http_response_code(404);
return;
}
$expected = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
$paid_status_id = (int)$this->config->get('payment_mypay_order_status_id');
if (($data['status'] ?? '') == 'success'
&& abs((float)($data['amount'] ?? 0) - (float)$expected) < 0.01
&& ($data['currency'] ?? '') == $order_info['currency_code']
&& (int)$order_info['order_status_id'] != $paid_status_id) {
$this->model_checkout_order->addHistory((int)$order_info['order_id'], $paid_status_id, 'Transaction ' . ($data['transaction_id'] ?? ''), true);
}
$this->response->setOutput('OK');
}
The signature format, field names, and expected response are made up for this example; take them from your payment system's documentation. In 3.0 the structure is the same, but payment_method in the order is a string with the name, the code is in $order_info['payment_code'], the route is extension/, and the history method is addOrderHistory().
What happens after the callback
The history method does the rest: when moving to a status from the processing group, it deducts stock and confirms coupons and rewards, and the event handler sends the customer a confirmation email at the first non-zero status. For details, see the article about addOrderHistory.
Meanwhile, the customer returns from the payment page to the return URL. This is usually checkout/success, which clears the cart and the checkout session, but only if order_id is in the session. Two consequences:
- Do not change the status on the return page. The customer may not return, and URL parameters are easy to forge. The status comes only from a verified callback.
- If the customer returns before the callback arrives, the success page will show "thank you," but the order will still be in status 0. This is normal. If the callback never arrives, the order will remain in "Missing Orders."
For a payment refusal, send the customer to checkout/failure or back to checkout: the cart is not cleared, so they can try again. If the payment went through but the status did not change, the diagnostic steps are described in the article order status after payment. For an overview of Ukrainian payment solutions, see the blog: card payment in OpenCart.
Summary
- The module consists of a model (
getMethodin 3.0,getMethodswithoptionand the codecode.підкодin 4.1), a controller with a button or form, and a handlerconfirmorcallback. - The controller's
index()returns HTML; take the amount for the payment system fromorder.total, recalculated viacurrency_value. - In
confirm(), check that the session has your payment method selected. - There is no session in the callback: verify the signature over the raw body, the payment status, the amount, the currency, and the order's payment method, and do not change an order that is already paid.
- Set the status only through the history method of the catalog model, never with a direct
UPDATE, and never on the return page.
Other materials on the cart, checkout, and orders are on the section page Cart, checkout and orders in code.