PHP code example of teamtools / php-sdk
1. Go to this page and download the library: Download teamtools/php-sdk 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/ */
teamtools / php-sdk example snippets
teamtools-php-sdk
├── app
│ └── index.php
├── vendor
├── composer.json
└── composer.lock
sh
sh
$data = [
'name' => 'Awesome customer',
'email' => '[email protected] ',
'phone' => '+1234567890',
'city' => 'Berlin',
'country' => 'Germany'
];
$customer = new Customer($data);
try {
var_dump($customer->save(true));
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
$customer = Customer::getByID('580a0ffcbffebc8b0e8b456a');
$customer->name = 'New Customer Name';
try {
var_dump($customer->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$customer = Customer::getByID('580a0ffcbffebc8b0e8b456a');
var_dump($customer);
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$customers = $customer = Customer::getByTag('new');
var_dump($customers);
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
// all customers
$customers = Customer::getAll();
foreach ($customers as $customer) {
var_dump($customer->name);
}
// search customers for 'awesome' in searchable attributes
$customers = Customer::getAll(['keyword' => 'awesome']);
foreach ($customers as $customer) {
var_dump($customer->name);
}
sh
try {
$customer = Customer::getByID('580a0ffcbffebc8b0e8b456aff');
var_dump($customer->delete());
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$customer = Customer::getByID('57e3a147bffebc75388b4571');
$newCustomerId = '57ecf1f6bffebcc5098b4585';
//migrate specific endusers
$ids = ['57ecf1b0bffebcc3098b4582', '57ecf1b0bffebcc3098b4587'];
var_dump($customer->migrateEndusers($newCustomerId, $ids));
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$customer = Customer::getByID('57e3a147bffebc75388b4571');
$newCustomerId = '57ecf1f6bffebcc5098b4585';
//migrate all endusers
var_dump($customer->migrateEndusers($newCustomerId));
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('582191a10957473545bdff43');
$endUsers = $customer->getEndUsers();
var_dump($endUsers);
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('57fbe76c0957475156bdff59');
$events = $customer -> getEvents();
var_dump($events);
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
var_dump(Customer::restore('57fce361bffebc231c8b45cb'));
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
var_dump(EndUser::restore('5822eaccbffebcaa088b458e'));
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
$data = [
'name' => 'department',
'prettyName' => 'Department',
'type' => 'text',
'description' => "Customer's department",
'bute($data);
try {
Customer::saveAttribute($attribute);
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
$data = [
'name' => 'testAttribute',
'prettyName' => 'Test attribute',
'type' => 'text',
'description' => "EndUser's test attribute",
');
try {
EndUser::saveAttribute($attribute);
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$customer = Customer::getByID('56582c53095747b64b9215f7');
$customer->name = 'SDK awesome customer';
$response = $customer->save(true);
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$customer = Customer::getByID('56582c53095747b64b9215f7');
$customer->name = 'SDK awesome customer';
$response = $customer->save();
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('5739c7fbbffebc4c0b8b4567', 'subscription');
var_dump($customer);
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
use teamtools\Entities\Feature;
use teamtools\Entities\Attribute;
$data = [
'name' => 'status',
'prettyName' => 'Feature status',
'type' => 'number',
'description' => 'Status of feature',
' echo $ex->getMessage();
}
sh
use teamtools\Entities\Feature;
use teamtools\Entities\Attribute;
$data = [
'id' => '5656c899bffebc47078b456e',
'name' => 'status',
'prettyName' => 'Feature status',
'type' => 'number',
'description' => 'Status of feature. 1 - active; 0 - inactive.',
'
sh
$data = [
'name' => 'Feature B',
'description' => 'Feature B',
'uniqueKey' => 'feat-B'
];
try {
$feature = new Feature($data);
$feature->save();
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$feature = Feature::getByID('5655c5edbffebc40078b459c');
$feature->dependency_ids = ['5655c5f6bffebc40078b459e'];
$feature->save();
} catch (TTException $ex) {
echo $ex->getMessage();
}
sh
try {
$feature = Feature::getByID('5655c5edbffebc40078b459c');
$feature->dependency_ids = [];
$feature->save();
} catch (TTException $ex) {
echo $ex->getMessage();
}
$data = [
'name' => 'Standard group',
'default' => true //if ommited and this is first group in the system, group will become default
];
$group = new Group($data);
try {
var_dump($group->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
$data = [
'name' => 'Basic group',
'defaultPackage' => '5733052dbffebc46088b456b',
'default' => true
];
$group = new Group($data);
try {
var_dump($group->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$group = Group::getByID('573301dbbffebc46088b4567');
var_dump($group->getPackages());
} catch (TTException $ex) {
echo $ex->getMessage();
}
$data = [
'name' => 'Basic package',
'groupId' => '573301dbbffebc46088b4567',
'default' => 'true' // assign this package as default in its group
];
$package = new Package($data);
try {
var_dump($package->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
$data = [
'name' => 'Pro package',
'featureIds' => [
'573305cdbffebc46088b4571',
'573305e5bffebc46088b4575'
],
'groupId' => '573301dbbffebc46088b4567'
];
$package = new Package($data);
try {
var_dump($package->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$packages = Package::getAll();
var_dump($packages);
} catch (TTException $ex) {
echo $ex->getMessage();
}
$data = [
'packageId' => '57330639bffebc46088b4579',
'trial' => '30',
'currency' => 'USD',
'pricing' => [
'type' => 'unit',
'interval' => [
'type' => 'month',
'amount' => 2
],
'unit' => 'enduser',
'amount' => 200 //amount in cents
]
];
$plan = new Plan($data);
try {
var_dump($plan->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
$data = [
'packageId' => '57330639bffebc46088b4579',
'trial' => '30',
'currency' => 'USD',
'pricing' => [
'type' => 'tier',
'interval' => [
'type' => 'month',
'amount' => 2
],
'unit' => 'enduser',
'levels' => [
[
'condition' => [
'min' => 1,
'max' => -1
],
'expression' => [
[
'type' => 'unit',
'unit' => 'enduser',
'amount' => 120 //amount in cents
]
]
]
]
]
];
$plan = new Plan($data);
try {
var_dump($plan->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('5730838fbffebc290b8b4591');
$customer->groupId = 'default';
var_dump($customer->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('5730838fbffebc290b8b4591');
$subscriptionData = [
'groupId' => 'default'
];
var_dump($customer->subscribe($subscriptionData));
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('5730838fbffebc290b8b4591');
$subscriptionData = [
'groupId' => '573301dbbffebc46088b4567',
'packageId' => '5733052dbffebc46088b456b',
'manual' => 'false',
'stripeToken' => 'xxxx',
'force' => false
];
var_dump($customer->subscribe($subscriptionData));
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('5730838fbffebc290b8b4591');
var_dump($customer->getSubscription());
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$customer = Customer::getByID('56c73ce5bffebc47078b4619');
var_dump($customer->unsubscribe());
} catch (TTException $ex) {
echo $ex->getMessage();
}
use teamtools\Entities\Invoice;
try {
$invoice = Invoice::getByID('56cc581abffebc5b078b4575');
var_dump($invoice);
} catch (TTException $ex) {
echo $ex->getMessage();
}
use teamtools\Entities\Invoice;
$data = [
'invoiceDate' => '2016-01-22',
'dueDate' => '2016-02-22',
'customerId' => '56c73ce5bffebc47078b4619',
'items' => [
[
'description' => 'Initial account setup',
'amount' => 1750
],
[
'description' => 'Application adjustments',
'amount' => 450000
]
]
];
$invoice = new Invoice($data);
try {
var_dump($invoice->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
use teamtools\Entities\Invoice;
try{
$invoice = Invoice::getByID('56cc581abffebc5b078b4575');
$invoice->settle();
var_dump($invoice->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
use teamtools\Entities\Invoice;
try {
$invoice = Invoice::getByID('56cc581abffebc5b078b4575');
$invoice->applyPayment(['amount' => 350]);
var_dump($invoice->save());
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$invoice = Invoice::getByID('57e3a348bffebc83318b457e');
var_dump($invoice->getRefunds());
var_dump($invoice->getPayments());
} catch (TTException $ex) {
echo $ex->getMessage();
}
use teamtools\Entities\EndUser;
$endusers = [
[
'firstName' => 'Mary',
'lastName' => 'Jones',
'email' => '[email protected] ',
'phone' => '+1234123412',
'country' => 'USA',
'city' => 'Chicago',
'customerId' => '5704f67cbffebc47078b4574'
],
[
'firstName' => 'Peter',
'lastName' => 'Johnson',
'email' => '[email protected] ',
'phone' => '+1234123412',
'country' => 'USA',
'city' => 'Chicago',
'customerId' => '5704f67cbffebc47078b4574'
],
[
'id' => '57050433bffebc46078b457f',
'firstName' => 'John',
'lastName' => 'Smith',
'email' => '[email protected] ',
'phone' => '+1234123412',
'country' => 'USA',
'city' => 'Chicago',
'customerId' => '5704f67cbffebc47078b4574'
]
];
try {
var_dump(EndUser::saveAll($endusers, false));
} catch (TTException $ex) {
echo $ex->getMessage();
}
try {
$webEvent = WebEvent::getByID('57334232bffebc77088b4574');
var_dump($webEvent);
} catch (TTException $ex) {
echo $ex->getMessage();
}