DOKU JS Integration Guide
What is DOKU JS ?
The DOKU JS Integration feature enables non-PCI DSS certified merchants to securely collect customer card details for online transactions by embedding a Javascript into their HTML files while still having the freedom to manages how their checkoutpage will looks like. This process ensures compliance with PCI DSS standards and provides flexibility for various integration models—either through by using the merchant’s own front-end integrated or with tokenization
Integration steps
Understanding the complete payment flow from merchant perspective:
1. Generate session_id
Generate a session id to be used for the transaction in this guide
2. Include the SDK
Add the SDK script to your HTML page:
<!-- For sandbox/testing environment -->
<script src="https://api-sandbox.doku.com/card-session/sandbox/v2.0.0/card-session.js"></script>
<!-- For production environment -->
<script src="https://api.doku.com/card-session/production/v2.0.0/card-session.js"></script>3. Initialize the SDK
Configure the SDK with your environment and client ID:
// Initialize SDK (environment is pre-configured in the SDK file)
PG.init().then((sdkInstance) => {
// Verify environment
console.log('Environment:', PG.getEnvironment());
console.log('API URL:', PG.getApiBaseUrl());
// Important: Chain the Device Data API status check
return sdkInstance.isDeviceDataApiSuccessful();
})
.then(() => {
// This block runs ONLY if both initialization AND the Device Data API check succeeded.
// Proceed with secure payment flow (e.g., enabling the Pay button)
document.getElementById('payButton').disabled = false;
})
.catch((error) => {
// This block catches all failures (critical init error OR Device Data API error).
// Proceed with secure payment flow (e.g., disable payment and alert the user/system)
document.getElementById('payButton').disabled = true;
});
4. Card Validation
Cards Number(PAN) has several rules to validate is validity, Use built-in validation utilities before submitting:
// Validate card number
if (!PG.card.validateCardNumber(cardNumber)) {
alert('Invalid card number'); //can be replaced with your own alert
return;
}
// Detect card brand
const brand = PG.card.detectBrand(cardNumber); // 'visa', 'mastercard', 'amex', 'discover', 'jcb', 'dinersclub', 'unionpay'
// Validate expiry
if (!PG.card.validateExpiry(month, year)) {
alert('Invalid or expired card'); //can be replaced with your own alert
return;
}
// Validate CVV
if (!PG.card.validateCvv(cvv, brand)) {
alert('Invalid CVV'); //can be replaced with your own alert
return;
}5. Submit the payment
Submit the card data with the session ID from step 1:
const cardData = {
session_id: document.getElementById('paymentSessionId'),
card_number: document.getElementById('cardNumber'),
expiry_month: document.getElementById('expiryMonth'),
expiry_year: document.getElementById('expiryYear'),
cvv: document.getElementById('cvv'),
card_holder_first_name: document.getElementById('firstName').value,
card_holder_last_name: document.getElementById('lastName').value,
card_holder_email: document.getElementById('email').value,
card_holder_phone_number: document.getElementById('phone')?.value,
billing_information: {
address: document.getElementById('billingAddress').value,
city: document.getElementById('billingCity').value,
postal_code: document.getElementById('billingPostalCode').value,
country: document.getElementById('billingCountry').value,
},
};
// Submit to DOKU
PG.payment.collectCardData(cardData, function(error, response) {
if (error) {
alert('Payment failed: ' + error.message);
} else {
alert('Payment successful!');
}
});7. Handle 3D Secure
When 3DS authentication(OTP) is required, the SDK automatically redirects the user to issuer's OTP page after 1.5 seconds:
PG.payment.collectCardData(cardData, function(error, response) {
if (response.requires_action) {
// SDK will automatically redirect to response.action_url after 1.5s
// You can show a loading message during this time
showMessage('Redirecting to authentication page...');
// The redirect happens automatically - no need to call window.location.href
}
});When user completes authentication, they'll be redirected back to your callback URL:
// Check for 3DS callback in URL (on your callback page)
const urlParams = new URLSearchParams(window.location.search);
const paymentRequestId = urlParams.get('payment_request_id');
const status = urlParams.get('status');
if (paymentRequestId && status) {
// User returned from 3DS
if (status === 'success') {
showSuccessMessage('Payment authenticated successfully!');
} else {
showErrorMessage('Payment authentication failed');
}
}8.Receives Notification
Follow this Notification Guides to receives notification
Handling Response Code
Follow this Response Code documentation for handling each cases
Last updated
Was this helpful?