Ir al contenido

Inicio rápido

El camino más corto desde cero hasta un pago completado. Se asume que ya tiene una clave pública, una clave secreta y al menos una wallet de liquidación configurada. Mantenga la clave secreta en su servidor.

Ventana de terminal
npm install stablezact-pay

2. Crear el checkout en su servidor y abrirlo en el navegador

Sección titulada «2. Crear el checkout en su servidor y abrirlo en el navegador»

Todo pago parte de un checkout que su servidor crea. El importe se fija allí, de modo que el navegador no puede cambiar lo que se cobra al cliente. Su servidor envía su clave secreta; el navegador solo recibe la reference del checkout.

// Your backend, for example POST /api/checkout
const response = await fetch('https://hub.stablezact.com/api/v1/payments/checkout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.STABLEZACT_SECRET_KEY}`,
},
body: JSON.stringify({
amount: 49.99,
currency: 'USD',
orderId: 'ORD-1234',
callbackUrl: 'https://yourstore.com/webhooks/stablezact',
}),
});
const { checkout } = await response.json();
// Send checkout.reference (it starts with "ckr_") to your frontend.

Después, pase esa referencia al SDK:

import { useState } from 'react';
import { StablezactPayment } from 'stablezact-pay';
import 'stablezact-pay/dist/style.css';
export default function Checkout() {
const [open, setOpen] = useState(false);
const [reference, setReference] = useState(null);
async function startPayment() {
const res = await fetch('/api/checkout', { method: 'POST' });
const data = await res.json();
setReference(data.reference);
setOpen(true);
}
return (
<>
<button onClick={startPayment}>Pay with crypto</button>
{reference && (
<StablezactPayment
isOpen={open}
onClose={() => { setOpen(false); setReference(null); }}
publicKey="pk_your_public_key"
reference={reference}
onSuccess={({ paymentId, transactionHash }) => {
// The customer has submitted. Do not fulfil here. See step 3.
console.log('submitted', paymentId, transactionHash);
}}
onError={(error) => console.error(error)}
/>
)}
</>
);
}

apiUrl no es necesario: el SDK usa producción (hub.stablezact.com) por defecto. Consulte Configuración para staging.

¿No usa React? El mismo flujo en JavaScript simple:

const stablezact = new StablezactVanilla({
publicKey: 'pk_your_public_key',
theme: 'light',
});
document.getElementById('payButton').addEventListener('click', async () => {
const { reference } = await (await fetch('/api/checkout', { method: 'POST' })).json();
stablezact.open(
{ reference },
{
onSuccess: (result) => console.log('submitted', result),
onError: (error) => console.error(error),
}
);
});

Confirme desde su backend, donde el navegador no puede falsificar la respuesta:

Ventana de terminal
curl https://hub.stablezact.com/api/v1/payments/PAYMENT_UUID/status \
-H "X-Public-Key: pk_your_public_key"

Lea outcome:

{ "payment": { "outcome": "paid", "isFinal": true } }

paid significa que el dinero del cliente ha llegado y puede completar el pedido. Los otros dos valores son pending y failed. Esa es toda la regla, y es la misma sin importar cómo haya pagado el cliente.

Mejor aún, deje que se lo avisemos nosotros: consulte Webhooks.