import { loadSecureFields } from '@purse-eu/web-sdk';
let secureForm;
async function initCvvOnlyForm(tokenBrand) {
const sf = await loadSecureFields('sandbox');
secureForm = await sf.initSecureFields({
tenantId: '[your vault tenant_id]',
apiKey: '[your purse api_key (optional)]',
config: {
brands: [tokenBrand],
fields: {
cvv: {
target: 'cvv-only-placeholder',
placeholder: 'ex: 123',
ariaLabel: 'CVV',
iframeTitle: 'CVV',
}
},
styles: { color: '#181818' }
}
});
await secureForm.render();
}
async function displaySavedCards() {
const response = await fetch('/your-server/tokens');
const tokens = await response.json();
tokens.forEach(token => {
const button = document.createElement('button');
button.textContent = token.description.display_token || token.id;
button.onclick = async () => {
await initCvvOnlyForm(token.description.brand_name);
const result = await secureForm.submit();
await fetch('/your-server/pay-with-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
vaultFormToken: result.vault_form_token,
walletTokenId: token.id,
}),
});
};
document.getElementById('saved-cards-list').appendChild(button);
});
}
async function deleteToken(tokenId) {
await fetch(`/your-server/tokens/${tokenId}`, { method: 'DELETE' });
}