Add format validation for Merchant ID field

This commit is contained in:
Pedro Silva 2023-07-11 18:25:37 +01:00
parent c963a79b47
commit 512150a889
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
2 changed files with 45 additions and 21 deletions

View file

@ -245,18 +245,22 @@ return function ( ContainerInterface $container, array $fields ): array {
'gateway' => Settings::CONNECTION_TAB_ID, 'gateway' => Settings::CONNECTION_TAB_ID,
), ),
'merchant_id_production' => array( 'merchant_id_production' => array(
'title' => __( 'Live Merchant Id', 'woocommerce-paypal-payments' ), 'title' => __( 'Live Merchant Id', 'woocommerce-paypal-payments' ),
'classes' => array( State::STATE_ONBOARDED === $state->production_state() ? 'onboarded' : '', 'ppcp-always-shown-element' ), 'classes' => array( State::STATE_ONBOARDED === $state->production_state() ? 'onboarded' : '', 'ppcp-always-shown-element' ),
'type' => 'ppcp-text-input', 'type' => 'ppcp-text-input',
'desc_tip' => true, 'desc_tip' => true,
'description' => __( 'The merchant id of your account ', 'woocommerce-paypal-payments' ), 'description' => __( 'The merchant id of your account. Should be exactly 13 alphanumeric uppercase letters.', 'woocommerce-paypal-payments' ),
'default' => false, 'maxlength' => 13,
'screens' => array( 'custom_attributes' => array(
'pattern' => '[A-Z0-9]{13}',
),
'default' => false,
'screens' => array(
State::STATE_START, State::STATE_START,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID, 'gateway' => Settings::CONNECTION_TAB_ID,
), ),
'client_id_production' => array( 'client_id_production' => array(
'title' => __( 'Live Client Id', 'woocommerce-paypal-payments' ), 'title' => __( 'Live Client Id', 'woocommerce-paypal-payments' ),
@ -303,18 +307,22 @@ return function ( ContainerInterface $container, array $fields ): array {
'gateway' => Settings::CONNECTION_TAB_ID, 'gateway' => Settings::CONNECTION_TAB_ID,
), ),
'merchant_id_sandbox' => array( 'merchant_id_sandbox' => array(
'title' => __( 'Sandbox Merchant Id', 'woocommerce-paypal-payments' ), 'title' => __( 'Sandbox Merchant Id', 'woocommerce-paypal-payments' ),
'classes' => array( State::STATE_ONBOARDED === $state->sandbox_state() ? 'onboarded' : '', 'ppcp-always-shown-element' ), 'classes' => array( State::STATE_ONBOARDED === $state->sandbox_state() ? 'onboarded' : '', 'ppcp-always-shown-element' ),
'type' => 'ppcp-text-input', 'type' => 'ppcp-text-input',
'desc_tip' => true, 'desc_tip' => true,
'description' => __( 'The merchant id of your account ', 'woocommerce-paypal-payments' ), 'description' => __( 'The merchant id of your account. Should be exactly 13 alphanumeric uppercase letters.', 'woocommerce-paypal-payments' ),
'default' => false, 'maxlength' => 13,
'screens' => array( 'custom_attributes' => array(
'pattern' => '[A-Z0-9]{13}',
),
'default' => false,
'screens' => array(
State::STATE_START, State::STATE_START,
State::STATE_ONBOARDED, State::STATE_ONBOARDED,
), ),
'requirements' => array(), 'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID, 'gateway' => Settings::CONNECTION_TAB_ID,
), ),
'client_id_sandbox' => array( 'client_id_sandbox' => array(
'title' => __( 'Sandbox Client Id', 'woocommerce-paypal-payments' ), 'title' => __( 'Sandbox Client Id', 'woocommerce-paypal-payments' ),

View file

@ -260,17 +260,33 @@ class SettingsRenderer {
return $field; return $field;
} }
// Custom attribute handling.
$custom_attributes = array();
$config['custom_attributes'] = array_filter( (array) $config['custom_attributes'], 'strlen' );
if ( $config['maxlength'] ) {
$config['custom_attributes']['maxlength'] = absint( $config['maxlength'] );
}
if ( ! empty( $config['custom_attributes'] ) ) {
foreach ( $config['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
$html = sprintf( $html = sprintf(
'<input '<input
type="text" type="text"
autocomplete="off" autocomplete="off"
class="%s" class="input-text %s"
name="%s" name="%s"
value="%s" value="%s"
>', %s
>',
esc_attr( implode( ' ', $config['class'] ) ), esc_attr( implode( ' ', $config['class'] ) ),
esc_attr( $key ), esc_attr( $key ),
esc_attr( $value ) esc_attr( $value ),
implode( ' ', $custom_attributes )
); );
return $html; return $html;