[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/balanced-php/example/ -> events-and-callbacks.php (source)

   1  <?php
   2  /*
   3   * Welcome weary traveller. Sick of polling for state changes? Well today have
   4   * I got good news for you. Run this example below to see how to get yourself
   5   * some callback goodness and to understand how events work.
   6  */
   7  require(__DIR__ . "/vendor/autoload.php");
   8  
   9  Httpful\Bootstrap::init();
  10  RESTful\Bootstrap::init();
  11  Balanced\Bootstrap::init();
  12  
  13  // create a new marketplace
  14  $key = new Balanced\APIKey();
  15  $key->save();
  16  Balanced\Settings::$api_key = $key->secret;
  17  $marketplace = new Balanced\Marketplace();
  18  $marketplace->save();
  19  
  20  // let"s create a requestb.in
  21  $ch = curl_init("http://requestb.in/api/v1/bins");
  22  curl_setopt($ch, CURLOPT_POST, true);
  23  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  24  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  25          'Content-Type: application/json',
  26          'Content-Length: ' . 0)
  27  );
  28  $result = json_decode(curl_exec($ch));
  29  $bin_name = $result->name;
  30  $callback_url = "http://requestb.in/" . $bin_name;
  31  $requests_url = "http://requestb.in/api/v1/bins/" . $bin_name . "/requests";
  32  
  33  printf("let's create a callback\n");
  34  $marketplace->createCallback($callback_url);
  35  
  36  printf("let's create a card and associate it with a new account\n");
  37  $card = $marketplace->cards->create(array(
  38      "card_number" => "5105105105105100",
  39      "expiration_month" => "12",
  40      "expiration_year" => "2015"
  41  ));
  42  $buyer = $marketplace->createBuyer("[email protected]", $card->uri);
  43  
  44  printf("generate a debit (which implicitly creates and captures a hold)\n");
  45  $buyer->debit(100);
  46  
  47  foreach ($marketplace->events as $event) {
  48      printf("this was a %s event, it occurred at %s\n",
  49          $event->type,
  50          $event->occurred_at
  51      );
  52  }
  53  
  54  printf("ok, let's check with requestb.in to see if our callbacks fired at %s\n", $callback_url);
  55  printf("we received callbacks, you can view them at http://requestb.in/%s?inspect\n",
  56      $bin_name
  57  );
  58  
  59  ?>


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1