Reputation: 464
I'm trying to do the following:
I understand that I will need to use web hooks for this and have created a test web hook to do this, which currently looks like this:
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_......");
// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);
// Do something with $event_json
http_response_code(200); // PHP 5.4 or greater
The event that I need to listen to is:
customer.subscription.trial_will_end
But how do I use this event in the web hook to get the customers ID and then add them to the plan whilst charging them at at the same time?
Kind Regards, Nic
Upvotes: 1
Views: 1073
Reputation: 17523
Depending on what exactly you want to do, you might not need to use webhooks at all.
If you want to charge a $1 setup fee when a customer subscribes, then not charge them anything for 3 months, then start charging them $x / month (or any other interval), here's what you should do:
trial_end
parameter)This will result in the following:
To answer your initial question, the customer.subscription.trial_will_end
event that is sent will include a subscription object in its data.object
attribute. You can then use this subscription object to retrieve the customer ID by looking at the customer
attribute.
The code would look something like this:
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_...");
// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);
// Verify the event by fetching it from Stripe
$event = \Stripe\Event::retrieve($event_json->id);
// Do something with $event
if ($event->type == "customer.subscription.trial_will_end") {
$subscription = $event->data->object;
$customer_id = $subscription->customer;
}
http_response_code(200); // PHP 5.4 or greater
Upvotes: 3