Home / Admin / WP Simple Pay: Custom Server-Side Validation Before Payment Creation
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Custom Server-Side Validation Before Payment Creation

Perform additional custom server-side validation prior to allowing a payment to be created.

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/ro8wgkow/
 */
add_action(
	'simpay_before_payment_create',
	/**
	 * @param \WP_REST_Request WordPress REST API request.
	 */
	function( $request ) {
		// "Stripe Metadata Label" of the custom field.
		$custom_field_name = 'Customer DOB'; 
		
		// Retrieve form values.
		$fields = $request->get_param( 'form_values' );
		
		// Field not found.
		if ( ! isset( $fields['simpay_field'][ $custom_field_name ] ) ) {
			throw new \Exception( 'You must be 18 years or older to purchase.' );
		}
		// Custom validation.
		$dob = strtotime( intval( $fields['simpay_field'][ $custom_field_name ] ) );
		if ( false === $dob || $dob > strtotime( '-18 years' ) ) {
			throw new \Exception( 'You must be 18 years or older to purchase.' );
		}
		// Payment can be created.
	}
);

Comments

Add a Comment