Jump to content

Capturing post data to PW from an external application


psy
 Share

Recommended Posts

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',
))


 

  • Like 5
Link to comment
Share on other sites

  • 4 years later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...