emile 1 year ago
parent 207074acfe
commit a6da681209

@ -8,6 +8,7 @@ urlpatterns = [
path('products/', views.customer_products, name='customerproducts'), path('products/', views.customer_products, name='customerproducts'),
path('pricing/', views.pricing, name='pricing'), path('pricing/', views.pricing, name='pricing'),
path('initiate_checkout/', views.initiate_checkout, name='initiatecheckout'), path('initiate_checkout/', views.initiate_checkout, name='initiatecheckout'),
path('check-order-status/<str:merchant_id>/<str:order_id>/', views.check_order_status, name='check_order_status'),
path('webhooks/', views.webhook_handler, name='webhook_handlers'), path('webhooks/', views.webhook_handler, name='webhook_handlers'),

@ -4,6 +4,7 @@ from django.http import JsonResponse
import requests import requests
from .decorators import * from .decorators import *
import uuid import uuid
import base64
# Create your views here. # Create your views here.
@customer_login_required @customer_login_required
@ -37,6 +38,17 @@ def pricing(request, *args, **kwargs):
def basic_auth_header(username, password):
credentials = f"merchant.{username}:{password}"
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
return f"Basic {encoded_credentials}"
@customer_login_required @customer_login_required
def initiate_checkout(request): def initiate_checkout(request):
api_username = 'merchant.TEST06127800' api_username = 'merchant.TEST06127800'
@ -45,6 +57,8 @@ def initiate_checkout(request):
merchant_name = 'Ositcom Sal' merchant_name = 'Ositcom Sal'
order_id = str(uuid.uuid4())[:8] order_id = str(uuid.uuid4())[:8]
payload = { payload = {
'apiOperation': 'INITIATE_CHECKOUT', 'apiOperation': 'INITIATE_CHECKOUT',
@ -57,7 +71,8 @@ def initiate_checkout(request):
'order.amount': '100.00', 'order.amount': '100.00',
'order.currency': 'USD', 'order.currency': 'USD',
'order.description': 'description_of_order', 'order.description': 'description_of_order',
'order.notificationUrl' : 'https://newosina.osinode.com/webhooks/' 'order.notificationUrl' : 'https://newosina.osinode.com/webhooks/',
'interaction.returnUrl' : f"https://newosina.osinode.com/check-order-status/{merchant_id}/{order_id}/"
} }
try: try:
@ -70,6 +85,7 @@ def initiate_checkout(request):
parsed_data = dict(item.split('=') for item in response_data.split('&')) parsed_data = dict(item.split('=') for item in response_data.split('&'))
session_id = parsed_data.get('session.id') session_id = parsed_data.get('session.id')
return JsonResponse({'session_id': session_id}, status=200) return JsonResponse({'session_id': session_id}, status=200)
else: else:
print('Response Status Code:', response.status_code) print('Response Status Code:', response.status_code)
return JsonResponse({'error': 'Failed to initiate checkout'}, status=500) return JsonResponse({'error': 'Failed to initiate checkout'}, status=500)
@ -79,6 +95,30 @@ def initiate_checkout(request):
def check_order_status(request, merchant_id, order_id):
api_password = '37846250a67c70e7fe9f82cf6ca81f93'
url = f"https://creditlibanais-netcommerce.gateway.mastercard.com/api/rest/version/78/merchant/{merchant_id}/order/{order_id}"
headers = {
'Content-Type': 'application/json',
'Authorization': basic_auth_header(merchant_id, api_password)
}
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
order_details = response.json()
return JsonResponse(order_details)
else:
error_message = 'Failed to retrieve order details: ' + response.text
return JsonResponse({'error': error_message}, status=500)
except Exception as e:
error_message = 'Exception: ' + str(e)
return JsonResponse({'error': error_message}, status=500)
def webhook_handler(request): def webhook_handler(request):
project_type = ProjectType.objects.create(name='Hello') project_type = ProjectType.objects.create(name='Hello')

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save