Payments & customers
Tokenization
Tokenize cards securely right from your frontend using recaudo.js v2. The library automatically loads the SDK of the payment processor (Conekta, Stripe) configured in your account and tokenizes the card transparently.
Integration in 4 steps
Include the JavaScript library
<script src="https://recaudo.app/js/recaudo.min.js"></script>
Initialize the library
Initialize recaudo.js with your Publishable Key. The init() method loads the SDK of the configured processor.
// Crear instancia con tu Publishable Key
const recaudo = new Recaudo('pk_live_tu_publishable_key');
// Inicializar (carga el SDK del procesador automáticamente)
await recaudo.init();
// Verificar que está listo
console.log('Gateway:', recaudo.getGateway()); // 'conekta' o 'stripe'
Tokenize the card
Card data is tokenized directly with the payment processor. It never goes through your server.
async function handlePayment() {
try {
const token = await recaudo.createToken({
number: '4242424242424242',
exp_month: 12,
exp_year: 2027,
cvv: '123',
name: 'Juan Pérez',
email: 'juan@example.com',
// Campos opcionales (requeridos para AMEX)
address: 'Av. Reforma 123',
city: 'CDMX',
state: 'CDMX',
postal_code: '06600',
country: 'MX'
});
// token contiene:
// - id: Token del procesador (tok_xxx para Conekta, pm_xxx para Stripe)
// - gateway: 'conekta' o 'stripe'
// - card: { brand, last4, exp_month, exp_year }
// Enviar al backend para procesar
await fetch('/tu-backend/procesar-pago', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
token: token.id,
card: token.card, // Incluir info de tarjeta para mejor tracking
amount: 100.00
})
});
} catch (error) {
// error.type: 'validation_error', 'gateway_error', 'sdk_error'
console.error('Error:', error.message);
}
}
Process the charge from your backend
Use the token to create a charge or a subscription. Include the card object for better tracking.
curl -X POST https://recaudo.app/api/v1/charges \
-H "Authorization: Bearer sk_live_tu_secret_key" \
-H "Content-Type: application/json" \
-d '{
"amount": 100.00,
"currency": "MXN",
"token": "tok_abc123...",
"card": {
"brand": "visa",
"last4": "4242",
"exp_month": 12,
"exp_year": 2027
},
"description": "Compra en tienda",
"receipt_email": "cliente@example.com"
}'
curl -X POST https://recaudo.app/api/v1/subscriptions \
-H "Authorization: Bearer sk_live_tu_secret_key" \
-H "Content-Type: application/json" \
-d '{
"plan": "5f1c7b3e-2a94-4d18-b6c0-9e3f8a1d7c25",
"customer": "8e250c25-9d84-4892-98ec-d71fc1fcd3c7",
"token": "tok_abc123...",
"card": {
"brand": "visa",
"last4": "4242",
"exp_month": 12,
"exp_year": 2027
}
}'
Save a card from your server
Send the card once with your secret key. Recaudo stores it encrypted in its vault and registers it with every active gateway of your business. Keep only the returned id: if a gateway declines a charge, Recaudo retries with another one without asking for the card again.
curl -X POST https://recaudo.app/api/v1/payment-methods \
-H "Authorization: Bearer sk_live_..." \
-H "Content-Type: application/json" \
-d '{"customer": "8c2f…", "card_details": {"number": "4242424242424242", "exp_month": 12, "exp_year": 2030, "cvv": "123", "name": "Ana López"}}'
{
"object": "payment_method",
"data": {
"id": "5d2c…",
"customer_id": "8c2f…",
"type": "card",
"card_brand": "visa",
"card_last4": "4242",
"card_exp_month": 12,
"card_exp_year": 2030,
"is_default": true,
"supports_orchestration": true
},
"gateways": [
{
"gateway": "billpocket",
"gateway_account": "1a7e…"
},
{
"gateway": "conekta",
"gateway_account": "77c0…"
}
]
}
gateways lists where the card is ready; gateway tokens are never returned.
Note
Link for the customer to save a card
Create a "save card" link for a customer and send it. The customer saves the card without paying; when done you get the payment_method.attached webhook with the id you use in POST /v1/charges, subscriptions and collection retries.
curl -X POST https://recaudo.app/api/v1/payment-methods/setup-links \
-H "Authorization: Bearer sk_live_..." \
-H "Content-Type: application/json" \
-d '{"customer": "8c2f…"}'
{
"object": "setup_link",
"id": "3b91…",
"customer": "8c2f…",
"url": "https://recaudo.app/pay/k3J9…",
"status": "active",
"expires_at": "2026-11-14T12:00:00-06:00"
}
Note
Token object (createToken response)
{
"id": "tok_2sR5Vx1J5K...", // Token del procesador (tok_xxx Conekta, pm_xxx Stripe)
"gateway": "conekta", // Procesador utilizado
"card": {
"brand": "visa",
"last4": "4242",
"exp_month": 12,
"exp_year": 2027
},
"created_at": "2024-01-15T10:30:00.000Z"
}
API endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /v1/tokens/config |
Get the processor configuration (used by recaudo.js to load the right SDK) |
| GET | /v1/tokens/public-key |
Get public key for encryption (legacy v1) |
| POST | /v1/tokens |
Create token from encrypted data (legacy v1) |
| GET | /v1/tokens/:token |
Get information about a token |
| DELETE | /v1/tokens/:token |
Delete a token |
recaudo.js methods
| Method | Description |
|---|---|
new Recaudo(publishableKey) |
Constructor. Creates an instance with your Publishable Key. |
init() |
Loads the SDK of the configured processor. Must be called before createToken(). |
mountCard(selector, options?) |
Mounts the secure card field. Required with Stripe, which does not accept card numbers from your own inputs: then call createToken({ name, email }). The bank may ask for 3D Secure when saving. |
requiresCardElement() |
true when the account processes with Stripe and you must use mountCard() instead of your own card inputs. |
createToken(cardData) |
Tokenizes the card data with the processor. |
getGateway() |
Returns the processor name ('conekta', 'stripe'). |
isInitialized() |
Returns true if init() was already called. |
detectCardBrand(number) |
Detects the card brand (visa, mastercard, amex, etc). |
Error handling
recaudo.js errors include a type to make them easier to handle:
try {
await recaudo.init();
const token = await recaudo.createToken(cardData);
} catch (error) {
switch (error.type) {
case 'validation_error':
// Datos de tarjeta inválidos
showError(error.message);
break;
case 'gateway_error':
// Error del procesador (tarjeta rechazada, etc)
showError(error.message);
break;
case 'sdk_error':
// Error cargando el SDK del procesador
showError('Error de conexión. Intenta de nuevo.');
break;
case 'configuration_error':
// Procesador no configurado
showError('Error de configuración.');
break;
default:
showError('Error inesperado.');
}
}