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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By ngrmm
      I would like to create pages from a json feed.
      So i decode my json and create them via API.
      $jsonData = json_decode($jsonFromOtherWebsite); foreach($jsonData as $jsonDataItem) { $pageTitle = $jsonDataItem->name; $p = new Page(); $p->template = 'import_page'; $p->parent = $pages->get(xxxx); $p->title = $pageTitle; $p->save(); } Let's say the source (json) changes and i have to do another import.
      Then I want to compare the new json with the existing pages to see if there are new ones and if there some aren't there anymore.

      Is there a way to compare the new JsonData with my existing pw-pages with the API.
      Something like
      foreach($jsonData as $jsonDataItem) { // check if a page with this title exist if($pages->find("template=import_page, title=$jsonDataItem->name") { // update existing field values $getExistingPage = $pages->find("template=import_page, title=$jsonDataItem->name"); // update value $getExistingPage->setAndSave('field', $jsonDataItem->x); } else { // create new page $pageTitle = $jsonDataItem->name; $p = new Page(); $p->template = 'import_page'; $p->parent = $pages->get(xxxx); $p->title = $pageTitle; $p->save(); } } // search for pages wich are not anymore in the json and hide/delete them // …  
    • By jploch
      Hey folks,
      for a module (a pagebuilder based on PageTable) I need to save some settings as JSON. The values are saved for each page table item (a pw page). It's working well, but I am looking for ways to improve the structure I have. As I'm not that experienced with JSON, maybe someone more experienced can take a look and tell me if my approach is good practice. 

      My goal is to make all the items accessible by page id, without looping over them (using objects instead of arrays):
      // access from template with pw page var $jsonObject->items->{$page}->cssClass; Her is an example of my JSON structure:
      { "items": { "3252": { "id": "3252", "cssClass": "pgrid-main", "breakpoints": { "base": { "css": { "grid-column-end": "auto", "grid-row-end": "auto", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "60px", "padding-right": "60px", "padding-top": "60px", "padding-bottom": "60px", "background-color": "rgb(255, 255, 255)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3686": { "id": "3686", "cssClass": "test_global", "breakpoints": { "base": { "css": { "grid-column-end": "-1", "grid-row-end": "span 1", "grid-column-start": "1", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3687": { "id": "3687", "cssClass": "block_editor-3687", "breakpoints": { "base": { "css": { "grid-column-end": "span 2", "grid-row-end": "span 1", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3696": { "id": "3696", "cssClass": "block_editor-3696", "breakpoints": { "base": { "css": { "grid-column-end": "span 2", "grid-row-end": "span 1", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } } }, "breakpointActive": "base", "breakpointActiveSize": "@media (min-width: 576px)" }  
    • By hellerdruck
      Hi all
      I need to export all the texts from a website to a translation company (as json or csv or txt...). How can this be done? Of course manually, but this website is huge and it would take me years...
      Also, as a second step, importing the translation ...
      Any ideas anyone? Tutorials? Plugins?
      Thanks for your help.
    • By abdulqayyum
      hello processwire community,
      i am using LoginRegister module for front-end user registration. i am facing an issue verification email not receiving while WireMailSmtp is working perfectly and user receives message
      "Thank you, a confirmation code has been emailed to you. When you receive the email, click the link it contains, or paste the confirmation code below."
      received this message in front-end but does not receive any email relevant it.
      please help me in this case how i can solve it.
      Please check screen shots for SMTP test and message which receives user.
      Thanks AbdulQayyum,

      First screenshot SMTP test message, second when user receives message after clicking register button.
    • By louisstephens
      So I have been hard at work creating url segments for a template (api) and everything is working swimmingly in creating a simple end point for svelte.js. I have however, run into a few questions that I can wrap my head around.
      In my api template I have:
      if($input->urlSegment1 === 'clients') { header('Content-Type: application/json'); $clients = $pages->find("template=clients"); $client_array = array(); foreach ($clients as $client) { $id = $client->id; $title = $client->title; $url = $client->url; $clientName = $client->client_name; $clientColor = $client->client_color->value; $assigned = $client->assigned_to->user_full_name; $client_array[] = array( 'id' => $id, 'code' => $title, 'name' => $clientName, 'associated_users' => $assigned, 'url' => $url ); } $client_json = json_encode($client_array, true); echo $client_json; } The output json from this is:
      [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was curious is it possible to add in "clients" before this output json so it would appear as 
      clients: [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was not really sure of how to tackle this in my php code, and have spent more time than I care to admit trying to figure it out. Another question I have is that "associated_users" is returning null, which in this instance is correct. It is a multi page field that is set to pull a custom name field from the users template, ie "Louis Stephens" would be associated with the first page. I understand that I need to use a foreach to get the correct data, but I was really unsure of how to place this inside an array, or update the array with the new data. Any help with any of this would greatly be appreciated.
×
×
  • Create New...