The original idea behind the plugin is to collect payment data from customers to process them offline. However, we have many customers that using the API Request feature to integrate WooCommerce with other 3rd party payment gateways. It’s important to say that we don’t claim that the plugin can integrate any 3rd payment gateway out of the box. To make the plugin as flexible as possible we added more features for connecting to APIs.
The WooCommerce API parameters are very simple you add a parameter to the request, each parameter has a key and value. For example, if you added Order Total, you can assign a key to it, for example, total_amount then the plugin automatically will assign this key to the Order Total value. total_amount => 35.5 and the same for all of the other values.
Moreover, we created a WordPress filter in order to add any custom API parameters you need.
<?php
add_filter('custom_payment_gateways_api_data', 'add_more_parameters', 10, 2);
function add_more_parameters($wc_parameters, $order_id){
$wc_parameters['checksum'] = md5($order_id);
// you can modify/add more parameters but always return the $wc_parameters
return $wc_parameters;
}
If you need to redirect the user back to your website and either mark the Order as complete or failed. You need to provide the return URL to your payment provider API. Each provider has different keys for the parameters, you always need to check your payment provider’s API documentation.
At the success or failure URL at the Extra API Parameters option, the key should be as in your 3rd payment gateway documentation and the value should be something like this: https://yourdomain.com/?wc-api=wc_custom_payment_gateway
To process the incoming request from your payment gateway you need to use the custom_payment_process_returned_result action. The following is an example of how to process a failed notifications. Feel free to adjust according to your payment gateway documentation.
<?php
add_action('custom_payment_process_returned_result', 'process_3rd_gateway_orders');
function process_3rd_gateway_orders(){
// assuming that the 3rd party payment send the status of the payment as 'status' in the request body
if(isset($_REQUEST['status']) && $_REQUEST['status'] === 'failure'){
$order = new WC_Order($_REQUEST['order_id']);
$order->update_status('failed');
$error = $_REQUEST['error_Message'];
$order->add_order_note( sprintf( "Payments Failed: '%s'", $error ) );
wc_add_notice( $error, 'error' );
wp_redirect( wc_get_checkout_url() );
exit;
}
}