· 4 years ago · Jun 21, 2021, 08:54 PM
1import hashlib
2import base64
3import json
4
5import requests
6from datetime import datetime
7import calendar
8import string
9from random import *
10import hmac
11
12
13def get_checkout_url(amount, complete_checkout_url):
14 idempotency_key = 'avsdvaew' # Unique for each 'Create Payment' request.
15
16 http_method = 'post' # Lower case.
17 path = '/v1/checkout' # Portion after the base URL.
18
19 # salt: randomly generated for each request.
20 min_char = 8
21 max_char = 12
22 allchar = string.ascii_letters + string.punctuation + string.digits
23 salt = "".join(choice(allchar)for x in range(randint(min_char, max_char)))
24
25 # Current Unix time.
26 d = datetime.utcnow()
27 timestamp = calendar.timegm(d.utctimetuple())
28
29 access_key = '858F6B1E478704567C60' # The access key received from Rapyd.
30 secret_key = '20fef915f944b605fd98199609ebb6e70c427d1530e3ec651cd596b7154e8d42c3b630383a95af65' # Never transmit the secret key by itself.
31
32 body = {
33 "amount": amount,
34 "country": "US",
35 "currency": "USD",
36 "requested_currency": "USD",
37 "merchant_reference_id": "950ae8c6-76",
38 }
39
40 to_sign = http_method + path + salt + str(timestamp) + access_key + secret_key + json.dumps(body, separators=(',',':'))
41
42 h = hmac.new(bytes(secret_key, 'utf-8'), bytes(to_sign, 'utf-8'), hashlib.sha256)
43
44 signature = base64.urlsafe_b64encode(str.encode(h.hexdigest()))
45 headers = {
46 'access_key': access_key,
47 'signature': signature.decode(),
48 'salt': salt,
49 'timestamp': str(timestamp),
50 'Content-Type': 'application\/json'
51 }
52
53 r = requests.post('https://sandboxapi.rapyd.net/v1/checkout', headers=headers, json=body)
54 return r.json()