def client_from_stored_values(): for f in ["local.pem", "tokens.json"]: try: open("temp/" + f) exists = True except: exists = False break if exists: f = open("temp/local.pem", 'r') pem = f.read() f = open("temp/tokens.json", 'r') token = f.read() token = json.loads(token) client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=pem, tokens=token) else: claim_code = get_claim_code_from_server() pem = key_utils.generate_pem() client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=pem) token = json.dumps(client.pair_pos_client(claim_code)) if not os.path.exists("temp"): os.makedirs("temp") f = open("temp/local.pem", 'w') f.write(pem) f = open("temp/tokens.json", 'w') f.write(token) return client
def test_unsigned_request_rates(self): """tests whether the generic wrapper returns properly when asked for rates """ new_client = Client() request = new_client.unsigned_request('/rates/EUR') self.assertIn('rate', request.json()['data'])
def test_passes_errors_when_creating_invoice(self): """web errors should be gracefully passed to the client""" new_client = Client() def a_request(url, request): return {'status_code': 403, 'content': b'{"error": "this is a 403 error"}'} with HTTMock(a_request): with self.assertRaisesRegex(BitPayBitPayError, "403: this is a 403 error"): new_client.create_invoice({"price": 20, "currency": "USD"})
def test_passes_errors_when_pairing(self): """web errors should be gracefully passed to the client""" new_client = Client() def a_request(url, request): return {'status_code': 403, 'content': b'{"error": "this is a 403 error"}'} with HTTMock(a_request): with self.assertRaisesRegex(BitPayBitPayError, "403: this is a 403 error"): new_client.pair_pos_client("a1B2c3d")
def step_impl(context): badAddress = ROOT_ADDRESS.split(":") badAddress = badAddress[0] + ":" + badAddress[1] + ":999" newclient = Client(api_uri=badAddress, insecure=True) try: newclient.pair_pos_client("1a2C3d4") raise "That should totally not have worked" except Exception as error: global exception exception = error
def step_impl(context): claim_code = get_claim_code_from_server() global client client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=PEM) try: client.pair_pos_client(claim_code) except Exception as error: if error.args[0] == "500: Unable to create token because of too many requests.": time.sleep(60) client.pair_pos_client(claim_code) assert client.tokens['pos']
def test_passes_errors_when_pairing(self): """web errors should be gracefully passed to the client""" new_client = Client() def a_request(url, request): return { 'status_code': 403, 'content': b'{"error": "this is a 403 error"}' } with HTTMock(a_request): with self.assertRaisesRegex(BitPayBitPayError, "403: this is a 403 error"): new_client.pair_pos_client("a1B2c3d")
def test_passes_errors_when_creating_invoice(self): """web errors should be gracefully passed to the client""" new_client = Client() def a_request(url, request): return { 'status_code': 403, 'content': b'{"error": "this is a 403 error"}' } with HTTMock(a_request): with self.assertRaisesRegex(BitPayBitPayError, "403: this is a 403 error"): new_client.create_invoice({"price": 20, "currency": "USD"})
def post(self, request, **kwargs): tour_id = self.kwargs.get('tour_id') try: tour = Tour.objects.get(id=tour_id) if tour.get_remaining_seats() == 0: raise Http404 except: raise Http404 if hasattr(settings, 'NGROK_URL'): ipn_url = settings.NGROK_URL + reverse('bitpay-ipn') else: ipn_url = self.request.build_absolute_uri(reverse('bitpay-ipn')) try: f = open(settings.BITPAY_API_KEY_FILE, 'r') pem = f.read() except Exception as ex: logging.error(ex) return HttpResponse(status=500) if settings.BITPAY_TEST is True: client = Client('https://test.bitpay.com', False, pem, tokens={'merchant': settings.BITPAY_API_TOKEN}) else: client = Client('https://bitpay.com', False, pem, tokens={'merchant': settings.BITPAY_API_TOKEN}) order_id = "{}-{}-{}".format(self.request.user.id, tour.id, int(time.time())) bitpay_invoice_payload = { "price": str(tour.get_price()), "currency": "USD", "transactionSpeed": "medium", "fullNotifications": "true", "notificationURL": ipn_url + '?order_id=' + order_id, "redirectURL": self.request.build_absolute_uri(reverse('tour_success', kwargs={'tour_id': tour_id})), "orderId": order_id, "token": client.tokens['merchant'] } if self.request.user.email: bitpay_invoice_payload['buyer'] = { 'email': self.request.user.email } try: response = client.create_invoice(bitpay_invoice_payload) return HttpResponseRedirect(response['url']) except Exception as ex: logging.error(ex) return HttpResponse(status=500)
def test_pair_code_check(self): """tests whether the pairing code is syntatically correct""" new_client = Client(api_uri="https://test.bitpay.com") if int(sys.version[0]) == 3: with self.assertRaisesRegex(BitPayArgumentError, "pairing code is not legal"): new_client.pair_pos_client("abcd") else: with self.assertRaisesRegexp(BitPayArgumentError, "pairing code is not legal"): new_client.pair_pos_client("abcd")
def step_impl(context): claim_code = get_claim_code_from_server() global client client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=PEM) try: client.pair_pos_client(claim_code) except Exception as error: if error.args[ 0] == "500: Unable to create token because of too many requests.": time.sleep(60) client.pair_pos_client(claim_code) assert client.tokens['pos']
def step_impl(context): global pairing_code client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=PEM) pairing_code = client.create_token("merchant")
from splinter import Browser import time import os import six import json import re from bitpay.client import Client from bitpay import key_utils ROOT_ADDRESS = os.environ['RCROOTADDRESS'] USER_NAME = os.environ['RCTESTUSER'] PASSWORD = os.environ['RCTESTPASSWORD'] PEM = '-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEICg7E4NN53YkaWuAwpoqjfAofjzKI7Jq1f532dX+0O6QoAcGBSuBBAAK\noUQDQgAEjZcNa6Kdz6GQwXcUD9iJ+t1tJZCx7hpqBuJV2/IrQBfue8jh8H7Q/4vX\nfAArmNMaGotTpjdnymWlMfszzXJhlw==\n-----END EC PRIVATE KEY-----\n' client = Client() invoice = None exception = None @given(u'the user pairs with BitPay with a valid pairing code') def step_impl(context): claim_code = get_claim_code_from_server() global client client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=PEM) try: client.pair_pos_client(claim_code) except Exception as error: if error.args[ 0] == "500: Unable to create token because of too many requests.": time.sleep(60) client.pair_pos_client(claim_code) assert client.tokens['pos']
def test_pair_code_check(self): """tests whether the pairing code is syntatically correct""" new_client = Client() with self.assertRaisesRegex(BitPayArgumentError, "pairing code is not legal"): new_client.pair_pos_client("abcd")
def client(self): return Client(api_uri=self.settings.url, pem=self.settings.pem)
import bitpay.key_utils as key_utils from bitpay.client import Client with open("keys/local.pem", 'r') as f: key = f.read() client = Client(api_uri="https://bitpay.com", insecure=False, pem=key, tokens={}) token = client.create_merchant_token() print(token) # This should print something like the following: # {'data': [{'policies': [{'policy': 'id', 'method': 'inactive', 'params': ['Txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx']}], 'token': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'facade': 'merchant', 'dateCreated': 1490000000000, 'pairingExpiration': 1490000000000, 'pairingCode': 'xxxxxxx'}]} with open("tokens/local.txt", 'w') as f: f.write(str(token))