probably very simple question, but having no knowledge, it remains difficult for me to implement this thing in my project.
I have a custom webhook, implemented on WordPress, which allows me to receive responses from Apple, based on in-app purchases.
The webhook works, correctly receives and saves all the requested values.
The only problem remains the redirect.
Below they explained to me that I should load the load_url, to be able to redirect the user to the desired page at the end of the purchase in the app.
this is my code:
Front end:
– I call my product to purchase using a js function, where there is product_id and user_email:
<button onclick="twinr_iap_purchase('PRODUCT_ID','EMAIL_USER')">Product</button>
when pressed, the entire applem flow opens which allows me to make the purchase correctly.
Backend:
– webhook data, which I receive perfectly:
// Definizione dell'azione per il webhook
function custom_webhook_action() {
// Controlla se la richiesta è una richiesta POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Ottieni i dati inviati con la richiesta
$data = json_decode(file_get_contents('php://input'), true);
// Verifica se sono presenti i dati richiesti
if (isset($data['twinr_product_id']) && isset($data['store_product_id']) && isset($data['platform']) && isset($data['purchase_token']) && isset($data['success']) && isset($data['user_info'])) {
// Esegui qui le azioni necessarie con i dati ricevuti dal webhook
// Ad esempio, puoi salvare i dati nel database, inviare notifiche, ecc.
// Esempio: Salvataggio dei dati nel database
global $wpdb;
// Estrai i dati dalla richiesta
$twinr_product_id = $data['twinr_product_id'];
$store_product_id = $data['store_product_id'];
$platform = $data['platform'];
$purchase_token = $data['purchase_token'];
$success = $data['success'];
$user_info = $data['user_info'];
// Prepara il messaggio di log per la risposta
$response_message = "Funzione twinr_iap_purchase chiamata per il prodotto Twinr con ID: $twinr_product_id";
// Prepara i dati per l'inserimento nel database
$data_to_insert = array(
'twinr_product_id' => $twinr_product_id,
'store_product_id' => $store_product_id,
'platform' => $platform,
'purchase_token' => $purchase_token,
'success' => $success,
'user_info' => $user_info,
'response' => $response_message, // Messaggio di log inserito nel campo response
);
// Esegui l'inserimento dei dati nella tabella del database
$wpdb->insert('swh_tlk_webhook_requests', $data_to_insert);
// Ottieni l'ID dell'inserimento appena effettuato
$request_id = $wpdb->insert_id;
// Chiamata alla funzione twinr_iap_purchase
twinr_iap_purchase($twinr_product_id);
// Cambia la risposta in base ai dati ricevuti
if ($success) {
// Se l'acquisto ha avuto successo, invia una risposta di successo a Twinr
$response_data = array(
'success' => true,
'data' => 'Purchase completed successfully.',
'load_url' => 'https://www.yourwebsite.xxx/' // Includi il campo load_url solo se l'acquisto è completato con successo
);
} else {
// Se l'acquisto ha fallito, invia una risposta di errore a Twinr
$response_data = array(
'success' => false,
'data' => 'Purchase failed.'
);
}
// Aggiorna la colonna 'response' nella tabella con il messaggio di risposta
$wpdb->update('swh_tlk_webhook_requests', array('response' => json_encode($response_data)), array('id' => $request_id));
// Invia una risposta al webhook
wp_send_json_success($response_data);
} else {
// Invia un messaggio di errore se i dati richiesti non sono presenti
wp_send_json_error('Missing required data in the webhook request.');
}
} else {
// Invia un messaggio di errore se la richiesta non è una richiesta POST
wp_send_json_error('Invalid request method. Only POST requests are allowed.');
}
}
// Registra l'azione per il webhook
add_action('wp_ajax_custom_webhook', 'custom_webhook_action');
add_action('wp_ajax_nopriv_custom_webhook', 'custom_webhook_action');
the response data is provided to me in this json:
{
"twinr_product_id": "TWINR_PRODUCT_ID",
"store_product_id": "STORE_PRODUCT_ID",
"platform": "android or ios",
"purchase_token": "PURCHASE_RECEIPT",
"success": true //true or false,
"user_info": "USERNAME",
}
e mi hanno spiegato che la risposta dovrebbe essere da:
{
"success": true,
"data": "Purchase completed successfully."
}
to
{
"success": true,
"data": "Purchase completed successfully.",
"load_url": "https://www.redirect.yoursite/"
}
I tried to redirect to load_url, in the inserted code but without success.
What am I doing wrong?
AMCode is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.