linups / ontraport-laravel
Laravel integration for the Ontraport API: contacts, tags, sequences and transactions.
Requires
- php: ^8.1
- ext-curl: *
- ext-json: *
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- ontraport/sdk-php: ^1.3
Requires (Dev)
- laravel/framework: ^10.0|^11.0|^12.0|^13.0
- phpunit/phpunit: ^10.5|^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel integration for the Ontraport API: create and update contacts, manage tags and sequences, and log or charge transactions.
- PHP 8.1+
- Laravel 10, 11, 12 or 13
Installation
composer require linups/ontraport-laravel
The service provider is auto-discovered. Add your API credentials to .env:
ONTRAPORT_APP_ID=your-app-id ONTRAPORT_APP_KEY=your-api-key
Optionally publish the config file (config/ontraport.php):
php artisan vendor:publish --tag=ontraport-config
Credentials are resolved in this order:
config('ONTRAPORT_APP_ID')/config('ONTRAPORT_APP_KEY'): legacy top-level keys, if your app sets them.config('ontraport.app_id')/config('ontraport.app_key'): works withphp artisan config:cache.env('ONTRAPORT_APP_ID')/env('ONTRAPORT_APP_KEY').
Usage
Resolve the client from the container (it is a singleton) or inject it:
use Linups\OntraportLaravel\Ontraport; public function store(Request $request, Ontraport $ontraport) { $ontraport->addUser(['email' => $request->email, 'firstname' => $request->name]); $ontraport->addTagByEmail($request->email, [12, 15]); }
Contacts are created automatically. Every method that takes an e-mail looks the contact up first and creates it if it does not exist. This includes
deleteUser(),retrieveUser()andgetTransactions().
Empty values are skipped.
addUser()andupdateUser()do not send fields whose value is''ornull, so blank form input never overwrites data already stored in Ontraport.
Methods
| Method | Returns |
|---|---|
addUser(array $data) |
object: create or update a contact by e-mail |
updateUser($email, array $data) |
object |
retrieveUser(string $email) |
object (contact in ->data) |
getUserEmail(int $id) |
string |
deleteUser($email) |
object |
getContactList(array $params) |
raw JSON string |
getUserList($limit = 50, $offset = 0, ?int $since = null) |
array of contact objects, oldest first; $since (unix seconds) keeps only contacts created after it |
addTag($tagIds, $contactIds) |
raw JSON string |
addTagByEmail($email, array|string $tags) |
object |
removeTagsByEmail(string $email, array $tags) |
object (API errors are returned, not thrown) |
addSequence($seqIds, $contactIds) |
raw JSON string |
addSequenceByEmail($email, array|string $seqIds) |
raw JSON string |
getTransactions($email) |
raw JSON string (purchases) |
getAffiliateList(?array $filter = null) |
raw JSON string (commissions) |
logTransaction($email, ['price' => .., 'sync_id' => ..]) |
raw JSON string |
chargeUser(array $data) |
object with ->status success or fail (and ->message) |
addUser() and chargeUser() also accept checkout-style keys and map them to Ontraport fields:
billing_address1→addressbilling_address2→address2billing_city→citybilling_state→statebilling_zip→zipbilling_country→countrypayment_number→ccNumberpayment_expire_month→ccExpirationMonthpayment_expire_year→ccExpirationYear
Charging
$result = $ontraport->chargeUser([ 'email' => 'jane@example.com', // or 'contact_id' => 123 'price' => 149, 'product_id' => 40, 'type' => 'subscription', // sale | charge | subscription | infinite-subscription // optional: 'payment_gateway', 'payment_count', 'trial' => ['price', 'payment_count', 'unit'] ]); if ($result->status === 'fail') { // $result->message }
The defaults for the gateway, invoice template, trial period and subscription count are set
under ontraport.charge in the config file.
When a charge fails, the package logs an error and dispatches
Linups\OntraportLaravel\Events\ChargeFailed. Card fields in the event and log are redacted.
To be notified, listen for the event:
Event::listen(ChargeFailed::class, function (ChargeFailed $event) { Mail::to('billing@example.com')->send(new ChargeFailedMail($event->data, $event->result)); });
Errors
Failed calls throw Linups\OntraportLaravel\Exceptions\OntraportException, which extends \Exception.
The message keeps its <pre>print_r(...)</pre> format. Card numbers, expiry dates and security
codes are redacted. getContext() returns the same details as an array, and getMethodName()
returns the name of the method that failed.
Ontraport SDK
The package uses the official ontraport/sdk-php (^1.3),
installed by Composer. Its OntraportAPI\ classes are available to your app as well.
Testing
composer install
composer test
License
MIT