Skip to content
Advertisement

Trying to connect to Stripe API could someone take me through what this webhook is doing step by step (php)

As the title says I’m trying to use webhooks to automate some of the Stripe events which occur when a customer goes through the subscription process.

In my webhooks.php file these are the bits of code that I don’t understand:

/* The are my comments */

// These are comments from the Stripe docs

/* First snippet of code - Not sure what this does? */
$payload = @file_get_contents('php://input');

/* Second snippet of code - Not sure what this does? 
what is the $event variable refering to and why is $payload being 
passed into it? */

$event = null;
try {
    $event = StripeEvent::constructFrom(
        json_decode($payload, true)
    );
} catch (UnexpectedValueException $e) {
    // Invalid payload
    echo '⚠️  Webhook error while parsing basic request.';
    http_response_code(400);
    exit();
}

/* Third snippet of code - I understand the switch and case keywords to 
basically be if and else statements so if the type of the $event variable is 
equal to 'customer.subscription.trial_will_end' then run '$subscription = 
$event -> data -> object'. However what is the $subscription variable being 
assigned to in this case (what does $event -> data -> object mean?) */

// Handle the event
switch ($event->type) {
    case 'customer.subscription.trial_will_end':
        $subscription = $event->data->object; // contains a StripeSubscription
        // Then define and call a method to handle the trial ending.
        // handleTrialWillEnd($subscription);
        break;
    case 'customer.subscription.created':
        $subscription = $event->data->object; // contains a StripeSubscription
        // Then define and call a method to handle the subscription being created.
        // handleSubscriptionCreated($subscription);
        break;
    case 'customer.subscription.deleted':
        $subscription = $event->data->object; // contains a StripeSubscription
        // Then define and call a method to handle the subscription being deleted.
        // handleSubscriptionDeleted($subscription);
        break;
    case 'customer.subscription.updated':
        $subscription = $event->data->object; // contains a StripeSubscription
        // Then define and call a method to handle the subscription being updated.
        // handleSubscriptionUpdated($subscription);
        break;
    default:
        // Unexpected event type
        echo 'Received unknown event type';
}

/* Fourth snippet of code - Why do we need to set this? */
http_response_code(200);

Advertisement

Answer

/* First snippet of code - Not sure what this does? */
$payload = @file_get_contents('php://input');

It reads the request body of the HTTP POST request that this script is receiving(a webhook handler is receiving a HTTP request Stripe sends your server). This is a standard PHP idiom.

/* Second snippet of code - Not sure what this does? 
what is the $event variable refering to and why is $payload being 
passed into it? */

$event is the meaningful Event(https://stripe.com/docs/api/events/object) object that is the body of the webhook request. Stripe’s PHP library has a function constructEvent to take the incoming request body and parse/convert it to an object you can work with, that’s what that code is doing.

However what is the $subscription variable being 
assigned to in this case (what does $event -> data -> object mean?) */

The Event contains a ‘payload’ object which is the actual API object the event is about. For example if a Subscription is created, you get a customer.subscription.created event and that payload is the Subscription object. It’s all covered in Stripe’s docs. https://stripe.com/docs/api/events/object#event_object-data-object /// https://stripe.com/docs/api/events/types#event_types-customer.subscription.created

/* Fourth snippet of code - Why do we need to set this? */

Because after receiving this incoming request from Stripe you have to reply back to let them know you got it. https://stripe.com/docs/webhooks/build#acknowledge-events-immediately

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement