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_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)