2026-02-16 20:11:59 +07:00
|
|
|
'use server'
|
|
|
|
|
|
|
|
|
|
import { YooCheckout, ICreatePayment } from '@a2seven/yoo-checkout';
|
2026-02-21 15:07:37 +07:00
|
|
|
import { getSessionData, deleteSession } from '@/app/actions/session';
|
2026-02-18 18:22:11 +07:00
|
|
|
import { API_BASE_URL } from '@/app/actions/definitions';
|
2026-02-16 20:11:59 +07:00
|
|
|
|
|
|
|
|
const checkout = new YooCheckout({ shopId: '1276731', secretKey: 'test_0Yns_0NHV5GJf6ypJ5HC4NSfnLO8SJkw-1PwrVWsDl4' });
|
|
|
|
|
|
2026-02-19 17:45:40 +07:00
|
|
|
export async function makePaymentOperation(email: string, tariffId: number, operationUuid: string, operationType: 'TARIFF' | 'TOKEN') {
|
2026-02-18 18:22:11 +07:00
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
|
|
formData.append('email', email);
|
|
|
|
|
formData.append('tariffId', tariffId.toString());
|
2026-02-19 17:45:40 +07:00
|
|
|
formData.append('operationType', operationType);
|
2026-02-18 18:22:11 +07:00
|
|
|
formData.append('operationUuid', operationUuid);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/payments/create`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: formData
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.ok) {
|
2026-02-19 17:23:52 +07:00
|
|
|
const parsed = await response.json();
|
|
|
|
|
if (parsed.paymentUuid) {
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
throw 'error'
|
|
|
|
|
}
|
2026-02-18 18:22:11 +07:00
|
|
|
} else {
|
2026-02-19 17:23:52 +07:00
|
|
|
throw 'error'
|
2026-02-18 18:22:11 +07:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 17:45:40 +07:00
|
|
|
export async function yooKasaCreatePayment(amount: number, tariff: number, operationType: 'TARIFF' | 'TOKEN') {
|
2026-02-16 20:11:59 +07:00
|
|
|
const userEmail = await getSessionData('email');
|
2026-02-19 17:23:52 +07:00
|
|
|
const timestamp = Date.now();
|
|
|
|
|
const random = Math.random().toString(36).substring(2, 10);
|
|
|
|
|
const idempotenceKey = `${userEmail}-${timestamp}-${amount}-${tariff}-${random}`;
|
2026-02-16 20:11:59 +07:00
|
|
|
|
2026-02-18 18:22:11 +07:00
|
|
|
if (!userEmail) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 20:11:59 +07:00
|
|
|
const createPayload: ICreatePayment = {
|
|
|
|
|
amount: {
|
2026-03-06 14:08:04 +07:00
|
|
|
value: amount.toLocaleString().replace(/[,\s.]/g, ''),
|
2026-02-16 20:11:59 +07:00
|
|
|
currency: 'RUB'
|
|
|
|
|
},
|
|
|
|
|
confirmation: {
|
2026-03-03 18:48:09 +07:00
|
|
|
type: 'embedded',
|
2026-02-16 20:11:59 +07:00
|
|
|
},
|
|
|
|
|
metadata: {
|
2026-02-18 14:21:57 +07:00
|
|
|
userMail: userEmail,
|
2026-02-16 20:11:59 +07:00
|
|
|
tariff: tariff
|
2026-02-19 17:45:40 +07:00
|
|
|
},
|
2026-03-03 18:48:09 +07:00
|
|
|
merchant_customer_id: userEmail,
|
2026-03-04 11:55:36 +07:00
|
|
|
capture: true,
|
|
|
|
|
...(operationType === 'TOKEN' && {save_payment_method: false})
|
2026-02-16 20:11:59 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2026-03-03 18:48:09 +07:00
|
|
|
const payment = await checkout.createPayment(createPayload, idempotenceKey);
|
|
|
|
|
const response = await makePaymentOperation(userEmail, tariff, payment.id, operationType);
|
2026-03-04 11:55:36 +07:00
|
|
|
|
2026-03-03 18:48:09 +07:00
|
|
|
if (payment && response) {
|
|
|
|
|
return payment?.confirmation.confirmation_token;
|
|
|
|
|
} else {
|
2026-03-10 12:34:37 +07:00
|
|
|
return null;
|
2026-03-03 18:48:09 +07:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2026-03-10 12:34:37 +07:00
|
|
|
return null;
|
2026-03-03 18:48:09 +07:00
|
|
|
}
|
2026-02-16 20:11:59 +07:00
|
|
|
}
|
2026-02-21 15:07:37 +07:00
|
|
|
|
|
|
|
|
export async function fetchPaymentsHistory() {
|
|
|
|
|
const email = await getSessionData('email');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
version: 1,
|
|
|
|
|
msg_id: 30005,
|
|
|
|
|
message_body: {
|
|
|
|
|
action: 'user_payments',
|
|
|
|
|
email: email
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
const parsed = await response.json();
|
|
|
|
|
if (parsed.message_code === 0) {
|
|
|
|
|
return {
|
|
|
|
|
payments: parsed.message_body.payments,
|
|
|
|
|
error: null
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw parsed.message_code;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|