Step 5: Update Custompayment.php (Payment Logic)

The Custompayment.php file contains the core logic for processing payments via the custom payment gateway. This file extends the BasePayment class and overrides certain methods to handle payment operations specific to your gateway.

Namespace

Ensure the namespace at the top of the file matches your plugin name. Replace Payments\Custom with your preferred namespace to align with your project structure.

check() Method:

In the check() method, you will define the configuration keys for your payment gateway. These include values like secret_key, public_key, and mode. Update these keys to match the settings required by your payment gateway. If your gateway requires other configuration values (like API credentials or environment settings), add them to the $configKeys array.

setup() Method:

The setup() method is used to configure the necessary settings for making API calls to the payment gateway.

Purpose:
  • This method sets up the necessary API headers, which will be used in subsequent requests to the payment gateway.

Creating Payments (createPayment() Method):

Overview:

  • createPayment(): Sends the payment data from Tutor LMS to your payment gateway.
  • The method generates a payment request by redirecting the user to a specific URL or using an iframe depending on your payment gateway’s integration method.

In the current implementation, createPayment() redirects the user to https://tutorlms.com/. This URL should be replaced with the actual URL where your payment gateway handles transactions.

  • Important: Replace the redirect URL with the payment gateway’s URL, and update the logic to integrate payment creation with your gateway API (e.g., sending payment data and handling responses).

How to Implement:

Retrieve the required values from the settings interface using $this->config->get('property_name'). You can get values like the secret_key, public_key, and mode that were defined in the plugin’s settings page.

Webhook and Order Data Verification (verifyAndCreateOrderData() Method):

Overview:

  • The verifyAndCreateOrderData() method receives data from the payment gateway to Tutor LMS.
  • This method verifies the webhook signature and processes the order data. Ensure that $get_data, $post_data, $server_variables, and $stream are correctly handled based on how your payment gateway sends the webhook data.

The order data should be updated to return the correct fields such as payment_status, transaction_id, and fees based on the response from your gateway.

How to Implement:

Retrieve the necessary values from the configuration interface (e.g., secret keys) and verify the webhook data accordingly. Use $returnData to return the order information.

Retrieving Data from Tutor LMS (via setData() and getData()):

The payment data sent by Tutor LMS can be accessed via the getData() method, which retrieves the details provided by the LMS when calling setData().

setData($data)

This method is used to set the payment data for the current payment object.

Parameters:
  • object $data: An object containing the payment data to be set.
Throws:
  • Throwable: If any error occurs while setting the data (i.e. if the parent setData method throws an error).

Additional Methods:

If your gateway requires extra methods for handling payments, such as refunds, cancellations, or payment status checks, add them to this class. Follow the pattern established by the existing methods to ensure consistency and reusability.

Was this helpful?