Fakultas Ilmu Komputer UI

Commit ceb6bd01 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

Merge branch 'PBI-8-PaymentIntegration' into 'staging'

Payment Integration

See merge request !45
parents b5c260dc e4ad485a
Pipeline #73244 passed with stages
in 8 minutes and 29 seconds
DATABASE_URL=postgres://<username>:<password>@127.0.0.1:5432/dietela-backend
TEST_DATABASE=test-dietela-backend
AUTH_STRING=
import json
from unittest.mock import patch
from rest_framework.test import APITestCase
from django.test import TestCase
from rest_framework import status
from rest_framework.response import Response
from .models import Cart
from .serializers import CartSerializer
from .views import PaymentViewSet
from authentication.models import CustomUser
from dietela_quiz.models import *
from dietela_program.models import DietelaProgram
from nutritionists.models import Nutritionist
from authentication.models import CustomUser
class CartTests(APITestCase):
......@@ -48,7 +53,6 @@ class CartTests(APITestCase):
nutritionist = cls.nutritionist_1,
)
def test_get_all_carts(self):
response = self.client.get(self.BASE_URL)
......@@ -88,3 +92,53 @@ class CartTests(APITestCase):
self.assertEqual(Cart.objects.count(), 3)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
@patch('payment.views.json.loads')
class TestPaymentIntegration(TestCase):
@classmethod
def setUpTestData(cls):
cls.PAYMENT_URL = "/payment/midtrans/"
cls.nutritionist = Nutritionist.objects.create(
full_name_and_degree="Test, S.Gz",
registration_certificate_no="1234567890",
university="Universitas Indonesia",
mastered_nutritional_problems="Manajemen berat badan, hipertensi",
handled_age_group="12-17 tahun (Remaja)",
another_practice_place="RSCM",
languages="Bahasa Indonesia, Bahasa Inggris",
)
cls.dietela_program = DietelaProgram.objects.create(
unique_code="PRG1",
name="Program 1",
price=350000.00,
)
cls.custom_user = CustomUser.objects.create_user(name='tes', email='email@email.com', password='abc')
cls.cart = Cart.objects.create(
program = cls.dietela_program,
nutritionist = cls.nutritionist,
user = cls.custom_user
)
def test_post_payment_succeed(self, mock_json_loads):
mock_json_return_value = {'token': 'abc', 'redirect_url': 'https://redirect.url'}
mock_json_loads.return_value = mock_json_return_value
response = self.client.post(self.PAYMENT_URL, {'cart_id': self.cart.id}, format='json')
json_response = json.loads(response.content)
self.assertIn('token', json_response)
self.assertIn('redirect_url', json_response)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_post_payment_failed_because_cart_has_been_paid(self, mock_json_loads):
mock_json_return_value = {'error_messages':
["transaction_details.order_id sudah digunakan"]
}
mock_json_loads.return_value = mock_json_return_value
response = self.client.post(self.PAYMENT_URL, {'cart_id': self.cart.id}, format='json')
json_response = json.loads(response.content)
self.assertNotIn('token', json_response)
self.assertNotIn('redirect_url', json_response)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
from rest_framework import routers
from .views import CartViewSet
from .views import CartViewSet, PaymentViewSet
router = routers.SimpleRouter()
router.register('payment/cart', CartViewSet)
router.register('payment/midtrans', PaymentViewSet, \
basename="Payment")
urlpatterns = []
......
import json
import os
from rest_framework import viewsets, status
from rest_framework.response import Response
import requests
from dietela_program.serializers import DietelaProgramSerializer
from dietela_program.models import DietelaProgram
from .serializers import CartSerializer
from .models import Cart
class CartViewSet(viewsets.ModelViewSet):
serializer_class = CartSerializer
queryset = Cart.objects.all()
......@@ -25,3 +27,47 @@ class CartViewSet(viewsets.ModelViewSet):
response['program'] = DietelaProgramSerializer(program).data
return Response(response, status=status.HTTP_201_CREATED, headers=headers)
class PaymentViewSet(viewsets.ViewSet):
MIDTRANS_URL = "https://app.sandbox.midtrans.com/snap/v1/transactions"
POST_PAYMENT_HEADERS = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Basic {os.environ.get('AUTH_STRING')}"
}
@staticmethod
def create_payload(cart):
payload = {
"transaction_details": {
"order_id": cart.id,
"gross_amount": cart.program.price,
},
"item_details": [{
"id": cart.program.unique_code,
"price": cart.program.price,
"quantity": 1,
"name": cart.program.name,
}],
"customer_details": {
"name": cart.user.name,
"email": cart.user.email,
}
}
return json.dumps(payload)
def create(self, request):
cart_id = request.data['cart_id']
cart = Cart.objects.get(pk=cart_id)
payload = self.create_payload(cart)
response = requests.post(self.MIDTRANS_URL, payload, headers=self.POST_PAYMENT_HEADERS)
json_response = json.loads(response.content)
if not('token' in json_response and 'redirect_url' in json_response):
return Response(json_response, status=status.HTTP_400_BAD_REQUEST)
return Response(json_response, status=status.HTTP_200_OK)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment