Magento 2: Payment Method Visibility Control via API
When developing Magento extensions or custom payment gateways for your store, you might face requirements to keep certain payment methods hidden from the frontend but available through the API. This allows you to offer special pricing, promotions, or restricted payment options without cluttering your store’s checkout process.
In this guide, I will demonstrate how to achieve this in Magento 2 using a combination of module configuration and API usage.
Step 1: Enable the Payment Method
First, ensure that the payment method is enabled in your Magento admin. Navigate to Stores > Configuration > Sales > Payment Methods and activate the desired payment gateway.
Step 2: Configure Payment Method Visibility
By default, Magento displays all active payment methods on the checkout page. To hide a specific payment method from the frontend while keeping it available via API, you can modify its configuration.
-
Open
app/code/Vendor/Module/etc/config.xml(replace Vendor and Module with your actual vendor and module names). -
Locate the
<payment_methods>section containing the desired payment method’s configuration.
<payment_methods>
<method_code active="0">
<title>Hide this payment method</title>
<!-- other settings -->
</method_code>
</payment_methods>
- Set
active="0"to disable the payment method from appearing on the frontend. Save the file.
Step 3: Access Hidden Payment Methods via API
With the payment method hidden, you can now access it programmatically using Magento’s REST or GraphQL APIs. Here are some examples:
REST API Example
To retrieve a list of all payment methods, including the hidden one, send a GET request to the following endpoint:
GET https://your-magento-store.com/rest/V1/config/payment-methods
You will receive JSON data containing all available payment methods.
GraphQL API Example
For more advanced queries and mutations, you can use Magento’s GraphQL API. Here’s an example of a query that fetches the list of payment methods:
{
config {
paymentMethods {
method_code
title
active
}
}
}
This query will return a list of all payment methods, including those with active="0".
Step 4: Programmatic Payment Method Selection
To select a hidden payment method programmatically during checkout, you can use the quote_payment_method API endpoint. Send a POST request to:
POST https://your-magento-store.com/rest/V1/guest-carts/{cart_id}/payment-methods
Include the desired payment method code in the JSON payload:
{
"method": "hide_this_payment_method"
}
Replace {cart_id} with your customer’s cart ID and update method with your hidden payment method’s code.
Conclusion
In Magento 2, hiding a payment method from the frontend while keeping it accessible via API requires a combination of configuration changes and programmatic selection during checkout. By following this approach, you can offer tailored payment options to specific customers or promotions without cluttering your store’s user interface.
Remember that proper security measures should be in place when dealing with API access, especially for sensitive data like hidden payment methods.
For more information on Magento 2 API development and customization, visit https://laravelcompany.com/magento-2-api-development to explore tutorials and best practices from our team of experienced developers.