From 381b46f444af17f5f3ac112eeb11170bcca3be9c Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 22 Jan 2025 17:26:59 +0100 Subject: [PATCH] Add custom data for payment method modals --- modules/ppcp-settings/services.php | 5 +- .../src/Data/PaymentSettings.php | 110 ++++++++++++++++++ .../src/Endpoint/PaymentRestEndpoint.php | 57 ++++++++- 3 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 modules/ppcp-settings/src/Data/PaymentSettings.php diff --git a/modules/ppcp-settings/services.php b/modules/ppcp-settings/services.php index 2d362e978..c01f19ea5 100644 --- a/modules/ppcp-settings/services.php +++ b/modules/ppcp-settings/services.php @@ -76,6 +76,9 @@ return array( $container->get( 'settings.service.sanitizer' ) ); }, + 'settings.data.payment' => static function ( ContainerInterface $container ) : PaymentSettings { + return new PaymentSettings(); + }, 'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint { return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) ); }, @@ -83,7 +86,7 @@ return array( return new CommonRestEndpoint( $container->get( 'settings.data.general' ) ); }, '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 { return new StylingRestEndpoint( diff --git a/modules/ppcp-settings/src/Data/PaymentSettings.php b/modules/ppcp-settings/src/Data/PaymentSettings.php new file mode 100644 index 000000000..eeb3edef4 --- /dev/null +++ b/modules/ppcp-settings/src/Data/PaymentSettings.php @@ -0,0 +1,110 @@ + 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; + } +} diff --git a/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php index eeff91898..694948f44 100644 --- a/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php +++ b/modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php @@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\MultibancoGateway; use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\MyBankGateway; use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\P24Gateway; use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\TrustlyGateway; +use WooCommerce\PayPalCommerce\Settings\Data\PaymentSettings; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXO; @@ -43,6 +44,46 @@ class PaymentRestEndpoint extends RestEndpoint { */ 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. * @@ -77,7 +118,7 @@ class PaymentRestEndpoint extends RestEndpoint { ), 'showLogo' => array( 'type' => 'toggle', - 'default' => false, + 'default' => $this->settings->get_paypal_show_logo(), 'label' => __( 'Show logo', 'woocommerce-paypal-payments' ), ), ), @@ -166,7 +207,7 @@ class PaymentRestEndpoint extends RestEndpoint { ), 'threeDSecure' => array( 'type' => 'radio', - 'default' => 'no-3d-secure', + 'default' => $this->settings->get_three_d_secure(), 'label' => __( '3D Secure', 'woocommerce-paypal-payments' ), '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.', @@ -224,7 +265,7 @@ class PaymentRestEndpoint extends RestEndpoint { ), 'cardholderName' => array( 'type' => 'toggle', - 'default' => false, + 'default' => $this->settings->get_fastlane_cardholder_name(), 'label' => __( 'Display cardholder name', 'woocommerce-paypal-payments' @@ -232,7 +273,7 @@ class PaymentRestEndpoint extends RestEndpoint { ), 'displayWatermark' => array( 'type' => 'toggle', - 'default' => false, + 'default' => $this->settings->get_fastlane_display_watermark(), 'label' => __( 'Display Fastlane Watermark', 'woocommerce-paypal-payments' @@ -686,6 +727,14 @@ class PaymentRestEndpoint extends RestEndpoint { 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(); }