Skip to content

Commit 1ae369a

Browse files
authored
Update bitpay_service.py
1 parent f698eda commit 1ae369a

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

blockchain_payment_integration/services/bitpay_service.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,29 @@ def create_payment(self, amount, currency):
2020
payload = {
2121
"price": amount,
2222
"currency": currency,
23-
"notificationURL": "https://your_notification_url.com",
24-
"redirectURL": "https://your_redirect_url.com",
25-
"orderId": "order_id_here",
23+
"notificationURL": os.getenv('NOTIFICATION_URL', 'https://your_notification_url.com'),
24+
"redirectURL": os.getenv('REDIRECT_URL', 'https://your_redirect_url.com'),
25+
"orderId": "order_id_here", # Replace with a unique order ID
2626
"itemDesc": "Payment for Order"
2727
}
2828

2929
try:
3030
response = requests.post(self.api_url, json=payload, headers=self.headers)
3131
response.raise_for_status() # Raise an error for bad responses
3232
payment_data = response.json()
33-
logger.info("BitPay payment created successfully: %s", payment_data)
34-
return payment_data
33+
34+
# Validate response structure
35+
if 'data' not in payment_data or 'id' not in payment_data['data']:
36+
logger.error("Invalid response structure from BitPay: %s", payment_data)
37+
raise ValueError("Invalid response from BitPay API")
38+
39+
logger.info("BitPay payment created successfully: %s", payment_data['data'])
40+
return payment_data['data']
3541
except requests.exceptions.HTTPError as http_err:
36-
logger.error("HTTP error occurred: %s", http_err)
42+
logger.error("HTTP error occurred while creating payment: %s", http_err)
3743
raise
3844
except Exception as err:
39-
logger.error("An error occurred: %s", err)
45+
logger.error("An error occurred while creating payment: %s", err)
4046
raise
4147

4248
def get_payment_status(self, invoice_id):
@@ -45,11 +51,44 @@ def get_payment_status(self, invoice_id):
4551
response = requests.get(f"{self.api_url}/{invoice_id}", headers=self.headers)
4652
response.raise_for_status()
4753
payment_data = response.json()
48-
logger.info("Retrieved payment status: %s", payment_data)
49-
return payment_data
54+
55+
# Validate response structure
56+
if 'data' not in payment_data:
57+
logger.error("Invalid response structure from BitPay: %s", payment_data)
58+
raise ValueError("Invalid response from BitPay API")
59+
60+
logger.info("Retrieved payment status: %s", payment_data['data'])
61+
return payment_data['data']
62+
except requests.exceptions.HTTPError as http_err:
63+
logger.error("HTTP error occurred while retrieving payment status: %s", http_err)
64+
raise
65+
except Exception as err:
66+
logger.error("An error occurred while retrieving payment status: %s", err)
67+
raise
68+
69+
def refund_payment(self, invoice_id, amount):
70+
"""Request a refund for a payment."""
71+
payload = {
72+
"amount": amount,
73+
"currency": "USD", # Adjust as necessary
74+
"invoiceId": invoice_id
75+
}
76+
77+
try:
78+
response = requests.post(f"{self.api_url}/{invoice_id}/refund", json=payload, headers=self.headers)
79+
response.raise_for_status()
80+
refund_data = response.json()
81+
82+
# Validate response structure
83+
if 'data' not in refund_data or 'id' not in refund_data['data']:
84+
logger.error("Invalid response structure from BitPay refund: %s", refund_data)
85+
raise ValueError("Invalid response from BitPay API for refund")
86+
87+
logger.info("Refund requested successfully: %s", refund_data['data'])
88+
return refund_data['data']
5089
except requests.exceptions.HTTPError as http_err:
51-
logger.error("HTTP error occurred: %s", http_err)
90+
logger.error("HTTP error occurred while requesting refund: %s", http_err)
5291
raise
5392
except Exception as err:
54-
logger.error("An error occurred: %s", err)
93+
logger.error("An error occurred while requesting refund: %s", err)
5594
raise

0 commit comments

Comments
 (0)