A PHP library for integrating Payload.
composer require payload/payload-api
sudo apt install php-curl
<?php
require_once('payload-php/import.php');
?>
Once you've installed the Payload PHP library to your environment,
we recommend
using the shorthand alias name of pl
for Payload\API
.
<?php
require_once('vendor/autoload.php');
use Payload\API as pl;
?>
To authenticate with the Payload API, you'll need a live or test API key. API keys are accessible from within the Payload dashboard.
use Payload\API as pl;
pl::$api_key = 'secret_key_3bW9JMZtPVDOfFNzwRdfE';
Tests are contained within the Tests/ directory. To run a test file enter the command in terminal
./vendor/bin/phpunit Tests/{__FILENAME__}.php
Test execution options can be viewed using the ./vendor/bin/phpunit
command.
Interfacing with the Payload API is done primarily through Payload Objects. Below is an example of
creating a customer using the Payload\Customer
object.
<?php
# Create a Customer
$customer = Payload\Customer::create(array(
'email'=>'matt.perez@example.com',
'name'=>'Matt Perez'
));
?>
<?php
# Create a Payment
$payment = Payload\Transaction::create(array(
'amount'=>100.0,
'type'=>'payment',
'payment_method'=>new Payload\PaymentMethod(array(
'card'=>array('card_number'=>'4242 4242 4242 4242'),
'type'=>'card'
))
));
?>
Object attributes are accessible through dot notation.
<?php
$customer->name;
?>
Updating an object is a simple call to the update
object method.
<?php
# Updating a customer's email
$customer->update(array( 'email'=>'matt.perez@newwork.com' ))
?>
Objects can be selected using any of their attributes.
<?php
# Select a customer by email
$customers = Payload\Customer::filter_by(
pl::attr()->email->eq('matt.perez@example.com')
);
?>
Use the pl::attr()
attribute helper
interface to write powerful queries with a little extra syntax sugar.
$payments = Payload\Transaction::filter_by(
pl::attr()->amount->gt(100),
pl::attr()->amount->lt(200),
pl::attr()->description->contains("Test"),
pl::attr()->created_at->gt('2019-02-01')
)->all()
To get further information on Payload's PHP library and API capabilities, visit the unabridged Payload Documentation.