- Add `telecart_forms` table migration and default checkout form seeder - Implement `FormsHandler` to fetch form schemas - Update `OrderCreateService` to handle custom fields in order comments - Add `update` method to QueryBuilder and Grammar - Add `Arr::except` helper - Update composer dependencies (Carbon, Symfony, PHPUnit, etc.) - Improve `MigratorService` error handling - Add unit tests for new functionality
110 lines
4.1 KiB
JavaScript
110 lines
4.1 KiB
JavaScript
import {defineStore} from "pinia";
|
|
import {isNotEmpty} from "@/helpers.js";
|
|
import {storeOrder} from "@/utils/ftch.js";
|
|
import {useCartStore} from "@/stores/CartStore.js";
|
|
import {YA_METRIKA_GOAL} from "@/constants/yaMetrikaGoals.js";
|
|
import {useYaMetrikaStore} from "@/stores/yaMetrikaStore.js";
|
|
import {useSettingsStore} from "@/stores/SettingsStore.js";
|
|
|
|
export const useCheckoutStore = defineStore('checkout', {
|
|
state: () => ({
|
|
form: {},
|
|
order: null,
|
|
isLoading: false,
|
|
validationErrors: {},
|
|
errorMessage: '',
|
|
}),
|
|
|
|
getters: {
|
|
hasError: (state) => {
|
|
return (field) => isNotEmpty(state.validationErrors[field]);
|
|
},
|
|
},
|
|
|
|
actions: {
|
|
async makeOrder() {
|
|
try {
|
|
this.errorMessage = '';
|
|
this.isLoading = true;
|
|
const data = window.Telegram.WebApp.initDataUnsafe;
|
|
|
|
console.log("Allows write to PM: ", data.user.allows_write_to_pm);
|
|
|
|
if (! data.user.allows_write_to_pm) {
|
|
console.log("Sending request");
|
|
const granted = await new Promise(resolve => {
|
|
window.Telegram.WebApp.requestWriteAccess((granted) => {
|
|
resolve(granted);
|
|
});
|
|
});
|
|
|
|
if (granted) {
|
|
data.user.allows_write_to_pm = true;
|
|
console.log('Пользователь разрешил отправку сообщений');
|
|
} else {
|
|
alert('Вы не дали разрешение — бот не сможет отправлять вам уведомления');
|
|
}
|
|
}
|
|
|
|
const response = await storeOrder({
|
|
...this.form,
|
|
tgData: data,
|
|
});
|
|
this.order = response.data;
|
|
|
|
if (! this.order.id) {
|
|
console.debug(response.data);
|
|
throw new Error('Ошибка создания заказа.');
|
|
}
|
|
|
|
const yaMetrika = useYaMetrikaStore();
|
|
yaMetrika.reachGoal(YA_METRIKA_GOAL.ORDER_CREATED_SUCCESS, {
|
|
price: this.order?.final_total_numeric,
|
|
currency: this.order?.currency,
|
|
});
|
|
yaMetrika.dataLayerPush({
|
|
"ecommerce": {
|
|
"currencyCode": useSettingsStore().currency_code,
|
|
"purchase": {
|
|
"actionField": {
|
|
"id": this.order.id,
|
|
'revenue': this.order?.final_total_numeric,
|
|
},
|
|
"products": this.order.products ? this.order.products.map((product, index) => {
|
|
return {
|
|
id: product.product_id,
|
|
name: product.name,
|
|
price: product.total_numeric,
|
|
position: index,
|
|
quantity: product.quantity,
|
|
};
|
|
}) : [],
|
|
}
|
|
}
|
|
});
|
|
|
|
await window.Telegram.WebApp.HapticFeedback.notificationOccurred('success');
|
|
await useCartStore().getProducts();
|
|
} catch (error) {
|
|
if (error.response?.status === 422) {
|
|
this.validationErrors = error.response._data.data;
|
|
} else {
|
|
console.error('Server error', error);
|
|
}
|
|
|
|
window.Telegram.WebApp.HapticFeedback.notificationOccurred('error');
|
|
|
|
this.errorMessage = 'Возникла ошибка при создании заказа.';
|
|
|
|
throw error;
|
|
} finally {
|
|
this.isLoading = false;
|
|
}
|
|
},
|
|
|
|
clearError(field) {
|
|
this.validationErrors[field] = null;
|
|
},
|
|
},
|
|
});
|