根据用户选择的价格动态更改 CF7 Stripe 模块数量

Change dynamically CF7 Stripe module amount depending on user selected price

使用 Contact Form 7 Stripe integration module 我想根据用户选择的复选框值自定义表单标签数量。

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

[条纹currency:eur amount:checkboxPriceValue“继续结账”“支付”]

如前所述here可以根据用户选择动态替换牛肚标签数量,但我无法做到。

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
);

如有任何帮助,我们将不胜感激!

现在我根据用户在提交时选中的复选框获得正确的金额,并且已成功传递给 Stripe!

这是我使用的 Stripe 简码:

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

现在复选框用于动态定义 Stripe 价格,这个简码对我来说只有两个选择:

**[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
);