<?php
if (!defined('ABSPATH')) exit;  // Exit if accessed directly


use PayPalCheckoutSdk\Orders\OrdersCreateRequest;

function wpgv__doajax_gift_card_pdf_save_func()
{
	// Get the nonce from POST data
	$nonce = isset($_POST['giftcard_nonce']) ? sanitize_text_field(wp_unslash($_POST['giftcard_nonce'])) : '';

	// Check nonce
	if (empty($nonce) || !wp_verify_nonce($nonce, 'giftcard_form_action')) {
		wp_send_json_error(array('message' => 'Invalid nonce'));
		wp_die();
	}

	$wpgv_enable_recaptcha = get_option('wpgv_enable_recaptcha') ? sanitize_text_field(get_option('wpgv_enable_recaptcha')) : '';
	$recaptchaSecretKey  = get_option('wpgv_recaptcha_secret_key') ? sanitize_text_field(get_option('wpgv_recaptcha_secret_key')) : '';
	$recaptcha_response = isset($_POST['recaptcha_response']) ? sanitize_text_field($_POST['recaptcha_response']) : '';

	if ($recaptcha_response && $wpgv_enable_recaptcha == 1) {

		$verifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
		$response = wp_remote_post($verifyUrl, array(
			'body' => array(
				'secret' => $recaptchaSecretKey,
				'response' => $recaptcha_response,
				'remoteip' => $_SERVER['REMOTE_ADDR']
			)
		));


		$responseBody = wp_remote_retrieve_body($response);
		$result = json_decode($responseBody);
		if (!$result->success) {
			wp_send_json_error(array('message' => 'reCAPTCHA verification failed.'));
			exit();
		}
	}

	global $wpdb;
	$voucher_table 	= $wpdb->prefix . 'giftvouchers_list';
	$invoice_setting_table = $wpdb->prefix . 'giftvouchers_invoice_settings';
	$invoice_options = $wpdb->get_row("SELECT * FROM $invoice_setting_table WHERE id = 1");
	$setting_options = get_data_settings_voucher();
	$idVoucher = sanitize_text_field(base64_decode($_POST['idVoucher']));
	$template = $idVoucher;
	$priceExtraCharges = sanitize_text_field(base64_decode($_POST['priceExtraCharges']));
	$priceVoucher = sanitize_text_field(base64_decode($_POST['priceVoucher']));
	$buyingfor = isset($_POST['buying_for']) ? sanitize_text_field(base64_decode($_POST['buying_for'])) : 'someone_else';
	$from = isset($_POST['from']) ? sanitize_text_field(base64_decode($_POST['from'])) : '';
	$for = sanitize_text_field(base64_decode($_POST['for']));
	$message = sanitize_textarea_field(base64_decode($_POST['message']));
	$email = sanitize_email(base64_decode($_POST['email']));

	$wpgv_coupon_code_type = get_option('wpgv_coupon_code_type') ? get_option('wpgv_coupon_code_type') : '';
	$wpgv_starting_coupon_number = get_option('wpgv_starting_coupon_number') ? get_option('wpgv_starting_coupon_number') : '';
	$wpgv_current_coupon_number = get_option('wpgv_current_coupon_number') ? get_option('wpgv_current_coupon_number') : '';
	$coupon_code_length = get_option('wpgv_coupon_code_length') ? get_option('wpgv_coupon_code_length') : 12;
	$code = null;

	if ($wpgv_coupon_code_type === "random") {
		$code = sanitize_text_field(base64_decode($_POST['couponcode']));
	} else if ($wpgv_coupon_code_type === "incremental") {
		if (!empty($wpgv_current_coupon_number)) {
			$code = intval($wpgv_current_coupon_number);
		} else {

			$code = intval($wpgv_starting_coupon_number);
		}
	}

	$code = str_pad($code, $coupon_code_length, '0', STR_PAD_LEFT);

	$shipping_email = sanitize_email(base64_decode($_POST['shipping_email']));
	$shipping = isset($_POST['shipping']) ? sanitize_text_field(base64_decode($_POST['shipping'])) : '';
	$firstname = isset($_POST['fisrtName']) ? sanitize_text_field(base64_decode($_POST['fisrtName'])) : '';
	$lastname = isset($_POST['lastName']) ? sanitize_text_field(base64_decode($_POST['lastName'])) : '';
	$address = isset($_POST['address']) ? sanitize_text_field(base64_decode($_POST['address'])) : '';
	$pincode = isset($_POST['postcode']) ? sanitize_text_field(base64_decode($_POST['postcode'])) : '';
	$shipping_method = isset($_POST['shipping_method']) ? sanitize_text_field(base64_decode($_POST['shipping_method'])) : '';
	$paymentmethod = sanitize_text_field(base64_decode($_POST['pay_method']));
	$paytext = '';
	if ($paymentmethod == 'Per Invoice') {
		$paytext = __('Per Invoice', 'gift-voucher');
	} else {
		$paytext = $paymentmethod;
	}
	$typeGiftCard = sanitize_text_field(base64_decode($_POST['typeGiftCard']));
	$send_email_date_time = isset($_POST['send_email_date_time']) ? base64_decode($_POST['send_email_date_time']) : 'send_instantly';

	$wpgv_customer_receipt = get_option('wpgv_customer_receipt') ? get_option('wpgv_customer_receipt') : 0;
	// Ensure the pdf saving option is defined to avoid notices when not set
	$wpgv_enable_pdf_saving = get_option('wpgv_enable_pdf_saving') ? get_option('wpgv_enable_pdf_saving') : 0;
	// check exp
	$wpgv_hide_expiry = get_option('wpgv_hide_expiry') ? get_option('wpgv_hide_expiry') : 'yes';
	$wpgv_expiry_date_format = get_option('wpgv_expiry_date_format') ? get_option('wpgv_expiry_date_format') : 'd.m.Y';
	$wpgv_add_extra_charges = get_option('wpgv_add_extra_charges_card') ? get_option('wpgv_add_extra_charges_card') : 0;
	$voucher_expiry_value = !empty(get_post_meta($idVoucher, 'wpgv_customize_template_voucher_expiry_value', true)) ? get_post_meta($idVoucher, 'wpgv_customize_template_voucher_expiry_value', true) : $setting_options->voucher_expiry; // format day and number
	$expiry_data = "";
	if ($wpgv_hide_expiry == 'no') {
		$expiry = __('No Expiration', 'gift-voucher');
		$expiry_data = __('No Expiry', 'gift-voucher');
	} else {
		$expiry = ($setting_options->voucher_expiry_type == 'days') ? date($wpgv_expiry_date_format, strtotime('+' . $voucher_expiry_value . ' days', time())) . PHP_EOL : $voucher_expiry_value;
		$expiry_data = $expiry;
	}

	//updaload image
	$upload = wp_upload_dir();
	$upload_dir = $upload['basedir'] . '/voucherpdfuploads/';
	if (!file_exists($upload_dir)) {
		mkdir($upload_dir, 0755, true);
	}

	$image_path = $upload_dir . 'giftcard.png';

	if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
		$image_tmp_path = $_FILES['image']['tmp_name'];

		if (move_uploaded_file($image_tmp_path, $image_path)) {
			$image = file_get_contents($image_path);
			$sizeimage = getimagesize($image_path);
		}
	}
	//updaload image

	$dirUrl = $upload['baseurl'] . '/voucherpdfuploads/';
	$pdf = new PDF();
	if (!empty($sizeimage)) {
		if ($typeGiftCard == 'landscape') {
			$pdf->AddPage("L", 'a4');
		} else {
			$pdf->AddPage('P', 'a4');
		}
		$pdf->imageCenterCell($upload_dir . "giftcard.png", 0, 0, $pdf->GetPageWidth(), $pdf->GetPageHeight());
	} else {
		$pdf->AddPage("L");
		$pdf->centreImage($upload_dir . "giftcard.png");
	}
	$curr_time = time();
	$upload = wp_upload_dir();
	$upload_dir = $upload['basedir'];
	$upload_dir = $upload_dir . '/voucherpdfuploads/' . $curr_time . $_POST['couponcode'] . '.pdf';
	$pdf->output($upload_dir, 'F');
	$upload_url = $curr_time . $_POST['couponcode'];
	$wpdb->insert(
		$voucher_table,
		array(
			'order_type'		=> 'vouchers',
			'template_id' 		=> $idVoucher,
			'buying_for'		=> $buyingfor,
			'from_name' 		=> $for,
			'to_name' 			=> $from,
			'amount'			=> $priceVoucher,
			'message'			=> $message,
			'shipping_type'		=> $shipping,
			'shipping_email'	=> $shipping_email,
			'firstname'			=> $firstname,
			'lastname'			=> $lastname,
			'email'				=> $email,
			'address'			=> $address,
			'postcode'			=> $pincode,
			'shipping_method'	=> $shipping_method,
			'pay_method'		=> $paymentmethod,
			'expiry'			=> $expiry_data,
			'couponcode'		=> $code,
			'voucherpdf_link'	=> $upload_url,
			'status'			=> 'unused',
			'payment_status'	=> 'Not Paid',
			'voucheradd_time'	=> current_time('mysql'),
			'email_send_date_time' => $send_email_date_time,
			'check_send_mail' => 'unsent',
		)
	);
	$lastid = $wpdb->insert_id;

	// coupon incremental
	if ($lastid && $wpgv_coupon_code_type === "incremental") {
		$coupon_code_length = get_option('wpgv_coupon_code_length') ? get_option('wpgv_coupon_code_length') : 12;
		$next_number = intval($code) + 1;
		$next_number_formatted = str_pad($next_number, $coupon_code_length, '0', STR_PAD_LEFT);
		update_option('wpgv_current_coupon_number', $next_number_formatted);
	}

	$create_note = sprintf(__('Voucher ordered by %s, Message: %s', 'gift-voucher'), $for, $message);
	$create_text = 'create';
	WPGV_Gift_Voucher_Activity::record($lastid, $create_text, '', $create_note);
	$titleVoucher = get_the_title($idVoucher);
	if (empty($titleVoucher)) {
		$titleVoucher = 'Gift Card';
	}
	//shipping as post
	$shipping_charges = 0;
	if ($shipping != 'shipping_as_email') {
		$preshipping_methods = explode(',', $setting_options->shipping_method);
		foreach ($preshipping_methods as $method) {
			$preshipping_method = explode(':', $method);
			if (trim(stripslashes($preshipping_method[1])) == trim(stripslashes($shipping_method))) {
				$shipping_charges = trim($preshipping_method[0]);
				break;
			}
		}
	}
	$value = $priceVoucher + $priceExtraCharges + $shipping_charges;

	// Get currency code early for use in Klarna integration
	$currency_code = isset($setting_options->currency_code) ? $setting_options->currency_code : $setting_options->currency;

	if (get_locale() == "cs_CZ") {
		$currency = wpgv_price_format_cs_CZ($value);
	} else {
		$currency = wpgv_price_format($value);
	}

	if ($invoice_options->is_invoice_active == 1) {
		$upload_dir = $upload['basedir'];
		$invoiceupload_dir = $upload_dir . '/voucherpdfuploads/' . $curr_time . $_POST['couponcode'] . '-invoice.pdf';
		// Use isolated PDF generation for invoice
		$invoice = wpgv_create_pdf_safe();
		if ($invoice) {
			require_once(WPGIFT__PLUGIN_DIR . '/templates/pdfstyles/invoice-1.php');
		}
		if ($wpgv_enable_pdf_saving) {
			$invoice->Output($invoiceupload_dir, 'F');
		} else {
			$invoice->Output('F', $invoiceupload_dir);
		}
	}
	//Customer Receipt
	if ($wpgv_customer_receipt) {
		$upload_dir = $upload['basedir'];
		$receiptupload_dir = $upload_dir . '/voucherpdfuploads/' . $curr_time . $_POST['couponcode'] . '-receipt.pdf';
		// Use isolated PDF generation for receipt
		$receipt = wpgv_create_pdf_safe();
		if ($receipt) {
			require_once(WPGIFT__PLUGIN_DIR . '/templates/pdfstyles/receipt.php');
		}
		if ($wpgv_enable_pdf_saving) {
			$receipt->Output($receiptupload_dir, 'F');
		} else {
			$receipt->Output('F', $receiptupload_dir);
		}
	}

	update_post_meta($lastid, 'wpgv_extra_charges', $priceExtraCharges);
	update_post_meta($lastid, 'wpgv_total_payable_amount', $value);

	$success_url = get_site_url() . '/voucher-payment-successful/?voucheritem=' . $lastid;
	$cancel_url = get_site_url() . '/voucher-payment-cancel/?voucheritem=' . $lastid;
	$notify_url = get_site_url() . '/voucher-payment-successful/?voucheritem=' . $lastid;
	//check payment

	$wpgv_paypal_client_id = get_option('wpgv_paypal_client_id') ? get_option('wpgv_paypal_client_id') : '';
	$wpgv_paypal_secret_key = get_option('wpgv_paypal_secret_key') ? get_option('wpgv_paypal_secret_key') : '';

	if ($paymentmethod == 'Paypal') {
		if (empty($wpgv_paypal_client_id) || empty($wpgv_paypal_secret_key)) {
			$error_message = "PayPal Client ID or Secret Key are both empty.";
			wp_send_json_error(array('message' => $error_message));
		} else {
			require_once(WPGIFT__PLUGIN_DIR . '/vendor/autoload.php');
			require_once(WPGIFT__PLUGIN_DIR . '/include/PayPalAuth.php');

			if (!PayPalAuth::isCredentialsValid()) {
				$error_message = "PayPal Client ID or Secret Key is invalid or the PayPal Mode is not compatible with the provided credentials.";
				wp_send_json_error(array('message' => $error_message));
			} else {
				require_once(WPGIFT__PLUGIN_DIR . '/vendor/autoload.php');
				require_once(WPGIFT__PLUGIN_DIR . '/include/PayPalAuth.php');

				$client = PayPalAuth::client();
				$request = new OrdersCreateRequest();
				$request->prefer('return=representation');
				// Resolve PayPal success page configured in settings (if any)
				$paypal_success_page_id = get_option('wpgv_giftvouchersuccesspage');
				if (!empty($paypal_success_page_id)) {
					$pp_page = get_post($paypal_success_page_id);
					if ($pp_page && $pp_page->post_status === 'publish') {
						$paypal_return_url = get_permalink($paypal_success_page_id) . '?voucheritem=' . $lastid;
					} else {
						$paypal_return_url = $success_url;
					}
				} else {
					$paypal_return_url = $success_url;
				}

				$paypal_amount = number_format((float) $value, 2, '.', '');
				$request->body = [
					"intent" => "CAPTURE",
					"purchase_units" => [[
						"reference_id" => $template_options->title,
						"amount" => [
							"value" => $paypal_amount,
							"currency_code" => $setting_options->currency_code
						]
					]],
					"application_context" => [
						"cancel_url" => $cancel_url,
						"return_url" => $paypal_return_url
					]
				];

				try {
					// Call API with your client and get a response for your call
					$response = $client->execute($request);
					// Normalize and persist the PayPal order id so it survives cross-site redirects
					$orderId = strval($response->result->id);
					if (session_status() == PHP_SESSION_NONE) {
						session_start();
					}
					$_SESSION["paypal_order_id"] = $orderId;
					// Persist to post meta so the success page can find the order id even if the session is lost
					if (!empty($lastid)) {
						update_post_meta($lastid, 'wpgv_paypal_order_id', sanitize_text_field($orderId));
						// Keep legacy meta key for compatibility
						update_post_meta($lastid, 'paypal_order_id', sanitize_text_field($orderId));
					}

					// Initialize an empty approve_link variable
					$approve_link = '';

					// If call returns body in response, you can get the deserialized version from the result attribute of the response
					foreach ($response->result->links as $link) {
						if ($link->rel == "approve") {
							$approve_link = $link->href;
						}
					}

					// Check if the approve_link is empty
					if (!empty($approve_link)) {
						wp_send_json_success(array('message' => "PayPal valid.", 'approve_link' => $approve_link));
					} else {
						wp_send_json_error(array('message' => "No PayPal approve link found."));
					}
				} catch (HttpException $ex) {
					// Log the exception for debugging (avoid printing raw exception to output)
					error_log('WPGV PayPal OrdersCreateRequest failed: ' . $ex->getMessage());
					wp_send_json_error(array('message' => 'PayPal request failed.'));
				}
			}
		}
	} elseif ($paymentmethod == 'Sofort') {

		$Sofortueberweisung = new Sofortueberweisung($setting_options->sofort_configure_key);

		$Sofortueberweisung->setAmount($value);
		$Sofortueberweisung->setCurrencyCode($setting_options->currency_code);

		$Sofortueberweisung->setReason($setting_options->reason_for_payment, $lastid);
		$Sofortueberweisung->setSuccessUrl($success_url, true);
		$Sofortueberweisung->setAbortUrl($cancel_url);
		// $Sofortueberweisung->setNotificationUrl($notify_url);

		$Sofortueberweisung->sendRequest();

		if ($Sofortueberweisung->isError()) {
			//SOFORT-API didn't accept the data
			// echo $Sofortueberweisung->getError();
			$error_message = "Sofort is invalid.";
			wp_send_json_error(array('message' => $error_message));
		} else {
			//buyer must be redirected to $paymentUrl else payment cannot be successfully completed!
			$paymentUrl = $Sofortueberweisung->getPaymentUrl();
			wp_send_json_success(array('message' => "Sofort valid.", 'approve_link' => $paymentUrl));
		}
	} elseif ($paymentmethod == 'Stripe') {
		$stripeimage = $dirUrl . "giftcard.png";
		$stripesuccesspageurl = get_option('wpgv_stripesuccesspage');

		// Resolve Stripe success permalink safely
		if (!empty($stripesuccesspageurl)) {
			$stripe_success_permalink = get_permalink($stripesuccesspageurl);
		} else {
			$stripe_success_permalink = get_site_url() . '/voucher-payment-successful/';
		}

		//set api key
		$stripe = array(
			"publishable_key" => $setting_options->stripe_publishable_key,
			"secret_key"      => $setting_options->stripe_secret_key,
		);

		$camount = ($value) * 100;
		$stripeemail = ($email) ? $email : $email;

		// Initialize Stripe using force loader function
		if (!wpgv_init_force_stripe($stripe['secret_key'])) {
			wp_send_json_error(array('message' => 'Failed to initialize Stripe.'));
			exit;
		}

		$is_stripe_ideal_enable = get_option('wpgv_stripe_ideal');

		// Prepare session data
		$session_data = [
			// 'payment_method_types' removed to allow Stripe to determine available methods
			'line_items' => [[
				'price_data' => [
					'currency' => $setting_options->currency_code,
					'unit_amount' => $camount,
					'product_data' => [
						'name' => $titleVoucher,
					],
				],
				'quantity' => 1,
			]],
			'mode' => 'payment',
			'success_url' => $stripe_success_permalink . '?voucheritem=' . $lastid . '&sessionid={CHECKOUT_SESSION_ID}',
			'cancel_url' => $cancel_url,
		];

		// Create Stripe session using force loader function
		$result = wpgv_create_force_stripe_session($session_data);

		if ($result['success']) {
			$session = $result['data'];
			// Persist Stripe session id so webhook can map session -> voucher
			$session_id = is_object($session) ? ($session->id ?? '') : (is_array($session) ? ($session['id'] ?? '') : '');
			if (!empty($session_id) && $lastid) {
				update_post_meta($lastid, 'wpgv_stripe_session_key', sanitize_text_field($session_id));
			}
			$stripeemail = ($email) ? $email : $email;
			wp_send_json_success(array('message' => "Stripe valid.", 'approve_link' => $session->url));
		} else {
			wp_send_json_error(array('message' => $result['data']));
		}
	} elseif ($paymentmethod == 'MultiSafepay') {
		$wpgv_multisafepay_test_mode = get_option('wpgv_multisafepay_test_mode') ? get_option('wpgv_multisafepay_test_mode') : '';
		$wpgv_multisafepay_api_id = get_option('wpgv_multisafepay_api_id') ? get_option('wpgv_multisafepay_api_id') : '0ff28d5cc3a6e7475be5fa174703788fa155fc94';
		$msp_api_url = $wpgv_multisafepay_test_mode ? 'https://testapi.multisafepay.com/v1/json/' : 'https://api.multisafepay.com/v1/json/';
			$msp = new \MultiSafepayAPI\Client;
			$msp->setApiKey($wpgv_multisafepay_api_id);
			$msp->setApiUrl($msp_api_url);
			$camount = ($value) * 100;
			try {
				$order_id = $lastid;

				// Resolve MultiSafepay success page if configured
				$multisafepay_page_id = get_option('wpgv_multisafepaysuccesspage');
				if (!empty($multisafepay_page_id)) {
					$msp_page = get_post($multisafepay_page_id);
					$msp_base = ($msp_page && $msp_page->post_status === 'publish') ? get_permalink($multisafepay_page_id) : $success_url;
				} else {
					$msp_base = $success_url;
				}
				$msp_redirect_url = add_query_arg('voucheritem', $lastid, $msp_base);

				$order = $msp->orders->post(array(
					"type" => "redirect",
					"order_id" => $lastid,
					"currency" => $setting_options->currency_code,
					"amount" => $camount,
					"description" => $titleVoucher,
					"payment_options" => array(
						"notification_url" => $notify_url,
						"redirect_url" => $msp_redirect_url,
						"cancel_url" => $cancel_url,
						"close_window" => "true"
					),
					"customer" => array(
						"ip_address" => $_SERVER['REMOTE_ADDR'],
						"forwarded_ip" => $_SERVER['REMOTE_ADDR'],
						"first_name" => $from,
						"email" => $email,
					),
				));

				wp_send_json_success(array('message' => "Per Invoice valid.", 'approve_link' => $msp->orders->getPaymentLink()));
			} catch (Exception $e) {
				wp_send_json_error(array('message' => "Error " . htmlspecialchars($e->getMessage())));
			}
	} elseif ($paymentmethod == 'Per Invoice') {
		// Resolve Per Invoice success page if configured in settings
		$perinvoice_page_id = get_option('wpgv_perinvoicesuccesspage');
		if (!empty($perinvoice_page_id)) {
			$perinvoice_page = get_post($perinvoice_page_id);
			$perinvoice_base = ($perinvoice_page && $perinvoice_page->post_status === 'publish') ? get_permalink($perinvoice_page_id) : $success_url;
		} else {
			$perinvoice_base = $success_url;
		}
		$perinvoice_url = add_query_arg(array('voucheritem' => $lastid, 'per_invoice' => '1'), $perinvoice_base);
		wp_send_json_success(array('message' => "Per Invoice valid.", 'approve_link' => $perinvoice_url));
	} elseif ($paymentmethod == 'Klarna') {
		// Klarna Payments v1 (Sessions API) - Modern integration
		$wpgv_klarna_enabled = get_option('wpgv_klarna') ? get_option('wpgv_klarna') : 0;
		if (!$wpgv_klarna_enabled) {
			wp_send_json_error(array('message' => 'Klarna is not enabled in settings.'));
		}
		$klarna_mode = get_option('wpgv_klarna_mode') ? get_option('wpgv_klarna_mode') : 'sandbox';
		if ($klarna_mode === 'live') {
			$merchant_id = get_option('wpgv_klarna_live_merchant_id');
			$shared_secret = get_option('wpgv_klarna_live_shared_secret');
			$klarna_sessions_url = 'https://api.klarna.com/payments/v1/sessions';
		} else {
			$merchant_id = get_option('wpgv_klarna_demo_merchant_id');
			$shared_secret = get_option('wpgv_klarna_demo_shared_secret');
			$klarna_sessions_url = 'https://api.playground.klarna.com/payments/v1/sessions';
		}
		if (empty($merchant_id) || empty($shared_secret)) {
			wp_send_json_error(array('message' => 'Klarna credentials are not configured.'));
		}

		// Amount in minor units (cents/øre)
		$amount_minor = intval(round(floatval($value) * 100));

		// Locale and country detection
		$klarna_purchase_override = get_option('wpgv_klarna_purchase_country');
		$locale_raw = function_exists('get_locale') ? get_locale() : 'en_US';
		$locale_key = str_replace('-', '_', $locale_raw);
		$locale_map = array(
			'de_AT' => array('locale' => 'de-AT', 'country' => 'AT'),
			'de_DE' => array('locale' => 'de-DE', 'country' => 'DE'),
			'en_GB' => array('locale' => 'en-GB', 'country' => 'GB'),
			'fr_FR' => array('locale' => 'fr-FR', 'country' => 'FR'),
			'nl_NL' => array('locale' => 'nl-NL', 'country' => 'NL'),
			'it_IT' => array('locale' => 'it-IT', 'country' => 'IT'),
			'es_ES' => array('locale' => 'es-ES', 'country' => 'ES'),
			'sv_SE' => array('locale' => 'sv-SE', 'country' => 'SE'),
			'fi_FI' => array('locale' => 'fi-FI', 'country' => 'FI'),
			'da_DK' => array('locale' => 'da-DK', 'country' => 'DK'),
			'nb_NO' => array('locale' => 'nb-NO', 'country' => 'NO'),
			'pl_PL' => array('locale' => 'pl-PL', 'country' => 'PL'),
			'pt_PT' => array('locale' => 'pt-PT', 'country' => 'PT'),
			'ro_RO' => array('locale' => 'ro-RO', 'country' => 'RO'),
			'cs_CZ' => array('locale' => 'cs-CZ', 'country' => 'CZ'),
			'sk_SK' => array('locale' => 'sk-SK', 'country' => 'SK'),
			'hu_HU' => array('locale' => 'hu-HU', 'country' => 'HU'),
			'bg_BG' => array('locale' => 'bg-BG', 'country' => 'BG'),
			'hr_HR' => array('locale' => 'hr-HR', 'country' => 'HR'),
			'lt_LT' => array('locale' => 'lt-LT', 'country' => 'LT'),
			'lv_LV' => array('locale' => 'lv-LV', 'country' => 'LV'),
			'sl_SI' => array('locale' => 'sl-SI', 'country' => 'SI'),
			'ee_EE' => array('locale' => 'et-EE', 'country' => 'EE'),
			'lu_LU' => array('locale' => 'fr-LU', 'country' => 'LU'),
			'mt_MT' => array('locale' => 'en-MT', 'country' => 'MT'),
			'cy_CY' => array('locale' => 'en-CY', 'country' => 'CY'),
		);
		// Country -> native language locale mapping for Klarna (when WP locale doesn't match)
		$country_locale_map = array(
			'SE' => 'sv-SE',
			'NO' => 'nb-NO',
			'DK' => 'da-DK',
			'FI' => 'fi-FI',
			'AT' => 'de-AT',
			'DE' => 'de-DE',
			'FR' => 'fr-FR',
			'IT' => 'it-IT',
			'ES' => 'es-ES',
			'NL' => 'nl-NL',
			'PL' => 'pl-PL',
			'PT' => 'pt-PT',
		);
		if (!empty($klarna_purchase_override)) {
			$override = trim($klarna_purchase_override);
			if (preg_match('/^[A-Za-z]{2}$/', $override)) {
				$purchase_country = strtoupper($override);
				// Use native language locale for country, or fallback to locale_raw language
				$klarna_locale = isset($country_locale_map[$purchase_country]) ? $country_locale_map[$purchase_country] : (strtolower(substr($locale_raw, 0, 2)) . '-' . $purchase_country);
			} else {
				$override_locale = str_replace('_', '-', $override);
				$klarna_locale = $override_locale;
				$parts = preg_split('/[-_]/', $override_locale);
				$purchase_country = isset($parts[1]) ? strtoupper($parts[1]) : strtoupper($override);
			}
		} else {
			if (isset($locale_map[$locale_key])) {
				$klarna_locale = $locale_map[$locale_key]['locale'];
				$purchase_country = $locale_map[$locale_key]['country'];
			} else {
				$parts = preg_split('/[_-]/', $locale_raw);
				if (count($parts) >= 2) {
					$purchase_country = strtoupper($parts[1]);
					// Use native language locale if available, else use WP locale's language
					$klarna_locale = isset($country_locale_map[$purchase_country]) ? $country_locale_map[$purchase_country] : (strtolower($parts[0]) . '-' . $purchase_country);
				} else {
					$klarna_locale = 'en-GB';
					$purchase_country = 'GB';
				}
			}
		}

		// Validate country/currency compatibility
		$country_currency_map = array(
			'AT' => 'EUR',
			'DE' => 'EUR',
			'NL' => 'EUR',
			'FR' => 'EUR',
			'IT' => 'EUR',
			'ES' => 'EUR',
			'SE' => 'SEK',
			'FI' => 'EUR',
			'DK' => 'DKK',
			'NO' => 'NOK',
			'GB' => 'GBP',
			'US' => 'USD',
		);
		$currency_upper = strtoupper($currency_code);
		if (isset($country_currency_map[$purchase_country]) && $currency_upper !== $country_currency_map[$purchase_country]) {
			wp_send_json_error(array('message' => sprintf("Klarna market '%s' requires currency '%s' but site is configured with '%s'. Change your Currency Code or set a matching Klarna Market Override.", $purchase_country, $country_currency_map[$purchase_country], $currency_upper)));
		}

		// Klarna Payments v1 Session payload
		// Per Klarna official docs: DO NOT include customer/billing/shipping in session (Step 1)
		// These belong ONLY in authorize() call (Step 2), not in session creation
		// Reference: https://docs.klarna.com/payments/web-payments/integrate-with-klarna-payments/integrate-via-sdk/step-1-initiate-a-payment/

		// Get configured Klarna success page - REQUIRED
		$klarna_success_page_id = get_option('wpgv_klarna_success_page');
		if (!$klarna_success_page_id || $klarna_success_page_id <= 0) {
			wp_send_json_error(array('message' => __('Klarna Success Page is not configured. Please go to Gift Voucher Pro Settings and select a Klarna Success Page.', 'gift-voucher')));
		}

		// Verify the page exists and is published
		$klarna_success_page = get_post($klarna_success_page_id);
		if (!$klarna_success_page || $klarna_success_page->post_status !== 'publish') {
			wp_send_json_error(array('message' => __('The configured Klarna Success Page does not exist or is not published. Please check your settings.', 'gift-voucher')));
		}

		// Verify the page contains the required shortcode
		if (strpos($klarna_success_page->post_content, '[wpgv_klarnasuccesspage]') === false) {
			wp_send_json_error(array('message' => __('The configured Klarna Success Page does not contain the [wpgv_klarnasuccesspage] shortcode. Please add the shortcode to the page.', 'gift-voucher')));
		}

		$base_success_url = get_permalink($klarna_success_page_id);
		$base_success_url = add_query_arg('voucheritem', $lastid, $base_success_url);

		// Ensure name is not empty and within Klarna limits (max 255 chars)
		$orderLineName = !empty($titleVoucher) ? substr(sanitize_text_field($titleVoucher), 0, 255) : 'Gift Card';

		$payload = array(
			'purchase_country' => $purchase_country,
			'purchase_currency' => $currency_upper,
			'locale' => $klarna_locale,
			'order_amount' => $amount_minor,
			'order_tax_amount' => 0,
			'order_lines' => array(array(
				'type' => 'digital',
				'reference' => 'giftcard-' . $lastid,
				'name' => $orderLineName,
				'quantity' => 1,
				'unit_price' => $amount_minor,
				'tax_rate' => 0,
				'total_amount' => $amount_minor,
				'total_tax_amount' => 0,
				'total_discount_amount' => 0,
			)),
			'merchant_urls' => array(
				'terms' => get_site_url(),
				'checkout' => $base_success_url . '&session_id={session_id}',
				'confirmation' => $base_success_url,
				'push' => get_site_url() . '/wp-admin/admin-ajax.php?action=wpgv_klarna_push_notification',
			),
			'client' => array(
				'locale' => $klarna_locale,
			),
		);
		$json_body = wp_json_encode($payload);

		$args = array(
			'headers' => array(
				'Authorization' => 'Basic ' . base64_encode($merchant_id . ':' . $shared_secret),
				'Content-Type' => 'application/json'
			),
			'body' => $json_body,
			'timeout' => 30,
		);

		error_log('Klarna Payments v1 - Creating session for: ' . $titleVoucher . ' - Amount: ' . $amount_minor . ' ' . $currency_upper);
		error_log('Klarna Payload: ' . $json_body);

		$response = wp_remote_post($klarna_sessions_url, $args);
		if (is_wp_error($response)) {
			wp_send_json_error(array('message' => 'Klarna API request failed: ' . $response->get_error_message()));
		}

		$code = wp_remote_retrieve_response_code($response);
		$body = wp_remote_retrieve_body($response);
		$data = json_decode($body, true);

		if ($code >= 200 && $code < 300) {
			// Store session data for later authorization
			// CRITICAL: Store the original $payload (what we sent), not $data (Klarna's response)
			// because we need order_lines, order_amount, etc. for create order step
			if (!empty($data['session_id'])) {
				update_post_meta($lastid, 'wpgv_klarna_session_id', sanitize_text_field($data['session_id']));
				update_post_meta($lastid, 'wpgv_klarna_session_data', wp_json_encode($payload));
			}

			// Return client_token to frontend for Klarna.js
			if (!empty($data['client_token'])) {
				$response_payload = array(
					'message' => 'Klarna session created.',
					'client_token' => $data['client_token'],
					'session_id' => $data['session_id'],
					'purchase_country' => $purchase_country,
					'purchase_currency' => $currency_upper,
				);
				// Include debug for admins
				if ((function_exists('current_user_can') && current_user_can('manage_options')) || (defined('WP_DEBUG') && WP_DEBUG)) {
					$response_payload['debug_payload'] = $payload;
					$response_payload['debug_klarna_response'] = $data;
				}
				wp_send_json_success($response_payload);
			} else {
				wp_send_json_error(array('message' => 'Klarna session created but no client_token returned.'));
			}
		} else {
			$error_msg = !empty($data['error_message']) ? $data['error_message'] : (!empty($data['error_code']) ? $data['error_code'] : $body);
			wp_send_json_error(array('message' => 'Klarna API error: ' . $error_msg));
		}
	} elseif ($paymentmethod == 'mollie') {

		require_once(WPGIFT__PLUGIN_DIR . '/vendor/autoload.php');
		$mollie_success_url = get_site_url() . '/mollie-payment-successful/?voucheritem=' . $lastid;

		$mollie_api_key = $setting_options->mollie_api_key;

		if (!$mollie_api_key) {
			wp_send_json_error(array('message' => "Mollie API key is missing."));
			exit;
		}

		try {
			$mollie = new Mollie\Api\MollieApiClient();
			$mollie->setApiKey($mollie_api_key);

			// Kiểm tra tài khoản đã được kích hoạt hay chưa
			$profile = $mollie->profiles->getCurrent();
			if (!$profile->status == 'active') {
				wp_send_json_error(array('message' => "Mollie account is not active."));
				exit;
			}

			$format_number_mollie = number_format($value, 2, '.', '');

			$description = !empty($template_options->title) ? $template_options->title : "Default payment description";
			if (strlen($description) > 191) {
				$description = substr($description, 0, 191);
			}

			$payment = $mollie->payments->create([
				"amount" => [
					"currency" => $setting_options->currency_code,
					"value" => $format_number_mollie
				],
				"description" => $description,
				"redirectUrl" => $mollie_success_url,
			]);
			if ($payment->id) {
				session_start();
				$_SESSION['payment_id'] = $payment->id;
			}

			wp_send_json_success(array('message' => "Mollie valid.", 'approve_link' => $payment->getCheckoutUrl()));
		} catch (Mollie\Api\Exceptions\ApiException $e) {
			wp_send_json_error(array('message' => "API call failed: " . htmlspecialchars($e->getMessage())));
		}
	}
	die();
}
add_action('wp_ajax_nopriv_wpgv_save_gift_card', 'wpgv__doajax_gift_card_pdf_save_func');
add_action('wp_ajax_wpgv_save_gift_card', 'wpgv__doajax_gift_card_pdf_save_func');
// PDF
class PDF extends WPGV_FPDF
{
	const DPI = 96;
	const MM_IN_INCH = 25.4;
	const A4_HEIGHT = 297;
	const A4_WIDTH = 210;
	const MAX_WIDTH = 800;
	const MAX_HEIGHT = 500;
	function pixelsToMM($val)
	{
		return $val * self::MM_IN_INCH / self::DPI;
	}
	function resizeToFit($imgFilename)
	{
		list($width, $height) = getimagesize($imgFilename);
		$widthScale = self::MAX_WIDTH / $width;
		$heightScale = self::MAX_HEIGHT / $height;
		$scale = min($widthScale, $heightScale);
		return array(
			round($this->pixelsToMM($scale * $width)),
			round($this->pixelsToMM($scale * $height))
		);
	}
	function centreImage($img)
	{
		list($width, $height) = $this->resizeToFit($img);
		// you will probably want to swap the width/height
		// around depending on the page's orientation
		$this->Image(
			$img,
			(self::A4_HEIGHT - $width) / 2,
			(self::A4_WIDTH - $height) / 2,
			$width,
			$height
		);
	}
	function imageCenterCell($file, $x, $y, $w, $h)
	{
		if (!file_exists($file)) {
			$this->Error('File does not exist: ' . $file);
		} else {
			list($width, $height) = getimagesize($file);
			$ratio = $width / $height;
			$zoneRatio = $w / $h;

			// Same Ratio, put the image in the cell
			if ($ratio == $zoneRatio) {
				$this->Image($file, $x, $y, $w, $h);
			}

			// Image is vertical and cell is horizontal
			if ($ratio < $zoneRatio) {
				$neww = $h * $ratio;
				$newx = $x + (($w - $neww) / 2);
				$this->Image($file, $newx, $y, $neww);
			}

			// Image is horizontal and cell is vertical
			if ($ratio > $zoneRatio) {
				$newh = $w / $ratio;
				$newy = $y + (($h - $newh) / 2);
				$this->Image($file, $x, $newy, $w);
			}
		}
	}
}


