Merge branch 'development'
This commit is contained in:
@@ -49,6 +49,9 @@ if(isset($get_content) && $get_content!=''){
|
||||
elseif ($v[0] == 'equipmentid') {
|
||||
//build up search
|
||||
$clause .= ' AND e.rowID = :'.$v[0];
|
||||
|
||||
//UPDATE VERSION STATUS
|
||||
$sw_version_latest_update = 1;
|
||||
}
|
||||
elseif ($v[0] == 'servicedate') {
|
||||
//build up service coverage
|
||||
@@ -69,6 +72,7 @@ if(isset($get_content) && $get_content!=''){
|
||||
elseif ($v[0] == 'h_equipmentid') {
|
||||
//build up search
|
||||
$clause .= ' AND h.equipmentid = :'.$v[0];
|
||||
|
||||
}
|
||||
elseif ($v[0] == 'status') {
|
||||
//Update status based on status
|
||||
|
||||
88
api/v2/get/payment.php
Normal file
88
api/v2/get/payment.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
defined($security_key) or exit;
|
||||
|
||||
//------------------------------------------
|
||||
// Payment Status Retrieval
|
||||
//------------------------------------------
|
||||
// This endpoint retrieves payment details for verification
|
||||
|
||||
//Connect to DB
|
||||
$pdo = dbConnect($dbname);
|
||||
|
||||
//NEW ARRAY
|
||||
$criterias = [];
|
||||
|
||||
//Check for $_GET variables
|
||||
if(isset($get_content) && $get_content!=''){
|
||||
$requests = explode("&", $get_content);
|
||||
foreach ($requests as $y){
|
||||
$v = explode("=", $y);
|
||||
$criterias[$v[0]] = $v[1];
|
||||
}
|
||||
}
|
||||
|
||||
// Validate payment_id
|
||||
if (empty($criterias['payment_id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required parameter: payment_id'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
$payment_id = $criterias['payment_id'];
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 1: Fetch transaction
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$sql = 'SELECT * FROM transactions WHERE txn_id = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$payment_id]);
|
||||
$transaction = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$transaction) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Payment not found'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 2: Fetch transaction item
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$sql = 'SELECT * FROM transactions_items WHERE txn_id = ? LIMIT 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$payment_id]);
|
||||
$item = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$item) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Payment item not found'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 3: Parse item_options JSON
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$item_options = [];
|
||||
if (!empty($item['item_options'])) {
|
||||
$item_options = json_decode($item['item_options'], true);
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 4: Return payment details
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$messages = json_encode([
|
||||
'payment_id' => $transaction['txn_id'],
|
||||
'payment_status' => $transaction['payment_status'],
|
||||
'payment_amount' => $transaction['payment_amount'],
|
||||
'currency' => 'EUR', // Default currency
|
||||
'serial_number' => $item_options['serial_number'] ?? null,
|
||||
'equipment_id' => $item_options['equipment_id'] ?? null,
|
||||
'hw_version' => $item_options['hw_version'] ?? null,
|
||||
'version_id' => $item['item_id'],
|
||||
'payer_email' => $transaction['payer_email'],
|
||||
'customer_name' => trim(($transaction['first_name'] ?? '') . ' ' . ($transaction['last_name'] ?? '')),
|
||||
'created' => $transaction['created']
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
echo $messages;
|
||||
|
||||
?>
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
defined($security_key) or exit;
|
||||
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
//------------------------------------------
|
||||
// Products Software Licenses
|
||||
//------------------------------------------
|
||||
@@ -12,7 +14,7 @@ $pdo = dbConnect($dbname);
|
||||
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
|
||||
|
||||
//default whereclause
|
||||
list($whereclause,$condition) = getWhereclauselvl2("software_licenses",$permission,$partner,'get');
|
||||
list($whereclause,$condition) = getWhereclauselvl2("products_software_licenses",$permission,$partner,'get');
|
||||
|
||||
//NEW ARRAY
|
||||
$criterias = [];
|
||||
@@ -52,12 +54,20 @@ if(isset($criterias['totals']) && $criterias['totals'] ==''){
|
||||
$sql = 'SELECT count(*) as count FROM products_software_licenses '.$whereclause.'';
|
||||
}
|
||||
elseif (isset($criterias['list']) && $criterias['list'] =='') {
|
||||
//SQL for list
|
||||
$sql = 'SELECT l.*, u.username, v.name as version_name FROM products_software_licenses l LEFT JOIN users u ON l.user_id = u.id LEFT JOIN products_software_versions v ON l.version_id = v.rowID '.$whereclause.' ORDER BY l.created DESC';
|
||||
//SQL for list
|
||||
$sql = 'SELECT l.*, v.name as version_name, v.version, e.serialnumber as assigned_serial
|
||||
FROM products_software_licenses l
|
||||
LEFT JOIN products_software_versions v ON l.version_id = v.rowID
|
||||
LEFT JOIN equipment e ON l.license_key = e.sw_version_license
|
||||
'.$whereclause.' ORDER BY l.created DESC';
|
||||
}
|
||||
else {
|
||||
//SQL for paged
|
||||
$sql = 'SELECT l.*, u.username, v.name as version_name FROM products_software_licenses l LEFT JOIN users u ON l.user_id = u.id LEFT JOIN products_software_versions v ON l.version_id = v.rowID '.$whereclause.' ORDER BY l.created DESC LIMIT :page,:num_licenses';
|
||||
//SQL for paged
|
||||
$sql = 'SELECT l.*, v.name as version_name, v.version, e.serialnumber as assigned_serial
|
||||
FROM products_software_licenses l
|
||||
LEFT JOIN products_software_versions v ON l.version_id = v.rowID
|
||||
LEFT JOIN equipment e ON l.license_key = e.sw_version_license
|
||||
'.$whereclause.' ORDER BY l.created DESC LIMIT :page,:num_licenses';
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
@@ -245,16 +245,16 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
//Check if there's a valid license for this upgrade
|
||||
if ($final_price > 0 && $sw_version_license) {
|
||||
//Check if the license is valid
|
||||
$sql = 'SELECT status, start_at, expires_at
|
||||
$sql = 'SELECT status, starts_at, expires_at
|
||||
FROM products_software_licenses
|
||||
WHERE license_key = ? AND equipment_id = ?';
|
||||
WHERE license_key = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$sw_version_license, $equipment_rowid]);
|
||||
$stmt->execute([$sw_version_license]);
|
||||
$license = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($license && $license['status'] == 1) {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$start_at = $license['start_at'];
|
||||
$start_at = $license['starts_at'];
|
||||
$expires_at = $license['expires_at'];
|
||||
|
||||
//Check if license is within valid date range
|
||||
|
||||
@@ -281,16 +281,16 @@ if (isset($criterias['sn']) && $criterias['sn'] != ''){
|
||||
$license_applied = false;
|
||||
if ($final_price > 0 && $sw_version_license) {
|
||||
//Check if the license is valid
|
||||
$sql = 'SELECT status, start_at, expires_at
|
||||
$sql = 'SELECT status, starts_at, expires_at
|
||||
FROM products_software_licenses
|
||||
WHERE license_key = ? AND equipment_id = ?';
|
||||
WHERE license_key = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$sw_version_license, $equipment_rowid]);
|
||||
$stmt->execute([$sw_version_license]);
|
||||
$license = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($license && $license['status'] == 1) {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$start_at = $license['start_at'];
|
||||
$start_at = $license['starts_at'];
|
||||
$expires_at = $license['expires_at'];
|
||||
|
||||
//Check if license is within valid date range
|
||||
|
||||
@@ -354,6 +354,42 @@ elseif(isset($post_content['dealer_closeby'])){
|
||||
echo json_encode(['error' => "Latitude or longitude not provided."]);
|
||||
}
|
||||
}
|
||||
elseif(isset($post_content['action']) && $post_content['action']=='unsubscribe'){
|
||||
//++++++++++++++++++++++
|
||||
//Process DEALER UNSUBSCRIBE
|
||||
//++++++++++++++++++++++
|
||||
|
||||
// Check if email is provided
|
||||
if (isset($post_content['email']) && !empty($post_content['email'])) {
|
||||
$email = $post_content['email'];
|
||||
|
||||
try {
|
||||
// Update dealer status to 0 (inactive) where email matches
|
||||
$sql = 'UPDATE dealers SET status = 0 WHERE email = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
if ($stmt->execute([$email])) {
|
||||
// Check if any rows were affected
|
||||
if ($stmt->rowCount() > 0) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'success', 'message' => 'Dealer unsubscribed successfully']);
|
||||
} else {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'No dealer found with this email']);
|
||||
}
|
||||
} else {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Database update failed']);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Database error occurred']);
|
||||
}
|
||||
} else {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Email not provided']);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//++++++++++++++++++++++
|
||||
|
||||
278
api/v2/post/payment.php
Normal file
278
api/v2/post/payment.php
Normal file
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
defined($security_key) or exit;
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
//------------------------------------------
|
||||
// Payment Creation (for Software Upgrades)
|
||||
//------------------------------------------
|
||||
// This endpoint creates a Mollie payment and stores transaction data
|
||||
|
||||
//Connect to DB
|
||||
$pdo = dbConnect($dbname);
|
||||
|
||||
//CONTENT FROM API (POST)
|
||||
$post_content = json_decode($input, true);
|
||||
|
||||
|
||||
// Validate required inputs
|
||||
if (empty($post_content['serial_number']) || empty($post_content['version_id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Missing required fields: serial_number, version_id'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
$serial_number = $post_content['serial_number'];
|
||||
$version_id = $post_content['version_id'];
|
||||
$user_data = $post_content['user_data'] ?? [];
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 1: Get equipment data from serial_number
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$sql = 'SELECT rowID, sw_version, sw_version_license, hw_version FROM equipment WHERE serialnumber = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$serial_number]);
|
||||
$equipment = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$equipment) {
|
||||
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Device not found with serial number: ' . $serial_number], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
$equipment_id = $equipment['rowID'];
|
||||
$current_sw_version = trim(strtolower(ltrim($equipment['sw_version'], '0')));
|
||||
$sw_version_license = $equipment['sw_version_license'] ?? null;
|
||||
$hw_version = $equipment['hw_version'] ?? '';
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 2: Get version data from version_id
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$sql = 'SELECT rowID as version_id, version, name, description, hw_version
|
||||
FROM products_software_versions
|
||||
WHERE rowID = ? AND status = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$version_id]);
|
||||
$version = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$version) {
|
||||
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Software version not found or inactive'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 3: Calculate price SERVER-SIDE (same logic as software_update.php)
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$final_price = '0.00';
|
||||
$final_currency = '';
|
||||
|
||||
// Check if version has upgrade paths defined
|
||||
$sql = 'SELECT COUNT(*) as path_count FROM products_software_upgrade_paths WHERE to_version_id = ? AND is_active = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$version_id]);
|
||||
$path_count_result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$has_upgrade_paths = ($path_count_result['path_count'] > 0);
|
||||
|
||||
if (!$has_upgrade_paths) {
|
||||
// No upgrade paths defined = FREE (lines 240-242 in software_update.php)
|
||||
$final_price = '0.00';
|
||||
} else {
|
||||
// Check for valid upgrade path FROM current version
|
||||
$sql = 'SELECT pup.price, pup.currency
|
||||
FROM products_software_upgrade_paths pup
|
||||
JOIN products_software_versions from_ver ON pup.from_version_id = from_ver.rowID
|
||||
WHERE pup.to_version_id = ?
|
||||
AND LOWER(TRIM(LEADING "0" FROM from_ver.version)) = ?
|
||||
AND pup.is_active = 1';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$version_id, $current_sw_version]);
|
||||
$upgrade_path = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($upgrade_path) {
|
||||
$final_price = $upgrade_path['price'] ?? '0.00';
|
||||
$final_currency = $upgrade_path['currency'] ?? 'EUR';
|
||||
} else {
|
||||
// No upgrade path FROM current version
|
||||
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'No valid upgrade path from current version'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 4: Check license validity (lines 280-311 in software_update.php)
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
if ($final_price > 0 && $sw_version_license) {
|
||||
$sql = 'SELECT status, starts_at, expires_at
|
||||
FROM products_software_licenses
|
||||
WHERE license_key = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$sw_version_license]);
|
||||
$license = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($license && $license['status'] == 1) {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$starts_at = $license['starts_at'];
|
||||
$expires_at = $license['expires_at'];
|
||||
|
||||
// Check if license is within valid date range
|
||||
if ((!$starts_at || $starts_at <= $now) && (!$expires_at || $expires_at >= $now)) {
|
||||
$final_price = '0.00';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 5: Verify price > 0 (free upgrades shouldn't reach payment API)
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
if ($final_price <= 0) {
|
||||
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'This upgrade is free. No payment required.'], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 6: DEBUG MODE - Log but continue to real Mollie
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
if (debug) {
|
||||
debuglog("DEBUG MODE: Creating real Mollie payment for testing");
|
||||
debuglog("DEBUG: Serial Number: $serial_number, Version ID: $version_id, Price: $final_price");
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 7: Call Mollie API to create payment
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
try {
|
||||
// Initialize Mollie
|
||||
require dirname(__FILE__, 4).'/initialize.php';
|
||||
|
||||
// Format price for Mollie (must be string with 2 decimals)
|
||||
$formatted_price = number_format((float)$final_price, 2, '.', '');
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 7A: Generate transaction ID BEFORE creating Mollie payment
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Generate unique transaction ID (same as placeorder.php)
|
||||
$txn_id = strtoupper(uniqid('SC') . substr(md5(mt_rand()), 0, 5));
|
||||
|
||||
// Build webhook URL and redirect URL with actual transaction ID
|
||||
$protocol = 'https';
|
||||
$hostname = $_SERVER['SERVER_NAME'];
|
||||
$path = '/';
|
||||
$webhook_url = "{$protocol}://{$hostname}{$path}webhook_mollie.php";
|
||||
$redirect_url = "{$protocol}://{$hostname}{$path}?page=softwaretool&payment_return=1&order_id={$txn_id}";
|
||||
|
||||
if (debug) {
|
||||
debuglog("DEBUG: Transaction ID: {$txn_id}");
|
||||
debuglog("DEBUG: redirectUrl being sent to Mollie: " . $redirect_url);
|
||||
}
|
||||
|
||||
// Create payment with Mollie
|
||||
$payment = $mollie->payments->create([
|
||||
'amount' => [
|
||||
'currency' => $final_currency ?: 'EUR',
|
||||
'value' => "{$formatted_price}"
|
||||
],
|
||||
'description' => "Software upgrade Order #{$txn_id}",
|
||||
'redirectUrl' => "{$redirect_url}",
|
||||
'webhookUrl' => "{$webhook_url}",
|
||||
'metadata' => [
|
||||
'order_id' => $txn_id,
|
||||
'serial_number' => $serial_number,
|
||||
'version_id' => $version_id,
|
||||
'equipment_id' => $equipment_id
|
||||
]
|
||||
]);
|
||||
|
||||
$mollie_payment_id = $payment->id;
|
||||
$checkout_url = $payment->getCheckoutUrl();
|
||||
|
||||
if (debug) {
|
||||
debuglog("DEBUG: Mollie payment created successfully");
|
||||
debuglog("DEBUG: Payment ID: $mollie_payment_id");
|
||||
debuglog("DEBUG: Redirect URL sent: $redirect_url");
|
||||
debuglog("DEBUG: Redirect URL from Mollie object: " . $payment->redirectUrl);
|
||||
debuglog("DEBUG: Full payment object: " . json_encode($payment));
|
||||
debuglog("DEBUG: Checkout URL: $checkout_url");
|
||||
}
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 8: Store transaction in DB using txn_id (order ID)
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// Split name into first/last (simple split on first space)
|
||||
$full_name = $user_data['name'] ?? '';
|
||||
$name_parts = explode(' ', $full_name, 2);
|
||||
$first_name = $name_parts[0] ?? '';
|
||||
$last_name = $name_parts[1] ?? '';
|
||||
|
||||
// BUILD UP PARTNERHIERARCHY FROM USER
|
||||
$partner_product = json_encode(array("salesid"=>$partner->salesid,"soldto"=>$partner->soldto), JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$sql = 'INSERT INTO transactions (txn_id, payment_amount, payment_status, payer_email, first_name, last_name,
|
||||
address_street, address_city, address_state, address_zip, address_country, account_id, payment_method, accounthierarchy, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
$txn_id, // Use generated transaction ID, not Mollie payment ID
|
||||
$final_price,
|
||||
0, // 0 = pending
|
||||
$user_data['email'] ?? '',
|
||||
$first_name,
|
||||
$last_name,
|
||||
$user_data['address'] ?? '',
|
||||
$user_data['city'] ?? '',
|
||||
'', // address_state (not collected)
|
||||
$user_data['postal'] ?? '',
|
||||
$user_data['country'] ?? '',
|
||||
$serial_number,
|
||||
0, // payment method
|
||||
$partner_product,
|
||||
date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
// Get the database ID
|
||||
$transaction_id = $pdo->lastInsertId();
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 9: Store transaction item with serial_number in item_options
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$item_options = json_encode([
|
||||
'serial_number' => $serial_number,
|
||||
'equipment_id' => $equipment_id,
|
||||
'hw_version' => $hw_version,
|
||||
'mollie_payment_id' => $mollie_payment_id // Store Mollie payment ID in options
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$sql = 'INSERT INTO transactions_items (txn_id, item_id, item_price, item_quantity, item_options, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([
|
||||
$transaction_id, // Use database transaction ID (not txn_id string, not mollie_payment_id)
|
||||
$version_id,
|
||||
$final_price,
|
||||
1,
|
||||
$item_options,
|
||||
date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// STEP 10: Return checkout URL and payment ID
|
||||
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
$messages = json_encode([
|
||||
'checkout_url' => $checkout_url,
|
||||
'payment_id' => $mollie_payment_id
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
echo $messages;
|
||||
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Payment creation failed: ' . $e->getMessage()], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -14,12 +14,16 @@ $post_content = json_decode($input,true);
|
||||
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
|
||||
|
||||
//default whereclause
|
||||
list($whereclause,$condition) = getWhereclauselvl2("software_licenses",$permission,$partner,'');
|
||||
list($whereclause,$condition) = getWhereclauselvl2("products_software_licenses",$permission,$partner,'');
|
||||
|
||||
//SET PARAMETERS FOR QUERY
|
||||
$id = $post_content['rowID'] ?? ''; //check for rowID
|
||||
$command = ($id == '')? 'insert' : 'update'; //IF rowID = empty then INSERT
|
||||
if (isset($post_content['delete'])){$command = 'delete';} //change command to delete
|
||||
|
||||
// Check for bulk creation
|
||||
$is_bulk = isset($post_content['bulk']) && $post_content['bulk'] === true;
|
||||
|
||||
$date = date('Y-m-d H:i:s');
|
||||
|
||||
//CREATE EMPTY STRINGS
|
||||
@@ -27,12 +31,90 @@ $clause = '';
|
||||
$clause_insert ='';
|
||||
$input_insert = '';
|
||||
|
||||
//------------------------------------------
|
||||
// BULK LICENSE CREATION
|
||||
//------------------------------------------
|
||||
if ($command == 'insert' && $is_bulk && isAllowed('products_software_licenses',$profile,$permission,'C') === 1){
|
||||
|
||||
$version_id = $post_content['version_id'] ?? '';
|
||||
$serials = $post_content['serials'] ?? [];
|
||||
$transaction_id = $post_content['transaction_id'] ?? '';
|
||||
$license_type = $post_content['license_type'] ?? 0;
|
||||
$status = $post_content['status'] ?? 0;
|
||||
|
||||
if (empty($version_id) || empty($serials) || !is_array($serials)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid parameters for bulk creation']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$accounthierarchy = json_encode(array("salesid"=>$partner->salesid,"soldto"=>$partner->soldto), JSON_UNESCAPED_UNICODE);
|
||||
|
||||
// Prepare statement for bulk insert
|
||||
$sql = 'INSERT INTO products_software_licenses (version_id, license_key, license_type, status, transaction_id, accounthierarchy, created, createdby)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$created_count = 0;
|
||||
foreach ($serials as $serial) {
|
||||
if (empty($serial)) continue;
|
||||
|
||||
// Generate UUID for license key
|
||||
$license_key = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
|
||||
try {
|
||||
$stmt->execute([
|
||||
$version_id,
|
||||
$license_key,
|
||||
$license_type,
|
||||
$status,
|
||||
$transaction_id,
|
||||
$accounthierarchy,
|
||||
$date,
|
||||
$username
|
||||
]);
|
||||
|
||||
// Assign license to equipment if serial number exists
|
||||
$eq_sql = 'UPDATE equipment SET sw_version_license = ? WHERE serialnumber = ? AND accounthierarchy LIKE ?';
|
||||
$eq_stmt = $pdo->prepare($eq_sql);
|
||||
$eq_stmt->execute([$license_key, $serial, '%'.$partner->soldto.'%']);
|
||||
|
||||
$created_count++;
|
||||
} catch (Exception $e) {
|
||||
debuglog("Error creating license for serial $serial: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'created' => $created_count]);
|
||||
exit;
|
||||
}
|
||||
|
||||
//------------------------------------------
|
||||
// SINGLE LICENSE CREATION OR UPDATE
|
||||
//------------------------------------------
|
||||
//ADD STANDARD PARAMETERS TO ARRAY BASED ON INSERT OR UPDATE
|
||||
if ($command == 'update'){
|
||||
$post_content['updated'] = $date;
|
||||
$post_content['updatedby'] = $username;
|
||||
}
|
||||
elseif ($command == 'insert'){
|
||||
// Generate UUID for license key if not provided
|
||||
if (empty($post_content['license_key'])) {
|
||||
$post_content['license_key'] = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
}
|
||||
|
||||
$post_content['created'] = $date;
|
||||
$post_content['createdby'] = $username;
|
||||
$post_content['accounthierarchy'] = json_encode(array("salesid"=>$partner->salesid,"soldto"=>$partner->soldto), JSON_UNESCAPED_UNICODE);
|
||||
@@ -42,10 +124,10 @@ else {
|
||||
}
|
||||
|
||||
//CREATE NEW ARRAY AND MAP TO CLAUSE
|
||||
if(isset($post_content) && $post_content!=''){
|
||||
if(isset($post_content) && $post_content!=''){
|
||||
foreach ($post_content as $key => $var){
|
||||
if ($key == 'submit' || $key == 'rowID'){
|
||||
//do nothing
|
||||
if ($key == 'submit' || $key == 'rowID' || $key == 'serial' || $key == 'bulk' || $key == 'serials'){
|
||||
//do nothing - skip these fields
|
||||
}
|
||||
else {
|
||||
$criterias[$key] = $var;
|
||||
@@ -64,27 +146,43 @@ $input_insert = substr($input_insert, 1); //Clean clause - remove first comma
|
||||
|
||||
//QUERY AND VERIFY ALLOWED
|
||||
if ($command == 'update' && isAllowed('products_software_licenses',$profile,$permission,'U') === 1){
|
||||
|
||||
|
||||
$sql = 'UPDATE products_software_licenses SET '.$clause.' WHERE rowID = ? ';
|
||||
$execute_input[] = $id;
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
}
|
||||
elseif ($command == 'insert' && isAllowed('products_software_licenses',$profile,$permission,'C') === 1){
|
||||
|
||||
//INSERT NEW ITEM
|
||||
|
||||
//INSERT NEW ITEM
|
||||
$sql = 'INSERT INTO products_software_licenses ('.$clause_insert.') VALUES ('.$input_insert.')';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
|
||||
$new_license_id = $pdo->lastInsertId();
|
||||
$license_key = $post_content['license_key'];
|
||||
|
||||
// Assign license to equipment if serial number provided
|
||||
if (!empty($post_content['serial'])) {
|
||||
$serial = $post_content['serial'];
|
||||
$eq_sql = 'UPDATE equipment SET sw_version_license = ? WHERE serialnumber = ? AND accounthierarchy LIKE ?';
|
||||
$eq_stmt = $pdo->prepare($eq_sql);
|
||||
$eq_stmt->execute([$license_key, $serial, '%'.$partner->soldto.'%']);
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'license_id' => $new_license_id, 'license_key' => $license_key]);
|
||||
}
|
||||
elseif ($command == 'delete' && isAllowed('products_software_licenses',$profile,$permission,'D') === 1){
|
||||
|
||||
|
||||
$stmt = $pdo->prepare('DELETE FROM products_software_licenses WHERE rowID = ? ');
|
||||
$stmt->execute([ $id ]);
|
||||
$stmt->execute([ $id ]);
|
||||
|
||||
//Add deletion to changelog
|
||||
changelog($dbname,'products_software_licenses',$id,'Delete','Delete',$username);
|
||||
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} else
|
||||
{
|
||||
//do nothing
|
||||
|
||||
@@ -72,10 +72,9 @@ $hw_version = (isset($criterias['hw_version']))? $criterias['hw_version']:'';
|
||||
if ($command == 'update' && isAllowed('products_software_versions',$profile,$permission,'U') === 1){
|
||||
|
||||
//REMOVE LATEST FLAG FROM OTHER WHEN SEND
|
||||
//Max 2 latest flags per hw_version: 1 with price (has upgrade path with price) and 1 without
|
||||
if (isset($criterias['latest']) && $criterias['latest'] == 1){
|
||||
$sql = 'UPDATE products_software_versions SET latest = 0 WHERE hw_version = ? AND rowID != ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$hw_version, $id]);
|
||||
updateSoftwareLatestFlags($pdo, $id, $hw_version);
|
||||
}
|
||||
|
||||
$sql = 'UPDATE products_software_versions SET '.$clause.' WHERE rowID = ? ';
|
||||
@@ -84,18 +83,18 @@ if ($command == 'update' && isAllowed('products_software_versions',$profile,$per
|
||||
$stmt->execute($execute_input);
|
||||
}
|
||||
elseif ($command == 'insert' && isAllowed('products_software_versions',$profile,$permission,'C') === 1){
|
||||
|
||||
//REMOVE LATEST FLAG FROM OTHER IF SET
|
||||
if (isset($criterias['latest']) && $criterias['latest'] == 1){
|
||||
$sql = 'UPDATE products_software_versions SET latest = 0 WHERE hw_version = ?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$hw_version]);
|
||||
}
|
||||
|
||||
//INSERT NEW ITEM
|
||||
$sql = 'INSERT INTO products_software_versions ('.$clause_insert.') VALUES ('.$input_insert.')';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($execute_input);
|
||||
$new_id = $pdo->lastInsertId();
|
||||
|
||||
//REMOVE LATEST FLAG FROM OTHER IF SET
|
||||
//Max 2 latest flags per hw_version: 1 with price (has upgrade path with price) and 1 without
|
||||
if (isset($criterias['latest']) && $criterias['latest'] == 1){
|
||||
updateSoftwareLatestFlags($pdo, $new_id, $hw_version);
|
||||
}
|
||||
}
|
||||
elseif ($command == 'delete' && isAllowed('products_software_versions',$profile,$permission,'D') === 1){
|
||||
|
||||
|
||||
Reference in New Issue
Block a user