Welcome back, this part 5 of our WooCommerce Payment Gateway series. In the previous article, we were able to get our payment number and place it inside our admin area at the back-end inside the orders. Today, we shall do a sandbox test Payments API for WooCommerce payment gateway
We’re going to work with an API that’s basically going to receive information that can be sent as JSON.
If you don’t know how to work with APIs, this is a playlist of videos showing how to interact with different APIs using WordPress as a Content Management System.
API Review
We shall use this API that does mobile payments. However, you have to signup to get different credentials, then you will make a widget and you will also get an API key.
The API key and widget key allow your application to be authenticated.
This mobile gateway also needs your phone because that’s the default payment method. It needs the mobile money company id, the reason and the amount.
the rest of the other things are just optional if you at the documentation.
Demo and dummy API query of the Endpoint.
Before we plug this API into our payment gateway, we shall do a demo and dummy API query of that endpoint to see whether our information is going, then I will get that static information and make it dynamic all through. This is what I have done with this plugin.
Learn how to query an API from this video
Test Payments API Plugin Review
We shall open a custom page menu.
When you activate the API plugin, you will notice that we have a menu item that says API Test.
In the code, I opened an admin’s page and I called it API settings.
function wpdocs_register_my_custom_menu_page() {
add_menu_page(
__( 'API Test Settings', 'textdomain' ),
'API Test',
'manage_options',
'api-test.php',
'get_send_data',
'dashicons-testimonial',
85
);
}
You will see API Test in the admin menu. You will also notice that it runs it’s own page id api-test.php It runs the callback function get_send_data. I added a dash icon and gave it a position of 85 so that it is at the bottom. The message icon is coming from dashicons-testimonial
Getting the Test Payments API to work
Firstly, we need to post t a URL, then pass in our API key and widget key.
$url = 'https://e.patasente.com/phantom-api/pay-with-patasente/' . $api_key . '/' . $widget_key . '?phone=' . $phone . '&amount=' . $amount . '&mobile_money_company_id=' . $network_id . '&reason=' . 'Test';
When you look again at the API, you will see that there is a question mark that allows us to start adding different parameters which include;
- An Email that is optional
- The amount which is required
- The phone number which is also required in 13 digits.
- The mobile company id which either has to be a 1 or 2. 1 for MTN and 2 for Airtel Uganda.
- And then we also need to pass a reason that is stored at the back-end for this payment.
Now we’re just going to have some dummy data that we’re going to put in and see whether this works out for us.
My API key but this won’t work for you so please get your own API key, see the needs that it requires and work on it.
I have put my API key, widget key, a phone number that is 13 characters, the amount that I will be charged, the network id which is for MTN so we shall see how we can filter that and make it a function that will always allow us to detect what network it is and it will automatically send it to the right Network ID.
I have also put a reason which is Test
The following code snippet shows my API key, widget key, phone number and amount that I will be charged.
$api_key = '5384z9XT7';
$widget_key = '53525880bd675362d449b60185f82ddf';
$phone = '2578288658885555';
$amount = 500;
$network_id = '1'; // mtn
$reason = 'Test';
I am chaining all of that through this URL
$url = 'https://e.patasente.com/phantom-api/pay-with-patasente/' . $api_key . '/' . $widget_key . '?phone=' . $phone . '&amount=' . $amount . '&mobile_money_company_id=' . $network_id . '&reason=' . 'Test';
I have put all things that usually change in variables and chained them to the URL
Remember to replace the variables with your own API key, widget key and phone number.
Wp remote post function
The next thing I will use is wp_remote_post which is a default WordPress function. I have passed in our URL plus one extra thing which is the timeout that we need to have.
Most times APIs are slow when you send the data and by default, this is usually 5seconnds. However, you will find that an API sometimes requires 15 or 20 seconds or whatever it is. So we shall throw a huge timeout to see what really happens.
Eventually when we get our response from the URL call, if it’s an error, we return an error message and throw it at the back-end but it works out, then we get our response and dump it at our back-end. so that’s what the code is doing basically helping us demo that information.
$response = wp_remote_post( $url, array( 'timeout' => 45 ) );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
} else {
echo '<pre>';
var_dump( wp_remote_retrieve_body( $response ) );
echo '</pre>';
}
Testing the Query API Plugin
Let’s go and check what it does.
Activate the Query APIs plugin
Hit the Query Test admin menu item. On clicking it, you will receive a message telling you to make the payment and if you make that payment, then the message will be stored.
Conclusion
So that means our API is working with all the variables that we added. Then we’re ready to have your API integrated inside our WooCommerce payment gateway.
Find all the code here on Github
Proceed to Part 6 where we shall be Integrating Mobile Payments API into Custom WooCommerce Payment Gateway from Sandbox Test Area.
Leave a Reply