Ana içeriğe geç

İçeriğe Abone Ol

Bu örnekte, OneEntry API'sini kullanarak abonelik tabanlı içerik erişimini nasıl yöneteceğimizi gösteriyoruz. Bir kullanıcının aktif bir aboneliği olup olmadığını kontrol ediyoruz ve premium içeriğe erişim veriyoruz ya da onlardan abone olmalarını istiyoruz.

✅ Senaryonun amacı:

  • Sadece aboneliği olan kullanıcılar içeriğe erişebilir.
  • Kontrol, Orders ve User'a dayanmaktadır.
  • Yetkisi olmayan veya satın alma yapmamış olanlar, abone olma teklifi alacaklardır.

✅ İhtiyacınız olanlar:

  • OneEntry API ile kimlik doğrulama için geçerli bir PROJECT_URL ve APP_TOKEN.

📌 Önemli:

  • Bu örneklerde hataları ele almıyoruz.
  • Hataları trycatch içinde veya "await Promise.catch((error) => error)" gibi bir yapı ile ele alabilirsiniz.

Senaryo

1. oneEntry'i içe aktarın ve url ile token'ı tanımlayın

Örnek:

import { defineOneEntry } from 'oneentry';

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

2. defineOneEntry() fonksiyonu ile bir API istemcisi oluşturma

Örnek:

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

3. Kullanıcı yetkilendirmesi için veri hazırlama

Örnek:

[
{
"marker": "email_reg",
"value": "your-user@email.com"
},
{
"marker": "password_reg",
"value": "123456"
}
]

4. Yetkilendirme ve kullanıcı verilerini alma AuthProvider.auth()

Örnek:

const authResponse = await AuthProvider.auth('email', {
authData,
});
Sonuç:
{
"userIdentifier": "your-user@email.com",
"authProviderIdentifier": "email",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR...pZCI6MTYsImF1dGhQ"
"refreshToken": "1745494429101-...-2834edf8"
}

5. Kullanıcı siparişlerinin listesini Orders.getAllOrdersByMarker() ile alma

Örnek:

const orders = await Orders.getAllOrdersByMarker('subscriptions');
Sonuç:
{
"items": [
{
"id": 91,
"storageId": 2,
"createdDate": "2025-04-30T21:48:40.628Z",
"statusIdentifier": "active",
"formIdentifier": "subscription",
"formData": [
{
"type": "date",
"marker": "expired_date",
"value": {
"fullDate": "2025-05-07T00:00:00.000Z",
"formattedValue": "07-05-2025 00:00",
"formatString": "DD-MM-YYYY HH:mm"
}
},
{
"type": "list",
"marker": "subscription_time",
"value": [
{
"title": "1 ay",
"value": "1",
"extended": {
"type": "real",
"value": "100"
}
}
]
}
],
"attributeSetIdentifier": "subscriptions",
"totalSum": "100.00",
"currency": "",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Nakit"
},
"products": [
{
"id": 3,
"title": "1 ay",
"sku": "",
"previewImage": null,
"price": 100,
"quantity": 1
}
],
"isCompleted": false
}
],
"total": 1
}

6. Aktif bir aboneliği kontrol etme

Örnek:

const now = new Date();
const hasActiveSubscription = orders.items?.some(
(order: any) =>
new Date(
order.formData.find(
(d: { marker: string }) => d.marker === 'expired_date',
).value.fullDate,
) > now,
);
Sonuç:
true

7. Abonelik aktifse erişim sağlıyoruz, aksi takdirde bir sipariş oluşturup ödeme üretiyoruz

Örnek:

let content = null;
if (hasActiveSubscription) {
// ✅ Abonelik aktif → içeriği göster
content = await Pages.getPageByUrl('premium_page');
console.log('🎉 Erişim açıldı:', content);
} else {
// ❌ Abonelik yok → bir sipariş oluştur ve ödeme üret
const body = {
formIdentifier: 'subscription',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'date',
marker: 'expired_date',
value: {
fullDate: '2025-05-07T00:00:00.000Z',
formattedValue: '07-05-2025 00:00',
formatString: 'DD-MM-YYYY HH:mm',
},
},
{
type: 'list',
marker: 'subscription_time',
value: [
{
title: '1 ay',
value: '1',
extended: {
type: 'real',
value: '100',
},
},
],
},
],
products: [
{
productId: 3,
quantity: 1,
},
],
};
const order = await Orders.createOrder('subscriptions', body);
const payment = await Payments.createSession(order.id, 'session', false);
// console.log('💸 Aboneliğiniz için ödemeye devam edin:', payment)
}
Sonuç:
{
"id": 7,
"parentId": null,
"pageUrl": "premium_page",
"depth": 0,
"localizeInfos": {
"title": "Premium sayfa",
"menuTitle": "Premium sayfa",
"htmlContent": "",
"plainContent": ""
},
"isVisible": true,
"forms": [],
"blocks": [],
"type": "common_page",
"templateIdentifier": null,
"attributeSetIdentifier": null,
"attributeValues": {},
"isSync": false
}

Son örnek

// 1. oneEntry'i içe aktarın ve PROJECT_URL ile APP_TOKEN'ı tanımlayın
import { defineOneEntry } from 'oneentry';

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

// 2. Bir API istemcisi oluşturma
const { Users, Orders, Pages, Payments, AuthProvider, Forms } =
defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Kullanıcı yetkilendirmesi için veri hazırlama
// const form = await Forms.getFormByMarker('subscription');
// expired_date - Tarih
// subscription_time - liste

const authData = [
{
marker: 'email_reg',
value: 'your-user@email.com',
},
{
marker: 'password_reg',
value: '123456',
},
];

// 4. Yetkilendirme ve kullanıcı verilerini alma
const authResponse = await AuthProvider.auth('email', {
authData,
});
const user = await Users.getUser();

// 5. Kullanıcı siparişlerinin listesini alma
const orders = await Orders.getAllOrdersByMarker('subscriptions');

// 6. Aktif bir aboneliği kontrol etme
const now = new Date();
const hasActiveSubscription = orders.items?.some(
(order: any) =>
new Date(
order.formData.find(
(d: { marker: string }) => d.marker === 'expired_date',
).value.fullDate,
) > now,
);

// 7. Abonelik aktifse erişim sağlıyoruz, aksi takdirde bir sipariş oluşturup ödeme üretiyoruz
let content = null;
if (hasActiveSubscription) {
// ✅ Abonelik aktif → içeriği göster
content = await Pages.getPageByUrl('premium_page');
console.log('🎉 Erişim açıldı:', content);
} else {
// ❌ Abonelik yok → bir sipariş oluştur ve ödeme üret
const body = {
formIdentifier: 'subscription',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'date',
marker: 'expired_date',
value: {
fullDate: '2025-05-07T00:00:00.000Z',
formattedValue: '07-05-2025 00:00',
formatString: 'DD-MM-YYYY HH:mm',
},
},
{
type: 'list',
marker: 'subscription_time',
value: [
{
title: '1 ay',
value: '1',
extended: {
type: 'real',
value: '100',
},
},
],
},
],
products: [
{
productId: 3,
quantity: 1,
},
],
};
const order = await Orders.createOrder('subscriptions', body);
const payment = await Payments.createSession(order.id, 'session', false);
console.log('💸 Aboneliğiniz için ödemeye devam edin:', payment);
}