Formidable Form frm_after_create_entry create a double entry

I’m trying to develop a custom action for FormidableForm that let user go through the helloasso checkout process and register it when redirected to the “thank you” page.

In this action I want to use the “frm_after_create_entry” action hook to get the created entry id and redirect my user on an other page with this id as param.

The problem is that if i use this action the user entry is created twice.

Here’s my code :

<?php

require_once __DIR__ . '/../http-query-functions.php';
require_once __DIR__ . '/helloAsso-actions/HelloAssoAuthenticator.php';

class HelloAssoCheckoutAction extends FrmFormAction
{
    private string $user_form_id;
    private string $access_token;
    private string $data_to_send_for_checkout;
    private array $action_parameters;
    private array $user_form_values;

    function __construct()
    {
        $action_ops = array(
            'classes'   => 'dashicons dashicons-format-aside',
            'limit'     => 99,
            'priority'  => 50,
            'event'  => array('create'),
        );

        $this->FrmFormAction('HelloAssoCheckoutAction', __('HelloAsso Checkout', 'formidable'), $action_ops);

        add_filter('frm_validate_entry', array($this, 'prepare_checkout'), 10, 2);
        add_action('frm_after_create_entry', array($this, 'checkout'), 50, 2);
        add_action('init', array($this, 'register_helloasso_shortcodes'));
    }

    function register_helloasso_shortcodes()
    {
        add_shortcode('helloasso_checkout_status', array($this, 'set_checkout_status'));
    }


    function form($form_action, $args = array())
    {
       //... HTML form to set action parameters
    }


    function get_defaults()
    {
        return array(
            'template_name' => '',
            'my_content' => '',
        );
    }

    function prepare_checkout($errors, $values)
    {
        $this->user_form_id = $values['form_id'];
        $form_actions = FrmFormAction::get_action_for_form($this->user_form_id, 'helloassocheckoutaction');
        if (empty($form_actions)) {
            return;
        }
        $form_action = reset($form_actions);

        $this->action_parameters = $form_action->post_content;
        $this->user_form_values = $values;

        $this->connect_to_hello_asso($this->action_parameters['token_endpoint'], $this->action_parameters['client_id'], $this->action_parameters['client_secret']);

        return $errors;
    }

    private function connect_to_hello_asso($token_endpoint, $client_id, $client_secret)
    {
        $helloAsso_ahthenticator = new HelloAssoAuthenticator();
        $this->access_token = $helloAsso_ahthenticator->get_access_token($token_endpoint, $client_id, $client_secret);
    }


    function checkout($entry_id, $form_id)
    {

        $form_actions = FrmFormAction::get_action_for_form($form_id, 'helloassocheckoutaction');
        if (empty($form_actions)) {
            echo 'No action for this form, return errors';
            return;
        }

        $this->set_post_data($entry_id);


        $this->redirect_to_helloAsso_checkout($this->action_parameters['checkout_endpoint'], $this->access_token, $this->data_to_send_for_checkout);
    }

    private function set_post_data($entry_id)
    {
        $this->data_to_send_for_checkout = json_encode(array(
            "totalAmount" => $this->action_parameters['initial_amount'] * 100, // Amount in cents
            "initialAmount" => $this->action_parameters['initial_amount'] * 100,
            "itemName" => $this->action_parameters['campain_name'],
            "backUrl" => sanitize_url($this->action_parameters['back_url']),
            "errorUrl" => sanitize_url($this->action_parameters['error_url']),
            "returnUrl" => sanitize_url($this->action_parameters['success_url'] . "?entry=" . $entry_id),
            "containsDonation" => true,
            "payer" => array(
                "firstName" => sanitize_text_field($this->user_form_values['item_meta'][$this->action_parameters['name_field']]['first']),
                "lastName" => sanitize_text_field($this->user_form_values['item_meta'][$this->action_parameters['name_field']]['last']),
                "email" => sanitize_email($this->user_form_values['item_meta'][$this->action_parameters['mail_field']]),
                "dateOfBirth" => sanitize_text_field($this->user_form_values['item_meta'][$this->action_parameters['birthdate_field']]),
                "address" => sanitize_text_field($this->user_form_values['item_meta'][$this->action_parameters['address_field']]['line1']),
                "city" => sanitize_text_field($this->user_form_values['item_meta'][$this->action_parameters['address_field']]['city']),
                "zipCode" => sanitize_text_field($this->user_form_values['item_meta'][$this->action_parameters['address_field']]['zip']),
                "country" => "FRA"
            )
        ), true);
    }

    private function redirect_to_helloAsso_checkout($checkout_endpoint, $access_token, $form_data_json)
    {
        $response = create_curl_post_and_return_json_response($checkout_endpoint, $form_data_json, array(
            'Content-Type: application/json',
            'Authorization: Bearer ' . $access_token
        ));

        if (isset($response['error'])) {
            throw new Exception($response['error'] . ' ' . $response['message']);
        }

        header('Location: ' . wp_sanitize_redirect($response['redirectUrl']));
    }

    function set_checkout_status($atts)
    {
        if (isset($_GET['entry']) && isset($_GET['orderId']) && isset($_GET['code'])) {
            echo "paiement status for entry " . $_GET['entry'] . " is " . $_GET['code'] . " for order " . $_GET['orderId'];
            //TODO update the entry status 

        } else {
            echo "no entry id or code or order id";
        }
    }
}

To be sure that it came from the use of frm_after_create_entry

  • I removed it and my entry was saved only once,
  • I also remove the logic and just left an dummy code to be sure that it was not from my code.
  • I tried to add logic to check if the entry was existing to avoid a duplicate but then the entryId used was not the correct in the returnUrl.

I looked for other action that would let me get the entryId but I didn’t find it.

I admit that PHP and wordpress are not my primary stack and I may have used a great deal of LLM to get there.

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