PHP SDK
Our PHP SDK provides a fluent interface to the Cari Finance API, with support for Laravel and other PHP frameworks. It's designed to make implementing payments in your PHP applications simple and secure.
Installation
The SDK can be installed via Composer:
composer require carifinance/sdk
After installation, you'll need to include the Composer autoloader in your project:
require_once 'vendor/autoload.php';
Basic usage
To start using the SDK, initialize it with your API key:
use CariFinance\CariFinanceClient;
// Initialize with your API key
$cariFinance = new CariFinanceClient('sk_test_51NzaWTBOIWoX8E7wnMd1...');
// The SDK will automatically detect if you're using a test or live key
Creating charges
You can create a new charge for a single payment:
// Create a charge
$charge = $cariFinance->charges->create([
'amount' => 2000, // Amount in cents (e.g., $20.00)
'currency' => 'usd',
'provider' => 'card',
'card' => [
'token' => 'tok_visa' // Token obtained from the client-side SDK
],
'description' => 'Payment for order #123',
'customer' => [
'name' => 'John Doe',
'email' => 'john.doe@example.com'
]
]);
// Check if charge was successful
if ($charge->status === 'succeeded') {
// Payment was successful
echo "Payment successful! Charge ID: " . $charge->id;
} else {
// Payment failed
echo "Payment failed: " . $charge->failure_message;
}
Working with customers
You can create and manage customers:
// Create a new customer
$customer = $cariFinance->customers->create([
'name' => 'Jane Smith',
'email' => 'jane.smith@example.com',
'phone' => '+18765551234',
'address' => [
'line1' => '123 Main St',
'city' => 'Roseau',
'country' => 'DM'
]
]);
// Retrieve a customer
$customer = $cariFinance->customers->retrieve('cus_12345');
// Update a customer
$customer = $cariFinance->customers->update('cus_12345', [
'phone' => '+18765559876'
]);
// Delete a customer
$cariFinance->customers->delete('cus_12345');
Laravel integration
The SDK includes a Laravel service provider for easy integration. Add the following to your config/app.php
file:
'providers' => [
// Other service providers...
CariFinance\Laravel\CariFinanceServiceProvider::class,
],
'aliases' => [
// Other facades...
'CariFinance' => CariFinance\Laravel\Facades\CariFinance::class,
],
Publish the configuration file:
php artisan vendor:publish --provider="CariFinance\Laravel\CariFinanceServiceProvider"
This will create a config/carifinance.php
file where you can set your API keys.
You can then use the SDK in your controllers:
namespace App\Http\Controllers;
use CariFinance;
class PaymentController extends Controller
{
public function processPayment()
{
$charge = CariFinance::charges()->create([
'amount' => 2000,
'currency' => 'usd',
// other parameters...
]);
return response()->json($charge);
}
}
Additional resources
For more detailed information about the PHP SDK, refer to these resources: