Gateway/Response/TransactionHandler.php
public function vaultCard( array $response ){
        if (isset($response['card'])) {
            // Get the card token
            $cardToken = $response['card']['id'];
            
            // Prepare the card data
            $cardData = [];
            $cardData['expiryMonth']   = $response['card']['expiryMonth'];
            $cardData['expiryYear']    = $response['card']['expiryYear'];
            $cardData['last4']         = $response['card']['last4'];
            $cardData['paymentMethod'] = $response['card']['paymentMethod'];

            // Get the payment token
            $paymentToken = $this->vaultTokenFactory->create($cardData, $this->customerSession->getCustomer()->getId());
            
            try {
                // Check if the payment token exists
                $foundPaymentToken = $this->paymentTokenManagement->getByPublicHash( $paymentToken->getPublicHash(), $paymentToken->getCustomerId());

                // If the token exists activate it, otherwise create it
                if ($foundPaymentToken) {
                    $foundPaymentToken->setIsVisible(true);
                    $foundPaymentToken->setIsActive(true);
                    $this->paymentTokenRepository->save($foundPaymentToken);
                }
                else {
                    $paymentToken->setGatewayToken($cardToken);
                    $paymentToken->setIsVisible(true);
                    $this->paymentTokenRepository->save($paymentToken);
                } 

                $this->messageManager->addSuccessMessage( __('The payment card has been stored successfully') );
            }    
            catch (\Exception $ex) {
                $this->messageManager->addErrorMessage( $ex->getMessage() );
            }
        }
        else {
            $this->messageManager->addErrorMessage( __('Invalid gateway response. Please contact the site administrator.') );
        }
    }
public function vaultCard( array $response ){
        if (isset($response['card'])) {
            // Get the card token
            $cardToken = $response['card']['id'];
            
            // Prepare the card data
            $cardData = [];
            $cardData['expiryMonth']   = $response['card']['expiryMonth'];
            $cardData['expiryYear']    = $response['card']['expiryYear'];
            $cardData['last4']         = $response['card']['last4'];
            $cardData['paymentMethod'] = $response['card']['paymentMethod'];

            // Get the payment token
            $paymentToken = $this->vaultTokenFactory->create($cardData, $this->customerSession->getCustomer()->getId());
            
            try {
                // Check if the payment token exists
                $foundPaymentToken = $this->paymentTokenManagement->getByPublicHash( $paymentToken->getPublicHash(), $paymentToken->getCustomerId());

                // If the token exists activate it, otherwise create it
                if ($foundPaymentToken) {
                    $foundPaymentToken->setIsVisible(true);
                    $foundPaymentToken->setIsActive(true);
                    $this->paymentTokenRepository->save($foundPaymentToken);
                }
                else {
                    $paymentToken->setGatewayToken($cardToken);
                    $paymentToken->setIsVisible(true);
                    $this->paymentTokenRepository->save($paymentToken);
                } 

                $this->messageManager->addSuccessMessage( __('The payment card has been stored successfully') );
            }    
            catch (\Exception $ex) {
                $this->messageManager->addErrorMessage( $ex->getMessage() );
            }
        }
        else {
            $this->messageManager->addErrorMessage( __('Invalid gateway response. Please contact the site administrator.') );
        }
    }
public function vaultCard ( array $ response ) { if ( isset ( $ response [ 'card' ] ) ) { // Get the card token $ cardToken = $ response [ 'card' ] [ 'id' ] ; // Prepare the card data $ cardData = [ ] ; $ cardData [ 'expiryMonth' ] = $ response [ 'card' ] [ 'expiryMonth' ] ; $ cardData [ 'expiryYear' ] = $ response [ 'card' ] [ 'expiryYear' ] ; $ cardData [ 'last4' ] = $ response [ 'card' ] [ 'last4' ] ; $ cardData [ 'paymentMethod' ] = $ response [ 'card' ] [ 'paymentMethod' ] ; // Get the payment token $ paymentToken = $ this -> vaultTokenFactory -> create ( $ cardData , $ this -> customerSession -> getCustomer ( ) -> getId ( ) ) ; try { // Check if the payment token exists $ foundPaymentToken = $ this -> paymentTokenManagement -> getByPublicHash ( $ paymentToken -> getPublicHash ( ) , $ paymentToken -> getCustomerId ( ) ) ; // If the token exists activate it, otherwise create it if ( $ foundPaymentToken ) { $ foundPaymentToken -> setIsVisible ( true ) ; $ foundPaymentToken -> setIsActive ( true ) ; $ this -> paymentTokenRepository -> save ( $ foundPaymentToken ) ; } else { $ paymentToken -> setGatewayToken ( $ cardToken ) ; $ paymentToken -> setIsVisible ( true ) ; $ this -> paymentTokenRepository -> save ( $ paymentToken ) ; } $ this -> messageManager -> addSuccessMessage ( __ ( 'The payment card has been stored successfully' ) ) ; } catch ( \ Exception $ ex ) { $ this -> messageManager -> addErrorMessage ( $ ex -> getMessage ( ) ) ; } } else { $ this -> messageManager -> addErrorMessage ( __ ( 'Invalid gateway response. Please contact the site administrator.' ) ) ; } }
Adds a new card.

@param array $response
@return void
Adds a new card .