Getting Started: Quick Start Guide
Get the ConnexPay payments SDK running in your application in under 5 minutes with this step-by-step guide.
Step 1: Include the SDK
Add the ConnexPay SDK script to your HTML page. Always load from our CDN to ensure you have the latest version and maintain PCI compliance.
<script src="https://js.connexpay.com/sdk/v2/connexpay.min.js"></script>Step 2: Create a Container Element
Add an empty div where the payment form will be rendered:
<div id="connexpay-element"></div>Step 3: Initialize the SDK
Initialize the SDK with your client ID:
const connexpay = ConnexPay('your_client_id');Step 4: Create a Checkout Session (Backend)
On your server, create a checkout session with the payment details:
// Example Node.js backend endpoint
app.post('/api/checkout/session', async (req, res) => {
try {
const response = await fetch('https://sandboxsalesapi.connexpay.com/api/v2/Checkout/checkout-session', {
method: 'POST',
headers: {
'Authorization': `Bearer ${YOUR_API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
ClientId: 'your_client_id',
TenderTypeOptions: ['Credit', 'GooglePay'],
Sale: {
DeviceGuid: 'your_device_guid',
Amount: 125.50
}
})
});
const data = await response.json();
res.json({ checkoutSessionId: data.checkoutSessionId });
} catch (error) {
res.status(500).json({ error: 'Failed to create checkout session' });
}
});Step 5: Create the Payment Form
Fetch the checkout session ID from your backend and create the payment form:
<script>
const connexpay = ConnexPay('your_client_id');
// Wait for SDK to be ready
const initializePaymentForm = async () => {
try {
// Get checkout session from your backend
const response = await fetch('/api/checkout/session', { method: 'POST' });
const { checkoutSessionId } = await response.json();
// Configure appearance (optional)
await connexpay.setAppearance({
theme: 'light',
layout: 'accordion'
});
// Set customer information (optional)
await connexpay.setCustomer({
firstName: 'Sarah',
lastName: 'Johnson',
email: '[email protected]'
});
// Create the payment form
await connexpay.createPaymentForm({
element: '#connexpay-element',
checkoutSessionID: checkoutSessionId
});
} catch (error) {
console.error('Failed to initialize payment form:', error);
}
};
// Initialize when SDK is ready
if (connexpay.isSDKReady()) {
initializePaymentForm();
} else {
connexpay.on('ready', initializePaymentForm);
}
</script>Step 6: Handle Payment Submission
Add a function to process payments when the user submits the form:
<button onclick="submitPayment()">Pay Now</button>
<script>
async function submitPayment() {
try {
const result = await connexpay.confirmPayment();
if (result) {
console.log('Payment successful:', result);
// Redirect to success page or show confirmation
window.location.href = '/payment-success';
}
} catch (error) {
console.error('Payment failed:', error);
// Show error message to user
alert('Payment failed: ' + error.message);
}
}
</script>Complete Working Example
Here's a complete HTML file that puts it all together:
<!DOCTYPE html>
<html>
<head>
<title>ConnexPay Quick Start</title>
<script src="https://js.connexpay.com/sdk/v2/connexpay.min.js"></script>
</head>
<body>
<h1>Checkout</h1>
<!-- Payment form container -->
<div id="connexpay-element"></div>
<!-- Submit button -->
<button id="submit-button" onclick="submitPayment()" disabled>
Pay Now
</button>
<script>
const connexpay = ConnexPay('your_client_id');
const submitButton = document.getElementById('submit-button');
const initializePaymentForm = async () => {
try {
// Get checkout session from backend
const response = await fetch('/api/checkout/session', { method: 'POST' });
const { checkoutSessionId } = await response.json();
// Configure the payment form
await connexpay.setAppearance({ theme: 'light', layout: 'accordion' });
await connexpay.setCustomer({
firstName: 'Sarah',
lastName: 'Johnson',
email: '[email protected]'
});
// Create payment form
await connexpay.createPaymentForm({
element: '#connexpay-element',
checkoutSessionID: checkoutSessionId
});
} catch (error) {
console.error('Failed to initialize payment form:', error);
}
};
// Enable submit button when form is rendered
connexpay.on('rendered', () => {
submitButton.disabled = false;
});
// Handle errors
connexpay.on('error', (error) => {
console.error('SDK Error:', error);
alert('Error: ' + error.message);
});
// Initialize when ready
if (connexpay.isSDKReady()) {
initializePaymentForm();
} else {
connexpay.on('ready', initializePaymentForm);
}
// Payment submission
async function submitPayment() {
submitButton.disabled = true;
submitButton.textContent = 'Processing...';
try {
const result = await connexpay.confirmPayment();
if (result.sale) {
console.log('Payment successful:', result.sale.guid);
window.location.href = '/payment-success';
}
} catch (error) {
console.error('Payment failed:', error);
alert('Payment failed: ' + error.message);
submitButton.disabled = false;
submitButton.textContent = 'Pay Now';
}
}
</script>
</body>
</html>Next Steps
Now that you have a basic integration working:
- Understand the concepts: Read the Core Concepts guide
- Customize the appearance: Learn about Appearance & Theming
- Handle errors properly: Review Error Handling
Testing Your Integration
Use these test values to verify your integration:
- Test Credit Card: 4111 1111 1111 1111
- Expiry: Any future date
- CVV: Any 3-digit number
Updated 3 months ago
Did this page help you?
