Saltar al contenido principal

Hacer un pedido como invitado

Realiza un pedido como invitado, sin registro de usuario.

Qué hace este ejemplo:

  • El usuario no ha iniciado sesión
  • El usuario rellena un formulario de contacto (por ejemplo, guest_order)
  • El usuario selecciona productos (puedes predefinir una lista)
  • El pedido se crea sin token de acceso. El grupo de usuarios invitados debe tener permiso para crear pedidos y sesiones de pago

Requisitos previos:

  • Un PROJECT_URL y un APP_TOKEN válidos para la API de OneEntry.
  • Un almacén de pedidos con el marcador guest_orders y un formulario de pedido vinculado con el marcador guest_order.
  • Permiso para que el grupo de usuarios invitados cree pedidos y sesiones de pago.
  • Una lista de productId y quantity

Notas:

  • Los campos del formulario (marker, type) deben coincidir con la configuración del formulario en el panel de administración.
  • Para enviar una notificación sobre un pedido, usa Events.
  • 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 { Orders, Payments } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. Preparar los datos que normalmente llegan del formulario del frontend​

const guestFormData = [
{
marker: 'guest_name',
type: 'string',
value: 'John Smith',
},
{
marker: 'guest_phone',
type: 'string',
value: '+12025550147',
},
{
marker: 'guest_email',
type: 'string',
value: 'john@example.com',
},
];
const productsData = [
{
productId: 15,
quantity: 1,
},
];
const body: IOrderData = {
formIdentifier: 'guest_order',
paymentAccountIdentifier: 'cash',
formData: guestFormData,
products: productsData,
};
Resultado:
{
"formIdentifier": "guest_order",
"paymentAccountIdentifier": "cash",
"formData": [
{
"marker": "guest_name",
"type": "string",
"value": "John Smith"
},
{
"marker": "guest_phone",
"type": "string",
"value": "+12025550147"
},
{
"marker": "guest_email",
"type": "string",
"value": "john@example.com"
}
],
"products": [
{
"productId": 15,
"quantity": 1
}
]
}

4. Crear un pedido de invitado​

const order = await Orders.createOrder('guest_orders', body);

if ('statusCode' in order) {
throw new Error(order.message);
}
Resultado:
{
"id": 113,
"formIdentifier": "guest_order",
"paymentAccountIdentifier": "cash",
"formData": [
{
"marker": "guest_name",
"type": "string",
"value": "John Smith"
},
{
"marker": "guest_phone",
"type": "string",
"value": "+12025550147"
},
{
"marker": "guest_email",
"type": "string",
"value": "john@example.com"
}
],
"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": "guest_upcoming",
"statusLocalizeInfos": {
"title": "Upcoming"
},
"createdDate": "2026-06-06T11:37:20.044Z"
}

5. Crear una sesión de pago​

const paymentSession = await Payments.createSession(
order.id,
'session',
false,
);

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

console.log(paymentSession);
Resultado:
{
"id": 62,
"createdDate": "2026-06-06T11:37:21.260Z",
"updatedDate": "2026-06-06T11:37:21.260Z",
"type": "session",
"status": "waiting",
"paymentAccountId": 1,
"orderId": 113,
"amount": null,
"paymentUrl": null
}

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

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

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

console.log(sessionByOrderId, paymentSession);
Resultado:
[
{
"id": 62,
"createdDate": "2026-06-06T11:37:21.260Z",
"updatedDate": "2026-06-06T11:37:21.260Z",
"type": "session",
"status": "waiting",
"paymentAccountId": 1,
"orderId": 113,
"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 { Orders, Payments } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Prepare the data that usually comes from the frontend form
const guestFormData = [
{
marker: 'guest_name',
type: 'string',
value: 'John Smith',
},
{
marker: 'guest_phone',
type: 'string',
value: '+12025550147',
},
{
marker: 'guest_email',
type: 'string',
value: 'john@example.com',
},
];
const productsData = [
{
productId: 15,
quantity: 1,
},
];
const body: IOrderData = {
formIdentifier: 'guest_order',
paymentAccountIdentifier: 'cash',
formData: guestFormData,
products: productsData,
};

// 4. Create a guest order
const order = await Orders.createOrder('guest_orders', body);

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

// 5. Create a payment session
const paymentSession = await Payments.createSession(
order.id,
'session',
false,
);

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

console.log(paymentSession);

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

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

console.log(sessionByOrderId, paymentSession);