gateway = $gateway; $gateway_id = $this->gateway->id; if ( is_user_logged_in() && $this->gateway->supports( 'tokenization' ) ) { $this->tokens = $this->filter_tokens_by_gateway( $gateway_id, WC_Payment_Tokens::get_customer_tokens( get_current_user_id() ) ); $this->saved_payment_methods(); } if ( $this->gateway->supports( 'tokenization' ) ) { $this->use_new_payment_method_checkbox(); } $this->load_scripts(); $this->payment_fields(); } /** * Grab and display our saved payment methods. * * @since 2.6 */ public function saved_payment_methods() { $html = '

'; foreach ( $this->tokens as $token ) { $html .= $this->saved_payment_method( $token ); } $html .= '

'; $html .= '
'; echo apply_filters( 'wc_payment_gateway_form_saved_payment_methods_html', $html, $this ); } /** * Outputs a saved payment method from a token. * * @since 2.6 * @param WC_Payment_Token $token Payment Token * @return string Generated payment method HTML */ public function saved_payment_method( $token ) { $html = sprintf( '', esc_attr( $this->gateway->id ), esc_attr( $token->get_id() ), checked( $token->is_default(), true, false ) ); $html .= sprintf( '
'; return apply_filters( 'wc_payment_gateway_form_saved_payment_method_html', $html, $token, $this ); } /** * Outputs a saved payment method's title based on the passed token. * * @since 2.6 * @param WC_Payment_Token $token Payment Token * @return string Generated payment method title HTML */ public function saved_payment_method_title( $token ) { if ( 'CC' == $token->get_type() && is_callable( array( $token, 'get_card_type' ) ) ) { $type = esc_html__( ucfirst( $token->get_card_type() ), 'woocommerce' ); } else if ( 'eCheck' === $token->get_type() ) { $type = esc_html__( 'eCheck', 'woocommerce' ); } $type = apply_filters( 'wc_payment_gateway_form_saved_payment_method_title_type_html', $type, $token, $this ); $title = $type; if ( is_callable( array( $token, 'get_last4' ) ) ) { $title .= ' ' . sprintf( esc_html__( 'ending in %s', 'woocommerce' ), $token->get_last4() ); } if ( is_callable( array( $token, 'get_expiry_month' ) ) && is_callable( array( $token, 'get_expiry_year' ) ) ) { $title .= ' ' . sprintf( esc_html__( '(expires %s)', 'woocommerce' ), $token->get_expiry_month() . '/' . substr( $token->get_expiry_year(), 2 ) ); } return apply_filters( 'wc_payment_gateway_form_saved_payment_method_title_html', $title, $token, $this ); } /** * Outputs payment fields for entering new card info. * * @since 2.6 */ public function payment_fields() { if ( $this->gateway->supports( 'credit_card_form' ) ) { echo $this->credit_card_payment_fields(); if ( $this->gateway->supports( 'tokenization' ) ) { $this->save_payment_method_checkbox(); } } else if ( $this->gateway->supports( 'echeck_form' ) ) { echo $this->echeck_payment_fields(); if ( $this->gateway->supports( 'tokenization' ) ) { $this->save_payment_method_checkbox(); } } else { echo apply_filters( 'wc_payment_gateway_form_payment_fields_html', '', $this ); } } /** * Filters out tokens not for the current gateway. * * @since 2.6 * @param string $gateway_id Gateway ID * @param array $tokens Set of tokens * @return array Subset of tokens based on gateway ID */ public function filter_tokens_by_gateway( $gateway_id, $tokens ) { $filtered_tokens = array(); foreach ( $tokens as $key => $token ) { if ( $gateway_id === $token->get_gateway_id() ) { $filtered_tokens[ $key ] = $token; } } return $filtered_tokens; } /** * Outputs fields for entering credit card information. * * @since 2.6 */ public function credit_card_payment_fields() { $html = ''; $fields = array(); $cvc_class = ''; if ( $this->gateway->supports( 'credit_card_form_cvc_on_saved_method' ) ) { $cvc_class = ' show-if-payment-method-selected'; } $default_fields = array( 'card-number-field' => '

', 'card-expiry-field' => '

', 'card-cvc-field' => '

', ); $fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->gateway->id ) ); ?>
gateway->id ); ?> gateway->id ); ?>
'

', 'account-number' => '

', ); $fields = wp_parse_args( $fields, apply_filters( 'woocommerce_echeck_form_fields', $default_fields, $this->gateway->id ) ); ?>
gateway->id ); ?> gateway->id ); ?>
', esc_attr( $this->gateway->id ) ); $html .= sprintf( '', esc_attr( $this->gateway->id ) ); $html .= sprintf( '', esc_attr( $this->gateway->id ), esc_html__( 'Save to Account', 'woocommerce' ) ); $html .= '

'; echo $html; } public function use_new_payment_method_checkbox() { $html = ''; $html .= ''; echo '
' . $html . '
'; } public function load_scripts() { $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; wp_enqueue_script( 'woocommerce-payment-gateway-form', plugins_url( '/assets/js/frontend/payment-gateway-form' . $suffix . '.js', WC_PLUGIN_FILE ), array( 'jquery' ), WC()->version ); wp_localize_script( 'woocommerce-payment-gateway-form', 'woocommercePaymentGatewayParams', array( 'gatewayID' => $this->gateway->id, 'userLoggedIn' => (bool) is_user_logged_in(), ) ); } }