انتقل إلى المحتوى الرئيسي

الدفع مقابل الطلب من قبل مستخدم مسجل

في هذا المثال، نوضح كيفية معالجة الدفع لطلب تم تقديمه من قبل مستخدم مسجل باستخدام واجهة برمجة التطبيقات OneEntry.

✅ هدف السيناريو:

  • تفويض المستخدم المسجل
  • إنشاء الطلب
  • إنشاء جلسة الدفع

✅ ما تحتاجه:

  • PROJECT_URL و APP_TOKEN صالحين للمصادقة مع واجهة برمجة التطبيقات OneEntry.
  • مستخدم مسجل
  • تخزين الطلبات المكونة مع علامة "orders"

📌 مهم:

  • هذه الأمثلة لا تشمل معالجة الأخطاء.
  • يمكنك إدارة الأخطاء باستخدام كتلة try-catch أو من خلال استخدام بناء مثل await Promise.catch((error) => error).

📚 انظر في الوثائق:

📦 مرجع SDK:

جربها مباشرة

قم بتشغيل هذه الطريقة بشكل تفاعلي في JS SDK sandbox — قم بتوصيل Project URL و App Token في الزيارة الأولى، ثم افتح:

السيناريو

1. استيراد defineOneEntry من SDK وتعريف PROJECT_URL و APP_TOKEN

مثال:

import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

2. إنشاء عميل API

مثال:

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

3. تفويض المستخدم باستخدام 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);
console.log(user);
النتيجة:
{
"userIdentifier": "kvasssukr.net@gmail.com",
"authProviderIdentifier": "email",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR...pZCI6MTYsImF1dGhQ"
"refreshToken": "1745494429101-...-2834edf8"
}

4. إنشاء طلب باستخدام Orders.createOrder()

البيانات:

{
"formIdentifier": "order",
"paymentAccountIdentifier": "cash",
"formData": {
"en_US": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
]
},
"products": [
{
"productId": 15,
"quantity": 1
}
]
}

مثال:

const body = {
formIdentifier: 'order',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'string',
marker: 'name',
value: 'Christina Thomas',
},
],
products: [
{
productId: 15,
quantity: 1,
},
],
};
const orderData = (await Orders.createOrder('orders', body as any).catch(
(e: any) => e,
)) as any;

if ('statusCode' in orderData) {
throw new Error(orderData.message);
}
النتيجة:
{
"id": 116,
"formIdentifier": "order",
"paymentAccountIdentifier": "cash",
"formData": [
{
"type": "string",
"marker": "name",
"value": "Christina Thomas"
}
],
"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. إنشاء جلسة الدفع باستخدام Payments.createSession()

مثال:

const sessionData = await Payments.createSession(
orderData?.id,
'session',
).catch((e: any) => e);
النتيجة:
{
"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. الحصول على كائن جلسة دفع واحدة بواسطة معرف الطلب باستخدام Payments.getSessionByOrderId()

مثال:

const sessionByOrderId = await Payments.getSessionByOrderId(orderData?.id).catch(
(e: any) => e,
);

console.log({ user, sessionData, sessionByOrderId });
النتيجة:
[
{
"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
}
]

المثال النهائي

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

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

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

// 3. User authorization with 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);
console.log(user);

// 4. Create order with [Orders.createOrder()](/docs/orders/createOrder)
const body = {
formIdentifier: 'order',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'string',
marker: 'name',
value: 'Christina Thomas',
},
],
products: [
{
productId: 15,
quantity: 1,
},
],
};
const orderData = (await Orders.createOrder('orders', body as any).catch(
(e: any) => e,
)) as any;

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

// 5. Creation of payment session with [Payments.createSession()](/docs/payments/createSession)
const sessionData = await Payments.createSession(
orderData?.id,
'session',
).catch((e: any) => e);

// 6. Get one payment session object by order identifier with [Payments.getSessionByOrderId()](/docs/payments/getSessionByOrderId)
const sessionByOrderId = await Payments.getSessionByOrderId(orderData?.id).catch(
(e: any) => e,
);

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