This repository was archived by the owner on Oct 21, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathwebpay.py
More file actions
112 lines (92 loc) · 3.34 KB
/
Copy pathwebpay.py
File metadata and controls
112 lines (92 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import requests
import json
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from base64 import b64encode
###############################################
########## BEGIN SCRIPT CONFIG SETUP ##########
###############################################
merchantId = "" # TODO: Replace with your merchantId.
orderId = "" # TODO: Replace with an orderId that you created.
access_token = "" # TODO: Replace with your access_token with PROCESS_CARDS permission.
target_env = "https://sandbox.dev.clover.com"
v2_merchant_path = "/v2/merchant/"
amount = 1000
tipAmount = 0
taxAmount = 0
cardNumber = "6011361000006668"
expMonth = 12
expYear = 2018
cvv = 123
zip_code = 94085
###############################################
########## END SCRIPT CONFIG SETUP ############
###############################################
if not access_token:
print("""Remember to set your access_token with PROCESS_CARDS permission on line 13.
For help creating an access_token, read https://docs.clover.com/clover-platform/docs/using-oauth-20""")
raise NameError("global name 'access_token' is not defined")
# GET /v2/merchant/{mId}/pay/key for the encryption information needed for
# the pay endpoint.
url = target_env + v2_merchant_path + merchantId + "/pay/key"
headers = {"Authorization": "Bearer " + access_token}
print("Requesting GET " + url)
response = requests.get(url, headers=headers)
if response.status_code != requests.codes.ok:
print("Response was not 200 OK!")
print("""Read "Troubleshooting common Clover REST API error codes" at
https://medium.com/clover-platform-blog/troubleshooting-common-clover-rest-api-error-codes-9aaa8885373""")
response.raise_for_status()
try:
response = response.json()
except ValueError:
print(response.text)
raise
print("Response:")
print(json.dumps(response, indent=4))
try:
modulus = int(response['modulus'])
exponent = int(response['exponent'])
prefix = str(response['prefix'])
except KeyError:
raise
# Construct an RSA public key using the modulus and exponent from GET
# /v2/merchant/{mId}/pay/key.
print("Creating public key...")
key = RSA.construct((modulus, exponent))
# Create a cipher from the RSA key and use it to encrypt the card number
# prepended with the prefix from GET /v2/merchant/{mId}/pay/key.
print("Encrypting card number...")
cipher = PKCS1_OAEP.new(key)
encrypted = cipher.encrypt((prefix + cardNumber).encode())
# Base64 encode the resulting encrypted data into a string.
cardEncrypted = b64encode(encrypted)
# POST to /v2/merchant/{mId}/pay.
post_url = target_env + v2_merchant_path + merchantId + '/pay'
headers = {"Authorization": "Bearer " + access_token}
post_data = {
"orderId": orderId,
"currency": "usd",
"amount": amount,
"tipAmount": tipAmount,
"taxAmount": taxAmount,
"expMonth": expMonth,
"expYear": expYear,
"cvv": cvv,
"cardEncrypted": cardEncrypted,
"last4": cardNumber[-4:],
"first6": cardNumber[0:6],
"zip": zip_code
}
print("Requesting POST " + post_url)
post_response = requests.post(post_url, headers=headers, data=post_data)
if post_response.status_code != requests.codes.ok:
print("Response was not 200 OK!")
post_response.raise_for_status()
try:
post_response = post_response.json()
except ValueError:
print(post_response.text)
raise
print("Response:")
print(json.dumps(post_response, indent=4))