I have set up a custom Woocommerce off-side payment gateway, the communication from my website to the Gateway works fine. The user gets redirected to the gateway and after the payment he gets returned to the (by Woocommerce) defined return URL.
In the API documentation of the gateway, it says that the payment gateway will add a query string variable to the return url. This variable can have the following values:
response=approved
response=declined
response=error
In the documentation is this exampple:
If backURL = http://www.test.com/return.asp
Then the response will be http://www.test.com/return.asp?response=approved
To process the order I have created the code below (first just for the state of response=approved
)
However, the order status does not get updated, when I test it. I hope somebody can point me in the right direction to find the error.
On top of the gateway I have extended query_vars with:
function add_query_vars_filter( $vars ){
$vars[] = "response";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
After sending the user to the gateway I have this function to handle and process the query string in the return URL:
function check_example_response(){
global $woocommerce;
$transaction_id = get_query_var( 'response' );
$order = new WC_Order( $order_id );
// check if payment was successful
if ($transaction_id == 'approved') {
//Update order status
$order->update_status( 'processing', 'Payment received, your order is currently being processed.' );
//Add admin order noote
$order->add_order_note('Payment Via Example Gateway<br />Transaction ID: '.$order);
//Add customer order note
$order->add_order_note('Payment Received.<br />Your order is currently being processed.<br />We will be shipping your order to you soon.<br />Transaction ID: '.$order, 1);
$message = 'Thank you for shopping with us.<br />Your transaction was successful, payment was received.<br />Your order is currently being processed.';
$message_type = 'success';
}
// Reduce stock levels
$order->reduce_order_stock();
// Empty cart
WC()->cart->empty_cart();
}}
I have also registered this function with:
add_action( 'woocommerce_api_wc_example', array( $this, 'check_example_response' ) );
WordPress also does not give any hints in DEBUG mode, so I am right now quite frustrated and hope somebody can help me.
Thank you!