Tot funcionant al 100% i amb Looker
This commit is contained in:
@@ -3,65 +3,108 @@ declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/common.php';
|
||||
|
||||
const KAPVOE_SHIPPING_PRICE = 7.99;
|
||||
|
||||
kapvoe_require_post();
|
||||
$config = kapvoe_load_config();
|
||||
$data = kapvoe_json_input();
|
||||
|
||||
$required = [
|
||||
'product_code',
|
||||
'product_name',
|
||||
'price',
|
||||
'quantity',
|
||||
'customer_name',
|
||||
'address',
|
||||
'postal_code',
|
||||
'city',
|
||||
'province',
|
||||
'phone',
|
||||
'email',
|
||||
];
|
||||
|
||||
$errors = kapvoe_validate_required($data, $required);
|
||||
|
||||
if (!filter_var((string)($data['email'] ?? ''), FILTER_VALIDATE_EMAIL)) {
|
||||
$errors['email'] = 'Correu electrònic invàlid';
|
||||
}
|
||||
if (!preg_match('/^\d{5}$/', (string)($data['postal_code'] ?? ''))) {
|
||||
$errors['postal_code'] = 'Codi postal invàlid';
|
||||
}
|
||||
|
||||
if (!preg_match('/^[0-9+\s]{8,20}$/', (string)($data['phone'] ?? ''))) {
|
||||
$errors['phone'] = 'Telèfon invàlid';
|
||||
}
|
||||
|
||||
$price = (float)str_replace(',', '.', (string)$data['price']);
|
||||
$quantity = max(1, (int)$data['quantity']);
|
||||
$quantity = max(1, (int)($data['quantity'] ?? 1));
|
||||
|
||||
if ($price <= 0) {
|
||||
$errors['price'] = 'Preu invàlid';
|
||||
$productCode = trim((string)($data['product_code'] ?? ''));
|
||||
$shippingMethod = ((string)($data['shipping_method'] ?? 'pickup') === 'shipping') ? 'shipping' : 'pickup';
|
||||
$shippingCost = $shippingMethod === 'shipping' ? KAPVOE_SHIPPING_PRICE : 0.0;
|
||||
|
||||
$address = trim((string)($data['address'] ?? ''));
|
||||
$postalCode = trim((string)($data['postal_code'] ?? ''));
|
||||
$city = trim((string)($data['city'] ?? ''));
|
||||
$province = trim((string)($data['province'] ?? ''));
|
||||
|
||||
if ($shippingMethod === 'shipping') {
|
||||
if ($address === '') {
|
||||
$errors['address'] = 'Adreça obligatòria si hi ha enviament';
|
||||
}
|
||||
if (!preg_match('/^\d{5}$/', $postalCode)) {
|
||||
$errors['postal_code'] = 'Codi postal invàlid';
|
||||
}
|
||||
if ($city === '') {
|
||||
$errors['city'] = 'Ciutat obligatòria si hi ha enviament';
|
||||
}
|
||||
if ($province === '') {
|
||||
$errors['province'] = 'Província obligatòria si hi ha enviament';
|
||||
}
|
||||
}
|
||||
|
||||
if ($errors) {
|
||||
kapvoe_json_response(['ok' => false, 'errors' => $errors], 422);
|
||||
}
|
||||
|
||||
$orderId = 'ORD-' . date('Ymd-His') . '-' . substr(bin2hex(random_bytes(3)), 0, 6);
|
||||
$unitAmountCents = (int)round($price * 100);
|
||||
|
||||
$payload = [
|
||||
'order_id' => $orderId,
|
||||
'product_code' => trim((string)$data['product_code']),
|
||||
'product_name' => trim((string)$data['product_name']),
|
||||
'unit_amount_cents' => $unitAmountCents,
|
||||
'quantity' => $quantity,
|
||||
'customer_name' => trim((string)$data['customer_name']),
|
||||
'address' => trim((string)$data['address']),
|
||||
'postal_code' => trim((string)$data['postal_code']),
|
||||
'city' => trim((string)$data['city']),
|
||||
'province' => trim((string)$data['province']),
|
||||
'phone' => trim((string)$data['phone']),
|
||||
'email' => trim((string)$data['email']),
|
||||
];
|
||||
|
||||
try {
|
||||
$catalogProduct = kapvoe_get_catalog_product_by_code($config, $productCode);
|
||||
|
||||
$realPrice = (float)($catalogProduct['europe_price_number'] ?? 0);
|
||||
if ($realPrice <= 0) {
|
||||
throw new RuntimeException("El producte {$productCode} no té un preu vàlid");
|
||||
}
|
||||
|
||||
$productName = trim((string)($catalogProduct['product_code'] ?? $productCode));
|
||||
$productImageUrl = trim((string)($catalogProduct['image_url'] ?? ''));
|
||||
|
||||
$orderId = 'ORD-' . date('Ymd-His') . '-' . substr(bin2hex(random_bytes(3)), 0, 6);
|
||||
|
||||
$unitAmountCents = (int)round($realPrice * 100);
|
||||
$subtotalCents = $unitAmountCents * $quantity;
|
||||
$shippingCostCents = (int)round($shippingCost * 100);
|
||||
$totalAmountCents = $subtotalCents + $shippingCostCents;
|
||||
|
||||
$payload = [
|
||||
'order_id' => $orderId,
|
||||
'product_code' => $productCode,
|
||||
'product_name' => $productName,
|
||||
'unit_amount_cents' => $unitAmountCents,
|
||||
'quantity' => $quantity,
|
||||
'customer_name' => trim((string)$data['customer_name']),
|
||||
'phone' => trim((string)$data['phone']),
|
||||
'email' => trim((string)$data['email']),
|
||||
'analytics_session_id' => trim((string)($data['analytics_session_id'] ?? '')),
|
||||
'analytics_page_url' => trim((string)($data['analytics_page_url'] ?? '')),
|
||||
'analytics_referrer' => trim((string)($data['analytics_referrer'] ?? '')),
|
||||
'analytics_user_agent' => trim((string)($data['analytics_user_agent'] ?? '')),
|
||||
'analytics_utm_source' => trim((string)($data['analytics_utm_source'] ?? '')),
|
||||
'analytics_utm_medium' => trim((string)($data['analytics_utm_medium'] ?? '')),
|
||||
'analytics_utm_campaign' => trim((string)($data['analytics_utm_campaign'] ?? '')),
|
||||
'analytics_device_type' => trim((string)($data['analytics_device_type'] ?? '')),
|
||||
'analytics_page_type' => trim((string)($data['analytics_page_type'] ?? 'catalog')),
|
||||
|
||||
'shipping_method' => $shippingMethod,
|
||||
'shipping_cost_cents' => $shippingCostCents,
|
||||
'subtotal_cents' => $subtotalCents,
|
||||
'total_amount_cents' => $totalAmountCents,
|
||||
|
||||
'address' => $address,
|
||||
'postal_code' => $postalCode,
|
||||
'city' => $city,
|
||||
'province' => $province,
|
||||
];
|
||||
|
||||
$session = kapvoe_create_checkout_session($config, $payload);
|
||||
|
||||
kapvoe_append_order($config, [
|
||||
@@ -69,8 +112,13 @@ try {
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'product_code' => $payload['product_code'],
|
||||
'product_name' => $payload['product_name'],
|
||||
'unit_price' => $price,
|
||||
'product_image_url' => $productImageUrl,
|
||||
'unit_price' => number_format($realPrice, 2, '.', ''),
|
||||
'quantity' => $quantity,
|
||||
'subtotal' => number_format($subtotalCents / 100, 2, '.', ''),
|
||||
'shipping_method' => $shippingMethod,
|
||||
'shipping_cost' => number_format($shippingCostCents / 100, 2, '.', ''),
|
||||
'total_amount' => number_format($totalAmountCents / 100, 2, '.', ''),
|
||||
'customer_name' => $payload['customer_name'],
|
||||
'address' => $payload['address'],
|
||||
'postal_code' => $payload['postal_code'],
|
||||
@@ -78,9 +126,24 @@ try {
|
||||
'province' => $payload['province'],
|
||||
'phone' => $payload['phone'],
|
||||
'email' => $payload['email'],
|
||||
'analytics_session_id' => $payload['analytics_session_id'],
|
||||
'analytics_page_url' => $payload['analytics_page_url'],
|
||||
'analytics_referrer' => $payload['analytics_referrer'],
|
||||
'analytics_user_agent' => $payload['analytics_user_agent'],
|
||||
'analytics_utm_source' => $payload['analytics_utm_source'],
|
||||
'analytics_utm_medium' => $payload['analytics_utm_medium'],
|
||||
'analytics_utm_campaign' => $payload['analytics_utm_campaign'],
|
||||
'analytics_device_type' => $payload['analytics_device_type'],
|
||||
'analytics_page_type' => $payload['analytics_page_type'],
|
||||
'payment_status' => 'pending',
|
||||
'stripe_session_id' => $session['id'] ?? '',
|
||||
'payment_intent_id' => '',
|
||||
'stock_updated' => '0',
|
||||
'stock_updated_at' => '',
|
||||
'webhook_processed_at' => '',
|
||||
'customer_email_sent' => '0',
|
||||
'admin_email_sent' => '0',
|
||||
'email_notifications_sent_at' => '',
|
||||
]);
|
||||
|
||||
kapvoe_json_response([
|
||||
|
||||
Reference in New Issue
Block a user