Jump to content
psy

Capturing post data to PW from an external application

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 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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.
    • By louisstephens
      So I reread my first draft, and it made absolutely no sense (I deleted it to hopefully better explain myself).  I am trying to make a system (that to me is a bit complicated) utilizing jquery and processwire together. My whole goal is to put a url like https://domain.com/launch?first_name=jim&occupation=builder in a script tag on another site(just a localhost .php page) to then pull out the data for that person and append to divs etc. Basically, the initial script tag would point to "launch" which has a content-type of "application/javascript". Using jquery, I would pull out the persons name and occupation and then make a specific ajax get request to "domain.com/api" (in json format) for a look up of the person. Essentially then I could pull that particular person's information from the json data, and do with it how I please in the "launch" page.  In processwire, I have a page structure like:
      People -Jim Bob (template: person ) --Occupations (template: basic-page) ---Builder (template: occupation) ---Greeter (template: occupation) It is really just a bunch of people with their occupations and a few fields to the occupation template. With the "api" (template: api) url, I was hoping to return all the data (of people) in json format like:
      Example Format:
      { "id": 1, "title": "Jim Bob", "occupations": { "builder": { "id": 44, "title": "Builder", "years_worked": 1, "etc": "ect", }, "Greeter": { "id": 44, "title": "Greeter", "years_worked": 1, "etc": "ect", }, } } Where I get lost is really outputting the page names and nesting in the occupations into json. I have used Pages2JSON before, but I was a bit lost on how to implement what i was thinking.
       
      I have access to all the local host files, but I was hoping to kind of build out a "system" where I could place the script tag/parameters in any project, and be able to interact with the data without doing an ajax call on the actual site. In a way, this would keep processwire handling all the data and requests, and my other "projects" just with a simple script tag. This might all be way too much/over complicated, but I couldn't quite wrap my head around how to achieve it. 
    • By Arunesh Dutta
      Hello all
      I am newbie.Wanted to know does processwire will allow to display external website content and other sources to my website using API powered by processwire
    • By rafaoski
      Hi everyone ...
      Generally, this is about the new mailHTML() function that does not work correctly on processwire 3.0.110
      https://github.com/processwire/processwire/blob/dev/wire/core/WireMailTools.php#L291
      https://processwire.com/api/ref/mail/mail-h-t-m-l/
      I do not know if I'm making a mistake trying to send an e-mail or if it's a bug in the core
      This is my code:
      <?php
      // $m = $mail->new(); // option A
      // $m = new WireMail(); // option B
         $m = wireMail(); // option C
         $m->mailHTML('alex@gmail.com', 'Hello', '<html><body><h1>Message Body</h1></body></html>');
         $m->send();
      ?>


      Details from Tracy Debugger
      SERVER DETAILS
      ProcessWire: 3.0.110
      PHP: 7.2.8
      Webserver: Apache/2.4.34 (Unix)
      MySQL: 10.1.34-MariaDB
      SERVER SETTINGS
      allow_url_fopen: 1
      max_execution_time: 120 (changeable)
      max_input_nesting_level: 64
      max_input_time: 60
      max_input_vars: 1000
      memory_limit: 256M
      post_max_size: 8M
      upload_max_filesize: 20M
      xdebug:
      xdebug.max_nesting_level:
      mod_rewrite: 1
      mod_security: *confirmed off
      GD: 2.2.5
      GIF: 1
      JPG: 1
      PNG: 1
      EXIF Support:
      FreeType: 1
      Imagick Extension:

      MODULE DETAILS
      TracyDebugger: 4.11.13
×
×
  • Create New...