Add custom data for payment method modals

This commit is contained in:
Emili Castells Guasch 2025-01-22 17:26:59 +01:00
parent 3ea8393c1c
commit 381b46f444
3 changed files with 167 additions and 5 deletions

View file

@ -76,6 +76,9 @@ return array(
$container->get( 'settings.service.sanitizer' ) $container->get( 'settings.service.sanitizer' )
); );
}, },
'settings.data.payment' => static function ( ContainerInterface $container ) : PaymentSettings {
return new PaymentSettings();
},
'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint { 'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint {
return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) ); return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) );
}, },
@ -83,7 +86,7 @@ return array(
return new CommonRestEndpoint( $container->get( 'settings.data.general' ) ); return new CommonRestEndpoint( $container->get( 'settings.data.general' ) );
}, },
'settings.rest.payment' => static function ( ContainerInterface $container ) : PaymentRestEndpoint { 'settings.rest.payment' => static function ( ContainerInterface $container ) : PaymentRestEndpoint {
return new PaymentRestEndpoint(); return new PaymentRestEndpoint($container->get('settings.data.payment'));
}, },
'settings.rest.styling' => static function ( ContainerInterface $container ) : StylingRestEndpoint { 'settings.rest.styling' => static function ( ContainerInterface $container ) : StylingRestEndpoint {
return new StylingRestEndpoint( return new StylingRestEndpoint(

View file

@ -0,0 +1,110 @@
<?php
/**
* Payment methods settings class
*
* @package WooCommerce\PayPalCommerce\Settings\Data
*/
declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings\Data;
class PaymentSettings extends AbstractDataModel {
/**
* Option key where profile details are stored.
*
* @var string
*/
protected const OPTION_KEY = 'woocommerce-ppcp-data-payment';
/**
* Get default values for the model.
*
* @return array
*/
protected function get_defaults(): array {
return array(
'paypal_show_logo' => false,
'three_d_secure' => 'no-3d-secure',
'fastlane_cardholder_name' => false,
'fastlane_display_watermark' => false,
);
}
/**
* Get PayPal show logo.
*
* @return bool
*/
public function get_paypal_show_logo(): bool {
return (bool) $this->data['paypal_show_logo'];
}
/**
* Get 3DSecure.
*
* @return string
*/
public function get_three_d_secure(): string {
return $this->data['three_d_secure'];
}
/**
* Get Fastlane cardholder name.
*
* @return bool
*/
public function get_fastlane_cardholder_name(): bool {
return (bool) $this->data['fastlane_cardholder_name'];
}
/**
* Get Fastlane display watermark.
*
* @return bool
*/
public function get_fastlane_display_watermark(): bool {
return (bool) $this->data['fastlane_display_watermark'];
}
/**
* Set PayPal show logo.
*
* @param bool $value The value.
* @return void
*/
public function set_paypal_show_logo( bool $value ): void {
$this->data['paypal_show_logo'] = $value;
}
/**
* Set 3DSecure.
*
* @param string $value The value.
* @return void
*/
public function set_three_d_secure( string $value ): void {
$this->data['three_d_secure'] = $value;
}
/**
* Set Fastlane cardholder name.
*
* @param bool $value The value.
* @return void
*/
public function set_fastlane_cardholder_name( bool $value ): void {
$this->data['fastlane_cardholder_name'] = $value;
}
/**
* Set Fastlane display watermark.
*
* @param bool $value The value.
* @return void
*/
public function set_fastlane_display_watermark( bool $value ): void {
$this->data['fastlane_display_watermark'] = $value;
}
}

View file

@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\MultibancoGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\MyBankGateway; use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\MyBankGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\P24Gateway; use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\P24Gateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\TrustlyGateway; use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\TrustlyGateway;
use WooCommerce\PayPalCommerce\Settings\Data\PaymentSettings;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXO; use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXO;
@ -43,6 +44,46 @@ class PaymentRestEndpoint extends RestEndpoint {
*/ */
protected $rest_base = 'payment'; protected $rest_base = 'payment';
/**
* The settings instance.
*
* @var PaymentSettings
*/
protected PaymentSettings $settings;
/**
* Field mapping for request to profile transformation.
*
* @var array
*/
private array $field_map = array(
'paypal_show_logo' => array(
'js_name' => 'paypalShowLogo',
'sanitize' => 'to_boolean',
),
'three_d_secure' => array(
'js_name' => 'threeDSecure',
'sanitize' => 'sanitize_text_field',
),
'fastlane_cardholder_name' => array(
'js_name' => 'FastlaneCardholderName',
'sanitize' => 'to_boolean',
),
'fastlane_display_watermark' => array(
'js_name' => 'FastlaneDisplayWatermark',
'sanitize' => 'to_boolean',
),
);
/**
* Constructor.
*
* @param PaymentSettings $settings The settings instance.
*/
public function __construct(PaymentSettings $settings) {
$this->settings = $settings;
}
/** /**
* Field mapping for request to profile transformation. * Field mapping for request to profile transformation.
* *
@ -77,7 +118,7 @@ class PaymentRestEndpoint extends RestEndpoint {
), ),
'showLogo' => array( 'showLogo' => array(
'type' => 'toggle', 'type' => 'toggle',
'default' => false, 'default' => $this->settings->get_paypal_show_logo(),
'label' => __( 'Show logo', 'woocommerce-paypal-payments' ), 'label' => __( 'Show logo', 'woocommerce-paypal-payments' ),
), ),
), ),
@ -166,7 +207,7 @@ class PaymentRestEndpoint extends RestEndpoint {
), ),
'threeDSecure' => array( 'threeDSecure' => array(
'type' => 'radio', 'type' => 'radio',
'default' => 'no-3d-secure', 'default' => $this->settings->get_three_d_secure(),
'label' => __( '3D Secure', 'woocommerce-paypal-payments' ), 'label' => __( '3D Secure', 'woocommerce-paypal-payments' ),
'description' => __( 'description' => __(
'Authenticate cardholders through their card issuers to reduce fraud and improve transaction security. Successful 3D Secure authentication can shift liability for fraudulent chargebacks to the card issuer.', 'Authenticate cardholders through their card issuers to reduce fraud and improve transaction security. Successful 3D Secure authentication can shift liability for fraudulent chargebacks to the card issuer.',
@ -224,7 +265,7 @@ class PaymentRestEndpoint extends RestEndpoint {
), ),
'cardholderName' => array( 'cardholderName' => array(
'type' => 'toggle', 'type' => 'toggle',
'default' => false, 'default' => $this->settings->get_fastlane_cardholder_name(),
'label' => __( 'label' => __(
'Display cardholder name', 'Display cardholder name',
'woocommerce-paypal-payments' 'woocommerce-paypal-payments'
@ -232,7 +273,7 @@ class PaymentRestEndpoint extends RestEndpoint {
), ),
'displayWatermark' => array( 'displayWatermark' => array(
'type' => 'toggle', 'type' => 'toggle',
'default' => false, 'default' => $this->settings->get_fastlane_display_watermark(),
'label' => __( 'label' => __(
'Display Fastlane Watermark', 'Display Fastlane Watermark',
'woocommerce-paypal-payments' 'woocommerce-paypal-payments'
@ -686,6 +727,14 @@ class PaymentRestEndpoint extends RestEndpoint {
update_option( $gateway->get_option_key(), $settings ); update_option( $gateway->get_option_key(), $settings );
} }
$wp_data = $this->sanitize_for_wordpress(
$request->get_params(),
$this->field_map
);
$this->settings->from_array( $wp_data );
$this->settings->save();
return $this->get_details(); return $this->get_details();
} }