Signature generation and verification
Overview
The data communication between the merchant web service and the ecommpay payment platform is protected by using the TLS protocol version 1.2 or later. This ensures the confidentiality of the data being transmitted, though the protocol cannot guarantee the message integrity and ensure that the message author possesses the secret key. Therefore, every message must be digitally signed by using the secret key issued by ecommpay for the merchant and known only to the merchant and the ecommpay payment platform.
Regardless of the interface that is used for working with the payment platform, digital signatures should be included in all requests, callbacks (for the requests processed within the asynchronous interaction model), and certain responses (for the requests processed within the synchronous interaction model). The responses that do not include signatures are usually the ones that contain only auxiliary information (for example, the response stating that the request has been declined due to the incorrect data) or general information without the payment or customer details (for example, the response containing the list of available banks). In other cases, signatures are included in the responses.
Thus, before you submit a request to the ecommpay payment platform, a digital signature should be generated and included in the request; after the callbacks and responses that contain signatures have been received from the payment platform, it is necessary to verify the received data by comparing the signatures to the ones generated on the merchant side. To implement the digital signature generation and verification, you can use either your own solutions or the SDK available from ecommpay. (For more information about the SDK, see Integration using SDK). The data integrity can be compromised for various reasons, but whenever such cases are detected, this data should not be considered valid.
This section describes the algorithms for the digital signature generation and the data integrity verification, including examples with the use of these algorithms and interactive forms for testing the workflows using signatures.
Signature generation
Signing algorithm
The algorithm input includes the following:
- Data to sign.
Generally, this is all the parameters or JavaScript object configObj with all its parameters except for the
signature
parameter. - Signing key.
For the purposes of debugging and testing your signing algorithm implementation, you are free to use any signing key. For signing requests in production environment, you are required to use your production secret key.
Depending on the algorithm implementation, its output may be either signature or signed JavaScript object configObj. Generally, the algorithm output consists of the object with integrated signature
parameter.
The algorithm description, the example, and test form below include the most common algorithm implementation that includes JavaScrpt object configObj as input (without the signature
parameter) and output (with the signature
parameter).
Thus, the algorithm includes the following steps:
-
Input validation Make sure the following requirements are met:
- Data conforms to the JavaScript object format.
- Data to sign does not contain any
signature
parameter even if it is empty. - The signing key is readily available.
-
Conversion of all the strings to UTF-8 with parameters sorted in natural order. Complete the following steps:
- Encode any Boolean values as follows: replace
false
with0
, replacetrue
with1
. Mind that this rule applies only to Boolean values. If any string parameter contains"false"
or"true"
string value, the value is not replaced with0
or1
but is treated as any other string value (for example therecurring: "{type: \"U\",register: true}"
parameter doesn’t require replacingtrue
with1
). - Leave any empty parameter values empty. In other words, do not replace any empty values with blank space or
null
. For instance,"payment_description":""
is replaced withpayment_description:
. - Convert all strings to UTF-8.
- Sort the strings in natural sort order and join them in a single string by using semicolon (;) as a delimiter.
- Encode any Boolean values as follows: replace
- Calculating HMAC code by using the key and the SHA-512 hash function. Calculate the HMAC code for the string by using the SHA-512 hash function and secret key. The HMAC code must be calculated as raw binary data.
- Encoding the HMAC code by using the Base64 scheme. Encoding the HMAC code by using the Base64 scheme to obtain the signature for the initial data.
- Adding signature. Add the
signature
parameter to the JavaScript object configObj using the signature from the previous step as its value.
Example of purchase request through Payment Page
Suppose that you need to generate a signature for a Payment Page request in the following scenario:
- Secret key obtained from the ecommpay support service:
secret
. - The configObj object that does not yet contain the signature parameter is the following:
{ "project_id": 12345, "payment_id": "X03936", "payment_amount": 2035, "payment_currency": "USD", "payment_description": "Guyliner purchase", "customer_first_name": "Jack", "customer_id": "user007", "customer_last_name": "Sparrow", "customer_phone": "02081234567", "close_on_missclick": true, "signature": "<signature that needs to be generated>" }
The task is to generate the signature; in other words, you need to compute the value for the signature
parameter. The signature is generated as follows:
- Make sure there is no
signature
parameter in your request even it is empty:{ "project_id": 12345, "payment_id": "X03936", "payment_amount": 2035, "payment_currency": "USD", "payment_description": "Guyliner purchase", "customer_first_name": "Jack", "customer_id": "user007", "customer_last_name": "Sparrow", "customer_phone": "02081234567", "close_on_missclick": true, "signature": "<signature that needs to be generated>" }
- Convert all strings to UTF-8 as per algorithm description:
project_id:12345 payment_id:X03936 payment_amount:2035 payment_currency:USD payment_description:Guyliner purchase customer_first_name:Jack customer_id:user007 customer_last_name:Sparrow customer_phone:02081234567 close_on_missclick:1
- Sort the strings in natural sort order:
close_on_missclick:1 customer_first_name:Jack customer_id:user007 customer_last_name:Sparrow customer_phone:02081234567 payment_amount:2035 payment_currency:USD payment_description:Guyliner purchase payment_id:X03936 project_id:12345
- Join all the strings in a single string by using semicolon (;) as a delimiter:
close_on_missclick:1;customer_first_name:Jack;customer_id:user007;customer_last_name:Sparrow;customer_phone:02081234567;payment_amount:2035;payment_currency:USD;payment_description:Guyliner purchase;payment_id:X03936;project_id:12345
- Calculate the HMAC code for the string by using the SHA-512 hash function and secret key, and then encode the HMAC code by using the Base64 scheme:
SyA3cx/dmFrwjRcpbnwEK9zaklWKR9buIfTctQob/EHUTutFLpI0zWpSDFEWEwbZt/04i83395RCdEhtUMw83A==
- Add the resulting signature in the configObj object:
{ "project_id": 12345, "payment_id": "X03936", "payment_amount": 2035, "payment_currency": "USD", "payment_description": "Guyliner purchase", "customer_first_name": "Jack", "customer_id": "user007", "customer_last_name": "Sparrow", "customer_phone": "02081234567", "close_on_missclick": true, "signature": "SyA3cx/dmFrwjRcpbnwEK9zaklWKR9buIfTctQob/EHUTutFLpI0zWpSDFEWEwbZt/04i83395RCdEhtUMw83A==" }
Request signature generation form
Signature verification
Verification algorithm
The algorithm input includes the following:
- Signed data to verify.
Generally, this is a callback or response body in JSON format with the
signature
parameter. - Verification key. It must be the same key that was previously used for signing the data to verify.
Depending on the algorithm implementation, its output may be either a generated signature or the information whether the generated signature matches the one included in the response or callback.
The algorithm description, the example, and test form below use the most common algorithm implementation that includes a callback or response body in JSON format as input (without the signature
parameter) and output (with the signature
parameter) which is the result of signature verification.
Thus, the algorithm includes the following steps:
-
Input validation Make sure the following requirements are met:
- Data conforms to the JSON format.
- Data to sign contains a
signature
parameter with the signature value. - The signing key is readily available.
- Extracting signature from the data to verify. Store the value of the
signature
parameter value for further reference and remove the parameter from the input data. - Generate signature for the data to verify. Complete steps 2 through 4 as specified in the signature generation algorithm description. (For more details, see the following link):
- Conversion of all the strings to UTF-8 with parameters sorted in natural order.
- Calculation of HMAC code by using the key and the SHA-512 hash function.
- Encoding the HMAC code by using the Base64 scheme.
- Signature matching Compare the generated signature with the signature stored in step 2. If the signatures match, the data are authentic and its integrity is considered confirmed; otherwise, the data is considered compromised and cannot be used for production purposes.
Example of callback verification
Suppose that you need to verify the signature of a callback in the following scenario:
- Signing key :
secret
. - The callback body contains the following information:
The signature is verified as follows:
- Remove the
signature
parameter and its value from the callback:{ "customer": { "id": "782572" }, "account": { "number": "424242******4242", "token": "c8175453f68ec7c8fb3f052b8d786c661261efebcb91155327a6c7b8f8e66359", "type": "visa", "card_holder": "TEST TEST", "expiry_month": "01", "expiry_year": "2025" }, "project_id": 28051, "payment": { "id": "5242723", "type": "purchase", "status": "success", "date": "2023-03-10T12:26:17+0000", "method": "card", "sum": { "amount": 5200, "currency": "EUR" }, "description": "" }, "operation": { "sum_initial": { "amount": 5200, "currency": "EUR" }, "sum_converted": { "amount": 5200, "currency": "EUR" }, "code": "0", "message": "Success", "provider": { "id": 6, "payment_id": "16784511766816", "auth_code": "563253", "endpoint_id": 6, "date": "2023-03-10T10:26:17+0000" }, "id": 5028800010128225, "type": "sale", "status": "success", "date": "2023-03-10T12:26:17+0000", "created_date": "2023-03-10T12:26:15+0000", "request_id": "1f6d3ac37444142f5bd27e7491faa360633fd5a2-fc98e73d475fa4cd6ee02fc6340c964f0267b3d8-05028801" }, "signature": "IszjSnH+UqFp88DF0giI/jUTDHOnfPxc83j2VD/jN4loB9wbHwiO5+KvHfdFE4nBPHhhxD6TXbOkGnRINFTTmg==" }
- Convert all parameter strings to UTF-8 according to the algorithm description:
customer:id:782572 account:number:424242******4242 account:token:c8175453f68ec7c8fb3f052b8d786c661261efebcb91155327a6c7b8f8e66359 account:type:visa account:card_holder:TEST TEST account:expiry_month:01 account:expiry_year:2025 project_id:28051 payment:id:5242723 payment:type:purchase payment:status:success payment:date:2023-03-10T12:26:17+0000 payment:method:card payment:sum:amount:5200 payment:sum:currency:EUR payment:description: operation:sum_initial:amount:5200 operation:sum_initial:currency:EUR operation:sum_converted:amount:5200 operation:sum_converted:currency:EUR operation:code:0 operation:message:Success operation:provider:id:6 operation:provider:payment_id:16784511766816 operation:provider:auth_code:563253 operation:provider:endpoint_id:6 operation:provider:date:2023-03-10T10:26:17+0000 operation:id:5028800010128225 operation:type:sale operation:status:success operation:date:2023-03-10T12:26:17+0000 operation:created_date:2023-03-10T12:26:15+0000 operation:request_id:1f6d3ac37444142f5bd27e7491faa360633fd5a2-fc98e73d475fa4cd6ee02fc6340c964f0267b3d8-05028801
- Sort the strings in natural sort order:
account:card_holder:TEST TEST account:expiry_month:01 account:expiry_year:2025 account:number:424242******4242 account:token:c8175453f68ec7c8fb3f052b8d786c661261efebcb91155327a6c7b8f8e66359 account:type:visa customer:id:782572 operation:code:0 operation:created_date:2023-03-10T12:26:15+0000 operation:date:2023-03-10T12:26:17+0000 operation:id:5028800010128225 operation:message:Success operation:provider:auth_code:563253 operation:provider:date:2023-03-10T10:26:17+0000 operation:provider:endpoint_id:6 operation:provider:id:6 operation:provider:payment_id:16784511766816 operation:request_id:1f6d3ac37444142f5bd27e7491faa360633fd5a2-fc98e73d475fa4cd6ee02fc6340c964f0267b3d8-05028801 operation:status:success operation:sum_converted:amount:5200 operation:sum_converted:currency:EUR operation:sum_initial:amount:5200 operation:sum_initial:currency:EUR operation:type:sale payment:date:2023-03-10T12:26:17+0000 payment:description: payment:id:5242723 payment:method:card payment:status:success payment:sum:amount:5200 payment:sum:currency:EUR payment:type:purchase project_id:28051
- Join all strings in a single string by using semicolon (;) as a delimiter:
account:card_holder:TEST TEST;account:expiry_month:01;account:expiry_year:2025;account:number:424242******4242;account:token:c8175453f68ec7c8fb3f052b8d786c661261efebcb91155327a6c7b8f8e66359;account:type:visa;customer:id:782572;operation:code:0;operation:created_date:2023-03-10T12:26:15+0000;operation:date:2023-03-10T12:26:17+0000;operation:id:5028800010128225;operation:message:Success;operation:provider:auth_code:563253;operation:provider:date:2023-03-10T10:26:17+0000;operation:provider:endpoint_id:6;operation:provider:id:6;operation:provider:payment_id:16784511766816;operation:request_id:1f6d3ac37444142f5bd27e7491faa360633fd5a2-fc98e73d475fa4cd6ee02fc6340c964f0267b3d8-05028801;operation:status:success;operation:sum_converted:amount:5200;operation:sum_converted:currency:EUR;operation:sum_initial:amount:5200;operation:sum_initial:currency:EUR;operation:type:sale;payment:date:2023-03-10T12:26:17+0000;payment:description:;payment:id:5242723;payment:method:card;payment:status:success;payment:sum:amount:5200;payment:sum:currency:EUR;payment:type:purchase;project_id:28051
- Calculate the HMAC code for the string by using the SHA-512 hash function and secret key, and then encode the HMAC code by using the Base64 scheme:
Y0qjN9dDnPTdddkVvXKS1pGp2z8ZpIl60P1CocND3YRxuBNx05ZMnhUaGFt90fPzgwsI/UpLw0q2RR/XTiDQBg==
- Compare the generated signature and the one included in the callback.
In our case, the signatures differ which means that the callback is invalid and must be ignored.
Signature verification form