This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
/
test_payouts.py
95 lines (86 loc) · 3.59 KB
/
test_payouts.py
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
from bitpay.bitpay_exceptions import *
import bitpay.bitpay_key_utils as bku
from bitpay.bitpay_client import *
import pprint
import requests
import json
import re
import os.path
#API_HOST = "https://bitpay.com" #for production, live bitcoin
API_HOST = "https://test.bitpay.com" #for testing, testnet bitcoin
KEY_FILE = "/tmp/key.priv"
TOKEN_FILE = "/tmp/token.priv"
pp = pprint.PrettyPrinter(indent=4)
# check if there is a preexisting key file
if os.path.isfile(KEY_FILE):
f = open(KEY_FILE, 'r')
key = f.read()
f.close()
print("Creating a bitpay client using existing private key from disk.")
else:
key = bku.generate_pem()
f = open(KEY_FILE, 'w')
f.write(key)
f.close()
client = Client(API_HOST, False, key)
def fetch_token(facade):
if os.path.isfile(TOKEN_FILE + facade):
f = open(TOKEN_FILE + facade, 'r')
token = f.read()
f.close()
print("Reading " + facade + " token from disk.")
client.tokens[facade] = token
else:
pairingCode = client.create_token(facade)
print("Creating " + facade + " token.")
print("Please go to: %s/dashboard/merchant/api-tokens then enter \"%s\" then click the \"Find\" button, then click \"Approve\"" % (API_HOST, pairingCode))
raw_input("When you've complete the above, hit enter to continue...")
print("token is: %s" % client.tokens[facade])
f = open(TOKEN_FILE + facade, 'w')
f.write(client.tokens[facade])
f.close()
def get_from_bitpay_api(client, uri, token):
payload = "?token=%s" % token
xidentity = bku.get_compressed_public_key_from_pem(client.pem)
xsignature = bku.sign(uri + payload, client.pem)
headers = {"content-type": "application/json",
"X-Identity": xidentity,
"X-Signature": xsignature, "X-accept-version": "2.0.0"}
try:
pp.pprint(headers)
print(uri + payload)
response = requests.get(uri + payload, headers=headers, verify=client.verify)
except Exception as pro:
raise BitPayConnectionError(pro.args)
if response.ok:
return response.json()['data']
client.response_error(response)
"""
POST to any resource
Make sure to include the proper token in the params
"""
def post_to_bitpay_api(client, uri, resource, params):
payload = json.dumps(params)
uri = uri + "/" + resource
xidentity = key_utils.get_compressed_public_key_from_pem(client.pem)
xsignature = key_utils.sign(uri + payload, client.pem)
headers = {"content-type": "application/json",
"accept": "application/json", "X-Identity": xidentity,
"X-Signature": xsignature, "X-accept-version": "2.0.0"}
try:
response = requests.post(uri, data=payload, headers=headers,
verify=client.verify)
except Exception as pro:
raise BitPayConnectionError(pro.args)
if response.ok:
return response.json()['data']
client.response_error(response)
fetch_token("payroll")
#create a payroll token
token = client.tokens['payroll']
print("Creating a payout batch now")
print("token = " + token)
# posting a payout batch
params = {"token":token, "notificationURL":"http://app-1494237706.000webhostapp.com/IPNlogger.php", "notificationEmail":"pieter@bitpay.com", "effectiveDate":"2017-08-23", "amount":"400","currency":"USD","instructions":[ {"label":"Test1","address":"mzDTjhkfJfatXHRUWKcE2BXxHt4Pfz2PK7","amount":"300"},{"label":"Test2","address":"mfadguj41aYgEwPATAFnkKcKQNqhpNTrdi","amount":"100"}]}
payoutBatch = post_to_bitpay_api(client, "https://test.bitpay.com", "payouts", params)
pp.pprint(payoutBatch)