Issues (15)

examples/test_merchant_facade.py (3 issues)

1
from bitpay.bitpay_exceptions import *
2
import bitpay.bitpay_key_utils as bku
3
from bitpay.bitpay_client import *
4
import pprint
5
import requests
6
import json
7
import re
8
import os.path
9
10
#API_HOST = "https://bitpay.com" #for production, live bitcoin
11
API_HOST = "https://test.bitpay.com" #for testing, testnet bitcoin
12
KEY_FILE = "/tmp/key.priv"
13
TOKEN_FILE = "/tmp/token.priv"
14
15
# check if there is a preexisting key file
16
if os.path.isfile(KEY_FILE):
17
    f = open(KEY_FILE, 'r')
18
    key = f.read()
19
    f.close()
20
    print("Creating a bitpay client using existing private key from disk.")
21
else:
22
    key = bku.generate_pem()
23
    f = open(KEY_FILE, 'w')
24
    f.write(key)
25
    f.close()
26
27
client = Client(API_HOST, False, key)
28
29 View Code Duplication
def fetch_token(facade):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
30
    if os.path.isfile(TOKEN_FILE + facade):
31
        f = open(TOKEN_FILE + facade, 'r')
32
        token = f.read()
33
        f.close()
34
        print("Reading " + facade + " token from disk.")
35
        #global client
36
        #client = Client(API_HOST, False, key, {facade: token})
37
        client.tokens[facade] = token
38
    else:
39
        pairingCode = client.create_token(facade)
40
        print("Creating " + facade + " token.")
41
        print("Please go to:  %s/dashboard/merchant/api-tokens  then enter \"%s\" then click the \"Find\" button, then click \"Approve\"" % (API_HOST, pairingCode))
42
        raw_input("When you've complete the above, hit enter to continue...")
43
        print("token is: %s" % client.tokens[facade])
44
        f = open(TOKEN_FILE + facade, 'w')
45
        f.write(client.tokens[facade])
46
        f.close() 
47
48 View Code Duplication
def get_from_bitpay_api(client, uri, token):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
49
    payload = "?token=%s" % token
50
    xidentity = bku.get_compressed_public_key_from_pem(client.pem)
51
    xsignature = bku.sign(uri + payload, client.pem)
52
    headers = {"content-type": "application/json",
53
                "X-Identity": xidentity,
54
                "X-Signature": xsignature, "X-accept-version": "2.0.0"}
55
    try:
56
        pp.pprint(headers)
57
        print(uri + payload)
58
        response = requests.get(uri + payload, headers=headers, verify=client.verify)
59
    except Exception as pro:
60
        raise BitPayConnectionError(pro.args)
61
    if response.ok:
62
        return response.json()['data']
63
    client.response_error(response)
64
65
"""
66
POST to any resource
67
Make sure to include the proper token in the params
68
"""
69 View Code Duplication
def post_to_bitpay_api(client, uri, resource, params):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
70
    payload = json.dumps(params)
71
    uri = uri + "/" + resource
72
    xidentity = key_utils.get_compressed_public_key_from_pem(client.pem)
73
    xsignature = key_utils.sign(uri + payload, client.pem)
74
    headers = {"content-type": "application/json",
75
               "accept": "application/json", "X-Identity": xidentity,
76
               "X-Signature": xsignature, "X-accept-version": "2.0.0"}
77
    try:
78
        response = requests.post(uri, data=payload, headers=headers,
79
                                 verify=client.verify)
80
    except Exception as pro:
81
        raise BitPayConnectionError(pro.args)
82
    if response.ok:
83
        return response.json()['data']
84
    client.response_error(response)
85
86
fetch_token("merchant")
87
88
#Now we assume that the pairing code that we generated along with the crypto keys is paired with your merchant account
89
#
90
print("We will create an invoice using the merchant facade")
91
92
invoice = client.create_invoice({"price": 50.00, "currency": "USD", "token": client.tokens['merchant']})
93
94
pp = pprint.PrettyPrinter(indent=4)
95
pp.pprint(invoice)
96
97
print("hopefully the above looks OK?")
98
99
print("continuing if we can...")
100
101
102
invoiceId = "REPLACE_BY_VALID_INVOICEID"
103
print("**")
104
print("Now fetching an invoice with invoiceId " + invoiceId)
105
print("**")
106
token = client.tokens['merchant']
107
invoice = get_from_bitpay_api(client, client.uri + "/invoices/" + invoiceId, token)
108
pp.pprint(invoice)
109
110