@malga/tokenization
v5.1.0
Published
Simple way to tokenize cards with Malga
Readme
Simple way to tokenize cards with Malga
Getting Started
First of all, you'll need to install our SDK into your project:
yarn add @malga/tokenization
# or
npm install @malga/tokenization
# or
pnpm add @malga/tokenizationAuthentication
The SDK runs in the browser, so the apiKey you pass to it is visible to anyone who opens your checkout page. Never use your account's secret key there.
- In your backend, create a sub-key for each checkout session with
POST /v1/auth, using only thetokensscope and a short expiration (expiresis in seconds):
curl --request POST 'https://api.malga.io/v1/auth' \
--header 'X-Client-Id: YOUR_CLIENT_ID' \
--header 'X-Api-Key: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{ "scope": ["tokens"], "expires": 900 }'- Return only the
publicKey(and yourclientId) to the front-end. The secret key stays on your server. - In the front-end, fetch that sub-key and only then create the
MalgaTokenizationinstance, passing thepublicKeyasapiKey.
Good practices:
- Use only the
tokensscope. The SDK doesn't need anything else. - Use a short expiration (for example,
900) and a new sub-key for each checkout session. Don't useexpires: 0in the front-end: a sub-key without expiration stays valid indefinitely. - Don't hardcode the key in your HTML, in front-end config files or in build-time variables that end up in the bundle.
- Protect the backend route that creates sub-keys: create them only for a valid purchase session in your system and rate limit that route.
- After
expires, the API rejects the sub-key with403. With a stringapiKey, calldestroy(), fetch a new sub-key and create a new instance. To renew it without recreating the fields, pass a function asapiKey(see Fetching the API key on demand).
Use https://sandbox-api.malga.io with your sandbox credentials while testing, together with sandbox: true.
Usage
- Add the identification keys for each field involving card data in your form
<form onSubmit="{handleGetTokenId}">
<section>
<div className="form-group">
<label htmlFor="card-number">Card Number</label>
<div id="card-number" className="form-control"></div>
</div>
<div className="form-group">
<label htmlFor="card-holder-name">Card Holder Name</label>
<div id="card-holder-name" className="form-control"></div>
</div>
<div className="form-group">
<label htmlFor="card-cvv">Card CVV</label>
<div id="card-cvv" className="form-control"></div>
</div>
<div className="form-group">
<label htmlFor="card-expiration-date">Card Expiration Date</label>
<div id="card-expiration-date" className="form-control"></div>
</div>
</section>
<button type="submit">Submit</button>
</form>- Now fetch the sub-key from your backend, configure our SDK with it and call the tokenize method in your function handling the form submit
import { MalgaTokenization } from '@malga/tokenization'
// Sub-key with scope ["tokens"] and a short expiration, created by your backend for this checkout session
const { publicKey, clientId } = await fetch('/checkout/malga-key', {
method: 'POST',
}).then((response) => response.json())
const malgaTokenization = new MalgaTokenization({
apiKey: publicKey,
clientId,
options: {
config: {
fields: {
cardNumber: {
container: 'card-number',
placeholder: '9999 9999 9999 9999',
},
cardHolderName: {
container: 'card-holder-name',
placeholder: 'Its a test',
},
cardExpirationDate: {
container: 'card-expiration-date',
placeholder: 'MM/YY',
},
cardCvv: {
container: 'card-cvv',
placeholder: '999',
},
},
styles: {
// With this object, it's possible to change the styles of input components
input: {
color: '#000',
'font-size': '16px',
},
},
preventAutofill: false,
},
sandbox: true,
},
})
// You can use others events like:
malgaTokenization.on('cardTypeChanged', (event) => {
console.log('cardTypeChanged', event)
})
malgaTokenization.on('validity', (event) => {
console.log('validation', event)
})
malgaTokenization.on('blur', (event) => {
console.log('blur', event)
})
malgaTokenization.on('focus', (event) => {
console.log('blur', event)
})
malgaTokenization.on('loading', ({ isLoading }) => {
submitButton.disabled = isLoading
})
async function handleSubmit(event) {
event.preventDefault()
const { tokenId } = await malgaTokenization.tokenize()
console.log({ tokenId })
// Now you just need to send the tokenId along with
// the rest of the transaction data to your API
}- Submit the form and see the magic happen 💳
For more details on the implementation, you can access our documentation by clicking here.
Loading and timeout
loading is true when a tokenize() starts and false when it finishes. If the card number iframe does not answer within 30 seconds, tokenize() resolves with { tokenId: '', error: { type: 'timeout', code: 0, message } } and loading goes back to false.
Fetching the API key on demand
apiKey also accepts a function that returns the key, sync or async. The SDK calls it on every tokenize(), right before sending the card data, so you can create a short-lived sub-key in your backend only when the customer submits the form. If the sub-key expires, the function can return a new one on the next tokenize(), without recreating the fields or losing the typed card.
const malgaTokenization = new MalgaTokenization({
apiKey: async () => {
// Your backend route returns the publicKey of a sub-key created with POST /v1/auth
const response = await fetch('/checkout/malga-key', { method: 'POST' })
const { publicKey } = await response.json()
return publicKey
},
clientId: '<YOUR_CLIENT_ID>',
options,
})loadingistruewhile the function runs.- If the function throws or returns an empty value,
tokenize()resolves without sending the card, with{ tokenId: '', error: { type: 'invalid_request_error', code: 0, message: 'Could not get the API key: ...' } }. The message doesn't include the error thrown by your function, so details of your backend don't reach the customer. Withdebug: true, that error is logged in the console. - If the function doesn't answer within 30 seconds,
tokenize()resolves witherror.type: 'timeout'. A key returned after that is ignored. - The 30 seconds of the function and the 30 seconds of the card number iframe are counted separately, so in the worst case
tokenize()takes up to 60 seconds to resolve. - Once the key arrives, the SDK checks the card fields again. If one was cleared in the meantime, it resolves with the missing card data error and sends nothing.
- If the instance is destroyed, or its card fields are replaced by another instance, while the function runs, the card is not sent. When the function returns a key,
tokenize()rejects; when it fails or times out,tokenize()resolves with the errors above. - The function is called on every
tokenize(). To keep one sub-key per checkout session, as recommended in the authentication docs, cache it in your function (or return the same sub-key from your backend route while it is valid) and fetch a new one before itsexpirestime is over. An expired sub-key is rejected by the API with403.
Missing card data
Before sending anything, tokenize() checks that every field configured in config.fields has a value. If any is empty, it resolves right away, without a request and without loading, with { tokenId: '', error: { type: 'invalid_request_error', code: 0, message, details } }, where details lists the missing fields (['cardCvv', 'cardExpirationDate']). Fields you didn't configure are not required.
Containers and more than one card
- A
containercan be any elementid(card-number,numero,card.number...). The collected data is keyed by the field (cardNumber,cardCvv...), never by the container name. - To render two or more cards on the same page, create one
MalgaTokenizationper card, each one with its own containers. A containeridmust never repeat on the page. - Each instance keeps its collected card in
sessionStorageundermalga-card-<cardNumber container>(it wasmalga-cardbefore). Only relevant if you read that key directly. - Two live instances can't share a container, and one instance can't use the same container for two fields. The constructor throws before rendering anything, naming the container:
ContainerInUseErrororDuplicatedContainerError. Calldestroy()on the old instance first.
Destroying an instance
destroy() undoes everything the constructor set up: it removes the iframes from the containers (and the classes the SDK added to them), stops waiting for containers that aren't in the DOM yet, removes the message listener and the on() handlers, clears the collected card from sessionStorage and frees the containers for a new instance. After it, tokenize() throws. Calling it more than once is safe.
Call it whenever the checkout unmounts. Without it, mounting the screen again throws ContainerInUseError, because the old instance still holds the containers.
Upgrading to 5.0.0: in 4.x, creating a new instance on the same containers without cleanup silently stacked a second set of iframes and kept the old listener alive. Now the constructor throws
ContainerInUseError. If your screen can mount more than once (ReactuseEffectwithout a cleanup,StrictMode, navigating back to the route), calldestroy()when it unmounts, as below.
// React
useEffect(() => {
const malgaTokenization = new MalgaTokenization({ apiKey, clientId, options })
return () => malgaTokenization.destroy()
}, [])Examples
If you have any doubts about the integration, you can click here to see some examples. The v2 example includes a backend route that creates the sub-key.
Contributing
Feel free to contribute to this project by submitting pull requests, creating documentation, or bringing ideas to make the project even better!
