This is the view:
class PhonePePaymentView(APIView):
def post(self, request):
payment_details = request.data
customer_data = request.data.get("customer")
print(customer_data["phone_number"])
try:
customer = Customer.objects.get(phone_number=customer_data["phone_number"])
except Customer.DoesNotExist:
serializer = CustomerSerializer(data=customer_data)
if serializer.is_valid():
customer = serializer.save()
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
merchant_id = settings.PHONEPE_MERCHANT_ID
salt_key = settings.PHONEPE_SALT_KEY
salt_index = int(settings.PHONEPE_SALT_INDEX)
print(merchant_id, salt_key, salt_index)
env = Env.PROD
phonepe_client = PhonePePaymentClient(
merchant_id=merchant_id, salt_key=salt_key, salt_index=salt_index, env=env
)
product_ids = payment_details.get("product_ids", [])
total_amount = sum(
Product.objects.filter(id__in=product_ids).values_list("price", flat=True)
)
if payment_details.get("coupon_code"):
coupon = Coupon.objects.get(code=payment_details.get("coupon_code"))
total_amount -= coupon.amount
pay_page_request = PgPayRequest.pay_page_pay_request_builder(
merchant_transaction_id=str(uuid.uuid4()),
amount=total_amount,
merchant_user_id=customer.id,
callback_url=settings.PHONEPE_S2S_CALLBACK_URL,
redirect_url=settings.PHONEPE_UI_REDIRECT_URL,
)