diff --git a/README.md b/README.md new file mode 100644 index 0000000..00fcf74 --- /dev/null +++ b/README.md @@ -0,0 +1,134 @@ +# NutriPlan - Supabase & Stripe Integration + +This project is a sample implementation of a web application named NutriPlan. It demonstrates how to integrate Supabase for user authentication and database operations (managing orders) and Stripe for handling payments (subscriptions via Stripe Checkout). + +The frontend is built with plain HTML, CSS, and JavaScript, focusing on a clean, modern, and mobile-first design. + +## Features + +* **User Authentication:** Sign-up, login, and logout functionality managed by Supabase Auth. +* **Product Display:** A clean, responsive grid displaying available subscription plans. +* **Stripe Checkout:** Secure payment processing through redirection to Stripe Checkout. +* **Order History:** Logged-in users can view their past orders, fetched from a Supabase database. +* **Secure API Calls:** The architecture is designed to use a serverless backend (like Supabase Edge Functions) to securely create Stripe Checkout sessions, preventing keys from being exposed on the client-side. + +## Tech Stack + +* **Frontend:** HTML, CSS, JavaScript +* **Authentication & Database:** [Supabase](https://supabase.io/) +* **Payments:** [Stripe](https://stripe.com/) +* **Fonts:** Google Fonts (Montserrat & Open Sans) + +## Project Structure + +``` +. +├── index.html # The main HTML file for the user interface. +├── style.css # The stylesheet for the application. +├── script.js # The core JavaScript file for all client-side logic. +└── README.md # This documentation file. +``` + +--- + +## Installation and Setup + +Follow these instructions to get the project running locally. + +### Prerequisites + +* A [Supabase](https://app.supabase.io/) account. +* A [Stripe](https://dashboard.stripe.com/register) account. +* A local web server to serve the files (e.g., VS Code Live Server, Python's `http.server`). + +### Step 1: Set up Supabase + +1. **Create a New Project:** + * Go to your Supabase dashboard and create a new project. + +2. **Get API Keys:** + * Navigate to your project's **Settings > API**. + * You will need the **Project URL** and the **`anon` public key**. + +3. **Create an `orders` Table:** + * Go to the **Table Editor** in your Supabase project. + * Create a new table named `orders`. + * Add the following columns: + * `id` (int8, is identity) - Primary Key + * `created_at` (timestamptz, default `now()`) + * `user_id` (uuid, foreign key to `auth.users.id`) - This links the order to a user. + * `product_name` (text) - The name of the purchased plan. + * `stripe_payment_intent` (text) - The payment intent ID from Stripe for reference. + * Make sure to **disable Row Level Security (RLS)** for this table for initial testing, or create appropriate policies for access. For production, you should enable RLS and create policies that only allow users to see their own orders. + +### Step 2: Set up Stripe + +1. **Get API Keys:** + * Go to your Stripe Dashboard. + * Navigate to **Developers > API keys**. + * You will need the **Publishable key** (`pk_test_...`). + +2. **Create Products:** + * In the Stripe Dashboard, go to the **Products** catalog. + * Create at least two products (e.g., "Basic Plan", "Premium Plan"). + * For each product, add a pricing plan (e.g., $9.99/month). + * Note down the **API ID** for each price (e.g., `price_123abc`). You will need this for the checkout button in `index.html`. + +### Step 3: Configure the Project + +1. **Clone or Download the Repository:** + * Get the project files onto your local machine. + +2. **Update `index.html`:** + * Open `index.html`. + * Find the `.product-card` divs. + * Update the `data-product-id` attribute with the **Price API IDs** you got from Stripe. + ```html +
+ ``` + +3. **Update `script.js`:** + * Open `script.js`. + * At the top of the file, replace the placeholder constants with your actual keys from Supabase and Stripe. + ```javascript + const SUPABASE_URL = 'YOUR_SUPABASE_URL'; // From Supabase Settings > API + const SUPABASE_ANON_KEY = 'YOUR_SUPABASE_ANON_KEY'; // From Supabase Settings > API + const STRIPE_PUBLISHABLE_KEY = 'YOUR_STRIPE_PUBLISHABLE_KEY'; // From Stripe Developers > API Keys + ``` + +### Step 4: (Advanced) Create the Backend Function + +For the Stripe Checkout to work, you need a secure backend environment to create the checkout session. The client-side code in `script.js` is prepared to call a serverless function. + +1. **Create a Supabase Edge Function:** + * Use the Supabase CLI to create a new function (e.g., `create-checkout-session`). + * This function will receive a `productId` from the client. + * Inside the function, use the Stripe Node.js library to create a checkout session. You will need your **Stripe Secret Key** here (store it as a Supabase secret). + * The function should return the `sessionId` to the client. + +2. **Client-side Call:** + * In `script.js`, uncomment and use the `supabase.functions.invoke` call to trigger your edge function. + +--- + +## Testing + +1. **Start a Local Server:** + * From the project's root directory, start a simple web server. For example, if you have Python 3: + ```bash + python -m http.server + ``` + * Open your browser and navigate to `http://localhost:8000` (or the appropriate port). + +2. **Test Authentication:** + * Since the example uses a dummy login, you'll need to either implement a full Supabase Auth UI or manually create a user in your Supabase dashboard and log in via the browser console to get a session. + * Once logged in, your email should appear, and the "Your Orders" section should be visible. + +3. **Test Checkout:** + * Click on a "Choose Plan" button. + * Check the browser's console. You should see a log message indicating a redirect would happen. + * If you have implemented the Supabase Edge Function, you should be redirected to the Stripe Checkout page. + +4. **Test Order History:** + * After a successful (test) payment, you'll need a mechanism (like a Stripe webhook) to write the order details back to your Supabase `orders` table. + * Once an order is in the table for the logged-in user, it should appear in the "Your Orders" section. \ No newline at end of file diff --git a/index.html b/index.html index d2b0850..2ebc0c9 100644 --- a/index.html +++ b/index.html @@ -1,121 +1,63 @@ - + - NutriPlan - Программа 'Фитнес' + NutriPlan - Your Personal Meal Plan + - - + -
-