-
I used a Twinr service, which performs a conversion of my website, I publish the test area (https://adt.swheel.net/), into two apps: for ios and android.
-
The Twinr company publishes the guide on their website to adapt to IOS in app purchases (https://help.twinr.dev/extensions/in-app-purchase-iap)
-
All the parameters required by the application have been satisfied, obviously including the product settings on both the Twinr and IOS sides.
-
To manage and direct purchases in the app after having set up my custom user agent, to recognize when a user is from the web or from an iOS or Android app, and having downloaded the WP Webhook plugin and inserted the URL of my webhook into Twinr in the their dedicated space Response submission URL.
The desired behavior should be:
- User chooses the paid plan
- is taken to the checkout page, where there is a cf7 form dedicated to the iOS plan.
- The user makes the purchase
- You are directed to the payment success page.
What I get:
- User fills out the form
- The data is not recorded in the database
- The user can purchase the product
- But once the purchase is completed and I receive confirmation from Apple that the product has been correctly purchased, the user is not redirected.
Below, on the backend side, I write the code of the cf7 module:
[email* email class:form-control mb-4]
[dynamic_hidden codice_univoco "CF7_GET key='cod'"]
[dynamic_hidden prezzo "1.00"]
[dynamic_hidden TwinrID "TWIAP687"]
<input type="submit" value="Invia IOS" class="wpcf7-form-control wpcf7-submit custom-submit-btn" data-module-id="Basic_IOS">
In my functions,php I call the dedicated file to save the data in the db, of the cf7 module with ID: 63:
include_once dirname(__FILE__) . '/moduli_ios/basic.php';
moduli_ios>basic.php:
<?php
// Inizializza la sessione
if (!session_id()) {
session_start();
// Setta i valori nella sessione
$_SESSION['verification_code'] = $_SESSION['verification_code'];
$_SESSION['id_piattaforma'] = $_SESSION['id_piattaforma'];
$_SESSION['id_servizio'] = $_SESSION['id_servizio'];
$_SESSION['id_piano'] = $_SESSION['id_piano'];
$_SESSION['link'] = $_SESSION['link'];
}
// Aggiungi l'hook per gestire l'invio del modulo CF7
add_action("wpcf7_before_send_mail", "wpcf7_piano_basic_ios");
// Funzione per gestire l'invio del modulo CF7
function wpcf7_piano_basic_ios($cf7) {
// Dettagli del database esterno
$username = 'XXX';
$password = 'XXX!';
$database = 'XXX';
$host = 'localhost';
// Creazione di un'istanza della classe wpdb
global $wpdb;
// Crea una nuova istanza di wpdb
$mydb = new wpdb($username, $password, $database, $host);
// Limita l'hook per farlo scattare solo su un particolare ID di modulo (opzionale)
if ($cf7->id() == 63) {
// Codice aggiunto per WordPress 4.9 2018
$submission = WPCF7_Submission::get_instance();
$data_instagram_reel_piano_gratuito = $submission->get_posted_data();
// Recupera i valori dalla sessione
$id_piattaforma = isset($_SESSION['id_piattaforma']) ? intval($_SESSION['id_piattaforma']) : 0;
$id_servizio = isset($_SESSION['id_servizio']) ? intval($_SESSION['id_servizio']) : 0;
$id_piano = isset($_SESSION['id_piano']) ? intval($_SESSION['id_piano']) : 0;
$link = isset($_SESSION['link']) ? sanitize_text_field($_SESSION['link']) : '';
// Recupera i valori dal modulo
$cf7_email = isset($data_instagram_reel_piano_gratuito["email"]) ? sanitize_email($data_instagram_reel_piano_gratuito["email"]) : '';
$cf7_cod_gioco = $data_instagram_reel_piano_gratuito["codice_univoco"];
// Ottieni data e ora correnti
$current_date = date('Y-m-d');
date_default_timezone_set('Europe/Rome');
$current_time = date('H:i:s');
$gratuito = "GRATUITO";
// Esegui l'inserimento nel database esterno con i valori del modulo e della sessione
$tabella_acquisto = $wpdb->prefix . 'tlk_acquisti';
$result = $mydb->insert(
$tabella_acquisto,
array(
'tlk_id_piattaforma' => $id_piattaforma,
'tlk_id_servizio' => $id_servizio,
'tlk_id_piano' => $id_piano,
'tlk_link' => $link,
'tlk_codice_transazione' => $cf7_cod_gioco,
'tlk_email' => $cf7_email,
'tlk_data_transazione' => $current_date,
'tlk_token_paypal' => $gratuito,
'tlk_ora_transazione' => $current_time
),
array(
'%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s'
)
);
if ($result) {
// Chiamata webhook
$webhook_data = array(
'twinr_product_id' => 'TWIAP687',
'store_product_id' => 'TWIAP687',
'platform' => 'ios', // O 'android' a seconda della piattaforma
'purchase_token' => 'RICEVUTA_ACQUISTO', // Aggiungi qui il token di acquisto
'success' => true, // Cambia questo valore in base all'esito dell'acquisto
'user_info' => $cf7_email // Aggiungi qui le informazioni sull'utente
);
// Simula una richiesta POST al webhook con i dati di Twinr
$_POST['data'] = json_encode($webhook_data);
// Richiama la funzione wpwh_fire_my_custom_logic_basic_ios con i dati simulati
wpwh_fire_my_custom_logic_basic_ios($webhook_data);
} else {
// Output di un messaggio di errore se il salvataggio nel database fallisce
echo '<script>alert("Si è verificato un errore durante il salvataggio dei dati. Si prega di riprovare più tardi.");</script>';
}
}
}
// Aggiungi un'azione per includere lo script JavaScript nel footer del sito
add_action('wp_footer', 'custom_purchase_redirect_script_basic');
function custom_purchase_redirect_script_basic() {
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
var submitButton = document.querySelector('.wpcf7-submit[data-module-id="Basic_IOS"]');
submitButton.addEventListener("click", function(event) {
event.preventDefault();
// Codice per aprire l'URL di acquisto nell'app iOS
var iosURL = "inapppurchase://twinr.dev?product_id=TWIAP687";
window.location.href = iosURL;
});
});
</script>
<?php
}
// Funzione per gestire il webhook custom_action
function wpwh_fire_my_custom_logic_basic_ios($webhook_data) {
// Verifica se l'URL del webhook corrisponde a quello atteso
$expected_webhook_url = 'https://adt.swheel.net/?wpwhpro_action=main_1088&wpwhpro_api_key=9wfwo6dwddxiwfqmw5z8mesqv951essva3kb5befstle7dd30pxmkqfojhfsjnfw';
if ($_SERVER['REQUEST_URI'] !== $expected_webhook_url) {
return;
}
// Qui inserisci la tua logica per il reindirizzamento in base allo stato dell'acquisto
if ($webhook_data['success'] === true) {
// Reindirizza l'utente alla pagina di pagamento effettuato
wp_redirect('https://adt.swheel.net/pagamento-effettuato');
exit;
} else {
// Reindirizza l'utente alla pagina di pagamento non effettuato
wp_redirect('https://adt.swheel.net/pagamento-non-effettuato');
exit;
}
}
?>
I don’t understand what I’m doing wrong, but doing some tests, I realize that this piece of code:
// Aggiungi un'azione per includere lo script JavaScript nel footer del sito
add_action('wp_footer', 'custom_purchase_redirect_script_basic');
function custom_purchase_redirect_script_basic() {
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
var submitButton = document.querySelector('.wpcf7-submit[data-module-id="Basic_IOS"]');
submitButton.addEventListener("click", function(event) {
event.preventDefault();
// Codice per aprire l'URL di acquisto nell'app iOS
var iosURL = "inapppurchase://twinr.dev?product_id=TWIAP687";
window.location.href = iosURL;
});
});
</script>
<?php
}
if I put it as in the example it directs the user to purchase in the Apple app, but without making me record the data in the database.
If I remove it and write another way to open this link, it doesn’t open it, but saves the data in the db.
In both cases, however, there is no redirection of the user after the purchase.
What am I doing wrong to achieve:
- User fills out form
- Data recorded in the database
- Call wordpress webhook
- Opening purchase in app
- Purchase completed redirection
Thank you
All tests are written in the application
AMCode is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.