A modern, reusable TypeScript package for securely storing credit card information using SmartCheckout.
npm install smartcheckout-sdk
import SmartCheckout from 'smartcheckout-sdk';
// Option A (recommended in SPAs): set meta or global for auto-detection
// <meta name="smartcheckout:api-base-url" content="https://api.example.com" />
// or: window.SMARTCHECKOUT_API_BASE_URL = import.meta.env.VITE_API_BASE_URL;
const smartcheckout = new SmartCheckout('pk_sandbox_xxxx');
// Create and mount a credit card form
const creditCardForm = await smartcheckout.initEmbeddedCreditCardForm({
onSuccess: (result) => {
console.log('Card stored! Token:', result.token);
// Use the token in your application
},
onError: (error) => {
console.error('Error:', error.message);
}
});
// Mount the form to a container
creditCardForm.mount('#container');
const smartcheckout = new SmartCheckout('pk_sandbox_xxxx');
// Create and mount a CVC verification form
const cvcForm = await smartcheckout.initCVCVerificationForm({
code: 'verification_code_from_url',
onSuccess: (result) => {
console.log('CVC verified successfully!');
},
onError: (error) => {
console.error('Verification failed:', error.message);
}
});
// Mount the form to a container
cvcForm.mount('#container');
<!-- Just provide a container element -->
<div id="container"></div>
That's it! The SmartCheckout package will handle everything else.
Creates a new SmartCheckout instance.
Parameters:
publishableKey
(string, required) - Your SmartCheckout publishable key
The SDK discovers the API base URL at runtime via:
- A
<meta name="smartcheckout:api-base-url" content="..." />
tag, or window.SMARTCHECKOUT_API_BASE_URL = '...'
, or- A built-in default suitable for local development
Returns: SmartCheckout instance
Creates a new embedded credit card form.
Parameters:
options
(object) - Form configuration options
Options:
onSuccess
(function) - Callback when card is stored successfullyonError
(function) - Callback when an error occursstyling
(object) - Custom field styling (optional)
Returns: Promise - The credit card form instance
Mounts the form to a container element.
Parameters:
selector
(string) - CSS selector for the container element
Returns: CreditCardForm - Returns self for chaining
Unmounts the form from its container.
Returns: CreditCardForm - Returns self for chaining
Submits the form programmatically.
Returns: Promise - Promise resolving to the card token data
Gets the current form state.
Returns: Object - Current form state
Updates the form styling.
Parameters:
styling
(object) - New styling options
Returns: CreditCardForm - Returns self for chaining
Creates a new CVC verification form.
Parameters:
options
(object) - Form configuration options
Options:
code
(string, required) - The verification code from the URLonSuccess
(function) - Callback when CVC is verified successfullyonError
(function) - Callback when verification failsstyling
(object) - Custom field styling (optional)
Returns: Promise - The CVC verification form instance
Mounts the form to a container element.
Parameters:
selector
(string) - CSS selector for the container element
Returns: CVCVerificationForm - Returns self for chaining
Unmounts the form from its container.
Returns: CVCVerificationForm - Returns self for chaining
Submits the form programmatically.
Returns: Promise - Promise resolving to the verification result
Updates the form styling.
Parameters:
styling
(object) - New styling options
Returns: CVCVerificationForm - Returns self for chaining
MIT License - see LICENSE file for details.