I have a wordpress website hosted on GoDaddy.
I am an advanced stripe user and have integrated stripe with many Ruby on Rails apps , along with stripe-webhook integration with the Rails. Also i am well versed in how web-hooks work. But recently i was made owner of a wordpress website hosted on GoDaddy and on that website i am supposed to receive stripe payment failed webhook and then trigger an email based on that webhook event. I am not able to make much connect with wordpress and stripe from online resources and need help on how to receive stripe-webhooks in wordpress website i.e where to put code to make that happen etc.
I have a wordpress website hosted on GoDaddy.
I am an advanced stripe user and have integrated stripe with many Ruby on Rails apps , along with stripe-webhook integration with the Rails. Also i am well versed in how web-hooks work. But recently i was made owner of a wordpress website hosted on GoDaddy and on that website i am supposed to receive stripe payment failed webhook and then trigger an email based on that webhook event. I am not able to make much connect with wordpress and stripe from online resources and need help on how to receive stripe-webhooks in wordpress website i.e where to put code to make that happen etc.
Share Improve this question asked Oct 13, 2016 at 16:51 Sahil DhankharSahil Dhankhar 1431 gold badge1 silver badge5 bronze badges 2- You could try using the Woocommerce Stripe plugin for testing purposes and review the code for ideas. – Dave Romsey Commented Oct 13, 2016 at 19:20
- 1 @DaveRomsey looks like they are not supporting webhooks as of now – Sahil Dhankhar Commented Oct 13, 2016 at 19:36
3 Answers
Reset to default 5I recently had the same problem and pippins stripe integration plugin seemed to answer it but it had a lot of extra code I did not need so I removed it and made a concise version just for the webhook integration: WPStripeWebhook. README is self explanatory. Basically make changes to includes/stripe_listener.php for your events. Also attaching readme here as per stackoverflow guidelines:
Usage:
Copy the complete folder WPStripeWebhook in wp-content/plugins. Go to website admin page.
Activate the WP Stripe webhook plugin for plugins section.
- After this Settings will start showing Stripe webhook settings section. Click on it. In the page fill the stripe keys and check test mode option if you want to test the plugin.
- In WPStripeWebhook/includes/stripe_listener.php, make changes for your
event type and email or whatever you want to do in response to
an event. It currently sends out an email.
Important notes and suggestions For live mode, add stripe webhook endpoint (stripe account -> settings -> account settings -> webhook) like this
htps://yourdomain?webhook-listener=stripe
For testing locally on your machine, you can use Ultrahook. Its awesome! Set up your keys and username and start ultrahook on your machine using:
ultrahook -k your_ultrahook_key stripe 8888
Add a webhook endpoint url in your stripe account similar to this:
htp://stripe.your_ultrahook_username.ultrahook/your_wp_website_folder_name/stripe-listener.php?webhook-listener=stripe
And it should start working for you. Also, you might see 404 in ultrahook console. Just ignore it. I would suggest setting up debugging too. It really helps. For debugging, add these to your wp_config.php
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
define('WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('SCRIPT_DEBUG', true );
After this, you should see a debug.log file in your wp-content folder and it will display errors and warnings and whatever you print using error_log()
For anyone interested. This can also be done fairly easily without a plugin.
- First add an endpoint in stripe. https://example/payment-failed
- Create a new wordpress page called Payment Failed with the same url.
- In your theme folder, create a new php file called page-payment-failed.php and write all of your webhook response code in here. This file will automatically be run when Stripe tries to access https://example/payment-failed.
A nice clean way of doing is using adding a custom endpoint to wordpress REST API
https://developer.wordpress/rest-api/extending-the-rest-api/adding-custom-endpoints/
function stripe_task() {
include_once ABSPATH . 'path/to/stripe/autoloader/or/init'; //change
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
// Handle the event
switch ($event->type) {
case 'customer.subscription.created':
// do something!
break;
// other type cases you wish to handle
// ...
// catch all
default:
http_response_code(400);
exit();
}
http_response_code(400);
}
add_action('rest_api_init',
function () {
register_rest_route( 'stripewebhooks/v1', '/task', array(
'methods' => 'POST',
'callback' => 'stripe_task',
'permission_callback' => function () {
return true; // security can be done in the handler
}
));
}
);
Then add the endpoint to Stripe
e.g. https://your-site/?rest_route=/stripewebhooks/v1/task
You can also increase security with signature verification: https://stripe/docs/webhooks/signatures