Skip to content

Commit 5f98a1c

Browse files
author
Shafaf
committed
Update: Razorpay api updated
1 parent a9353de commit 5f98a1c

8 files changed

Lines changed: 102 additions & 59 deletions

File tree

.env.sample

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
# APP_DB_DRIVER=postgresql
2-
# APP_DB_USER=postgres
3-
# APP_DB_PASS=postgres
4-
# APP_DB_HOST=localhost
5-
# APP_DB_PORT=5432
6-
# APP_DB_DATABASE=boilerplate_db
7-
81
# Set the POSTGRES_USER and POSTGRES_PASSWORD
92
export POSTGRES_USER=<user>
103
export POSTGRES_PASSWORD=<password>
114
export POSTGRES_HOST=<postgres>
125
export POSTGRES_PORT=<5432>
13-
export POSTGRES_DB=<razorpay>
14-
15-
16-
# SQLALCHEMY_POOL_SIZE=10
17-
# SQLALCHEMY_POOL_TIMEOUT=300
18-
# SQLALCHEMY_POOL_RECYCLE=100
19-
# SQLALCHEMY_MAX_OVERFLOW=5
20-
21-
# FLASK_WEBSERVER_ADDRESS=0.0.0.0
22-
# FLASK_WEBSERVER_PORT=8088
23-
24-
# GUNICORN_WEBSERVER_TIMEOUT=30
25-
# GUNICORN_WORKERS=5
26-
27-
# FLASK_CONFIG_FILE_PATH=config.py
6+
export POSTGRES_DB=<postgres_db>
287

29-
# MIGRATION_FOLDER_PATH=./src/migrations
8+
# Razorpay api key
9+
export RAZORPAY_API_KEY_ID="XXXXXXX"
10+
export RAZORPAY_API_SECRET="XXXXXXX"

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<div align="center">
22
<h1><code>Razorpay Python Client </code></h1>
3-
<p><strong><em>Python bindings for interacting with the Razorpay API
4-
5-
This is primarily meant for merchants who wish to perform interactions with the Razorpay API programatically.</em></strong></p>
3+
<!-- <p><strong><em>Python bindings for interacting with the Razorpay API This is primarily meant for merchants who wish to perform interactions with the Razorpay API programatically.</em></strong></p> -->
64
</div>
75

86
---

api/v1/plan.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from . import v1
55
from model.subscription import Plan
6+
from extension import db, client
67

7-
from extension import db
88

99
class Plans(MethodView):
1010
def get(self):
@@ -13,10 +13,13 @@ def get(self):
1313
{
1414
"id": plan.id,
1515
"name": plan.name,
16+
"period" : plan.period,
17+
"currency" : plan.currency,
1618
"description": plan.description,
1719
"amount": plan.amount,
1820
"intervals": plan.intervals,
19-
"plan_id": plan.plan_id
21+
"plan_id": plan.plan_id,
22+
"notes" : plan.notes
2023
}
2124
for plan in plans
2225
]
@@ -29,6 +32,8 @@ def post(self):
2932
plan_name = req.get("name")
3033
plan_amount = req.get("amount")
3134
plan_interval = req.get("interval")
35+
plan_period = req.get("period")
36+
currency = req.get("currency")
3237

3338
if plan_name is None:
3439
return jsonify({"message": "name is required"}), 400
@@ -38,18 +43,39 @@ def post(self):
3843

3944
if plan_interval is None:
4045
return jsonify({"message": "interval is required"}), 400
41-
42-
plan = Plan(
43-
name=plan_name,
44-
description=req.get("description"),
45-
amount=plan_amount,
46-
intervals=plan_interval,
47-
plan_id=req.get("plan_id")
48-
)
49-
50-
db.session.add(plan)
51-
db.session.commit()
52-
53-
return jsonify({"id": plan.id}), 201
46+
47+
48+
try:
49+
plan = client.plan.create({
50+
"period": plan_period,
51+
"interval": plan_interval,
52+
"item": {
53+
"name": plan_name,
54+
"amount": plan_amount,
55+
"currency": currency,
56+
"description": req.get("description")
57+
},
58+
"notes": req.get("notes")
59+
})
60+
61+
plan = Plan(
62+
name=plan_name,
63+
period=plan_period,
64+
currency=currency,
65+
description=req.get("description"),
66+
amount=plan_amount,
67+
intervals=plan_interval,
68+
plan_id=plan["id"],
69+
notes=req.get("notes")
70+
)
71+
72+
db.session.add(plan)
73+
db.session.commit()
74+
75+
return jsonify({"id": plan.id}), 201
76+
77+
except Exception as e:
78+
db.session.rollback()
79+
return jsonify({"error": str(e)}), 500
5480

5581
v1.add_url_rule("/plan", view_func=Plans.as_view("plan"))

api/v1/subscription.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from . import v1
55
from model.subscription import Subscription, Plan, User
6-
from extension import db
6+
from extension import db, client
77

88

99
class Subscriptions(MethodView):
@@ -36,24 +36,42 @@ def post(self):
3636
if not Plan.query.filter_by(id=plan_id).all():
3737
return jsonify({"message" : "Not a valid plan_id"})
3838

39+
# get plan id.
40+
razorpay_plan_id = Plan.query.filter_by(id=plan_id).first()
41+
3942
# check is valid user_id
4043
user_id = req.get("user_id")
4144
if user_id is not None:
4245
user_exists = User.query.filter_by(id=user_id).first()
4346
if not user_exists:
4447
return jsonify({"message": "Not a valid user_id"}), 400
4548

46-
sub = Subscription(
47-
plan_id = req.get("plan_id"),
48-
razorpay_user_id = req.get("razorpay_user_id"),
49-
subscription_id = req.get("subscription_id"),
50-
user_id = user_id,
51-
subscription_status = req.get("subscription_status")
52-
)
53-
54-
db.session.add(sub)
55-
db.session.commit()
49+
try:
50+
subscription = client.subscription.create({
51+
"plan_id": razorpay_plan_id.plan_id,
52+
"customer_notify": 1,
53+
"quantity": req.get("quantity"),
54+
"total_count": req.get("total_count"),
55+
"notes": req.get("notes")
56+
})
57+
58+
sub = Subscription(
59+
plan_id = req.get("plan_id"),
60+
total_count = req.get("total_count"),
61+
notes = req.get("notes"),
62+
quantity = req.get("quantity"),
63+
razorpay_user_id = req.get("razorpay_user_id"),
64+
subscription_id = req.get("subscription_id"),
65+
user_id = user_id,
66+
subscription_status = req.get("subscription_status")
67+
)
68+
69+
db.session.add(sub)
70+
db.session.commit()
5671

57-
return jsonify({"id": sub.id}), 201
72+
return jsonify({"id": sub.id, "payment_url" : subscription["short_url"]}), 201
73+
except Exception as e:
74+
db.session.rollback()
75+
return jsonify({"error": str(e)}), 500
5876

5977
v1.add_url_rule("/subscription", view_func=Subscriptions.as_view("subscription"))

connection.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import os
22

33
# Get the environment variables
4-
pg_user = os.getenv('POSTGRES_USER', "muhammedshafaf")
4+
pg_user = os.getenv('POSTGRES_USER', "postgres_user")
55
pg_pass = os.getenv('POSTGRES_PASSWORD', "postgres")
6-
pg_host = os.getenv('POSTGsRES_HOST', "localhost")
6+
pg_host = os.getenv('POSTGRES_HOST', "localhost")
77
pg_port = os.getenv('POSTGRES_PORT', "5432")
8-
pg_db = os.getenv('POSTsGRES_DB', "demodb")
8+
pg_db = os.getenv('POSTGRES_DB', "postgres_db")
99

1010

1111
SQLALCHEMY_DATABASE_URI = os.getenv(
1212
"DATABASE_URL",
1313
f"postgresql://{pg_user}:{pg_pass}@{pg_host}:{pg_port}/{pg_db}",
1414
)
15+
16+
RAZORPAY_KEY_ID = os.getenv('RAZORPAY_API_KEY_ID', None)
17+
RAZORPAY_SECRET = os.getenv('RAZORPAY_API_SECRET', None)

extension.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from flask_sqlalchemy import SQLAlchemy
22
from flask_migrate import Migrate
3+
import razorpay
4+
5+
from connection import RAZORPAY_KEY_ID, RAZORPAY_SECRET
36

47
db = SQLAlchemy()
5-
migrate = Migrate()
8+
migrate = Migrate()
9+
client = razorpay.Client(auth=(RAZORPAY_KEY_ID, RAZORPAY_SECRET))

model/subscription.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
from sqlalchemy import Column, String, Integer, ForeignKey, Enum
1+
from sqlalchemy import Column, String, Integer, ForeignKey, Enum, JSON
22
from sqlalchemy.orm import Mapped, mapped_column
33
from extension import db
44

55
class Plan(db.Model):
66
__tablename__ = "plan"
7+
PERIOD_CHOICES = ["daily", "weekly", "monthly", "yearly"]
78

89
id = Column(Integer, primary_key=True, nullable=False)
10+
period = Column(
11+
Enum(*PERIOD_CHOICES, name="period_types", native_enum=False),
12+
nullable=False,
13+
default="monthly"
14+
)
15+
currency = Column(String, nullable=False)
916
name = Column(String, nullable=False)
1017
description = Column(String(300), nullable=True)
1118
amount = Column(Integer, nullable=False)
1219
intervals = Column(Integer, nullable=False)
1320
plan_id = Column(String)
21+
notes = Column(JSON, nullable=False)
1422

1523
class Subscription(db.Model):
1624
__tablename__ = "subscription"
@@ -19,9 +27,12 @@ class Subscription(db.Model):
1927

2028
id = Column(Integer, primary_key=True, nullable=False)
2129
plan_id = Column(Integer, ForeignKey(Plan.id), nullable=False)
30+
total_count = Column(Integer, nullable=False)
2231
razorpay_user_id = Column(String)
2332
subscription_id = Column(String)
2433
user_id = Column(Integer, ForeignKey("user.id"), nullable=True)
34+
notes = Column(JSON, nullable=True)
35+
quantity = Column(Integer, nullable=True)
2536
subscription_status = Column(
2637
Enum(*STATUS_CHOICES, name="status", native_enum=False),
2738
nullable=False,

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ Flask==3.0.2
22
psycopg2==2.9.9
33
SQLAlchemy==2.0.29
44
Flask-SQLAlchemy
5-
Flask-Migrate
5+
Flask-Migrate
6+
razorpay==1.4.2
7+
setuptools

0 commit comments

Comments
 (0)