Skip to content

Commit b76eacf

Browse files
feat: subscription disable and card update
1 parent aeda221 commit b76eacf

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

src/routes/subscriptions.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'dotenv/config';
22
import { Hono } from "hono";
33
import { db } from '../db.js';
44
import { zValidator } from '@hono/zod-validator';
5-
import { SubscriptionCreate } from '../schema/index.js';
5+
import { SubscriptionCreate, SubscriptionDisable } from '../schema/index.js';
66
import { eq } from 'drizzle-orm';
77
import { subscriptionsTable } from '../db/schema/subscriptions.js';
88
import { plansTable } from '../db/schema/plans.js';
@@ -74,5 +74,62 @@ subscriptionsRouter.post("/",
7474
}
7575
)
7676

77+
subscriptionsRouter.post("/disable",
78+
zValidator('json', SubscriptionDisable),
79+
async (c) => {
80+
const validateReq = c.req.valid('json')
81+
const api = await fetch(`${process.env.PAYSTACK_BASE_URL}/subscription/disable`, {
82+
method: "POST",
83+
headers: {
84+
"Authorization": `Bearer ${process.env.PAYSTACK_SECRET_KEY}`,
85+
'Content-Type': "application/json"
86+
},
87+
body: JSON.stringify(validateReq)
88+
})
89+
90+
const data = await api.json()
91+
92+
if (!data["status"]) {
93+
return c.json({
94+
status: false,
95+
data: {}
96+
}, 500)
97+
}
98+
99+
const subscription = await db.update(subscriptionsTable).set({
100+
status: "disabled"
101+
}).where(eq(subscriptionsTable.code, validateReq.code))
102+
.returning()
103+
104+
return c.json({
105+
status: true,
106+
data: subscription[0]
107+
}, 200)
108+
}
109+
)
110+
111+
subscriptionsRouter.post("/:code/manage/email", async (c) => {
112+
const code = c.req.param('code')
113+
const api = await fetch(`${process.env.PAYSTACK_BASE_URL}/subscription/${code}/manage/email`, {
114+
method: "POST",
115+
headers: {
116+
"Authorization": `Bearer ${process.env.PAYSTACK_SECRET_KEY}`,
117+
'Content-Type': "application/json"
118+
}
119+
})
120+
121+
const data = await api.json()
122+
123+
if (!data["status"]) {
124+
return c.json({
125+
status: false,
126+
data: {}
127+
}, 500)
128+
}
129+
130+
return c.json({data}, 200)
131+
}
132+
)
133+
77134

78135
export default subscriptionsRouter

src/routes/transaction.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dotenv/config';
22
import { Hono } from "hono";
3-
import { db } from '../db.js';
43
import { zValidator } from '@hono/zod-validator';
54
import { Transaction } from '../schema/index.js';
65

src/schema/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export const SubscriptionCreate = z.object({
3737
start_date: z.date().optional()
3838
})
3939

40+
export const SubscriptionDisable = z.object({
41+
code: z.string(),
42+
token: z.string()
43+
})
44+
4045
/**
4146
* ========= Customer schema ========
4247
*/

0 commit comments

Comments
 (0)