PHP code example of openpublicmedia / pbs-membership-vault-php

1. Go to this page and download the library: Download openpublicmedia/pbs-membership-vault-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

openpublicmedia / pbs-membership-vault-php example snippets


use OpenPublicMedia\PbsMembershipVault\Client;

$api_key = 'xxxxxxxxxxxxxx';
$api_secret = 'xxxxxxxxxxx';
$station_id = 'xxxxxxxxxxx';

$client = new Client($api_key, $api_secret, $station_id);

$id = 'xxxxxxxxxxx';
$membership = $client->getMembershipById($id);
var_dump($membership);
class stdClass#34 (18) {
  public $grace_period => string
  public $update_date => string
  public $first_name => string
  public $last_name => string
  public $create_date => string
  public $offer => string
  public $notes => string
  public $current_state =>
  class stdClass#28 (2) {
    public $explanation =>
    class stdClass#35 (3) {
      public $status => string
      public $timing => string
      public $token_activated bool
    }
    public $has_access => bool
  }
  public $membership_id => string
  public $start_date => string
  public $status => string
  public $token => string
  public $additional_metadata => string
  public $activation_date => string
  public $provisional => bool
  public $expire_date => string
  public $pbs_profile =>
  class stdClass#24 (7) {
    public $first_name => string
    public $last_name => string
    public $UID => string
    public $birth_date => string
    public $retrieval_status =>
    class stdClass#23 (2) {
      public $status => int
      public $message => string
    }
    public $email => string
    public $login_provider => string
  }
  public $email => string
}

$memberships = $client->getActivatedMemberships();
var_dump(count($memberships));
int(51763)
foreach ($memberships as $membership) {
    var_dump($membership);
    class stdClass#29 (18) {...}
}

$start_date = new DateTime();
$expire_date = clone($start_date);
$expire_date->add(new DateInterval('P1Y'));

$membership = $client->addMembership(
    'membership_id',
    'first_name',
    'last_name',
    'offer',
    $start_date
    $expire_date
);

var_dump($membership);
class stdClass#29 (18) {...}

$membership_id = 'xxxxxxxx';

$membership = $client->getMembershipById($membership_id);
var_dump($membership->notes);
string(0) ""

$result = $client->updateMembership($membership_id, ['notes' => 'Updated notes']);
var_dump($result);
bool(true)

$membership = $client->getMembershipById($membership_id);
var_dump($membership->notes);
string(13) "Updated notes"

$start_date = new DateTime();
$expire_date = clone($start_date);
$expire_date->add(new DateInterval('P1Y'));

try {
    $membership = $client->addMembership(
        'membership_id',
        '',
        '',
        'offer',
        $start_date,
        $end_date
    );
} catch (BadRequestException $e) {
    $errors = json_decode($e->getMessage());
    var_dump($errors);
    class stdClass#31 (2) {
      public $first_name =>
      array(1) {
        [0] =>
        string(23) "This field is 

$membership_id = 'does_not_exist';
try {
    $membership = $client->getMembershipById($membership_id);
} catch (MembershipNotFoundException $e) {
    $response = json_decode($e->getMessage());
    var_dump($response);
    class stdClass#29 (2) {
      public $type =>
      string(2) "id"
      public $value =>
      string(17) "does_not_exist"
    }
}

$membership_id = 'ALREADY_ACTIVATED';
$uid = '11111111-2222-3333-4444-555555555555'
try {
    $membership = $client->activateMembership($membership_id, $uid);
} catch (MembershipActivatedException $e) {
    $response = json_decode($e->getMessage());
    var_dump($response);
    object(stdClass)#31 (3) {
      ["message"]=>
      string(101) "The membership ALREADY_ACTIVATED was already activated with UID 11111111-2222-3333-4444-555555555555"
      ["id"]=>
      string(18) "ALREADY_ACTIVATED"
      ["uid"]=>
      string(36) "11111111-2222-3333-4444-555555555555"
    }
    var_dump($e->getMembershipId());
    string(18) "ALREADY_ACTIVATED"
    var_dump($e->getPbsAccountUid());
    string(36) "11111111-2222-3333-4444-555555555555"

}

$membership_id = '0123456789ABCDEF';
$uid = '11111111-2222-3333-4444-555555555555'
try {
    $membership = $client->activateMembership($membership_id, $uid);
} catch (MembershipActivatedException $e) {
    $response = json_decode($e->getMessage());
    var_dump($response);
    object(stdClass)#31 (3) {
      ["message"]=>
      string(96) "The UID 11111111-2222-3333-4444-555555555555 has already activated membership 0123456789ABCDEF"
      ["id"]=>
      string(18) "0123456789ABCDEF"
      ["uid"]=>
      string(36) "11111111-2222-3333-4444-555555555555"
    }
    var_dump($e->getMembershipId());
    string(18) "0123456789ABCDEF"
    var_dump($e->getPbsAccountUid());
    string(36) "11111111-2222-3333-4444-555555555555"

}