Implement redirect in the webhook response

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?

New contributor

AMCode is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật