Code Samples
Working examples in the languages most often used to integrate Genius Checkout.
PHP — Create Session & Redirect
php
$response = Http::withToken($api_key)
->post('https://app.geniuscheckout.com/api/v1/checkout-sessions', [
'amount' => 2500,
'currency' => 'USD',
'success_url' => 'https://mystore.com/success',
'failure_url' => 'https://mystore.com/failed',
'customer' => ['name' => 'Jane', 'email' => '[email protected]'],
'metadata' => ['order_id' => '1042'],
]);
return redirect($response->json('checkout_url'));Node.js — Create Session
javascript
const res = await fetch('https://app.geniuscheckout.com/api/v1/checkout-sessions', {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
amount: 2500,
currency: 'USD',
success_url: 'https://mystore.com/success',
failure_url: 'https://mystore.com/failed',
}),
})
const { checkout_url } = await res.json()Python — Verify Session
python
import requests
r = requests.get(
f'https://app.geniuscheckout.com/api/v1/checkout-sessions/{session_id}',
headers={'Authorization': f'Bearer {api_key}'},
)
session = r.json()
if session['status'] == 'completed':
txn_id = session['transaction']['id']
token = session['transaction']['token_id']Webhook Handler (PHP)
php
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_GC_SIGNATURE'];
$timestamp = $_SERVER['HTTP_X_GC_TIMESTAMP'];
$expected = hash_hmac('sha256', "$timestamp.$payload", $secret);
if (! hash_equals($expected, $signature)) {
http_response_code(401);
exit;
}
// Reject events older than 5 minutes (replay protection)
if (abs(time() - (int) $timestamp) > 300) {
http_response_code(401);
exit;
}
$event = json_decode($payload, true);
$data = $event['payload_redacted'];
if ($event['event_type'] === 'payment.completed') {
mark_order_paid($data['metadata']['order_id'], $data['transaction_id']);
store_token($data['token_id']); // For recurring
}
http_response_code(200);Recurring Charge
php
$r = Http::withToken($api_key)
->post('https://app.geniuscheckout.com/api/v1/charge-token', [
'token_id' => $stored_token,
'amount' => 999,
'currency' => 'USD',
]);
if ($r->json('status') === 'captured') {
// Renewal successful
}Refund
php
$r = Http::withToken($api_key)
->post("https://app.geniuscheckout.com/api/v1/payments/{$txn_id}/refund", [
'amount' => 500, // Partial refund $5.00
'reason' => 'Customer request',
]);Idiomatic patterns
- Always include an
Idempotency-Key(a UUID) on POST requests. - Treat the webhook as the source of truth for fulfillment, not the redirect.
- Make your fulfillment idempotent on
transaction_id. - Store
token_idon the customer record if you'll need future charges.
