Created
June 16, 2025 12:07
-
-
Save cjavdev/d654be5d77d5da9b5ad1a0d680f59525 to your computer and use it in GitHub Desktop.
XSolla Webhook Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/webhooks', methods=['POST']) | |
def xsolla_webhook(): | |
# Get the signature from headers | |
signature = request.headers.get('Authorization') | |
# Get the raw request body as a string: | |
payload = request.data.decode('utf-8') | |
payload_with_key = "%s%s" % (payload, XSOLLA_WH_KEY) | |
# Verify the webhook signature if provided | |
if signature: | |
# Remove 'Signature ' prefix if present | |
if signature.startswith('Signature '): | |
signature = signature[10:] | |
# Calculate expected signature | |
expected_signature = hashlib.sha1(payload_with_key.encode('utf-8')).hexdigest() | |
if not hmac.compare_digest(signature, expected_signature): | |
return jsonify({'error': 'Invalid signature'}), 403 | |
# Process the webhook data | |
try: | |
data = json.loads(payload) | |
# Log the webhook event type | |
event_type = data.get('notification_type', 'unknown') | |
print(f"Received XSolla webhook: {event_type}") | |
# Return success response | |
return jsonify({'status': 'success'}), 200 | |
except json.JSONDecodeError: | |
return jsonify({'error': 'Invalid JSON payload'}), 400 | |
except Exception as e: | |
print(f"Error processing webhook: {str(e)}") | |
return jsonify({'error': 'Internal server error'}), 500 | |
return jsonify({'status': 'no signature'}), 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment