Saltar al contenido principal

Pagar un pedido como usuario registrado

Inicia la sesión de un usuario, crea un pedido y abre una sesión de pago para él. Con una cuenta de pago online, redirige al usuario a la paymentUrl de la sesión.

Qué hace este ejemplo:

  • Inicia la sesión de un usuario registrado
  • Crea un pedido
  • Crea una sesión de pago

Requisitos previos:

  • Un PROJECT_URL y un APP_TOKEN válidos para la API de OneEntry.
  • Un usuario registrado
  • Un almacenamiento de pedidos configurado con el marcador "orders"

Notas:

  • Los ejemplos manejan los errores de forma mínima: lanzan una excepción cuando una llamada devuelve un objeto de error.
  • Por defecto (isShell: true), el SDK devuelve un objeto de error en lugar de lanzar una excepción, así que comprueba el resultado, p. ej., 'statusCode' in result. Con isShell: false lanza la excepción y los errores se manejan con try/catch. Consulta Errors.

Documentación de la plataforma:

Referencia del SDK:

Pruébalo en vivo​

Ejecuta este ejemplo en el sandbox del SDK de JS. Conecta tu Project URL y App Token en la primera visita y luego abre:

Escenario​

1. Importar defineOneEntry y definir PROJECT_URL y APP_TOKEN​

import { defineOneEntry } from 'oneentry';
import type { IOrderData } from 'oneentry';

const PROJECT_URL = 'https://your-project.oneentry.cloud';
const APP_TOKEN = 'your-app-token';

2. Crear un cliente de API​

const { AuthProvider, Orders, Payments } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. Autentica al usuario con AuthProvider.auth('email', authData)​

const authData = {
authData: [
{
marker: 'email_reg',
value: 'your-email',
},
{
marker: 'password_reg',
value: 'your-password',
},
],
};
const user = await AuthProvider.auth('email', authData);

if ('statusCode' in user) {
throw new Error(user.message);
}

console.log(user);
Resultado:
{
"userIdentifier": "john@example.com",
"authProviderIdentifier": "email",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR...pZCI6MTYsImF1dGhQ",
"refreshToken": "1745494429101-...-2834edf8"
}

4. Crear un pedido con Orders.createOrder()​

const body: IOrderData = {
formIdentifier: 'order',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'string',
marker: 'name',
value: 'John Smith',
},
],
products: [
{
productId: 15,
quantity: 1,
},
],
};
const orderData = await Orders.createOrder('orders', body);

if ('statusCode' in orderData) {
throw new Error(orderData.message);
}
Resultado:
{
"id": 116,
"formIdentifier": "order",
"paymentAccountIdentifier": "cash",
"formData": [
{
"type": "string",
"marker": "name",
"value": "John Smith"
}
],
"products": [
{
"productId": 15,
"quantity": 1
}
],
"currency": "USD",
"totalSum": 400,
"bonusApplied": 0,
"totalDue": 400,
"discountConfig": {
"orderDiscounts": [],
"productDiscounts": [],
"coupon": null,
"settings": {
"allowStacking": false,
"maxDiscountValue": null,
"allowGiftStacking": false,
"maxBonusPaymentPercent": null,
"minBonusAmount": null,
"minOrderAmountForBonus": null,
"giftRefundPolicy": "KEEP_GIFT"
},
"additionalDiscountsMarkers": [],
"totalRaw": 400,
"totalSumWithDiscount": 400,
"excludedGiftProductIds": [],
"bonus": {
"availableBalance": 0,
"maxBonusDiscount": 0,
"minBonusAmount": null,
"minOrderAmountForBonus": null,
"bonusApplied": 0
},
"bonusApplied": 0,
"totalDue": 400
},
"statusIdentifier": "upcoming",
"statusLocalizeInfos": {
"title": "Upcoming"
},
"createdDate": "2026-06-06T11:38:21.644Z"
}

5. Crear una sesión de pago con Payments.createSession()​

const sessionData = await Payments.createSession(orderData.id, 'session');

if ('statusCode' in sessionData) {
throw new Error(sessionData.message);
}

Resultado:
{
"id": 65,
"createdDate": "2026-06-06T11:38:22.125Z",
"updatedDate": "2026-06-06T11:38:22.125Z",
"type": "session",
"status": "waiting",
"paymentAccountId": 1,
"orderId": 116,
"amount": null,
"paymentUrl": null
}

6. Obtener la sesión de pago por ID de pedido con Payments.getSessionByOrderId()​

const sessionByOrderId = await Payments.getSessionByOrderId(orderData.id);

if ('statusCode' in sessionByOrderId) {
throw new Error(sessionByOrderId.message);
}

console.log({ user, sessionData, sessionByOrderId });
Resultado:
[
{
"id": 65,
"createdDate": "2026-06-06T11:38:22.125Z",
"updatedDate": "2026-06-06T11:38:22.125Z",
"type": "session",
"status": "waiting",
"paymentAccountId": 1,
"orderId": 116,
"amount": null,
"paymentUrl": null
}
]

Ejemplo final​

// 1. Import defineOneEntry and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';
import type { IOrderData } from 'oneentry';

const PROJECT_URL = 'https://your-project.oneentry.cloud';
const APP_TOKEN = 'your-app-token';

// 2. Create an API client
const { AuthProvider, Orders, Payments } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Sign in
const authData = {
authData: [
{
marker: 'email_reg',
value: 'your-email',
},
{
marker: 'password_reg',
value: 'your-password',
},
],
};
const user = await AuthProvider.auth('email', authData);

if ('statusCode' in user) {
throw new Error(user.message);
}

console.log(user);

// 4. Create an order
const body: IOrderData = {
formIdentifier: 'order',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'string',
marker: 'name',
value: 'John Smith',
},
],
products: [
{
productId: 15,
quantity: 1,
},
],
};
const orderData = await Orders.createOrder('orders', body);

if ('statusCode' in orderData) {
throw new Error(orderData.message);
}

// 5. Create a payment session
const sessionData = await Payments.createSession(orderData.id, 'session');

if ('statusCode' in sessionData) {
throw new Error(sessionData.message);
}

// 6. Get the payment session by order ID
const sessionByOrderId = await Payments.getSessionByOrderId(orderData.id);

if ('statusCode' in sessionByOrderId) {
throw new Error(sessionByOrderId.message);
}

console.log({ user, sessionData, sessionByOrderId });