Complete Integration Examples
Here are complete, working examples of both SDK and API integration methods.
Option 1: SDK Integration (JavaScript)
Full HTML page with SDK integration:
<!DOCTYPE html>
<html>
<head>
<title>Payviox Payment</title>
</head>
<body>
<h1>Complete your purchase</h1>
<p>Total: $10.00</p>
<button onclick="initiatePayment()">Pay Now</button>
<script src="https://sdk.payviox.com/payviox.js"></script>
<script>
const payviox = new Payviox('your_api_token');
async function initiatePayment() {
try {
await payviox.createSession({
amount: 1000,
currency: 'USD',
customer: 'customer_123',
description: 'Premium Subscription',
paymentMethodId: 'pm_card',
order_id: 'order_' + Date.now(),
items: [{
name: 'Premium Subscription',
quantity: 1,
price: 1000
}]
}, {
redirect: true // Automatically redirect
});
} catch (error) {
alert('Payment failed: ' + error.message);
}
}
</script>
</body>
</html>
When you click “Pay Now”, the SDK automatically creates a session and redirects to the secure payment page.
Option 2: API Integration (cURL)
Create a payment session using the REST API:
curl -X POST https://api.payviox.com/session \
-H "Authorization: Bearer your_api_token" \
-H "Content-Type: application/json" \
-d '{
"amount": 1000,
"currency": "USD",
"customer": "customer_123",
"description": "Premium Subscription",
"paymentMethodId": "pm_card",
"order_id": "order_12345",
"items": [{
"name": "Premium Subscription",
"quantity": 1,
"price": 1000
}]
}'
Then redirect the customer to the returned session URL.
Webhook Handler Examples
Both integration methods require a webhook handler on your server.
<?php
// Receive webhook notification
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_SIGNATURE'];
// Verify signature
$computed = hash_hmac('sha256', $payload, 'your_webhook_token');
if (!hash_equals($computed, $signature)) {
http_response_code(401);
exit('Invalid signature');
}
// Parse webhook data
$data = json_decode($payload, true);
// Handle successful payment
if ($data['type'] === 'succeeded') {
// Fulfill the order
fulfillOrder($data['order_id']);
// Send confirmation email
sendConfirmationEmail($data);
}
// Return 200 OK
http_response_code(200);
echo json_encode(['status' => 'success']);
Testing Your Integration
Use test credentials
Use your test API token to avoid real charges:const payviox = new Payviox('pk_test_xxxxxxxxxxxxx');
Test card numbers
Use these test card numbers in the payment form:
4242 4242 4242 4242 - Successful payment
4000 0000 0000 0002 - Card declined
4000 0000 0000 0341 - Insufficient funds
Test webhooks locally
Use ngrok to expose your local server:Then configure the ngrok URL as your webhook URL. Monitor in dashboard
View all test transactions in your dashboard.
Next Steps
SDK Documentation
Learn more about the SDK
API Documentation
Explore the REST API
Webhook Guide
Complete webhook documentation
Best Practices
Security and best practices