-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implements classes for business logic
- Loading branch information
1 parent
8304224
commit dc40354
Showing
8 changed files
with
218 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
STRIPE_API_TEST_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,4 @@ yarn-error.log* | |
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
tasks.json | ||
.vscode/tasks.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {createObjectCsvWriter} from "csv-writer"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import Stripe from "stripe"; | ||
import {NextResponse} from "next/server"; | ||
import {secrets} from "@/config/secrets"; | ||
import {StripeService} from "@/services/api/stripe/StripeService"; | ||
|
||
async function generateCSVReport() { | ||
const stripeService = new StripeService(); | ||
const transactions = await stripeService.fetchBalanceTransactions(); | ||
const fees = await stripeService.fetchApplicationFees(); | ||
const refunds = await stripeService.fetchRefunds(); | ||
|
||
const csvWriter = createObjectCsvWriter({ | ||
path: "stripe_report.csv", | ||
header: [ | ||
{id: "type", title: "TYPE"}, | ||
{id: "amount", title: "AMOUNT"}, | ||
{id: "currency", title: "CURRENCY"}, | ||
{id: "description", title: "DESCRIPTION"}, | ||
{id: "created", title: "CREATED"}, | ||
], | ||
}); | ||
|
||
const data = [ | ||
...transactions.map((t) => ({ | ||
type: "Transaction", | ||
amount: t.amount, | ||
currency: t.currency, | ||
description: t.description, | ||
created: new Date(t.created * 1000).toISOString(), | ||
})), | ||
...fees.map((f) => ({ | ||
type: "Application Fee", | ||
amount: f.amount, | ||
currency: f.currency, | ||
description: f.description, | ||
created: new Date(f.created * 1000).toISOString(), | ||
})), | ||
...refunds.map((r) => ({ | ||
type: "Refund", | ||
amount: r.amount, | ||
currency: r.currency, | ||
description: r.description, | ||
created: new Date(r.created * 1000).toISOString(), | ||
})), | ||
]; | ||
|
||
await csvWriter.writeRecords(data); | ||
return path.resolve("stripe_report.csv"); | ||
} | ||
|
||
export async function GET() { | ||
try { | ||
const filePath = await generateCSVReport(); | ||
const fileBuffer = fs.readFileSync(filePath); | ||
fs.unlinkSync(filePath); // delete file after sending | ||
return new NextResponse(fileBuffer, { | ||
headers: { | ||
"Content-Type": "text/csv", | ||
"Content-Disposition": 'attachment; filename="stripe_report.csv"', | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Error generating report:", error); | ||
return new NextResponse("Error generating report", {status: 500}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
"use client" | ||
"use client"; | ||
|
||
export default function Home() { | ||
const handleDownload = () => { | ||
fetch("/api/stripe/report") | ||
.then((response) => { | ||
if (response.ok) { | ||
return response.blob(); | ||
} | ||
throw new Error("Network response was not ok."); | ||
}) | ||
.then((blob) => { | ||
const url = window.URL.createObjectURL(blob); | ||
const a = document.createElement("a"); | ||
a.style.display = "none"; | ||
a.href = url; | ||
a.download = "stripe_report.csv"; | ||
document.body.appendChild(a); | ||
a.click(); | ||
window.URL.revokeObjectURL(url); | ||
}) | ||
.catch((error) => console.error("Download error:", error)); | ||
}; | ||
return ( | ||
<main className="flex min-h-screen flex-col items-center justify-center p-24"> | ||
<button className="rounded-full bg-blue-600 p-3" onClick={() => console.log("Button clicked")}> | ||
Authenticate | ||
<h2>Stripe financial report:</h2> | ||
<button | ||
type="button" | ||
onClick={handleDownload} | ||
className="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800"> | ||
Download Report | ||
</button> | ||
<p></p> | ||
<div></div> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class EnvConfig { | ||
get stripeKey(): string { | ||
if (!process.env.STRIPE_KEY) { | ||
throw new Error("STRIPE_KEY environment not set"); | ||
} | ||
return process.env.STRIPE_KEY; | ||
} | ||
} | ||
|
||
export const secrets = new EnvConfig(); |
Oops, something went wrong.