<?php
/**
* Plugin Name: Notifica Area Riservata
* Plugin URI: https://www.digitalfollowers.com
* Description: Invia i dettagli dell'acquisto a un sistema esterno dopo il completamento dell'ordine in WooCommerce.
* Version: 1.0.2
* Author: Staff - Digital Followers
* Author URI: https://www.digitalfollowers.com
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
add_action('woocommerce_payment_complete', 'notify_purchase_to_external_api');
function notify_purchase_to_external_api($order_id) {
$order = wc_get_order($order_id);
$products_data = [];
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
$magentoBookId = $product->get_id();
$enableResources = get_post_meta($product->get_id(), 'enableResources', true);
$products_data[] = [
'magentoBookId' => $magentoBookId,
'enableResources' => filter_var($enableResources, FILTER_VALIDATE_BOOLEAN)
];
}
$payload_details = [
'customerId' => $order->get_user_id(),
'customerEmail' => $order->get_billing_email(),
'orderId' => $order->get_id(),
'products' => $products_data
];
$header = json_encode(['typ' => 'JWT', 'alg' => 'HS512']);
$payload = base64_encode(json_encode($payload_details));
$signature = hash_hmac('sha512', $header . "." . $payload, 'your_secret_key', true);
$jwt = base64_encode($header) . "." . base64_encode($payload) . "." . base64_encode($signature);
$ch = curl_init('https://areariservataedises.meetweb.dev/api/notify-purchased-products');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['data' => $jwt]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
// Handle error, maybe log it
error_log('Curl error: ' . $error);
} else {
$response_data = json_decode($response, true);
if (isset($response_data['error']) && $response_data['error']) {
// Handle API error, maybe log it
error_log('API error: ' . $response_data['error']);
} else {
// Handle successful response
// ...
}
}
}
?>