in Magento, How to make a payment method that sends POST variables to an external url?

in Magento, How to make a payment method that sends POST variables to an external url?

September 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
in Magento, How to make a payment method that sends POST variables to an external url?

Creating a Custom Payment Method for Magento

As an experienced developer, I understand your need to create a custom payment method that sends specific variables from a customer’s order to an external URL in Magento. This is a common requirement when integrating with third-party payment gateways.

Step-by-Step Guide to Implementing the Custom Payment Method

1. Create a Custom Model for the Payment Method

The first step is to create a custom model that will handle the logic of sending the variables to the external URL. You can follow these guidelines:

  • Extend the Magento\Payment\Model\Method\AbstractMethod class.
  • Override the canUseForCountry() and isActive() methods to determine if this payment method is active and available for a specific country.

Here’s an example of how you can create your custom model:

class MyCustomPayment extends \Magento\Payment\Model\Method\AbstractMethod
{
    public function canUseForCountry($country)
    {
        return true;
    }

    public function isActive()
    {
        return 1;
    }
}

2. Create a Configurable Payment Method

Next, you need to create the configurable payment method. This involves creating an admin configuration interface for your custom payment method and enabling it for use in checkout.

  • Create a system.xml file in your module’s etc directory.
  • Define the necessary configurations for your payment method, such as title, enabled status, order status, and any additional parameters required by the third-party gateway.

Here’s an example of how to define the configuration:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <mycustompayment>
                <title>My Custom Payment</title>
                <active>1</active>
                <order_status>processing</order_status>
                <!-- Define any additional parameters required by the third-party gateway -->
            </mycustompayment>
        </payment>
    </default>
</config>

3. Implement the Form Validation

To implement form validation for your custom payment method, you need to override the validate() method in your model.

  • Check if all the necessary variables are provided by the customer during checkout.
  • If any required variable is missing or invalid, return an error message indicating that the information is incomplete or incorrect.

Here’s an example of how to implement form validation:

public function validate()
{
    $data = $this->getInfoInstance()->getData();
    if (!$data['street']) {
        return false;
    }
    // Add additional validation logic as needed
}

4. Process the Payment and Send Variables to External URL

The final step is to process the payment in your custom payment method and send the required variables to the external URL using the POST method.

  • Retrieve the necessary variables from the customer’s order: total amount, order number, products description.
  • Use a PHP library like cURL or Guzzle to make a POST request to the external URL with the gathered information as parameters.

Here’s an example of how to send POST variables using cURL:

public function processPayment($payment = null)
{
    $curl = curl_init();
    $data = array(
        'total_amount' => $payment->getOrder()->getGrandTotal(),
        'order_number' => $payment->getOrder()->getIncrementId(),
        // Add other required variables
    );

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://third-party-gateway.com/payment-process", 
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            "Content-Type: application/x-www-form-urlencoded",
        ),
        CURLOPT_POSTFIELDS => http_build_query($data),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        // Handle the response from the third-party gateway
    }

    curl_close($curl);
}

Conclusion

Creating a custom payment method in Magento that sends variables to an external URL requires careful implementation of the model, configuration, form validation, and processing logic. By following this step-by-step guide, you can effectively integrate your custom payment method with the third-party gateway.

Remember to thoroughly test your custom payment method in various scenarios to ensure its reliability and compatibility with your specific requirements.

For further reference on Magento development best practices, feel free to explore our comprehensive guides at Laravel Company.

Checkout Extensions Admin Configuration

Our network