diff --git a/includes/class-wc-gateway-dummy.php b/includes/class-wc-gateway-dummy.php index fcccb96..7782820 100644 --- a/includes/class-wc-gateway-dummy.php +++ b/includes/class-wc-gateway-dummy.php @@ -45,7 +45,7 @@ class WC_Gateway_Dummy extends WC_Payment_Gateway { * Constructor for the gateway. */ public function __construct() { - + $this->icon = apply_filters( 'woocommerce_dummy_gateway_icon', '' ); $this->has_fields = false; $this->supports = array( @@ -121,10 +121,67 @@ public function init_form_fields() { ), 'default' => 'success', 'desc_tip' => true, - ) + ), + 'tokenization' => array( + 'title' => __( 'Enable tokenization', 'woocommerce-gateway-dummy' ), + 'desc' => __( 'Allow customers to save payment methods for future use.', 'woocommerce-gateway-dummy' ), + 'id' => 'woo_dummy_tokenization', + 'type' => 'checkbox', + 'default' => 'yes', + 'desc_tip' => true, + ), ); } + /** + * Initialize the gateway settings from the form fields. + * + * At present this is used to enable tokenization if the setting is enabled + * in the dashboard. + */ + public function init_settings() { + parent::init_settings(); + + // Tokenization settings. + if ( 'yes' === $this->get_option( 'tokenization' ) ) { + $this->supports[] = 'tokenization'; + } + } + + /** + * Save the payment method to the database. + * + * @return bool Whether the payment method was saved successfully. + */ + public function add_payment_method() { + $token = new WC_Payment_Token_Dummy(); + $token->set_token( 'dummy-' . $this->get_option( 'result' ) ); + $token->set_gateway_id( $this->id ); + $token->set_user_id( get_current_user_id() ); + return $token->save(); + } + + /** + * Display the payment fields for the shortcode checkout page. + * + * Modifies the payment fields displayed on the checkout page to include + * the any saved payment methods and the option to save a new payment method. + */ + public function payment_fields() { + $description = $this->get_description(); + + if ( $description ) { + // KSES is ran within get_description, but not here since there may be custom HTML returned by extensions. + echo wpautop( wptexturize( $description ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + } + + if ( $this->supports( 'tokenization' ) && is_checkout() ) { + $this->tokenization_script(); + $this->saved_payment_methods(); + $this->save_payment_method_checkbox(); + } + } + /** * Process the payment and return the result. * @@ -154,6 +211,14 @@ class_exists( 'WC_Pre_Orders_Order' ) // Remove cart WC()->cart->empty_cart(); + // Add payment method for tokenization. + if ( + isset( $_POST['wc-dummy-new-payment-method'] ) + && $_POST['wc-dummy-new-payment-method'] + ) { + $this->add_payment_method(); + } + // Return thankyou redirect return array( 'result' => 'success', diff --git a/includes/class-wc-payment-token-dummy.php b/includes/class-wc-payment-token-dummy.php new file mode 100644 index 0000000..256d052 --- /dev/null +++ b/includes/class-wc-payment-token-dummy.php @@ -0,0 +1,61 @@ +payment_gateways->payment_gateways(); + if ( ! isset( $gateways[ 'dummy' ] ) ) { + return false; + } + + $gateway = $gateways[ 'dummy' ]; + + if ( ! $gateway->enabled === 'yes' ) { + return false; + } + + if ( ! $gateway->supports( 'tokenization' ) ) { + return false; + } + + if ( $gateway->get_option( 'result' ) !== 'success' ) { + return false; + } + + return true; + } + + /** + * Get display name for the token. + * + * @param string $deprecated + * @return string Display name. + */ + public function get_display_name( $deprecated = '' ) { + return sprintf( + /* translators: %s: Payment token ID */ + __( 'Dummy Payment Token %s', 'woocommerce-gateway-dummy' ), + $this->get_id() ? '#' . $this->get_id() : '' + ); + } +} diff --git a/resources/js/frontend/index.js b/resources/js/frontend/index.js index bbf6181..64aa5ad 100644 --- a/resources/js/frontend/index.js +++ b/resources/js/frontend/index.js @@ -12,12 +12,37 @@ const defaultLabel = __( ); const label = decodeEntities( settings.title ) || defaultLabel; + /** * Content component */ const Content = () => { return decodeEntities( settings.description || '' ); }; + +const SavedPaymentContent = ( props ) => { + const supportsTokenization = settings.supports.includes( 'tokenization' ); + + if ( ! supportsTokenization ) { + return null; + } + + return ( +
+ { __( 'For testing tokenization support', 'woocommerce-gateway-dummy' ) } +
+