Creating a payment plugin is simple process. As with any Joomla plugin, there are two files.
In the XML file, the group of the plugin should be "tfshop".
The PHP file is the main file which processes the payment for orders. The plugin is responsible for:
Generally, you will have to pass two URLs to the payment gateway: (i) Notify URL where the payment gateway will send POST information and (ii) Redirect URL where users will be redirected to after the payment.
$uri = Uri::getInstance();
$notify_url = $uri->base() . 'index.php?option=com_tfshop&task=payment.post';
$return_url = $uri->base() . 'index.php?option=com_tfshop&task=payment.success';
There are three methods in the plugin:
onPaymentDisplayForm($product_id, $amount, $button)
This method returns the output of the form. The form has hidden fields and a submit button. For example,
$output = '';
$output .= '<form action="" class="d-inline" method="post">';
$output .= '<input type="hidden" name="product_id" value="' . $product_id . '" />';
$output .= '<input type="hidden" name="amount" value="' . $amount . '" />';
$output .= '<input type="submit" name="pay_now" id="pay_now" class="' . $button->btn_class . ' btn-sm mb-3" value="' . $button->btn_text . '" formtarget="_blank" />';
$output .= '</form>';
You can add other hidden fields for redirect URL, notify URL, order type and so on as required by the payment gateway. After clicking the submit button, users are redirected to the payment gateway.
In this method, you have two options:
For the second method, add the following fields to the form to call onPaymentLink() method of the plugin:
$output .= '<input type="hidden" name="option" value="com_tfshop" />';
$output .= '<input type="hidden" name="task" value="payment.pay" />';
This will take the user to the pay() method of payment controller which will call the onPaymentLink() method of this plugin.
The $pay is associative array containing otype (order type), product_id, and amount.
This method generates payment link and redirects users to the link. For example,
// Redirect to payment gateway
header('Location: ' . $url);
This method processes the payment and returns the $order or $payment array. This array is used by the component to create a new order.
In any method, you can get information about the product using component model methods.
BaseDatabaseModel::addIncludePath(JPATH_ROOT . '/components/com_tfshop/models', 'TfShopModel');
$productModel = BaseDatabaseModel::getInstance('product', 'TfShopModel');
$product = $productModel->getItem($product_id);
The $product is the object which contains details about the product.