# Resulting notification string (in format application/json)
request_body_json = """
{
"shop_id": 100,
"shop_order_id": "The store's order ID",
"description": "Your payment description",
"shop_amount": 95.00,
"shop_refund": 92.75,
"shop_currency": 840,
"payment_id": 1234,
"client_price": 97.55,
"ps_currency": "840",
"payway": "some_payway_name",
"ps_data": "{\"ps_payer_account\": \"525634XXXXXX7704\"}",
"created": "2026-06-01T15:38:07",
"updated": "2026-06-01T15:38:07",
"status": "success",
"addons": "{\"comment\": \"Notification text\"}",
"sign": "1d51960a52a6c7e8cf957d1a22ad2b3263603bace013facc335dd1429ecb4e97"
}"""
import json
parsed_request = json.loads(request_body_json)
# Resulting notification string (in format application/x-www-form-urlencoded)
if content_type == 'application/x-www-form-urlencoded':
request_body_form_urlencoded = (
'shop_id=100&shop_order_id=The+store%27s+order+ID&description=Your+payment+description&sh'
'op_amount=95.0&shop_refund=92.75&shop_currency=840&payment_id=1234&client_price=97.55&ps'
'_currency=840&payway=some_payway_name&ps_data=%7B%22ps_payer_account%22%3A+%22525634XXXX'
'XX7704%22%7D&created=2026-06-01T15%3A38%3A07&updated=2026-06-01T15%3A38%3A07&status=succ'
'ess&addons=%7B%22comment%22%3A+%22Notification+text%22%7D&sign=1d51960a52a6c7e8cf957d1a2'
'2ad2b3263603bace013facc335dd1429ecb4e97'
)
from urllib.parse import parse_qsl
parsed_request = dict(parse_qsl(request_body_form_urlencoded))
print(parsed_request)
# {
# 'shop_id': 100,
# 'shop_order_id': 'The store's order ID',
# 'description': 'Your payment description',
# 'shop_amount': 95.00,
# 'shop_refund': 92.75,
# 'shop_currency': 840,
# 'payment_id': '1234',
# 'client_price': 97.55,
# 'ps_currency': 840,
# 'payway': 'some_payway_name',
# 'ps_data': '{\"ps_payer_account\": \"525634XXXXXX7704\',
# 'created': '2026-06-01T15:38:07',
# 'updated': '2026-06-01T15:38:07',
# 'status': 'success',
# 'addons': '{\"comment\": \"Notification text\"}',
# 'sign': '1d51960a52a6c7e8cf957d1a22ad2b3263603bace013facc335dd1429ecb4e97',
# }
# Let's get a list of sorted keys for signature generation
keys = sorted(parsed_request)
# Let's remove the signed key from them.
keys.remove('sign')
print(keys)
# This is how the resulting list will look like
# ['addons',
# 'client_price',
# 'created',
# 'description',
# 'payment_id',
# 'payway',
# 'ps_currency',
# 'ps_data',
# 'shop_amount',
# 'shop_currency',
# 'shop_id',
# 'shop_order_id',
# 'shop_refund',
# 'status',
# 'updated']
# Let's compose a line using the sorted key values obtained in the previous step
values_to_sign = []
for k in keys:
if parsed_request[k] != '' and parsed_request[k] is not None:
values_to_sign.append(str(parsed_request[k]))
# Let's form the final signature and check its value with the value received in the response
string_to_sign = ':'.join(values_to_sign) + '<secret_key_here>'
import hashlib
sign = hashlib.sha256(string_to_sign.encode()).hexdigest()
assert sign == parsed_request['sign']