psy Posted October 20, 2016 Share Posted October 20, 2016 I wanted to view the contents of a JSON post in a web hook from an external application. In this instance the source application was Stripe posting event info at irregular intervals to a PW page URL. The process had to be unobtrusive. Solution was to send an email to myself. The web hook page template contained: // create a PW mail object using whatever method works for you $mail = wire()->modules('WireMailSmtp'); // Retrieve the request's body and parse it as JSON $stripe_input = @file_get_contents("php://input"); $event_json = json_decode($stripe_input); try { $body = "<pre>" . var_export($event_json, true) . "</pre>"; $mail->to('my@emailaddress.com'); $mail->from('from@emailaddress.com'); $mail->subject('test event_json'); $mail->bodyHTML($body); $mail->send(); } catch (\Exception $e) { $error = "Email not sent: " . $e->getMessage(); $mail->log($error); } Resulting email body contains nicely formatted code, eg: stdClass::__set_state(array( 'id' => 'evt_XXXXXXXXXXXXXXXX', 'object' => 'event', 'api_version' => '2016-07-06', 'created' => 1476900798, 'data' => stdClass::__set_state(array( 'object' => stdClass::__set_state(array( 'id' => 'sub_XXXXXXXXXXXXXXXX', 'object' => 'subscription', 'application_fee_percent' => NULL, 'cancel_at_period_end' => false, 'canceled_at' => NULL, 'created' => 1476900796, 'current_period_end' => 1508436796, 'current_period_start' => 1476900796, 'customer' => 'cus_XXXXXXXXXXXXXXXX', 'discount' => NULL, 'ended_at' => NULL, 'livemode' => true, 'metadata' => stdClass::__set_state(array( )), 'plan' => stdClass::__set_state(array( 'id' => 'annual', 'object' => 'plan', 'amount' => 8000, 'created' => 1474521586, 'currency' => 'usd', 'interval' => 'year', 'interval_count' => 1, 'livemode' => true, 'metadata' => stdClass::__set_state(array( )), 'name' => 'Annual', 'statement_descriptor' => NULL, 'trial_period_days' => NULL, )), 'quantity' => 1, 'start' => 1476900796, 'status' => 'active', 'tax_percent' => NULL, 'trial_end' => NULL, 'trial_start' => NULL, )), )), 'livemode' => true, 'pending_webhooks' => 1, 'request' => 'req_XXXXXXXXXXXXXXXX', 'type' => 'customer.subscription.created', )) 5 Link to comment Share on other sites More sharing options...
opalepatrick Posted August 9, 2021 Share Posted August 9, 2021 Thanks for this. Very useful for debugging 1 Link to comment Share on other sites More sharing options...
opalepatrick Posted August 10, 2021 Share Posted August 10, 2021 Just out of curiosity, have you ever had a situation where stripe post data is being sent correctly (in the sense that the required event json info is posted and shows as a response in the dashboard) but when I try to parse the post variable with file_get_contents etc, it turns up NULL? Link to comment Share on other sites More sharing options...
psy Posted August 11, 2021 Author Share Posted August 11, 2021 @opalepatrick not in my experience. One thing to check is to ensure you have the ampersand at the front of @file_get_contents to ensure it's by reference. Link to comment Share on other sites More sharing options...
opalepatrick Posted August 12, 2021 Share Posted August 12, 2021 Do you mean the @ symbol @psy as opposed to & ? If so, then yes I have done that but with no difference to the outcome. Thanks for replying. I have started a new question so I don't mess with your thread ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now