PHP library to interface with the Afterpay API
The recommended way to install is via Composer.
composer require culturekings/afterpay
Don't forget to include Composer's autoloader if you haven't already:
require 'vendor/autoload.php';
All the Afterpay API calls should be available via similar names in the main API factory.
$authorization = new \CultureKings\Afterpay\Authorization(
\CultureKings\Afterpay\Model\Authorization::SANDBOX_URI,
YOUR_MERCHANT_ID,
YOUR_SECRET_KEY
);
$configuration = \CultureKings\Afterpay\Factory\Api::configuration($authorization)->get()
$authorization = new \CultureKings\Afterpay\Authorization(
\CultureKings\Afterpay\Model\Authorization::SANDBOX_URI,
YOUR_MERCHANT_ID,
YOUR_SECRET_KEY
);
$payments = \CultureKings\Afterpay\Factory\Api::payments($auth)->listPayments(
[
'fromCreatedDate' => '2016-01-01T00:00:00.000Z',
'limit' => 1
]
);
$authorization = new \CultureKings\Afterpay\Authorization(
\CultureKings\Afterpay\Model\Authorization::SANDBOX_URI,
YOUR_MERCHANT_ID,
YOUR_SECRET_KEY
);
$payment = \CultureKings\Afterpay\Factory\Api::payments($auth)->get(
PAYMENT_ID
);
$authorization = new \CultureKings\Afterpay\Authorization(
\CultureKings\Afterpay\Model\Authorization::SANDBOX_URI,
YOUR_MERCHANT_ID,
YOUR_SECRET_KEY
);
$payment = \CultureKings\Afterpay\Factory\Api::payments($auth)->getByToken(
ORDER_TOKEN
);
$authorization = new \CultureKings\Afterpay\Authorization(
\CultureKings\Afterpay\Model\Authorization::SANDBOX_URI,
YOUR_MERCHANT_ID,
YOUR_SECRET_KEY
);
$payment = \CultureKings\Afterpay\Factory\Api::payments($auth)->authorise(
ORDER_TOKEN,
MERCHANT_REFERENCE,
WEBHOOK_EVENT_URL
);
$authorization = new \CultureKings\Afterpay\Authorization(
\CultureKings\Afterpay\Model\Authorization::SANDBOX_URI,
YOUR_MERCHANT_ID,
YOUR_SECRET_KEY
);
$payment = \CultureKings\Afterpay\Factory\Api::payments($auth)->capture(
ORDER_TOKEN,
MERCHANT_REFERENCE,
WEBHOOK_EVENT_URL
);
$authorization = new \CultureKings\Afterpay\Authorization(
\CultureKings\Afterpay\Model\Authorization::SANDBOX_URI,
YOUR_MERCHANT_ID,
YOUR_SECRET_KEY
);
$payment = \CultureKings\Afterpay\Factory\Api::payments($auth)->void(
PAYMENT_ID
);
$consumer = new \CultureKings\Afterpay\Model\Consumer();
$consumer->setEmail('john.doe@culturekings.com.au');
$consumer->setGivenNames('John');
$consumer->setSurname('Doe');
$consumer->setPhoneNumber('0534242323');
$merchantOptions = new \CultureKings\Afterpay\Model\MerchantOptions();
$merchantOptions->setRedirectConfirmUrl('https://www.merchant.com/confirm');
$merchantOptions->setRedirectCancelUrl('https://www.merchant.com/cancel');
$totalAmount = new \CultureKings\Afterpay\Model\Money();
$totalAmount->setAmount(mt_rand(1, 300));
$totalAmount->setCurrency('AUD');
$orderDetails = new \CultureKings\Afterpay\Model\OrderDetails();
$orderDetails->setConsumer($consumer);
$orderDetails->setMerchant($merchantOptions);
$orderDetails->setTotalAmount($totalAmount);
$orderToken = \CultureKings\Afterpay\Factory\Api::orders($auth)->create($orderDetails);
$order = \CultureKings\Afterpay\Factory\Api::orders($auth)->get($orderToken->getToken());
If a call to Afterpay fails, a \CultureKings\Afterpay\Exception\ApiException
will be thrown and the error message can be retrieved via the getErrorResponse
method.