Creating Payment Plugin

TF Shop

Creating a payment plugin is simple process. As with any Joomla plugin, there are two files.

  1. XML file
  2. PHP file

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:

  1. Display payment button form
  2. Redirect users to payment gateway
  3. Process payment
  4. Set data array for the order

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.

use Joomla\CMS\Uri\Uri;
$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:

1. onPaymentDisplayForm

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>';
return $output;

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:

  1. Provide the action attribute to the form where users will be redirected to the payment gateway directly.
  2. Call onPaymentLink method, which will be responsible for generating payment link and redirecting users to the payment gateway.

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.

2. onPaymentLink($pay)

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);

3. onPaymentProcess($data)

This method processes the payment and returns the $order or $payment array. This array is used by the component to create a new order.

Products

In any method, you can get information about the product using component model methods.

use Joomla\CMS\MVC\Model\BaseDatabaseModel;
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.

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive