TL;DR WooCommerce callback url that run a class with the same name broke. Like I could access my
valitorcallback
class by going tohttp://mywebsite.com/wc-api/valitorcallback
, is there way to enable that url or somehow access this class?
I developed a WooCommerce payment gateway plugin to connect to an Icelandic paypal like company Valitor.
The payment flow is simple:
- Customers add items to his cart and goes to checkout.
- Redirect customer of a web-store to Valitor with order info in the parameters of the request.
- Customer pays with his credit card or his debit card.
- The customer is redirected to provided thank-you page and Valitor calls another url (the callback) to notify that the payment is complete and with the order info and verification codes/method.
The only thing the plugin does is step 2 and 4. In step 4, if the order is valid then the plugin lower the stock and change the state of the order to says the payment has been completed.
The problem is after a WooCommerce update about 2 months ago the callback url broke, probably because of security reasons. I have not been able to find a code to enable this url again or to solve this. It think it can be done with add_action method or some hooks but I’ve not been able to get that to work.
Here is the guide I think is the key but I’m doing something wrong: http://docs.woothemes.com/document/payment-gateway-api/#section-4
The Code plugin structure is like this:
<?php
function initWooCommerceValitorGatewayPlugin()
{
class WooCommerceValitorGateway extends WC_Payment_Gateway
{
public function __construct()
{
// ...Varible code...
// Actions
add_action('woocommerce_update_options_payment_gateways_'.$this->id, array($this, 'process_admin_options'));
}
public function init_form_fields()
{
// ...Define settings code...
}
public function process_payment($orderId)
{
// ...Magic code...
// Redirect to Valitor with all necessary data
return array(
'result' => 'success',
'redirect' => add_query_arg($valitorData, $gatewayUrl)
);
}
// ...Helper functions code (like sending an email)...
}
}
class valitorcallback
{
public function __construct()
{
$this->verifyPayment();
}
public function verifyPayment()
{
// ...Verification code...
}
// ...Helper functions code (like sending an email)...
}
// Add plugin to wordpress/woocommerce
add_action('plugins_loaded', 'initWooCommerceValitorGatewayPlugin');
function addValitorGateway($methods)
{
$methods[] = 'WooCommerceValitorGateway';
return $methods;
}
// Add gateway method to woocommerce
add_filter('woocommerce_payment_gateways', 'addValitorGateway');
?>
Now you see the functions name and maybe can tell me where should add_action be placed.
I know of the mywebsite.com/wc-api/v3/… REST API but I was hoping that I could just enable the url again so I don’t have to code that part again and I’m not sure how to get that plugin settings from another file.
Thanks, Sigurður
EDIT: added TL;DR section at top and bold when I state the problem.
I found it, don’t know why I didn’t find it other than I was not using the right search words on google.
But here is it: WooCommerce Custom Payment Gateway
I don’t know why this is not mentioned in the docs but this is the action hook you’ll need:
I needed to move sum functions from the second class to the first and then have that right name of the action to make it work. It still returns
-1
but the right code is executed with all the GET variables from the url.Also I change the class name to
WC_Valitor_Gateway
and the plugin structure is like this: