refactor: clean up unused console logs and streamline Stripe integration
This commit is contained in:
parent
52801bebd1
commit
89248e3787
5 changed files with 8 additions and 78 deletions
|
|
@ -63,7 +63,6 @@ const PaymentForm = ({
|
||||||
|
|
||||||
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
|
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
console.log("submitting payment");
|
|
||||||
if (!stripe || !elements) {
|
if (!stripe || !elements) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -121,67 +120,3 @@ const PaymentForm = ({
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
/*
|
|
||||||
function PaymentForm2() {
|
|
||||||
const stripe = useStripe();
|
|
||||||
const elements = useElements();
|
|
||||||
|
|
||||||
const [message, setMessage] = useState<string | null>(null);
|
|
||||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
if (!stripe || !elements) {
|
|
||||||
// Stripe.js hasn't yet loaded.
|
|
||||||
// Make sure to disable form submission until Stripe.js has loaded.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsLoading(true);
|
|
||||||
|
|
||||||
const { error } = await stripe.confirmPayment({
|
|
||||||
elements,
|
|
||||||
confirmParams: {
|
|
||||||
// Make sure to change this to your payment completion page
|
|
||||||
return_url: "http://localhost:3000/success",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// This point will only be reached if there is an immediate error when
|
|
||||||
// confirming the payment. Otherwise, your customer will be redirected to
|
|
||||||
// your `return_url`. For some payment methods like iDEAL, your customer will
|
|
||||||
// be redirected to an intermediate site first to authorize the payment, then
|
|
||||||
// redirected to the `return_url`.
|
|
||||||
if (error.type === "card_error" || error.type === "validation_error") {
|
|
||||||
setMessage(error.message || "Validation error occurred.");
|
|
||||||
} else {
|
|
||||||
setMessage("An unexpected error occurred.");
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsLoading(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<form id="payment-form" onSubmit={handleSubmit}>
|
|
||||||
<PaymentElement
|
|
||||||
id="payment-element"
|
|
||||||
options={{
|
|
||||||
layout: "accordion",
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<button
|
|
||||||
disabled={isLoading || !stripe || !elements}
|
|
||||||
id="submit"
|
|
||||||
type="submit"
|
|
||||||
>
|
|
||||||
<span id="button-text">
|
|
||||||
{isLoading ? <div className="spinner" id="spinner"></div> : "Pay now"}
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
{message && <div id="payment-message">{message}</div>}
|
|
||||||
</form>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
// lib/stripe.js
|
|
||||||
import Stripe from "stripe";
|
import Stripe from "stripe";
|
||||||
import { env } from "~/env";
|
import { env } from "~/env";
|
||||||
|
|
||||||
// This check ensures we don't accidentally run this on the client
|
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
throw new Error("Stripe Secret Key should not be loaded on the client!");
|
throw new Error("Stripe Secret Key should not be loaded on the client!");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -96,15 +96,16 @@ const handler = async (
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "payment_intent.processing": {
|
case "payment_intent.processing": {
|
||||||
const paymentIntent = event.data.object;
|
//const paymentIntent = event.data.object;
|
||||||
|
|
||||||
console.log("PaymentIntent processing", paymentIntent);
|
//console.log("PaymentIntent processing", paymentIntent);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
console.log(`Unhandled event type: ${event.type}`);
|
//console.log(`Unhandled event type: ${event.type}`);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json({ received: true });
|
res.status(200).json({ received: true });
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,6 @@ export default PagamentoPage;
|
||||||
|
|
||||||
export const getServerSideProps = (async (context) => {
|
export const getServerSideProps = (async (context) => {
|
||||||
const id = context.params?.paymentId;
|
const id = context.params?.paymentId;
|
||||||
console.log("Received paymentId:", id);
|
|
||||||
if (typeof id !== "string") {
|
if (typeof id !== "string") {
|
||||||
console.error("Error: paymentId is not a string");
|
console.error("Error: paymentId is not a string");
|
||||||
return {
|
return {
|
||||||
|
|
@ -88,7 +87,6 @@ export const getServerSideProps = (async (context) => {
|
||||||
},
|
},
|
||||||
receipt_email: paymentData.email,
|
receipt_email: paymentData.email,
|
||||||
});
|
});
|
||||||
console.log("Created payment intent with client secret:", clientSecret);
|
|
||||||
|
|
||||||
if (!clientSecret) {
|
if (!clientSecret) {
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
import Stripe from "stripe";
|
|
||||||
import { env } from "~/env";
|
import { env } from "~/env";
|
||||||
|
import stripe from "~/lib/stripe";
|
||||||
import OrderTypeEnum from "~/schemas/public/OrderTypeEnum";
|
import OrderTypeEnum from "~/schemas/public/OrderTypeEnum";
|
||||||
import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum";
|
import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum";
|
||||||
import type { Payments, PaymentsId } from "~/schemas/public/Payments";
|
import type { Payments, PaymentsId } from "~/schemas/public/Payments";
|
||||||
|
|
@ -9,11 +9,9 @@ import { db } from "~/server/db";
|
||||||
import { NewMail } from "~/server/services/mailer";
|
import { NewMail } from "~/server/services/mailer";
|
||||||
import { getUser } from "~/server/services/user.service";
|
import { getUser } from "~/server/services/user.service";
|
||||||
|
|
||||||
const stripeApi = new Stripe(env.STRIPE_SECRET_KEY);
|
|
||||||
|
|
||||||
export const stripeHealthCheck = async () => {
|
export const stripeHealthCheck = async () => {
|
||||||
try {
|
try {
|
||||||
const endpoints = await stripeApi.webhookEndpoints.list();
|
const endpoints = await stripe.webhookEndpoints.list();
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
endpoints,
|
endpoints,
|
||||||
|
|
@ -48,7 +46,7 @@ export const createIntentHandler = async ({
|
||||||
.where("payments.id", "=", paymentId)
|
.where("payments.id", "=", paymentId)
|
||||||
.executeTakeFirstOrThrow();
|
.executeTakeFirstOrThrow();
|
||||||
|
|
||||||
const paymentIntent = await stripeApi.paymentIntents.create({
|
const paymentIntent = await stripe.paymentIntents.create({
|
||||||
amount: payment.amount_cent,
|
amount: payment.amount_cent,
|
||||||
//payment_method_configuration: "pmc_1P4NfjILe4KoQRqXYChFCTFj",
|
//payment_method_configuration: "pmc_1P4NfjILe4KoQRqXYChFCTFj",
|
||||||
automatic_payment_methods: {
|
automatic_payment_methods: {
|
||||||
|
|
@ -103,7 +101,7 @@ export const whIntentSucceededHandler = async ({
|
||||||
pm_id: string;
|
pm_id: string;
|
||||||
}) => {
|
}) => {
|
||||||
try {
|
try {
|
||||||
const paymentMethod = await stripeApi.paymentMethods.retrieve(pm_id);
|
const paymentMethod = await stripe.paymentMethods.retrieve(pm_id);
|
||||||
|
|
||||||
await db.transaction().execute(async (trx) => {
|
await db.transaction().execute(async (trx) => {
|
||||||
const payment = await trx
|
const payment = await trx
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue