SamplePayment.php File | EasyStore - Documentation | JoomShaper

EasyStore

Updated Last: 24 April 2024

SamplePayment.php File

The SamplePayment.php file functions as the central logic handler for integrating a custom payment gateway into EasyStore.

Step1: Rename SamplePayment.php File

First, rename the SamplePayment.php file by replacing "Sample" with the name of your payment plugin.

Step 2: Rename Namespace

Substitute "Sample" with the name of your payment gateway in the following line:
namespace JoomShaper\Plugin\EasyStore\Sample\Extension;

Step 3: Rename SamplePayment Class

Following that, you'll notice a class named SamplePayment that extends the PaymentGatewayPlugin class. Replace the class name SamplePayment with the name of your payment plugin.

Before proceeding, familiarize yourself with the PaymentGatewayPlugin parent class.

PaymentGatewayPlugin Class

The PaymentGatewayPlugin is an abstract class that extends the Joomla CMS Plugin, serving as a foundation for implementing various types of payment gateways within the EasyStore component. This class comprises three abstract methods that subclasses must implement:

onBeforePayment Method

Triggered before the payment process. The onBeforePayment method checks and prepares data before the payment process to ensure all required fields are set before processing a payment.

onPayment Method

Triggered when a payment event occurs. The onPayment method accepts an Event object as a parameter. The Event object contains cart data required for payment processing.

onPaymentNotify Method

This event is triggered when it receives a notification from the payment portal. It takes an Event object as a parameter. The Event object contains relevant data for payment notification, including raw payload, GET data, POST data, server variables, and an instance of the Order Class.

Step 4: onBeforePayment 

public function onBeforePayment(Event $event)
{
    $constants = new SampleConstants();
    $secretKey = $constants->getSecretKey();
    $merchantKey = $constants->getMerchantKey();
    $isRequiredFieldsFilled = !empty($secretKey) && !empty($merchantKey);

    $event->setArgument('result', $isRequiredFieldsFilled);
}

This method checks if the essential fields (secret key and merchant key in this case) are configured before initiating the payment process and returns a boolean value, true or false stating the result of the condition.

The result is set in the event arguments using the key result.

Please note you need to store the result in the event arguments using the key result as shown in the code.

Step 5: onPayment

The onPayment method handles the initiation of the payment process by interacting with a Payment Gateway API. It includes the necessary steps to prepare and send data to the API, handle the API response, and redirect the user to the Payment Gateway's checkout page.
We have declared all the necessary variables inside this method in the sample plugin. Only keep the ones you need to go to the Payment Gateway's checkout page

Important Considerations

  • Add the classes and methods provided by your Payment Gateway API inside the Try block.
  • Keep the necessary data you need to send in the API call based on the requirements of your Payment Gateway. Refer to the Payment Gateway API documentation for specific data expectations.
  • Make sure to send the order.id as metadata or any other format as stated in the Payment Gateway API documentation so that it can be processed in the onPaymentNotify method.
  • Enhance error handling to meet the specific needs of your integration. Log errors, display user-friendly messages, and ensure a smooth uaser experience even in case of failures.
  • Implement any security measures recommended by your Payment Gateway, such as data encryption and secure API endpoints.

Step 6: onPaymentNotify

This method handles payment notifications, updating order status based on the received payment information.