PHPackages                             afterpay-global/afterpay-sdk-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. afterpay-global/afterpay-sdk-php

ActiveLibrary[Payment Processing](/categories/payments)

afterpay-global/afterpay-sdk-php
================================

Official Afterpay SDK for PHP

1.8.0(2y ago)7181.3k—0.4%4[2 issues](https://github.com/afterpay/sdk-php/issues)1Apache-2.0PHP

Since Oct 27Pushed 2y ago3 watchersCompare

[ Source](https://github.com/afterpay/sdk-php)[ Packagist](https://packagist.org/packages/afterpay-global/afterpay-sdk-php)[ RSS](/packages/afterpay-global-afterpay-sdk-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (29)Used By (1)

[![Build](https://github.com/afterpay/sdk-php/workflows/Build/badge.svg)](https://github.com/afterpay/sdk-php/actions?query=workflow%3ABuild)[![PHP](https://camo.githubusercontent.com/9872feb7427492c44fc3a1719f05349f2b752a03a8459627fe8e0b688ef82e6f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344352e362d626c7565)](#prerequisites)[![Coding Standard](https://camo.githubusercontent.com/78371d091250c02532c982435d426cc073da788e8a920707bf19cf2129ef8e91/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f64696e672532305374616e646172642d5053522d2d31322d79656c6c6f77)](CONTRIBUTING.md#contributing)

Official Afterpay SDK for PHP
=============================

[](#official-afterpay-sdk-for-php)

Prerequisites
-------------

[](#prerequisites)

Minimum requirements:

- PHP 5.6+

Optional recommendations:

- MySQL 5.6+
- PHPUnit 5+

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/).

### For Production

[](#for-production)

For production usage, ensure the `dist` install method is used. This will exclude development resources such as tests and sample code.

```
composer require --prefer-dist afterpay-global/afterpay-sdk-php
```

### For Development

[](#for-development)

For development purposes, install from source using the `--prefer-source` option. This will include development resources such as tests and sample code.

```
composer require --prefer-source afterpay-global/afterpay-sdk-php
```

Note: If you need to switch between `source` and `dist` installations, you will need to first remove the package using the following command, then reinstall using one of the `require` commands above.

```
composer remove afterpay-global/afterpay-sdk-php
```

Configuration
-------------

[](#configuration)

Environment variables are expected to be defined in a PHP file, located at `./vendor/afterpay-global/afterpay-sdk-php/.env.php`.

A sample file is provided at `./vendor/afterpay-global/afterpay-sdk-php/sample.env.php` - use this file as a template to create your own environment configuration file.

It is possible to configure the SDK without this expected file, by manually setting the configuration options on each of the appropriate classes before using them in your application. As a last resort, the SDK will attempt to load its environment variables from the system with [`getenv`](https://www.php.net/manual/en/function.getenv.php).

Testing
-------

[](#testing)

Test with PHPUnit. Ensure you have the appropriate version of PHPUnit installed based on your PHP version.

Each release is verified in the following test environments:

PHPMySQLPHPUnit5.65.657.05.767.15.777.25.787.35.797.48.098.08.098.18.098.28.09However, it is always recommended to also test all software in your own unique environment prior to deploying to production.

### Unit Tests

[](#unit-tests)

These tests do not require any networking or persistent storage.

```
phpunit --colors=always ./vendor/afterpay-global/afterpay-sdk-php/test/unit
```

### Service Tests

[](#service-tests)

These tests assume you have configured a persistent storage provider in your `.env.php` configuration file. For example, if you provide credentials for a MySQL database, the SDK will verify that it can connect to the database with write access.

```
phpunit --colors=always ./vendor/afterpay-global/afterpay-sdk-php/test/service
```

### Network Tests

[](#network-tests)

These tests will verify that the SDK can communicate with the necessary external services using PHP's native cURL libraries.

```
phpunit --colors=always ./vendor/afterpay-global/afterpay-sdk-php/test/network
```

### Integration Tests

[](#integration-tests)

These tests assume you have configured valid Merchant credentials for the Afterpay/Clearpay Online API in your `.env.php` configuration file. If so, the SDK will verify that it can send requests to the API, and that the API responds as expected.

```
phpunit --colors=always ./vendor/afterpay-global/afterpay-sdk-php/test/integration
```

Usage
-----

[](#usage)

### Constructing Data Models

[](#constructing-data-models)

All the model classes are available in [src/Model](src/Model). These classes are intended to reflect the data models described in the official online documentation for both [Afterpay](https://developers.afterpay.com/afterpay-online/reference#models) and [Clearpay](https://developers.clearpay.co.uk/clearpay-online/reference#models). By using these model classes to construct HTTP Requests, validity of request format can be verified at runtime.

Sample code is provided in the [sample](sample) directory:

- [Using method calls](sample/ModelConstructionUsingMethodCalls.php)
- [Using associative arrays](sample/ModelConstructionUsingAssociativeArrays.php)
- [Using ordered arguments](sample/ModelConstructionUsingOrderedArguments.php)
- [Using JSON strings](sample/ModelConstructionUsingJsonStrings.php)
- [Combination of the above](sample/ModelConstruction.php)

### Making HTTP Requests

[](#making-http-requests)

Most API endpoints require authentication (with the exception of Ping). You can define your API credentials for your Afterpay/Clearpay merchant account using several different methods. You will also need to specify the ISO 3166-1 alpha-2 two-character country code of the merchant account. The following methods of specifying these details are supported:

1. Inside your `.env.php` file, as the `merchantId`, `secretKey` and `countryCode` properties of the `$afterpay_sdk_env_config`array.
2. As the `MERCHANT_ID`, `SECRET_KEY` and `COUNTRY_CODE` environment variables.
3. By manually defining an object of class `\Afterpay\SDK\MerchantAccount`, passing your account details using its `setMerchantId`, `setSecretKey` and `setCountryCode` methods, then passing this object to the the HTTP Request object using its `setMerchantAccount` method.

Sample code is provided in the [sample](sample) directory:

- [Making a "Ping" request](sample/HTTPRequestPing.php)
- [Making a "Get Configuration" request, and persisting the result](sample/HTTPRequestGetConfigurationWithPersistence.php)
- [Validating and creating a checkout](sample/HTTPRequestCreateCheckoutWithValidation.php)
- [Immediately capturing payment for a confirmed checkout](sample/HTTPRequestImmediatePaymentCapture.php)
- [Creating a payment auth for a confirmed checkout](sample/HTTPRequestDeferredPaymentAuth.php)
- [Capturing payment for a despatched shipment](sample/HTTPRequestDeferredPaymentCapture.php)
- [Voiding an unfulfillable portion of a payment auth](sample/HTTPRequestDeferredPaymentVoid.php)
- [Creating a refund](sample/HTTPRequestCreateRefund.php)

Troubleshooting
---------------

[](#troubleshooting)

### How To Retrieve Raw HTTP Logs

[](#how-to-retrieve-raw-http-logs)

All [Request](src/HTTP/Request.php) and [Response](src/HTTP/Response.php) objects provide a `getRawLog` method for returning a raw HTTP log. This is often the most useful data to facilitate an investigation, should a scenario be encountered where the Afterpay API does not behave as expected.

*Note: By default, any potentially sensitive information is obfuscated for privacy reasons. It is strongly recommended that this feature is not disabled, unless extraordinary precautions are taken to ensure the raw HTTP logs are not stored for any extended period of time, and destroyed immediately after their intended purpose has been fulfilled.*

For example:

```
require __DIR__ . '/vendor/autoload.php';

use Afterpay\SDK\HTTP\Request\CreateCheckout as AfterpayCreateCheckoutRequest;

$createCheckoutRequest = new AfterpayCreateCheckoutRequest([
    'amount' => [ '10.00', 'AUD' ],
    'consumer' => [
        'phoneNumber' => '0400 000 000',
        'givenNames' => 'Test',
        'surname' => 'Test',
        'email' => 'test@example.com'
    ],
    'billing' => [
        'name' => 'Joe Consumer',
        'line1' => 'Level 5',
        'line2' => '406 Collins Street',
        'area1' => 'Melbourne',
        'region' => 'VIC',
        'postcode' => '3000',
        'countryCode' => 'AU',
        'phoneNumber' => '0400 000 000'
    ],
    'shipping' => [
        'name' => 'Joe Consumer',
        'line1' => 'Level 5',
        'line2' => '406 Collins Street',
        'area1' => 'Melbourne',
        'region' => 'VIC',
        'postcode' => '3000',
        'countryCode' => 'AU',
        'phoneNumber' => '0400 000 000'
    ],
    'courier' => [
        'shippedAt' => '2019-01-01T00:00:00+10:00',
        'name' => 'Australia Post',
        'tracking' => 'AA0000000000000',
        'priority' => 'STANDARD'
    ],
    'items' => [
        [
            'name' => 'T-Shirt - Blue - Size M',
            'sku' => 'TSH0001B1MED',
            'quantity' => 10,
            'pageUrl' => 'https://www.example.com/page.html',
            'imageUrl' => 'https://www.example.com/image.jpg',
            'price' => [ '10.00', 'AUD' ],
            'categories' => [
                [ 'Clothing', 'T-Shirts', 'Under $25' ],
                [ 'Sale', 'Clothing' ]
            ]
        ]
    ],
    'discounts' => [
        [
            'displayName' => '20% off SALE',
            'amount' => [ '24.00', 'AUD' ]
        ]
    ],
    'merchant' => [
        'redirectConfirmUrl' => 'http://localhost',
        'redirectCancelUrl' => 'http://localhost'
    ],
    'taxAmount' => [ '0.00', 'AUD' ],
    'shippingAmount' => [ '0.00', 'AUD' ]
]);

$createCheckoutRequest->send();

echo $createCheckoutRequest->getRawLog();
```

```
########## BEGIN RAW HTTP REQUEST  ##########
POST /v2/checkouts HTTP/2
Host: global-api-sandbox.afterpay.com
Authorization: Basic MzM******************************************************************************************************************************************************************************TA=
User-Agent: afterpay-sdk-php/1.4.1 (PHP/8.1.1; cURL/7.77.0; Merchant/*****)
Accept: */*
Content-Type: application/json
Content-Length: 1223

{"amount":{"amount":"10.00","currency":"AUD"},"consumer":{"phoneNumber":"**** *** ***","givenNames":"****","surname":"****","email":"****************"},"billing":{"name":"*** ********","line1":"***** *","line2":"*** ******* ******","area1":"*********","region":"***","postcode":"****","countryCode":"AU","phoneNumber":"**** *** ***"},"shipping":{"name":"*** ********","line1":"***** *","line2":"*** ******* ******","area1":"*********","region":"***","postcode":"****","countryCode":"AU","phoneNumber":"**** *** ***"},"courier":{"shippedAt":"2019-01-01T00:00:00+10:00","name":"********* ****","tracking":"AA0000000000000","priority":"STANDARD"},"items":[{"name":"******* * **** * **** *","sku":"TSH0001B1MED","quantity":10,"pageUrl":"https:\/\/www.example.com\/page.html","imageUrl":"https:\/\/www.example.com\/image.jpg","price":{"amount":"10.00","currency":"AUD"},"categories":[["Clothing","T-Shirts","Under $25"],["Sale","Clothing"]]}],"discounts":[{"displayName":"20% off SALE","amount":{"amount":"24.00","currency":"AUD"}}],"merchant":{"redirectConfirmUrl":"http:\/\/localhost","redirectCancelUrl":"http:\/\/localhost"},"taxAmount":{"amount":"0.00","currency":"AUD"},"shippingAmount":{"amount":"0.00","currency":"AUD"}}
########## END RAW HTTP REQUEST    ##########
########## BEGIN RAW HTTP RESPONSE ##########
HTTP/2 201
date: Tue, 15 Sep 2020 14:20:49 GMT
content-type: application/json
content-length: 249
set-cookie: __cfduid=d05b6824b1ed88439e0b8edfe4905c7671600179649; expires=Thu, 15-Oct-20 14:20:49 GMT; path=/; domain=.afterpay.com; HttpOnly; SameSite=Lax; Secure
http_correlation_id: ed5d72kgz3ezlylm7g3qxiej6a
cf-cache-status: DYNAMIC
cf-request-id: 0533bcd2b60000fd36ac918200000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
strict-transport-security: max-age=31536000; includeSubDomains; preload
server: cloudflare
cf-ray: 5d32fd97883dfd36-SYD

{
  "token" : "001.ug27ke8qbpljfsssn4t98c60eq61767rva1e3f3g7g4nup7c",
  "expires" : "2020-09-15T17:20:49.267Z",
  "redirectCheckoutUrl" : "https://portal.sandbox.afterpay.com/au/checkout/?token=001.ug27ke8qbpljfsssn4t98c60eq61767rva1e3f3g7g4nup7c"
}
########## END RAW HTTP RESPONSE   ##########

```

Contributing
------------

[](#contributing)

See [CONTRIBUTING.md](CONTRIBUTING.md#contributing).

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance14

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 83.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Recently: every ~103 days

Total

27

Last Release

850d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6b06d7dc1c0b86fe52c006db737b9661023cb7fd14307cebe444fff23e37e098?d=identicon)[afterpay](/maintainers/afterpay)

---

Top Contributors

[![benfterpay](https://avatars.githubusercontent.com/u/28610907?v=4)](https://github.com/benfterpay "benfterpay (201 commits)")[![chesterz-afterpay](https://avatars.githubusercontent.com/u/61479260?v=4)](https://github.com/chesterz-afterpay "chesterz-afterpay (24 commits)")[![ScottAntonacAP](https://avatars.githubusercontent.com/u/76413218?v=4)](https://github.com/ScottAntonacAP "ScottAntonacAP (17 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/afterpay-global-afterpay-sdk-php/health.svg)

```
[![Health](https://phpackages.com/badges/afterpay-global-afterpay-sdk-php/health.svg)](https://phpackages.com/packages/afterpay-global-afterpay-sdk-php)
```

###  Alternatives

[omnipay/paypal

PayPal gateway for Omnipay payment processing library

3156.8M53](/packages/omnipay-paypal)[eduardokum/laravel-boleto

Biblioteca com boletos para o laravel

626351.9k2](/packages/eduardokum-laravel-boleto)[tbbc/money-bundle

This is a Symfony bundle that integrates moneyphp/money library (Fowler pattern): https://github.com/moneyphp/money.

1961.9M](/packages/tbbc-money-bundle)[2checkout/2checkout-php

2Checkout PHP Library

83740.3k2](/packages/2checkout-2checkout-php)[smhg/sepa-qr-data

Generate QR code data for SEPA payments

61717.2k5](/packages/smhg-sepa-qr-data)[omnipay/dummy

Dummy driver for the Omnipay payment processing library

271.2M33](/packages/omnipay-dummy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
