51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
import requests
|
|
import logging
|
|
from django.conf import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class ThawaniClient:
|
|
def __init__(self):
|
|
self.secret_key = getattr(settings, 'THAWANI_SECRET_KEY', '')
|
|
self.publishable_key = getattr(settings, 'THAWANI_PUBLISHABLE_KEY', '')
|
|
self.base_url = getattr(settings, 'THAWANI_BASE_URL', 'https://uatcheckout.thawani.om')
|
|
self.headers = {
|
|
'thawani-api-key': self.secret_key,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
|
|
def create_checkout_session(self, payload):
|
|
"""
|
|
Create a checkout session with Thawani.
|
|
Payload should include: client_reference_id, products, success_url, cancel_url, metadata.
|
|
"""
|
|
url = f"{self.base_url}/api/v1/checkout/session"
|
|
try:
|
|
response = requests.post(url, json=payload, headers=self.headers)
|
|
response.raise_for_status()
|
|
return response.json()
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"Thawani session creation failed: {e}")
|
|
if hasattr(e, 'response') and e.response:
|
|
logger.error(f"Thawani error response: {e.response.text}")
|
|
return None
|
|
|
|
def get_checkout_session(self, session_id):
|
|
"""
|
|
Retrieve a checkout session to check its status.
|
|
"""
|
|
url = f"{self.base_url}/api/v1/checkout/session/{session_id}"
|
|
try:
|
|
response = requests.get(url, headers=self.headers)
|
|
response.raise_for_status()
|
|
return response.json()
|
|
except requests.exceptions.RequestException as e:
|
|
logger.error(f"Thawani session retrieval failed: {e}")
|
|
return None
|
|
|
|
def get_payment_url(self, session_id):
|
|
"""
|
|
Construct the payment URL to redirect the user.
|
|
"""
|
|
return f"{self.base_url}/pay/{session_id}?key={self.publishable_key}"
|