I have a small problem dictated by my lack of experience.
I’m trying to create a webhook on my wordpress platform, to receive the response from Aplle, when a user makes an in app purchase.
My iOS app was created through a conversion thanks to Twinr services.
Once I set up my in app purchase on applestoreconnect with ID: TWIAP754
through this link, I can direct the user to purchase in app:
<a href="inapppurchase://twinr.dev?product_id=TWIAP754" id="purchaseLink">Plan: basci</a>
The problem lies in the fact that the user presses buy but there is no communication between apple and my webhook
I write everything I have done so that it can be useful to the community.
1. Being on wordpress, I installed the plugin: WP Webhooks.
2. The parameters of my webhooks are:
Name: main_5064
URL: https://www.swheel.net/?wpwhpro_action=main_5064&wpwhpro_api_key=pn0ojen4sj9azunbmclemgz7zwo5y7zorw3aoaptnbbuev4vzavzoh40u8njmgkk
Api key: pn0ojen4sj9azunbmclemgz7zwo5y7zorw3aoaptnbbuev4vzavzoh40u8njmgkk
3. In my page: page-test.php, this is what the code does:
Session Initialization: The session_start() function is called to start a PHP session, which is required if you want to save or access session data during script execution.
Function for sending POST requests to webhook: The send_webhook_request($data) function is responsible for sending a POST request to the specified webhook endpoint. Receives the data to send as the $data parameter and sends it to the webhook URL.
Function for saving data to database: The save_purchase_data_to_database($data) function is responsible for saving the data received from the POST request to the database. It takes the data to be saved as the $data parameter and inserts it into the specified database table.
Checking the POST request: The next section of the code checks whether a POST request was made using $_SERVER[‘REQUEST_METHOD’]. If the request is a POST, the data is received via $_POST.
Sending data to the webhook and saving to the database: If POST data has been received, it is sent to the webhook via the send_webhook_request() function and simultaneously saved to the database via the save_purchase_data_to_database() function.
Generating the HTML page: The final part of the code generates an HTML page that includes text information and a “save” link which, when clicked, generates a POST request containing the specific purchase data.
WordPress Theme Integration: The code is designed as a custom page template for WordPress, so it includes get_header() and get_footer() functions to include the header and footer of the current WordPress theme.
code:
<?php
/*
Template Name: Test
*/
// Inizializza la sessione
session_start();
// Funzione per inviare la richiesta POST all'endpoint del webhook
function send_webhook_request($data) {
// URL dell'endpoint del webhook
$webhook_url = 'https://www.swheel.net/?wpwhpro_action=main_5064&wpwhpro_api_key=pn0ojen4sj9azunbmclemgz7zwo5y7zorw3aoaptnbbuev4vzavzoh40u8njmgkk';
// Configura i dati per la richiesta POST
$post_data = array(
'product_id' => $data['product_id'] // Sostituisci con l'ID del prodotto corretto
// Puoi aggiungere altri dati se necessario
);
// Configura le opzioni della richiesta POST
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode($post_data)
)
);
// Crea il contesto della richiesta
$context = stream_context_create($options);
// Invia la richiesta POST all'endpoint del webhook
$response = file_get_contents($webhook_url, false, $context);
// Verifica se la richiesta è stata eseguita con successo
if ($response === false) {
return false; // Ritorna false se la richiesta ha avuto esito negativo
} else {
return true; // Ritorna true se la richiesta ha avuto esito positivo
}
}
// Funzione per salvare i dati nel database
function save_purchase_data_to_database($data) {
global $wpdb;
// Nome della tabella nel database
$table_name = 'adt_tlk_webhook_requests';
// Dati da salvare nel database
$purchase_data = array(
'twinr_product_id' => $data['product_id'], // Sostituisci con il nome corretto del campo nel tuo database
'response' => json_encode($data) // Salva la risposta come JSON nel campo 'response'
// Aggiungi altri campi se necessario
);
// Formato dei dati da inserire
$data_format = array(
'%s', // Formato per 'twinr_product_id'. Assicurati di usare il formato corretto per ogni campo nel tuo database
'%s' // Formato per 'response'
// Aggiungi altri formati se necessario
);
// Inserisci i dati nel database
$result = $wpdb->insert($table_name, $purchase_data, $data_format);
// Verifica se l'inserimento è avvenuto con successo
if ($result === false) {
// Visualizza eventuali errori
echo $wpdb->last_error;
return false; // Ritorna false se c'è stato un errore nell'inserimento
} else {
return true; // Ritorna true se l'inserimento è avvenuto con successo
}
}
// Verifica se è stata effettuata una richiesta POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Ricevi i dati dal POST
$purchaseData = $_POST;
// Verifica che siano stati ricevuti i dati dal POST
if (!empty($purchaseData)) {
// Invia i dati al webhook
$result = send_webhook_request($purchaseData);
// Salva i dati nel database
$db_result = save_purchase_data_to_database($purchaseData);
// Stampa un messaggio di successo o di errore
if ($result && $db_result) {
echo "Webhook inviato con successo e dati salvati nel database.";
} else {
echo "Si è verificato un errore durante l'invio del webhook o il salvataggio dei dati nel database.";
}
// Termina lo script per evitare il caricamento della pagina
exit();
}
}
// Se non è stata effettuata una richiesta POST, continua a generare la pagina normale
get_header();
?>
<main style="margin-top: 58px;">
<div class="container pt-4">
<div class="row">
<div style="color: #121314; font-size: 15px; text-align: center" class="col-md-12">
<h5 class="text-muted" style="padding-bottom: 15px;">Test</h5>
</div>
<div class="col-md-12 has-text-align-center">
<div class="card">
<div class="card-body">
<?php
echo $_SERVER['HTTP_USER_AGENT'].'<br>';
if (is_android_app()) {
echo "L'utente sta navigando da un'app Android.";
} elseif (is_ios_app()) {
echo "L'utente sta navigando da un'app iOS.";
} elseif (is_browser()) {
echo "L'utente sta navigando da un browser.";
}
?>
</div>
</div>
</div>
<div>
<h3>Test</h3>
<h1>Test Acquistoooo In-App</h1>
<!-- Link per l'acquisto in-app -->
<a href="inapppurchase://twinr.dev?product_id=TWIAP754" id="purchaseLink">Plan: basci</a>
</div>
</div>
</div>
</main>
<?php
// Include il piè di pagina del tema
get_footer();
?>
What I get:
1. User clicks on the link
2. In app purchase opens
3. User enters their credentials
4. Purchase message succeeded perfectly
– But it doesn’t save the data in the db
– No webhook response.
What am I doing wrong?
what should I do?
AMCode is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.