Quantcast
Channel: Takayuki Miyoshi – Contact Form 7
Viewing all articles
Browse latest Browse all 164

Customizing Stripe payment parameters

$
0
0

Stripe‘s payment parameters, such as the currency or the amount of money, are decided by the [stripe] form-tag placed in the form template. You might want to customize the parameters by changing them dynamically based on the user’s selection or some sort of calculation.

You can do it by using the wpcf7_stripe_payment_intent_parameters filter hook like in the following coding example:

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 = 1000 * SOME_CONSTANT;
		$params['amount'] = $amount;

		// Retrieve the buyer's email from the 'email-123' field value
		$receipt_email = $submission->get_posted_data( 'email-123' );

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

Viewing all articles
Browse latest Browse all 164