Đặt vé
✅ Mục đích của kịch bản:
- Người dùng đăng nhập bằng thông tin xác thực của họ
- Chọn loại vé, ngày và giờ từ lịch trình
- Thanh toán cho đơn hàng và nhận vé qua email
✅ Những gì bạn cần:
- Một PROJECT_URL và APP_TOKEN hợp lệ để xác thực với API OneEntry.
- Người dùng đã đăng ký để xác thực
- Biểu mẫu với mã "tickets_form" và các trường "schedule" và "tickets"
- Kho lưu trữ đơn hàng đã được cấu hình với mã "tickets"
📌 Quan trọng:
- Chúng tôi không xử lý lỗi trong các ví dụ này.
- Bạn có thể xử lý lỗi trong trycatch hoặc trong một cấu trúc như "await Promise.catch((error) => error)"
Kịch bản
1. Nhập oneEntry và định nghĩa url và token
Ví dụ:
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
2. Tạo một khách hàng API với hàm defineOneEntry()
Ví dụ:
const { AuthProvider, Forms, Orders, Payments } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
3. Xác thực người dùng với AuthProvider.auth()
Dữ liệu:
[
{
"marker": "email_reg",
"value": "your-user@email.com"
},
{
"marker": "password_reg",
"value": "123456"
}
]
Ví dụ:
const authResponse = await AuthProvider.auth('email', {
authData,
});
Kết quả:
{
"userIdentifier": "your-user@email.com",
"authProviderIdentifier": "email",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR...pZCI6MTYsImF1dGhQ"
"refreshToken": "1745494429101-...-2834edf8"
}
4. Chúng tôi nhận dữ liệu biểu mẫu từ OneEntry CMS với Forms.getFormByMarker() để tạo một biểu mẫu trên giao diện người dùng
Ví dụ:
const formData = await Forms.getFormByMarker('tickets_form');
Kết quả:
{
"id": 9,
"attributeSetId": 12,
"type": "order",
"localizeInfos": {
"title": "Vé",
"titleForSite": "Vé",
"successMessage": "",
"unsuccessMessage": "",
"urlAddress": "",
"database": "0",
"script": "0"
},
"version": 6,
"position": 1,
"identifier": "tickets_form",
"processingType": "script",
"templateId": null,
"attributes": [
{
"type": "timeInterval",
"marker": "schedule",
"isLogin": null,
"isSignUp": null,
"position": 1,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Lịch trình",
"intervals": [
{
"id": "ef9cf848-7373-4b81-98bd-7ad33aa951e2",
"range": [
"2025-04-26T21:00:00.000Z",
"2025-04-26T21:00:00.000Z"
],
"external": [],
"intervals": [
{
"id": "f2042e47-c650-4fc6-ae1c-efdb109b450f",
"end": {
"hours": 21,
"minutes": 30
},
"start": {
"hours": 16,
"minutes": 30
},
"period": 90
}
],
"inEveryWeek": true,
"inEveryMonth": true
}
]
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "entity",
"marker": "tickets",
"isLogin": null,
"isSignUp": null,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [
{
"id": 8,
"depth": 0,
"title": "Vé",
"parentId": null,
"position": "0|hzzzz3:"
},
{
"id": "p-8-10",
"depth": 1,
"title": "Đồng",
"parentId": 8,
"position": 1,
"selected": true
},
{
"id": "p-8-11",
"depth": 1,
"title": "Bạc",
"parentId": 8,
"position": 2,
"selected": true
},
{
"id": "p-8-12",
"depth": 1,
"title": "Vàng",
"parentId": 8,
"position": 3,
"selected": true
}
],
"validators": {
"requiredValidator": {
"strict": true
}
},
"localizeInfos": {
"title": "Vé"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
}
5. Tạo một đơn hàng trong kho lưu trữ đơn hàng với Orders.createOrder()
Dữ liệu:
{
"formIdentifier": "tickets_form",
"paymentAccountIdentifier": "cash",
"formData": {
"en_US": [
{
"type": "entity",
"marker": "tickets",
"value": [
"p-8-10"
]
}
]
},
"products": [
{
"productId": 10,
"quantity": 1
}
]
}
Ví dụ:
const order = await Orders.createOrder('tickets', body);
Kết quả:
{
"id": 92,
"formIdentifier": "tickets_form",
"paymentAccountIdentifier": "cash",
"formData": [
{
"type": "entity",
"marker": "tickets",
"value": [
"p-8-10"
]
}
],
"products": [
{
"productId": 10,
"quantity": 1
}
],
"currency": "USD",
"totalSum": 100,
"createdDate": "2025-04-30T21:54:48.918Z"
}
6. Tạo phiên thanh toán với Payments.createSession()
Ví dụ:
const payment = await Payments.createSession(order.id, 'session', false);
Kết quả:
{
"id": 49,
"createdDate": "2025-04-30T21:54:50.058Z",
"updatedDate": "2025-04-30T21:54:50.058Z",
"type": "session",
"status": "waiting",
"paymentAccountId": 1,
"orderId": 92,
"paymentUrl": null
}
8. Lấy một đối tượng phiên thanh toán theo mã đơn hàng với Payments.getSessionByOrderId()
Ví dụ:
const sessionByOrderId = await Payments.getSessionByOrderId(order.id);
Kết quả:
{
"id": 49,
"createdDate": "2025-04-30T21:54:50.058Z",
"updatedDate": "2025-04-30T21:54:50.058Z",
"type": "session",
"status": "waiting",
"paymentAccountId": 1,
"orderId": 92,
"paymentUrl": null
}
Ví dụ cuối cùng
// 1. Nhập oneEntry và định nghĩa PROJECT_URL và APP_TOKEN
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
// 2. Tạo một khách hàng API
const { AuthProvider, Forms, Orders, Payments } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. Xác thực người dùng
const authData = [
{
marker: 'email_reg',
value: 'your-user@email.com',
},
{
marker: 'password_reg',
value: '123456',
},
];
const authResponse = await AuthProvider.auth('email', {
authData,
});
// 4. Chúng tôi nhận dữ liệu biểu mẫu từ OneEntry CMS để tạo một biểu mẫu trên giao diện người dùng
const formData = await Forms.getFormByMarker('tickets_form');
// 5. Tạo một đơn hàng trong kho lưu trữ đơn hàng
const body = {
formIdentifier: 'tickets_form',
paymentAccountIdentifier: 'cash',
formData: [
// {
// type: 'timeInterval',
// marker: 'schedule',
// value: {},
// },
{
type: 'entity',
marker: 'tickets',
value: ['p-8-10'],
},
],
products: [
{
productId: 10, // vé đồng
quantity: 1,
},
],
};
const order = await Orders.createOrder('tickets', body);
// 6. Tạo phiên thanh toán
const paymentSession = await Payments.createSession(
order.id,
'session',
false,
);
// 7. Cập nhật phiên theo id
let updateSession = await Payments.updateSessionById(paymentSession.id, {
status: 'expired_ticket',
paymentUrl: '',
});
// 8. Lấy một đối tượng phiên thanh toán theo mã đơn hàng
let sessionByOrderId = await Payments.getSessionByOrderId(order.id);
console.log(sessionByOrderId);