PHP code example of utopia-payments / utopia-php
1. Go to this page and download the library: Download utopia-payments/utopia-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
utopia-payments / utopia-php example snippets
use Utopia\Utopia;
$utopia = new Utopia(getenv('UTOPIA_API_KEY'));
$session = $utopia->checkoutSessions->create([
'product_cart' => [['product_id' => 'pdt_…', 'quantity' => 1]],
'customer' => ['email' => '[email protected] '],
'return_url' => 'https://your-store.com/thank-you',
'metadata' => ['order_id' => '1001'],
]);
header('Location: ' . $session['checkout_url']);
$utopia->checkoutSessions->create([
'product_cart' => [['name' => 'Order #1001', 'unit_amount' => 12550, 'currency' => 'AED', 'quantity' => 2]],
'return_url' => 'https://your-store.com/orders/1001',
]);
$plan = $utopia->products->create([
'name' => 'Pro',
'price' => 9900,
'currency' => 'AED',
'billing' => 'recurring',
'billing_interval' => 'month',
]);
$session = $utopia->checkoutSessions->create([
'product_cart' => [['product_id' => $plan['product_id']]],
'customer' => ['email' => '[email protected] '],
]);
// Later: stop renewing when the paid period ends.
$utopia->subscriptions->cancel('sub_…', atPeriodEnd: true);
use Utopia\Webhook;
use Utopia\Exception\WebhookVerificationException;
$payload = file_get_contents('php://input');
try {
$event = Webhook::verify($payload, getallheaders(), getenv('UTOPIA_WEBHOOK_SECRET'));
} catch (WebhookVerificationException $e) {
http_response_code(400);
exit;
}
if ($event['type'] === 'payment.succeeded') {
fulfil($event['data']['metadata']['order_id']);
}
http_response_code(204);
use Illuminate\Http\Request;
use Utopia\Exception\WebhookVerificationException;
use Utopia\Webhook;
Route::post('/webhooks/utopia', function (Request $request) {
try {
$event = Webhook::verify(
$request->getContent(),
$request->headers->all(),
config('services.utopia.webhook_secret'),
);
} catch (WebhookVerificationException $e) {
return response()->noContent(400);
}
// …
return response()->noContent();
});
$page = $utopia->payments->list(['limit' => 50, 'status' => 'succeeded']);
foreach ($utopia->payments->all(['limit' => 100]) as $payment) {
echo $payment['payment_id'], PHP_EOL;
}
use Utopia\Exception\NotFoundException;
use Utopia\Exception\UtopiaException;
try {
$utopia->payments->retrieve('pay_…');
} catch (NotFoundException $e) {
// …
} catch (UtopiaException $e) {
echo $e->getHttpStatus(), ' ', $e->getErrorCode(), ': ', $e->getMessage();
}
new Utopia('sk_live_…', [
'timeout' => 30, // seconds
'max_retries' => 2,
'base_url' => 'https://utopia-payments.com/api/v1',
]);
bash
composer