- WordPress core y plugins - Tema Twenty Twenty-Four configurado - Plugin allow-unfiltered-html.php simplificado - .gitignore configurado para excluir wp-config.php y uploads 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
375 lines
14 KiB
PHP
Executable File
375 lines
14 KiB
PHP
Executable File
<?php
|
|
/*
|
|
Plugin Name: Restrict Content Pro - REST API CUSTOM
|
|
Plugin URI: https://tecno-group.mx
|
|
Description: Registrar membresías
|
|
Author: Francisco Zamora Paredes
|
|
Author URI: https://tecno-group.mx
|
|
Version: 1.0
|
|
*/
|
|
|
|
// Constantes para la API de RCP y credenciales de autenticación
|
|
define('RCP_API_URL', 'https://analisisdepreciosunitarios.com/wp-json/rcp/v1/');
|
|
define('RCP_USERNAME', 'site000010');
|
|
define('RCP_PASSWORD', 'D3fS DsYM RFkr Xu25 0oiW uKRB');
|
|
|
|
// Acción que se ejecuta al cargar la página
|
|
add_action('wp_loaded', 'memberships_call_api');
|
|
|
|
function memberships_call_api() {
|
|
|
|
// Verificar si el parámetro "api" en la URL es igual a "memberships"
|
|
if (isset($_GET['api']) && $_GET['api'] === 'memberships') {
|
|
|
|
// Verificar si se recibió una petición POST
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
// Obtener los datos del formulario usando $_POST
|
|
$contact_id = $_POST['contact_id'];
|
|
$user_email = $_POST['user_email'];
|
|
$contact_phone = $_POST['contact_phone'];
|
|
$contact_first_name = $_POST['contact_first_name'];
|
|
$contact_last_name = $_POST['contact_last_name'];
|
|
$order_number = $_POST['order_number'];
|
|
$order_date = $_POST['order_date'];
|
|
$transaction_id = $_POST['transaction_id'];
|
|
$order_table = $_POST['order_table'];
|
|
$order_items_sku = explode(',', $_POST['order_items_sku']);
|
|
$order_total = $_POST['order_total'];
|
|
$object_id = null;
|
|
$subscription_string = '';
|
|
|
|
// Guardar datos en archivo de texto
|
|
$data = array(
|
|
'contact_id' => $contact_id,
|
|
'user_email' => $user_email,
|
|
'contact_phone' => $contact_phone,
|
|
'contact_first_name' => $contact_first_name,
|
|
'contact_last_name' => $contact_last_name,
|
|
'order_number' => $order_number,
|
|
'order_date' => $order_date,
|
|
'transaction_id' => $transaction_id,
|
|
'order_table' => $order_table,
|
|
'order_items_sku' => $_POST['order_items_sku'],
|
|
'order_total' => $order_total,
|
|
);
|
|
|
|
$file_path = plugin_dir_path(__FILE__) . 'api_requests.txt';
|
|
|
|
file_put_contents($file_path, json_encode($data) . PHP_EOL, FILE_APPEND | LOCK_EX);
|
|
|
|
// Imprimir los datos recibidos
|
|
//echo 'Datos recibidos por la API: ' . json_encode($data) . PHP_EOL . PHP_EOL;
|
|
|
|
// Lógica para asignar object_id y subscription_string basado en order_items_sku
|
|
foreach ($order_items_sku as $sku) {
|
|
|
|
if ($sku === 'TGE-167') {
|
|
$object_id = 5;
|
|
$subscription_string = 'Vip Bronce';
|
|
break;
|
|
|
|
} elseif ($sku === 'TGE-168') {
|
|
$object_id = 2;
|
|
$subscription_string = 'Vip Plata';
|
|
break;
|
|
|
|
} elseif ($sku === 'TGE-169') {
|
|
$object_id = 3;
|
|
$subscription_string = 'Vip Oro';
|
|
break;
|
|
|
|
} elseif ($sku === 'TGE-170') {
|
|
$object_id = 4;
|
|
$subscription_string = 'Vip Platino';
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Imprimir $object_id
|
|
echo '$object_id: ' . json_encode($object_id) . PHP_EOL . PHP_EOL;
|
|
|
|
// Verificar si el email ya existe en la base de datos
|
|
$user_id = email_exists($user_email);
|
|
|
|
if (!$user_id) {
|
|
|
|
// El usuario no existe, crear uno nuevo
|
|
$username = strstr($user_email, '@', true) . rand(1, 1000);
|
|
$password = wp_generate_password();
|
|
$user_id = wp_create_user($username, $password, $user_email);
|
|
wp_set_current_user($user_id);
|
|
wp_set_auth_cookie($user_id);
|
|
update_user_meta($user_id, 'first_name', $contact_first_name);
|
|
update_user_meta($user_id, 'last_name', $contact_last_name);
|
|
wp_new_user_notification($user_id, null, 'user');
|
|
|
|
} else {
|
|
|
|
// El usuario ya existe, actualizar datos si es necesario
|
|
wp_set_current_user($user_id);
|
|
wp_set_auth_cookie($user_id);
|
|
update_user_meta($user_id, 'first_name', $contact_first_name);
|
|
update_user_meta($user_id, 'last_name', $contact_last_name);
|
|
}
|
|
|
|
// Agregar el cliente sin verificar si ya existe
|
|
//add_customer($user_id);
|
|
$server_response = add_customer($user_id);
|
|
|
|
// Obtener el customer id
|
|
//$customer_id = get_customer_id($user_email);
|
|
// Verificar si $server_response es un valor positivo entero
|
|
if (is_numeric($server_response) && intval($server_response) > 0) {
|
|
|
|
// Si es un valor positivo entero, asignar directamente a $customer_id
|
|
$customer_id = intval($server_response);
|
|
echo 'El $customer_id es: ' . $customer_id . PHP_EOL . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
// Si no es un valor positivo entero, obtener $customer_id usando get_customer_id
|
|
$customer_id = get_customer_id($user_email);
|
|
echo 'El $customer_id es: ' . $customer_id . PHP_EOL . PHP_EOL;
|
|
|
|
}
|
|
|
|
// Agregar nueva membresía
|
|
//add_new_membership($customer_id, $object_id);
|
|
add_new_membership($customer_id, $object_id, $contact_id, $order_number);
|
|
|
|
// Agregar nuevo pago
|
|
register_payment($user_id, $order_total, $subscription_string, $transaction_id, $status = 'complete');
|
|
|
|
// Enviar email a webhook
|
|
//send_to_webhook($data);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
// Función para agregar un cliente usando la API de RCP
|
|
function add_customer($user_id) {
|
|
|
|
// Endpoint de la API de RCP para agregar un nuevo cliente
|
|
$add_customer_url = RCP_API_URL . 'customers/new';
|
|
|
|
// Datos para el nuevo cliente (usando el user_id proporcionado)
|
|
$new_customer_data = array(
|
|
|
|
'user_id' => $user_id,
|
|
|
|
);
|
|
|
|
// Realizar la solicitud POST para agregar el nuevo cliente
|
|
$add_customer_response = wp_remote_post($add_customer_url, array(
|
|
|
|
'headers' => array(
|
|
|
|
'Authorization' => 'Basic ' . base64_encode(RCP_USERNAME . ':' . RCP_PASSWORD),
|
|
|
|
),
|
|
|
|
'body' => $new_customer_data,
|
|
|
|
));
|
|
|
|
// Obtener la respuesta del servidor
|
|
$server_response = wp_remote_retrieve_body($add_customer_response);
|
|
|
|
// Verificar si la solicitud de agregar el cliente fue exitosa
|
|
if (is_wp_error($add_customer_response)) {
|
|
|
|
// Manejar el error de la solicitud para agregar el cliente y mostrar la respuesta del servidor
|
|
echo 'Error al agregar el cliente: ' . $add_customer_response->get_error_message() . PHP_EOL . PHP_EOL;
|
|
echo 'Respuesta del servidor: ' . $server_response . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
// Cliente agregado exitosamente y mostrar la respuesta del servidor
|
|
echo 'Respuesta del servidor (nuevo cliente agregado): ' . $server_response . PHP_EOL . PHP_EOL;
|
|
}
|
|
|
|
// Devolver el valor de $server_response
|
|
return $server_response;
|
|
}
|
|
|
|
// Función para obtener el ID de un cliente usando la API de RCP
|
|
function get_customer_id($user_email) {
|
|
|
|
// Endpoint de la API de RCP para obtener un cliente
|
|
$api_url = RCP_API_URL . 'customers';
|
|
|
|
// Parámetros para la consulta GET
|
|
$params = array(
|
|
|
|
'user_email' => $user_email,
|
|
|
|
);
|
|
|
|
// Realizar la solicitud GET a la API de RCP
|
|
$response = wp_remote_get(add_query_arg($params, $api_url), array(
|
|
|
|
'headers' => array(
|
|
|
|
'Authorization' => 'Basic ' . base64_encode(RCP_USERNAME . ':' . RCP_PASSWORD),
|
|
|
|
),
|
|
));
|
|
|
|
// Verificar si la solicitud fue exitosa
|
|
if (is_wp_error($response)) {
|
|
|
|
// Manejar el error de la solicitud
|
|
echo 'Error en la solicitud: ' . $response->get_error_message() . PHP_EOL . PHP_EOL;
|
|
return null;
|
|
|
|
}
|
|
|
|
// Obtener el cuerpo de la respuesta
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
// Mostrar la respuesta del servidor en el debug
|
|
echo 'Respuesta del servidor al consultar el customer_id: ' . $body . PHP_EOL . PHP_EOL;
|
|
|
|
// Decodificar la respuesta JSON
|
|
$customer_data = json_decode($body, true);
|
|
|
|
// Verificar si se encontró el cliente
|
|
if (!empty($customer_data) && isset($customer_data['id'])) {
|
|
|
|
// Cliente encontrado, devolver el customer_id como un número entero
|
|
return intval($customer_data['id']);
|
|
|
|
} else {
|
|
|
|
// Cliente no encontrado, devolver null
|
|
return null;
|
|
|
|
}
|
|
}
|
|
|
|
// Función para agregar una nueva membresía usando la API de RCP
|
|
function add_new_membership($customer_id, $object_id, $contact_id, $order_number) {
|
|
|
|
// Endpoint de la API de RCP para agregar una nueva membresía
|
|
$api_url = RCP_API_URL . 'memberships/new';
|
|
|
|
// Datos para la nueva membresía
|
|
$membership_data = array(
|
|
|
|
'customer_id' => $customer_id,
|
|
'object_id' => $object_id,
|
|
'gateway_customer_id' => $contact_id,
|
|
'gateway_subscription_id' => $order_number,
|
|
'status' => 'active'
|
|
|
|
);
|
|
|
|
// Mostrar los datos que se enviarán a la API en el debug
|
|
echo 'Datos enviados a la API (agregar membresía): ' . json_encode($membership_data) . PHP_EOL . PHP_EOL;
|
|
|
|
// Realizar la solicitud POST a la API de RCP para agregar la membresía
|
|
$response = wp_remote_post($api_url, array(
|
|
|
|
'headers' => array(
|
|
|
|
'Authorization' => 'Basic ' . base64_encode(RCP_USERNAME . ':' . RCP_PASSWORD),
|
|
|
|
),
|
|
|
|
'body' => $membership_data,
|
|
|
|
));
|
|
|
|
// Verificar si la solicitud fue exitosa y devolver la respuesta como un echo
|
|
if (is_wp_error($response)) {
|
|
|
|
// Manejar el error de la solicitud y devolverlo como un echo
|
|
echo 'Error al agregar la membresía: ' . $response->get_error_message() . PHP_EOL . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
// Membresía agregada exitosamente y devolver la respuesta del servidor como un echo
|
|
echo 'Respuesta del servidor (agregar membresía): ' . wp_remote_retrieve_body($response) . PHP_EOL . PHP_EOL;
|
|
|
|
}
|
|
}
|
|
|
|
function register_payment($user_id, $order_total, $subscription_string, $transaction_id, $status = 'complete') {
|
|
|
|
// Endpoint de la API de RCP para registrar un nuevo pago
|
|
$api_url = RCP_API_URL . 'payments/new';
|
|
|
|
// Datos para el nuevo pago
|
|
$payment_data = array(
|
|
'user_id' => $user_id,
|
|
'amount' => $order_total,
|
|
'subscription' => $subscription_string,
|
|
'transaction_id' => $transaction_id,
|
|
'status' => $status,
|
|
'gateway' => 'manual'
|
|
);
|
|
|
|
// Mostrar los datos que se enviarán a la API en el debug
|
|
echo 'Datos enviados a la API (registrar pago): ' . json_encode($payment_data) . PHP_EOL . PHP_EOL;
|
|
|
|
// Realizar la solicitud POST a la API de RCP para registrar el pago
|
|
$response = wp_remote_post($api_url, array(
|
|
|
|
'headers' => array(
|
|
'Authorization' => 'Basic ' . base64_encode(RCP_USERNAME . ':' . RCP_PASSWORD),
|
|
),
|
|
|
|
'body' => $payment_data,
|
|
));
|
|
|
|
// Verificar si la solicitud fue exitosa y devolver la respuesta como un echo
|
|
if (is_wp_error($response)) {
|
|
|
|
// Manejar el error de la solicitud y devolverlo como un echo
|
|
echo 'Error al registrar el pago: ' . $response->get_error_message() . PHP_EOL . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
// Pago registrado exitosamente y devolver la respuesta del servidor como un echo
|
|
echo 'Respuesta del servidor (registrar pago): ' . wp_remote_retrieve_body($response) . PHP_EOL . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
// Función para enviar todos los datos del pedido a webhook con debug y guardar en archivo
|
|
function send_to_webhook($data) {
|
|
|
|
//$webhook_url = "https://webhook.site/59f0499f-93ea-4a7b-becd-1a870bdb0ec2";
|
|
//$webhook_url = "https://webhook.site/59f0499f-93ea-4a7b-becd-1a870bdb0ec2";
|
|
|
|
// Mostrar los datos que se enviarán a la API en el debug
|
|
echo 'Datos enviados al webhook (pedido completado): ' . json_encode($data) . PHP_EOL . PHP_EOL;
|
|
|
|
// Guardar los datos que se envían al webhook en un archivo
|
|
$file_path_request = plugin_dir_path(__FILE__) . 'api_requests.txt';
|
|
file_put_contents($file_path_request, 'Datos a enviar al webhook: ' . json_encode($data) . PHP_EOL, FILE_APPEND | LOCK_EX);
|
|
|
|
$ch = curl_init($webhook_url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Convertir los datos a JSON
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
'Content-Type: application/json',
|
|
'Content-Length: ' . strlen(json_encode($data))
|
|
));
|
|
|
|
// Realizar la solicitud al webhook
|
|
$webhook_response = curl_exec($ch);
|
|
|
|
// Debug: Imprimir la respuesta del webhook
|
|
echo 'Respuesta del webhook: ' . $webhook_response . PHP_EOL;
|
|
|
|
// Guardar la respuesta del webhook en un archivo
|
|
$file_path_response = plugin_dir_path(__FILE__) . 'api_requests.txt';
|
|
file_put_contents($file_path_response, 'Respuesta del webhook: ' . $webhook_response . PHP_EOL, FILE_APPEND | LOCK_EX);
|
|
|
|
// Cierra la conexión
|
|
curl_close($ch);
|
|
} |