Docs

Subscriptions Articles & FAQ

How to get subscription id from webhooks for during cancel subscription.

Sample code snippet to get subscription and customer details from webhooks during cancel subscriptio

4683667

2013-11-14T12:25:08Z

2023-01-17T08:29:10Z

3034

74

6

164447

How do I get the subscription id from web-hooks during cancel subscription.

How do I get the subscription id from web-hooks during cancel subscription. 

Once you've configured your webhook URL (Settings (Configure Chargebee) » Webhooks » ), events will be posted back based on the event type. The event data will be posted as request body with content type set as application/JSON.

Sample code snippets to get subscription/customer details from the RAW POST DATA:

PHP:

$webhook_request = file_get_contents('php://input');
$event = ChargeBee_Event::deserialize($webhook_request);
$eventType = $event->eventType;  // to get the event type
$content = $event->content();
$subscription_id = $content->subscription()->id;   //get subscription ID
$customer_email = $content->customer()->email;   // get customer email ID

JAVA:

BufferedReader reader = request.getReader();
Event event = new Event(reader);
EventType eventType = event.eventType();   // to get the event type
Event.Content content = event.content();
String subscriptionId = content.subscription().id();  //get subscription ID
String customerEmail = content.customer().email();    // get customer email ID

Ruby:

event =  ChargeBee::Event.deserialize(request.body.string)
event_type = event.event_type   // to get the event type
content = event.content
subscription_id = content.subscription.id  //get subscription ID
customer_email = content.customer.email    // get customer email ID

.NET:

Event events = new Event(Request.InputStream);
EventTypeEnum eventType = (EventTypeEnum)events.EventType;  // to get the event type
Event.EventContent content = events.Content;
String subscriptionId = content.Subscription.Id;  //get subscription ID
String customerEmail = content.Customer.Email;     // get customer email ID  

Make sure you do basic authentication for the webhook URL. More details here.

Was this article helpful?
Loading…