Class ValueLinkApi


  • public class ValueLinkApi
    extends java.lang.Object
    ValueLinkApi - Implementation of ValueLink Encryption and Transport
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean debug  
      protected Delegator delegator  
      protected javax.crypto.SecretKey kek  
      protected java.lang.String merchantId  
      static java.lang.String module  
      protected javax.crypto.SecretKey mwk  
      protected java.lang.Long mwkIndex  
      protected java.util.Properties props  
      protected java.lang.String terminalId  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ValueLinkApi()  
      protected ValueLinkApi​(Delegator delegator, java.util.Properties props)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] copyBytes​(byte[] source, byte[] target, int position)
      Copies a byte[] into another byte[] starting at a specific position
      java.security.KeyPair createKeys()
      Create a set of public/private keys using ValueLinks defined parameters
      protected byte[] cryptoViaKek​(byte[] content, int mode)
      actual kek encryption/decryption code
      java.lang.String decryptPin​(java.lang.String pin)
      Decrypt an encrypted pin using the configured keys
      byte[] decryptViaKek​(byte[] content)
      Ue the KEK to decrypt a value
      java.lang.String encryptPin​(java.lang.String pin)
      Encrypt the defined pin using the configured keys
      byte[] encryptViaKek​(byte[] content)
      Use the KEK to encrypt a value usually the MWK
      byte[] generateKek​(java.security.PrivateKey privateKey)
      Generate a key exchange key for use in encrypting the mwk
      byte[] generateMwk()
      Generate a new MWK
      byte[] generateMwk​(byte[] desBytes)
      Generate a new MWK
      byte[] generateMwk​(javax.crypto.SecretKey mwkdes3)
      Generate a new MWK
      java.math.BigDecimal getAmount​(java.lang.String amount)
      Returns a BigDecimal from a ValueLink formatted amount String
      java.lang.String getAmount​(java.math.BigDecimal amount)
      Returns a ValueLink formatted amount String
      static byte[] getByteRange​(byte[] bytes, int offset, int length)
      Returns a new byte[] from the offset of the defined byte[] with a specific number of bytes
      protected static javax.crypto.Cipher getCipher​(java.security.Key key, int mode, javax.crypto.spec.IvParameterSpec iv)  
      java.lang.String getCurrency​(java.lang.String currency)  
      java.lang.String getDateString()
      Returns a date string formatted as directed by ValueLink
      protected javax.crypto.SecretKey getDesEdeKey​(byte[] rawKey)  
      protected javax.crypto.spec.DHParameterSpec getDHParameterSpec()  
      GenericValue getGenericValue()
      Gets the cached value object for this merchant's keys
      java.util.Map<java.lang.String,​java.lang.Object> getInitialRequestMap​(java.util.Map<java.lang.String,​java.lang.Object> context)
      Creates a Map of initial request values (MerchID, AltMerchNo, Modes, MerchTime, TermTxnNo, EncryptID) Note: For 2010 (assign working key) transaction, the EncryptID will need to be adjusted
      static ValueLinkApi getInstance​(Delegator delegator, java.util.Properties props)
      Obtain an instance of the ValueLinkApi; this method will always return an existing reference if one is available
      static ValueLinkApi getInstance​(Delegator delegator, java.util.Properties props, boolean reload)
      Obtain an instance of the ValueLinkApi
      protected byte[] getKek()  
      protected javax.crypto.SecretKey getKekKey()  
      protected byte[] getMwk()  
      protected javax.crypto.SecretKey getMwkKey()  
      protected byte[] getPinCheckSum​(byte[] pinBytes)  
      java.security.PrivateKey getPrivateKey()
      Get merchant Private Key
      protected byte[] getPrivateKeyBytes()  
      protected byte[] getRandomBytes​(int length)  
      java.security.PublicKey getValueLinkPublicKey()
      Get a public key object for the ValueLink supplied public key
      java.lang.Long getWorkingKeyIndex()
      Returns the current working key index
      java.lang.StringBuffer outputKeyCreation​(boolean kekOnly, java.lang.String kekTest)
      Output the creation of public/private keys + KEK to the console for manual database update @
      protected java.util.Map<java.lang.String,​java.lang.Object> parseResponse​(java.lang.String response)  
      void reload()
      Reloads the keys in the object cache; use this when re-creating keys
      java.util.Map<java.lang.String,​java.lang.Object> send​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.Object> request)
      Transmit a request to ValueLink
      java.util.Map<java.lang.String,​java.lang.Object> send​(java.util.Map<java.lang.String,​java.lang.Object> request)
      Transmit a request to ValueLink
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • module

        public static final java.lang.String module
      • props

        protected java.util.Properties props
      • kek

        protected javax.crypto.SecretKey kek
      • mwk

        protected javax.crypto.SecretKey mwk
      • merchantId

        protected java.lang.String merchantId
      • terminalId

        protected java.lang.String terminalId
      • mwkIndex

        protected java.lang.Long mwkIndex
      • debug

        protected boolean debug
    • Constructor Detail

      • ValueLinkApi

        protected ValueLinkApi()
      • ValueLinkApi

        protected ValueLinkApi​(Delegator delegator,
                               java.util.Properties props)
    • Method Detail

      • getInstance

        public static ValueLinkApi getInstance​(Delegator delegator,
                                               java.util.Properties props,
                                               boolean reload)
        Obtain an instance of the ValueLinkApi
        Parameters:
        delegator - Delegator used to query the encryption keys
        props - Properties to use for the Api (usually payment.properties)
        reload - When true, will replace an existing instance in the cache and reload all properties
        Returns:
        ValueLinkApi reference
      • getInstance

        public static ValueLinkApi getInstance​(Delegator delegator,
                                               java.util.Properties props)
        Obtain an instance of the ValueLinkApi; this method will always return an existing reference if one is available
        Parameters:
        delegator - Delegator used to query the encryption keys
        props - Properties to use for the Api (usually payment.properties)
        Returns:
        Obtain an instance of the ValueLinkApi
      • encryptPin

        public java.lang.String encryptPin​(java.lang.String pin)
        Encrypt the defined pin using the configured keys
        Parameters:
        pin - Plain text String of the pin
        Returns:
        Hex String of the encrypted pin (EAN) for transmission to ValueLink
      • decryptPin

        public java.lang.String decryptPin​(java.lang.String pin)
        Decrypt an encrypted pin using the configured keys
        Parameters:
        pin - Hex String of the encrypted pin (EAN)
        Returns:
        Plain text String of the pin @
      • send

        public java.util.Map<java.lang.String,​java.lang.Object> send​(java.util.Map<java.lang.String,​java.lang.Object> request)
                                                                    throws HttpClientException
        Transmit a request to ValueLink
        Parameters:
        request - Map of request parameters
        Returns:
        Map of response parameters
        Throws:
        HttpClientException
      • send

        public java.util.Map<java.lang.String,​java.lang.Object> send​(java.lang.String url,
                                                                           java.util.Map<java.lang.String,​java.lang.Object> request)
                                                                    throws HttpClientException
        Transmit a request to ValueLink
        Parameters:
        url - override URL from what is defined in the properties
        request - request Map of request parameters
        Returns:
        Map of response parameters
        Throws:
        HttpClientException
      • outputKeyCreation

        public java.lang.StringBuffer outputKeyCreation​(boolean kekOnly,
                                                        java.lang.String kekTest)
        Output the creation of public/private keys + KEK to the console for manual database update @
      • createKeys

        public java.security.KeyPair createKeys()
                                         throws java.security.NoSuchAlgorithmException,
                                                java.security.InvalidAlgorithmParameterException,
                                                java.security.spec.InvalidKeySpecException
        Create a set of public/private keys using ValueLinks defined parameters
        Returns:
        KeyPair object containing both public and private keys
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.InvalidAlgorithmParameterException
        java.security.spec.InvalidKeySpecException
      • generateKek

        public byte[] generateKek​(java.security.PrivateKey privateKey)
                           throws java.security.NoSuchAlgorithmException,
                                  java.security.spec.InvalidKeySpecException,
                                  java.security.InvalidKeyException
        Generate a key exchange key for use in encrypting the mwk
        Parameters:
        privateKey - The private key for the merchant
        Returns:
        byte array containing the kek
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.spec.InvalidKeySpecException
        java.security.InvalidKeyException
      • getValueLinkPublicKey

        public java.security.PublicKey getValueLinkPublicKey()
                                                      throws java.security.NoSuchAlgorithmException,
                                                             java.security.spec.InvalidKeySpecException
        Get a public key object for the ValueLink supplied public key
        Returns:
        PublicKey object of ValueLinks's public key
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.spec.InvalidKeySpecException
      • getPrivateKey

        public java.security.PrivateKey getPrivateKey()
                                               throws java.security.spec.InvalidKeySpecException,
                                                      java.security.NoSuchAlgorithmException
        Get merchant Private Key
        Returns:
        PrivateKey object for the merchant
        Throws:
        java.security.spec.InvalidKeySpecException
        java.security.NoSuchAlgorithmException
      • generateMwk

        public byte[] generateMwk()
        Generate a new MWK
        Returns:
        Hex String of the new encrypted MWK ready for transmission to ValueLink @
      • generateMwk

        public byte[] generateMwk​(byte[] desBytes)
        Generate a new MWK
        Parameters:
        desBytes - byte array of the DES key (24 bytes)
        Returns:
        Hex String of the new encrypted MWK ready for transmission to ValueLink @
      • generateMwk

        public byte[] generateMwk​(javax.crypto.SecretKey mwkdes3)
        Generate a new MWK
        Parameters:
        mwkdes3 - pre-generated DES3 SecretKey
        Returns:
        Hex String of the new encrypted MWK ready for transmission to ValueLink @
      • encryptViaKek

        public byte[] encryptViaKek​(byte[] content)
        Use the KEK to encrypt a value usually the MWK
        Parameters:
        content - byte array to encrypt
        Returns:
        encrypted byte array
      • decryptViaKek

        public byte[] decryptViaKek​(byte[] content)
        Ue the KEK to decrypt a value
        Parameters:
        content - byte array to decrypt
        Returns:
        decrypted byte array
      • getDateString

        public java.lang.String getDateString()
        Returns a date string formatted as directed by ValueLink
        Returns:
        ValueLink formatted date String
      • getWorkingKeyIndex

        public java.lang.Long getWorkingKeyIndex()
        Returns the current working key index
        Returns:
        Long number of the current working key index
      • getAmount

        public java.lang.String getAmount​(java.math.BigDecimal amount)
        Returns a ValueLink formatted amount String
        Parameters:
        amount - BigDecimal value to format
        Returns:
        Formatted String
      • getAmount

        public java.math.BigDecimal getAmount​(java.lang.String amount)
        Returns a BigDecimal from a ValueLink formatted amount String
        Parameters:
        amount - The ValueLink formatted amount String
        Returns:
        BigDecimal object
      • getCurrency

        public java.lang.String getCurrency​(java.lang.String currency)
      • getInitialRequestMap

        public java.util.Map<java.lang.String,​java.lang.Object> getInitialRequestMap​(java.util.Map<java.lang.String,​java.lang.Object> context)
        Creates a Map of initial request values (MerchID, AltMerchNo, Modes, MerchTime, TermTxnNo, EncryptID) Note: For 2010 (assign working key) transaction, the EncryptID will need to be adjusted
        Returns:
        Map containing the inital request values
      • getGenericValue

        public GenericValue getGenericValue()
        Gets the cached value object for this merchant's keys
        Returns:
        Cached GenericValue object
      • reload

        public void reload()
        Reloads the keys in the object cache; use this when re-creating keys
      • getDHParameterSpec

        protected javax.crypto.spec.DHParameterSpec getDHParameterSpec()
      • cryptoViaKek

        protected byte[] cryptoViaKek​(byte[] content,
                                      int mode)
                               throws GeneralException
        actual kek encryption/decryption code
        Throws:
        GeneralException
      • getCipher

        protected static javax.crypto.Cipher getCipher​(java.security.Key key,
                                                       int mode,
                                                       javax.crypto.spec.IvParameterSpec iv)
                                                throws GeneralException
        Throws:
        GeneralException
      • getPinCheckSum

        protected byte[] getPinCheckSum​(byte[] pinBytes)
      • getRandomBytes

        protected byte[] getRandomBytes​(int length)
      • getMwkKey

        protected javax.crypto.SecretKey getMwkKey()
      • getKekKey

        protected javax.crypto.SecretKey getKekKey()
      • getDesEdeKey

        protected javax.crypto.SecretKey getDesEdeKey​(byte[] rawKey)
      • getMwk

        protected byte[] getMwk()
      • getKek

        protected byte[] getKek()
      • getPrivateKeyBytes

        protected byte[] getPrivateKeyBytes()
      • parseResponse

        protected java.util.Map<java.lang.String,​java.lang.Object> parseResponse​(java.lang.String response)
      • getByteRange

        public static byte[] getByteRange​(byte[] bytes,
                                          int offset,
                                          int length)
        Returns a new byte[] from the offset of the defined byte[] with a specific number of bytes
        Parameters:
        bytes - The byte[] to extract from
        offset - The starting postition
        length - The number of bytes to copy
        Returns:
        a new byte[]
      • copyBytes

        public static byte[] copyBytes​(byte[] source,
                                       byte[] target,
                                       int position)
        Copies a byte[] into another byte[] starting at a specific position
        Parameters:
        source - byte[] to copy from
        target - byte[] coping into
        position - the position on target where source will be copied to
        Returns:
        a new byte[]