Home > OS >  Stripe Webhook integrate with Wordpress doesn't work
Stripe Webhook integrate with Wordpress doesn't work

Time:08-03

I've created a webhook from Stripe dashboard with this url: "https://womanverse.ro/stripewebhooks/v1/task", listening to these 3 events:

charge.succeeded
checkout.session.async_payment_succeeded
checkout.session.completed

I don't receive any event, even if the checkout is completed succesfully. The error rate is 100%.

    function stripe_task() {
       
    // webhook.php
    //
    // Use this sample code to handle webhook events in your integration.
    //
    // 1) Paste this code into a new file (webhook.php)
    //
    // 2) Install dependencies
    //   composer require stripe/stripe-php
    //
    // 3) Run the server on http://localhost:4242
    //   php -S localhost:4242
    
    require 'vendor/autoload.php';
    
    // This is your Stripe CLI webhook secret for testing your endpoint locally.
    $endpoint_secret = 'we_1LRN7DRPLRnj1UZwK5c';
    
    $payload = @file_get_contents('php://input');
    $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
    $event = null;
    
    try {
      $event = \Stripe\Webhook::constructEvent(
        $payload, $sig_header, $endpoint_secret
      );
    } catch(\UnexpectedValueException $e) {
      // Invalid payload
      http_response_code(400);
      exit();
    } catch(\Stripe\Exception\SignatureVerificationException $e) {
      // Invalid signature
      http_response_code(400);
      exit();
    }
    
    // Handle the event
    switch ($event->type) {
      case 'checkout.session.completed':
        $session = $event->data->object;
      // ... handle other event types
      default:
        echo 'Received unknown event type ' . $event->type;
    }
    
    http_response_code(200);
} 

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
            }  
        ));
    }
);

So this is the code added in my-theme, functions.php file to create an endpoint from wordpress documentation. Why the error rate is 100% and I don't receive any notification?

enter image description here

enter image description here

CodePudding user response:

The error rate was 100% because I was receiving error 404 due to incorrect url. I added https://womanverse.ro/?rest_route=/stripewebhooks/task "?rest_route=".

Then I was receiving error 500 and 400, solved that by changing the code with this:

function stripe_task() {

    //require 'vendor/autoload.php';
   
    // This is your Stripe CLI webhook secret for testing your endpoint locally.
    $endpoint_secret = 'whsec_C8V8uGGkVnpRO6nL';
    
    $payload = @file_get_contents('php://input');
    $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
    $event = null;
    
    try {
      $event = \Stripe\Webhook::constructEvent(
        $payload, $sig_header, $endpoint_secret
      );
    } catch(\UnexpectedValueException $e) {
      // Invalid payload
      http_response_code(400);
      exit();
    } catch(\Stripe\Exception\SignatureVerificationException $e) {
      // Invalid signature
      http_response_code(400);
      exit();
    }
    
    // Handle the event
    switch ($event->type) {
      case 'checkout.session.completed':
        $session = $event->data->object;
         echo $session;
         http_response_code(200);
      // ... handle other event types
      default:
        echo 'Received unknown event type ' . $event->type;
    }
    
    http_response_code(200);
}
  • Related