Skip to content

Commit

Permalink
Razor pay payment gateway integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuhin-thinks committed Aug 26, 2023
1 parent 26754f7 commit 8a3408c
Show file tree
Hide file tree
Showing 18 changed files with 458 additions and 0 deletions.
22 changes: 22 additions & 0 deletions razorpay_second/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'razorpay_second.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file.
16 changes: 16 additions & 0 deletions razorpay_second/razorpay_second/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for razorpay_second project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'razorpay_second.settings')

application = get_asgi_application()
125 changes: 125 additions & 0 deletions razorpay_second/razorpay_second/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"""
Django settings for razorpay_second project.
Generated by 'django-admin startproject' using Django 4.2.4.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-0taejj70e00spj%u_#p%fj62o11s849l!$%$e-^t%0f0%1d6b%"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
"src",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.humanize",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "razorpay_second.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = "razorpay_second.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_URL = "/static/"
MEDIA_URL = "media/"
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
20 changes: 20 additions & 0 deletions razorpay_second/razorpay_second/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
URL configuration for razorpay_second project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [path("admin/", admin.site.urls), path("", include("src.urls"))]
16 changes: 16 additions & 0 deletions razorpay_second/razorpay_second/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for razorpay_second project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'razorpay_second.settings')

application = get_wsgi_application()
Empty file added razorpay_second/src/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions razorpay_second/src/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from src.models import Coffee

# Register your models here.
admin.site.register(Coffee)
6 changes: 6 additions & 0 deletions razorpay_second/src/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class SrcConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'src'
13 changes: 13 additions & 0 deletions razorpay_second/src/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models
from django.core.validators import MinValueValidator


# Create your models here.
class Coffee(models.Model):
name = models.CharField(max_length=100)
amount = models.IntegerField(validators=[MinValueValidator(0)])
payment_id = models.CharField(max_length=100, blank=True, null=True)
paid = models.BooleanField(default=False)

def __str__(self):
return str(self.name)
38 changes: 38 additions & 0 deletions razorpay_second/src/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Buy Me a Laptop!</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container{
max-width: 80%;
background-color: rgb(228, 228, 228);
margin: auto;
padding: 23px;
}
</style>
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous" />
<title>Hello, world!</title>
</head>
<body>
{% block content %}
{% endblock content %}
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
</body>
</html>
56 changes: 56 additions & 0 deletions razorpay_second/src/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{% extends "base.html" %}
{% block content %}
<div>
<div class="d-flex align-items-center justify-content-center mx-auto flex-column"
style="min-height: 100vh">
<div class="p-5">
<h1>Buy Programmer a Laptop!</h1>
</div>
<div class="d-flex flex-row">
<div class="text-center mx-5">
<img src="https://purepng.com/public/uploads/large/purepng.com-laptopelectronicslaptopcomputer-941524676166s0nuj.png"
alt=""
class="img-fluid img-responsive"
style="height: 300px" />
</div>
<form method="post">
{% csrf_token %}
<div class="form-group mb-2 p-3">
<lablel for="inputName">Enter your name</lablel>
<input type="name"
name="name"
class="form-control"
id="inputName"
placeholder="Enter name" />
</div>
<div class="form-group mb-2 p-3">
<label for="inputAmt">Enter donation amount</label>
<input type="number"
name="amount"
class="form-control"
id="inputAmt"
placeholder="Enter amount" />
</div>
<button type="submit" class="btn btn-success btn-block m-3">Submit</button>
</form>
</div>
{% if payment %}
<div class="text-center m-3">
<form action="{% url 'success' %}" method="post">
{% csrf_token %}
<script src="https://checkout.razorpay.com/v1/checkout.js"
data-key="{{ key }}"
data-amount="{{ payment.amount }}"
data-order_id="{{ payment.id }}"
data-name="Buy Programmer a Laptop"
data-description="Donate to a good cause"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="INR"
data-buttontext="Pay with Razorpay"></script>
</form>
</div>
{% endif %}
</div>
</div>
{% endblock content %}
49 changes: 49 additions & 0 deletions razorpay_second/src/templates/success.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% extends "base.html" %}
{% load static %}
{% load humanize %}
{% block content %}
<div class="d-flex justify-content-center align-items-center"
style="min-height: 100vh">
{% if is_success %}
<div class="text-center m-5">
<img src="{% static 'images/success.png' %}"
alt=""
class="img-fluid"
style="height: 200px" />
</div>
{% elif not is_success or not greet_text %}
<div class="text-center m-5">
<img src="{% static 'images/error.png' %}"
alt=""
class="img-fluid"
style="height: 200px" />
</div>
{% endif %}
<div class="text-center">
{% if is_success %}
<h2 class="display-5">
<i class="fas fa-check-circle text-success"></i>
Payment Successful
</h2>
<p>
Thanks {{ user.name }}, for your payment of {{ payment.amount|intcomma }}.
<br />
Payment Id: {{ payment.id }}
</p>
{% elif not is_success and not greet_text %}
<h2 class="display-5">
<i class="fas fa-times-circle text-danger"></i>
Uh Oh! Something went wrong.
</h2>
<p>You should not be here.</p>
{% else %}
<h2 class="display-5">
<i class="fas fa-times-circle text-danger"></i>
Payment Failed
</h2>
{% endif %}
<p class="lead">{% firstof greet_text "Sorry, Did you forget to pay again?" %}</p>
<a href="{% url 'home' %}" class="btn btn-primary">Return Home</a>
</div>
{% endblock content %}
</div>
3 changes: 3 additions & 0 deletions razorpay_second/src/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions razorpay_second/src/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path, include

from .views import home, success

urlpatterns = [
path('', home, name='home'),
path('success/', success, name='success'),
]
Loading

0 comments on commit 8a3408c

Please sign in to comment.