Search code examples
javascriptphppluginscontact-form-7

Change dynamically CF7 Stripe module amount depending on user selected price


With the Contact Form 7’s Stripe integration module I would like to customize the form tag amount depending on the user selected checkbox value.

<input type="checkbox" name="checkboxPrice" value="10€">
<input type="checkbox" name="checkboxPrice" value="30€">

[stripe currency:eur amount:checkboxPriceValue "Proceed to checkout" "Pay"]

As mentionned here it's possible to dynamically replace the tripe tag amount depending on the user selected choice but i'm not able to do it.

add_filter(
    'wpcf7_stripe_payment_intent_parameters',
 
    function ( $params ) {
 
        // Get the WPCF7_Submission object
        $submission = WPCF7_Submission::get_instance();
 
        // Retrieve the currency from the 'your-currency' field value
        $currency = (array) $submission->get_posted_data( 'your-currency' );
        $currency = (string) array_shift( $currency );
        $params['currency'] = strtolower( $currency );
 
        // Calculate the amount
        $amount = checkboxPriceValue;
        $params['amount'] = $amount;
 
        $receipt_email = $submission->get_posted_data( 'your-email' );
 
        if ( is_email( $receipt_email ) ) {
            $params['receipt_email'] = $receipt_email;
        }
 
        // See https://stripe.com/docs/api/payment_intents/create
        // for the full list of available parameters
 
        return $params;
    },
 
    10, 1
);

Any help would be appreciated !


Solution

  • Now i get the correct amount depending on the user selected checkbox on submit and it's succesfully passed to Stripe !

    Here is the Stripe shortcode I use :

    **[stripe currency:eur amount:1000 "Go to checkout" "Pay !"]**
    

    Now checkbox is used to define dynamically Stripe price, only two choice for me with this shortcode :

    **[checkbox* checkbox-price class:checkbox-price-unit use_label_element exclusive default:1 "10€" "18€"]**
    
    
    
    $('input:checkbox[name=checkbox-price]').on('change', function(){
    
                let $checkboxPriceItem = $('input:checkbox[name=checkbox-price]'),
                    $stripeAmount      = $('#stripeAmount');
    
                if ( $checkboxPriceItem[0].checked ) {
                    $stripeAmount.val('1000');
                } else if ( $checkboxPriceItem[1].checked ) {
                    $stripeAmount.val('1800');
                }   
    });
    
    // The stripe field with the price value
    <input type="hidden" name="stripeAmount" value="1000" id="stripeAmount">
    
    // Customize stripe price 
    add_filter('wpcf7_stripe_payment_intent_parameters',
     
        function ( $params ) {
     
            // Get the WPCF7_Submission object
            $submission = WPCF7_Submission::get_instance();
      
            // Retrieve the currency from the 'your-currency' field value
            //$currency = (array) $submission->get_posted_data( 'your-currency' );
            //$currency = (string) array_shift( $currency );
    
            // Put your currency here in uppercase
            $currency = 'EUR'; 
            $params['currency'] = strtolower( $currency );
    
            // get value from input name=stripeAmount
            $amount = (array) $submission->get_posted_data( 'stripeAmount' );
            $amount = (string) array_shift( $amount );
    
            // Optional, add security so that user can't change this value with JS 
            if ( $amount == 1000 || $amount == 1800) {
              $params['amount'] = $amount;
            } else {
              return false;
            }
    
            // Retrieve the buyer's email from the 'your-email' field value
            $receipt_email = $submission->get_posted_data( 'your-email' );
     
            if ( is_email( $receipt_email ) ) {
                $params['receipt_email'] = $receipt_email;
            }
     
            // See https://stripe.com/docs/api/payment_intents/create
            // for the full list of available parameters
     
            return $params;
        },
     
        10, 1
    );