WC_Payment_Token_Data_Store::create()publicWC 3.0.0

Create a new payment token in the database.

Method of the class: WC_Payment_Token_Data_Store{}

Hooks from the method

Return

null. Nothing (null).

Usage

$WC_Payment_Token_Data_Store = new WC_Payment_Token_Data_Store();
$WC_Payment_Token_Data_Store->create( $token );
$token(WC_Payment_Token) (required) (passed by reference — &)
Payment token object.

Changelog

Since 3.0.0 Introduced.

WC_Payment_Token_Data_Store::create() code WC 9.4.2

public function create( &$token ) {
	if ( false === $token->validate() ) {
		throw new Exception( __( 'Invalid or missing payment token fields.', 'woocommerce' ) );
	}

	global $wpdb;
	if ( ! $token->is_default() && $token->get_user_id() > 0 ) {
		$default_token = WC_Payment_Tokens::get_customer_default_token( $token->get_user_id() );
		if ( is_null( $default_token ) ) {
			$token->set_default( true );
		}
	}

	$payment_token_data = array(
		'gateway_id' => $token->get_gateway_id( 'edit' ),
		'token'      => $token->get_token( 'edit' ),
		'user_id'    => $token->get_user_id( 'edit' ),
		'type'       => $token->get_type( 'edit' ),
	);

	$wpdb->insert( $wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data );
	$token_id = $wpdb->insert_id;
	$token->set_id( $token_id );
	$this->save_extra_data( $token, true );
	$token->save_meta_data();
	$token->apply_changes();

	// Make sure all other tokens are not set to default.
	if ( $token->is_default() && $token->get_user_id() > 0 ) {
		WC_Payment_Tokens::set_users_default( $token->get_user_id(), $token_id );
	}

	do_action( 'woocommerce_new_payment_token', $token_id, $token );
}