Class MaksuPay
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic enumstatic enumstatic enum -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcalculateSignature(byte[] data, String privateKey) Calculates a digital signature for the specified data using the provided private key.static StringcalculateSignatureForJSONHeader(byte[] data, String privateKey) Calculates a digital signature for the specified data using the provided private key.static StringcalculateSignatureForJSONHeader(byte[] data, PrivateKey privateKey) Calculates a digital signature for the specified data using the provided private key.Creates a MaksuPay request string from the specified request parameters.createMaksuPayRequestAsForm(String merchantKey, String merchantCert, String maksuURL, Map<String, String> requestParams) Creates a MaksuPay request html form string from the specified request parameters.static Stringstatic StringpublicKeySHA256Hash(String certificate) static Stringstatic HttpResponse<byte[]> sendAPIRequest(String apiURL, int timeout, String merId, String merchantKey, String merchantCert, String request) static HttpResponse<byte[]> sendAPIRequest(String apiURL, int timeout, String merId, String merchantKey, String merchantCert, String request, StringBuilder requestHeadersDbg) static HttpResponse<byte[]> sendAPIRequest(String apiURL, int timeout, String merId, PrivateKey merchantKey, X509Certificate merchantCert, Map<String, Object> request, StringBuilder requestHeadersDbg) Sends a MaksuPay API JSON request to the specified HTTPS endpoint with a signature and other required headers.static booleanvalidateApiResponse(String maksuCerts, String pkHash, String sigB64, byte[] data) Validates a MaksuPay API response signature using the provided certificates and response parameters.static booleanvalidateApiResponse(X509Certificate[] maksuCerts, String pkHash, String sigB64, byte[] data) Validates a MaksuPay API response signature using the provided certificates and response parameters.booleanvalidateResponse(String maksuCerts, Map<String, String> responseParams) Validates a MaksuPay response signature using the provided certificates and response parameters.booleanvalidateResponse(X509Certificate[] maksuCerts, Map<String, String> responseParams) Validates a MaksuPay response signature using the provided certificates and response parameters.static booleanvalidateSignature(PublicKey publicKey, byte[] data, byte[] signature) Validates a digital signature against the specified data using the provided RSA public key.static booleanvalidateSignatureJSONHeader(PublicKey publicKey, String xPayloadSignature, byte[] data) Validates a digital signature against the specified data using the provided RSA public key.
-
Field Details
-
CERT_HEADER
- See Also:
-
CERT_FOOTER
- See Also:
-
PUB_HEADER
- See Also:
-
PUB_FOOTER
- See Also:
-
PRI_HEADER
- See Also:
-
PRI_FOOTER
- See Also:
-
-
Constructor Details
-
MaksuPay
public MaksuPay()
-
-
Method Details
-
createMaksuPayRequest
public String createMaksuPayRequest(String merchantKey, String merchantCert, Map<String, String> requestParams) throws GeneralSecurityExceptionCreates a MaksuPay request string from the specified request parameters.This method typically serializes or concatenates the provided parameters into the format expected by the MaksuPay backend.
- Parameters:
merchantKey- Merchant private key in PKCS#8 PEM format.merchantCert- Merchant certificate (X.509 PEM) containing the public key corresponding to the signing private key.requestParams- A map containing the request parameters, where the key is the parameter name and the value is the corresponding value.- Returns:
- A string representing the formatted MaksuPay request that can be sent to the payment gateway via HTTP POST.
- Throws:
NullPointerException- ifmerchantKey,merchantCertorrequestParamsis null.GeneralSecurityException- if the private key is invalid or the signing operation fails.
-
createMaksuPayRequestAsForm
public String createMaksuPayRequestAsForm(String merchantKey, String merchantCert, String maksuURL, Map<String, String> requestParams) throws GeneralSecurityExceptionCreates a MaksuPay request html form string from the specified request parameters.This method typically serializes or concatenates the provided parameters into the format expected by the MaksuPay backend.
- Parameters:
merchantKey- Merchant private key in PKCS#8 PEM format.merchantCert- Merchant certificate (X.509 PEM) containing the public key corresponding to the signing private key.maksuURL- MaksuPay urlrequestParams- A map containing the request parameters, where the key is the parameter name and the value is the corresponding value.- Returns:
- A string representing the formatted MaksuPay request that can be sent to the payment gateway via HTTP POST.
- Throws:
NullPointerException- ifmerchantKey,merchantCertorrequestParamsis null.GeneralSecurityException- if the private key is invalid or the signing operation fails.
-
validateResponse
Validates a MaksuPay response signature using the provided certificates and response parameters.This method reconstructs the canonical signature base string from the provided parameters, extracts the signature from
responseParams, and verifies it using the public key extracted frommaksuCerts.- Parameters:
maksuCerts- The public certificate or certificate bundle used to verify the response signature. Must contain the public key corresponding to the signing key.responseParams- A map containing the response parameters returned by the MaksuPay gateway. The map must include thesignaturefield (andpublicKeyHashif sent), along with all parameters required to construct the signature base string.- Returns:
trueif the response signature is valid; otherwisefalse.- Throws:
NullPointerException- ifmaksuCertsorresponseParamsis null.IllegalArgumentException- if the signature value inresponseParamsis malformed or cannot be decoded.
-
validateResponse
Validates a MaksuPay response signature using the provided certificates and response parameters.This method reconstructs the canonical signature base string from the provided parameters, extracts the signature from
responseParams, and verifies it using the public key extracted frommaksuCerts.- Parameters:
maksuCerts- The array of public certificates or certificate bundles used to verify the response signature. Must contain the public key corresponding to the signing key.responseParams- A map containing the response parameters returned by the MaksuPay gateway. The map must include thesignaturefield (andpublicKeyHashif sent), along with all parameters required to construct the signature base string.- Returns:
trueif the response signature is valid; otherwisefalse.- Throws:
NullPointerException- ifmaksuCertsorresponseParamsis null.IllegalArgumentException- if the signature value inresponseParamsis malformed or cannot be decoded.
-
calculateSignature
public static String calculateSignature(byte[] data, String privateKey) throws GeneralSecurityException Calculates a digital signature for the specified data using the provided private key.This method typically computes the signature using RSA with SHA-256 (RS256), but the actual algorithm depends on the implementation of the underlying cryptographic provider. Ensure that the private key corresponds to the public key used to validate the signature on the receiving side.
- Parameters:
data- The raw byte array containing the data to be signed.privateKey- The private key in PEM or another compatible string format used to generate the digital signature.- Returns:
- A Base64-encoded string representing the generated digital signature.
- Throws:
NullPointerException- ifdataorprivateKeyis null.GeneralSecurityException- if the private key is invalid or the signing operation fails.
-
sendAPIRequest
public static HttpResponse<byte[]> sendAPIRequest(String apiURL, int timeout, String merId, PrivateKey merchantKey, X509Certificate merchantCert, Map<String, Object> request, StringBuilder requestHeadersDbg) throws GeneralSecurityException, IOException, InterruptedExceptionSends a MaksuPay API JSON request to the specified HTTPS endpoint with a signature and other required headers.This method serializes the request parameters, signs the payload using the provided RSA private key, attaches certificate information, applies the timeout settings, and submits the HTTPS POST request to the MaksuPay API.
- Parameters:
apiURL- The full HTTPS API endpoint URL to which the request will be sent.timeout- The request timeout in milliseconds. A network-related exception may be thrown if the timeout is exceeded.merId- The merchant identifier assigned by MaksuPay.merchantKey- The merchant's RSA private key used to generate the request signature.merchantCert- The merchant’s X.509 certificate corresponding to the signing private key and optionally used to include certificate metadata in the request.request- A map containing the request message to be serialized to JSON and signed before sending to the MaksuPay API. Keys represent parameter names and values represent the parameter values.- Returns:
- The HTTP response returned by the MaksuPay server. The caller is responsible for reading and closing the response body.
- Throws:
NullPointerException- ifapiURL,merId,merchantKey,merchantCertorrequestis null.InterruptedException- if the request is interruptedIOException- if the HTTP request fails due to connection or TLS errors.GeneralSecurityException- if signing the request withmerchantKeyfails.
-
sendAPIRequest
public static HttpResponse<byte[]> sendAPIRequest(String apiURL, int timeout, String merId, String merchantKey, String merchantCert, String request) throws GeneralSecurityException, IOException, InterruptedException -
sendAPIRequest
public static HttpResponse<byte[]> sendAPIRequest(String apiURL, int timeout, String merId, String merchantKey, String merchantCert, String request, StringBuilder requestHeadersDbg) throws GeneralSecurityException, IOException, InterruptedException -
calculateSignatureForJSONHeader
public static String calculateSignatureForJSONHeader(byte[] data, String privateKey) throws GeneralSecurityException Calculates a digital signature for the specified data using the provided private key. Useful for creating a payload signature for a JSON API message, typically used in theX-Payload-SignatureHTTP header.This method typically computes the signature using RSA with SHA-256 (RS256), but the actual algorithm depends on the implementation of the underlying cryptographic provider. Ensure that the private key corresponds to the public key used to validate the signature on the receiving side.
- Parameters:
data- The raw byte array containing the data (JSON message body) that will be signed.privateKey- The private key in PEM or another compatible string format used to generate the digital signature.- Returns:
- A Base64-encoded string representing the generated digital signature, prefixed with
"RSA-SHA256;". - Throws:
NullPointerException- ifdataorprivateKeyis null.GeneralSecurityException- if the private key is invalid or the signing operation fails.
-
calculateSignatureForJSONHeader
public static String calculateSignatureForJSONHeader(byte[] data, PrivateKey privateKey) throws GeneralSecurityException Calculates a digital signature for the specified data using the provided private key. Useful for creating a payload signature for a JSON API message, typically used in theX-Payload-SignatureHTTP header.This method typically computes the signature using RSA with SHA-256 (RS256), but the actual algorithm depends on the implementation of the underlying cryptographic provider. Ensure that the private key corresponds to the public key used to validate the signature on the receiving side.
- Parameters:
data- The raw byte array containing the data (JSON message body) that will be signed.privateKey- The private key in PEM or another compatible string format used to generate the digital signature.- Returns:
- A Base64-encoded string representing the generated digital signature, prefixed with
"RSA-SHA256;". - Throws:
NullPointerException- ifdataorprivateKeyis null.GeneralSecurityException- if the private key is invalid or the signing operation fails.
-
validateApiResponse
public static boolean validateApiResponse(String maksuCerts, String pkHash, String sigB64, byte[] data) Validates a MaksuPay API response signature using the provided certificates and response parameters.- Parameters:
maksuCerts- The public certificate or certificate bundle used to verify the response signature. Must contain the public key corresponding to the signing key.pkHash- The public hash from theX-Public-Key-Hashheader to validate, provided as a raw byte array (typically Base64-decoded bytes).sigB64- The digital signature from theX-Payload-Signatureheader to validate, provided as a raw byte array (typically Base64-decoded bytes).data- The raw byte array representing the original data that was signed.- Returns:
trueif the response signature is valid; otherwisefalse.- Throws:
NullPointerException- ifmaksuCerts,pkHash,sigB64ordatais null.
-
validateApiResponse
public static boolean validateApiResponse(X509Certificate[] maksuCerts, String pkHash, String sigB64, byte[] data) Validates a MaksuPay API response signature using the provided certificates and response parameters.- Parameters:
maksuCerts- The array of public certificates or certificate bundles used to verify the response signature. Must contain the public key corresponding to the signing key.pkHash- The public hash from theX-Public-Key-Hashheader to validate, provided as a raw byte array (typically Base64-decoded bytes).sigB64- The digital signature from theX-Payload-Signatureheader to validate, provided as a raw byte array (typically Base64-decoded bytes).data- The raw byte array representing the original data that was signed.- Returns:
trueif the response signature is valid; otherwisefalse.- Throws:
NullPointerException- ifmaksuCerts,pkHash,sigB64ordatais null.
-
getVersion
-
validateSignature
public static boolean validateSignature(PublicKey publicKey, byte[] data, byte[] signature) throws GeneralSecurityException Validates a digital signature against the specified data using the provided RSA public key.This method typically performs RSA verification using SHA-256 (RS256), depending on the capabilities of the underlying cryptographic provider. Ensure that the
signaturebytes match the signing algorithm and padding mode used to generate the signature.- Parameters:
publicKey- The RSA public key used to verify the signature. Must correspond to the private key that generated the signature.data- The raw byte array representing the original data that was signed.signature- The digital signature to validate, provided as a raw byte array (typically Base64-decoded bytes).- Returns:
trueif the signature is valid for the given data and public key; otherwisefalse.- Throws:
NullPointerException- ifpublicKey,data, orsignatureis null.GeneralSecurityException- if the verification process fails due to an invalid key, incorrect format, unsupported algorithm, or other cryptographic-related issues.
-
validateSignatureJSONHeader
public static boolean validateSignatureJSONHeader(PublicKey publicKey, String xPayloadSignature, byte[] data) throws GeneralSecurityException Validates a digital signature against the specified data using the provided RSA public key.This method typically performs RSA verification using SHA-256 (RS256), depending on the configuration of the underlying cryptographic provider. Ensure that the
xPayloadSignaturebytes match the signing algorithm and padding mode used to generate the signature.- Parameters:
publicKey- The RSA public key used to verify the signature. Must correspond to the private key that was used to generate the signature.xPayloadSignature- The digital signature from theX-Payload-Signatureheader to validate, provided as a raw byte array (typically Base64-decoded bytes).data- The raw byte array representing the original data that was signed.- Returns:
trueif the signature is valid for the given data and public key; otherwisefalse.- Throws:
NullPointerException- ifpublicKey,data, orxPayloadSignatureis null.GeneralSecurityException- if the verification process fails due to an invalid key, incorrect format, unsupported algorithm, or other cryptographic-related issues.
-
publicKeySHA256Hash
- Throws:
GeneralSecurityException
-
publicKeySHA256Hash
- Throws:
GeneralSecurityException
-