137 lines
5.5 KiB
PHP
137 lines
5.5 KiB
PHP
<?php
|
|
/**
|
|
* TELVERO BACKOFFICE - API PROXY (FINAL VERSION WITH ATTRIBUTION FIX)
|
|
*/
|
|
ini_set('display_errors', 0);
|
|
error_reporting(E_ALL);
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
use Automattic\WooCommerce\Client;
|
|
use Mollie\Api\MollieApiClient;
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// --- CONFIGURATIE ---
|
|
$postcode_tech_key = '7d9136fb-3633-446d-b5d2-fe26a999bd62';
|
|
$mollie_api_key = 'test_sfUxawrmbxWSTdqhmg5vm3hc7yvmNP';
|
|
$site_url = 'https://telvero.nl';
|
|
|
|
$woocommerce = new Client(
|
|
$site_url,
|
|
'ck_f20c3d254df090816aa552dd312998cffac41866',
|
|
'cs_6d2df33dc9e003a3804d20f8079dead853b8e689',
|
|
['version' => 'wc/v3', 'timeout' => 400, 'verify_ssl' => false]
|
|
);
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
// 1. POSTCODE CHECK
|
|
if ($action === 'postcode_check') {
|
|
$postcode = str_replace(' ', '', $_GET['postcode']);
|
|
$url = "https://postcode.tech/api/v1/postcode?postcode={$postcode}&number=" . $_GET['number'];
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer {$postcode_tech_key}"]);
|
|
echo curl_exec($ch); curl_close($ch); exit;
|
|
}
|
|
|
|
// 2. PRODUCTEN OPHALEN (Inclusief Variatie Details)
|
|
if ($action === 'get_products') {
|
|
try {
|
|
$products = $woocommerce->get('products', ['status' => 'publish', 'per_page' => 100]);
|
|
$enriched = [];
|
|
foreach ($products as $product) {
|
|
$p = (array)$product;
|
|
if ($product->type === 'variable') {
|
|
$p['variation_details'] = $woocommerce->get("products/{$product->id}/variations", ['per_page' => 100]);
|
|
} else { $p['variation_details'] = []; }
|
|
$enriched[] = $p;
|
|
}
|
|
echo json_encode($enriched);
|
|
} catch (Exception $e) { echo json_encode(['error' => $e->getMessage()]); }
|
|
exit;
|
|
}
|
|
|
|
// 3. ORDER AANMAKEN
|
|
if ($action === 'create_order') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
try {
|
|
$method_input = $input['payment_method'];
|
|
$mediacode = $input['mediacode_internal'] ?? 'Geen';
|
|
|
|
// Map methoden naar officiƫle Mollie plugin IDs
|
|
if ($method_input === 'mollie_methods_ideal') {
|
|
$wc_gateway_id = 'mollie_wc_gateway_ideal';
|
|
$mollie_id = 'ideal';
|
|
} elseif ($method_input === 'rve_riverty') {
|
|
$wc_gateway_id = 'mollie_wc_gateway_riverty';
|
|
$mollie_id = 'riverty';
|
|
} elseif ($method_input === 'mollie_methods_creditcard') {
|
|
$wc_gateway_id = 'mollie_wc_gateway_creditcard';
|
|
$mollie_id = 'creditcard';
|
|
}
|
|
|
|
// --- ATTRIBUTION DATA & ORDER INITIALISATIE ---
|
|
$input['payment_method'] = $wc_gateway_id;
|
|
$input['payment_method_title'] = 'iDEAL (via Mollie)';
|
|
$input['customer_note'] = "Bron: Sales Panel | Mediacode: " . $mediacode;
|
|
|
|
// Metadata toevoegen bij creatie (Direct zichtbaar in Custom Fields)
|
|
$input['meta_data'][] = ['key' => 'Mediacode', 'value' => $mediacode];
|
|
$input['meta_data'][] = ['key' => 'Origin', 'value' => 'Sales Panel'];
|
|
$input['meta_data'][] = ['key' => '_wc_order_attribution_utm_campaign', 'value' => $mediacode];
|
|
$input['meta_data'][] = ['key' => '_wc_order_attribution_utm_source', 'value' => 'SalesPanel'];
|
|
|
|
// A. WooCommerce Order aanmaken
|
|
$order = $woocommerce->post('orders', $input);
|
|
if (!$order || !isset($order->id)) throw new Exception("WooCommerce order mislukt.");
|
|
|
|
// B. Mollie Setup
|
|
$mollie = new MollieApiClient();
|
|
$mollie->setApiKey($mollie_api_key);
|
|
$is_sub = (stripos(json_encode($order->line_items), 'abonnement') !== false);
|
|
$payment_value = number_format((float)$order->total, 2, '.', '');
|
|
if ($mollie_id === 'ideal' && $is_sub) $payment_value = "0.01";
|
|
|
|
// C. Webhook & Redirect URLs
|
|
$webhookUrl = "{$site_url}/wc-api/mollie_wc_gateway_ideal?order_id={$order->id}&key={$order->order_key}&filter_flag=1";
|
|
$redirectUrl = "{$site_url}/checkout/order-received/{$order->id}/?key={$order->order_key}&order_id={$order->id}&filter_flag=onMollieReturn&utm_campaign={$mediacode}";
|
|
|
|
$paymentData = [
|
|
"amount" => ["currency" => "EUR", "value" => $payment_value],
|
|
"description" => "Order #" . $order->id . " [" . $mediacode . "]",
|
|
"redirectUrl" => $redirectUrl,
|
|
"webhookUrl" => $webhookUrl,
|
|
"method" => $mollie_id,
|
|
"metadata" => ["order_id" => (string)$order->id, "mediacode" => $mediacode]
|
|
];
|
|
|
|
$payment = $mollie->payments->create($paymentData);
|
|
|
|
// D. MATCHING & PERMANENTE MEDIACODE (Update met Mollie ID)
|
|
$woocommerce->put("orders/" . $order->id, [
|
|
'meta_data' => [
|
|
['key' => '_mollie_payment_id', 'value' => $payment->id],
|
|
['key' => '_transaction_id', 'value' => $payment->id],
|
|
['key' => '_payment_method', 'value' => $wc_gateway_id],
|
|
['key' => 'Mediacode', 'value' => $mediacode],
|
|
['key' => '_mediacode', 'value' => $mediacode]
|
|
]
|
|
]);
|
|
|
|
// E. Email trigger (Customer Note)
|
|
$woocommerce->post("orders/{$order->id}/notes", [
|
|
'note' => "Betaallink voor uw bestelling: " . $payment->getCheckoutUrl(),
|
|
'customer_note' => true
|
|
]);
|
|
|
|
echo json_encode(['id' => $order->id, 'payment_url' => $payment->getCheckoutUrl()]);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(422);
|
|
echo json_encode(['error' => $e->getMessage()]);
|
|
}
|
|
exit;
|
|
} |