示例#1
0
 def test_callback_for_paid_invoice_fires(self):
     one_shot_server = OneShotServer(TEST_PORT, CONTENT_PAID)
     one_shot_server.start()
     q = Queue()
     event_source = BitPayEventSource(TEST_TOKEN,
                                      callback=lambda status: q.put(status),
                                      event_endpoint=TEST_ENDPOINT)
     event_source.start()
     status = q.get()
     self.assertEqual(status, STATUS_PAID)
示例#2
0
 def test_returns_no_connection_status_on_error_when_retrying_is_disabled(
         self):
     q = Queue()
     event_source = BitPayEventSource(TEST_TOKEN,
                                      callback=lambda status: q.put(status),
                                      event_endpoint=TEST_ENDPOINT,
                                      retrying=False)
     event_source.start()
     status = q.get()
     self.assertEqual(status, STATUS_NO_CONNECTION)
示例#3
0
 def __init__(self, invoice_id, url, status, bitcoin_uri, event_token,
              bip70_url, payment_request):
     self.invoice_id = invoice_id
     self.url = url
     self.status = status
     self.bitcoin_uri = bitcoin_uri
     self.event_token = event_token
     self.bip70_url = bip70_url
     self.payment_request = payment_request
     self.event_source = BitPayEventSource(event_token,
                                           callback=self._status_callback)
示例#4
0
 def test_retries_on_connection_errors(self):
     q = Queue()
     event_source = BitPayEventSource(TEST_TOKEN,
                                      callback=lambda status: q.put(status),
                                      event_endpoint=TEST_ENDPOINT,
                                      retrying=True)
     event_source.start()
     time.sleep(0.1)
     one_shot_server = OneShotServer(TEST_PORT, CONTENT_EXPIRED)
     one_shot_server.start()
     status = q.get()
     self.assertEqual(status, STATUS_EXPIRED)
示例#5
0
class BitPayInvoice:
    def __init__(self, invoice_id, url, status, bitcoin_uri, event_token,
                 bip70_url, payment_request):
        self.invoice_id = invoice_id
        self.url = url
        self.status = status
        self.bitcoin_uri = bitcoin_uri
        self.event_token = event_token
        self.bip70_url = bip70_url
        self.payment_request = payment_request
        self.event_source = BitPayEventSource(event_token,
                                              callback=self._status_callback)

    def get_url(self):
        return self.url

    def get_bitcoin_uri(self):
        return self.bitcoin_uri

    def get_bitcoin_uri_with_bluetooth_address(self, bluetooth_address):
        (scheme, netloc, path, query, fragment) = \
                urlparse.urlsplit(self.bitcoin_uri)
        query_parts = urlparse.parse_qs(query)
        query_parts['amount'] = float(query_parts['amount'][0])
        query_parts['r'] = 'bt:%s' % bluetooth_address.replace(':', '')
        query = urllib.urlencode(query_parts)
        return urlparse.urlunsplit((scheme, netloc, path, query, fragment))

    def get_event_token(self):
        return self.event_token

    def get_bip70_url(self):
        return self.bip70_url

    def get_payment_request(self):
        return self.payment_request

    def watch(self, callback):
        """Start a thread that will watch the invoice for status updates. The
        callback will be called with a new status in the form of one of the
        strings defined in the module bitpayeventsource."""
        self.callback = callback
        self.event_source.start()

    def stop_watching(self):
        self.event_source.stop()

    def _status_callback(self, status):
        self.callback(status)

    @staticmethod
    def construct_from_json(json):
        invoice_id = json['id']
        url = json['url']
        status = json['status']

        r = requests.get(url)
        match = re.search(BITCOIN_URI_REGEX, r.text)
        if not match:
            raise Exception("Unable to extract Bitcoin URI")

        bitcoin_uri = match.group(0)
        bitcoin_uri = bitcoin_uri.replace('"', '').replace('&', '&')

        r2 = requests.get(EVENT_TOKEN_URL % invoice_id)
        json = r2.json()
        if not 'data' in json or not 'token' in json['data']:
            raise Exception("Unable to get event token")
        event_token = json['data']['token']

        bip70_url = BIP70_URL % invoice_id
        r3 = requests.get(bip70_url, headers=BIP70_HEADER)
        payment_request = r3.content

        return BitPayInvoice(invoice_id, url, status, bitcoin_uri, event_token,
                             bip70_url, payment_request)
示例#6
0
 def test_can_be_stopped(self):
     event_source = BitPayEventSource(TEST_TOKEN,
                                      callback=lambda status: status,
                                      event_endpoint=TEST_ENDPOINT)
     event_source.start()
     event_source.stop()