|
1 | | -# cloudcore-ecom |
| 1 | +# Cloudcore Ecom |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +E-commerce starter for [Cloudcore CMS](https://github.com/cloudcore-cms/cloudcore-cms). Adds products, subscriptions, Stripe, and PayPal to your CMS. |
| 6 | + |
| 7 | +Not a standalone service — copy the source files into your CMS. One Worker, one database, one auth system. |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +### 1. Copy ecom files into your CMS |
| 12 | + |
| 13 | +```bash |
| 14 | +# Clone this repo |
| 15 | +git clone https://github.com/cloudcore-cms/cloudcore-ecom.git |
| 16 | + |
| 17 | +# Copy the source files into your CMS |
| 18 | +cp -r cloudcore-ecom/src/* your-cms/src/ecom/ |
| 19 | + |
| 20 | +# Copy the migration |
| 21 | +cp cloudcore-ecom/migrations/001_ecom.sql your-cms/src/db/migrations/002_ecom.sql |
| 22 | +``` |
| 23 | + |
| 24 | +### 2. Run the migration |
| 25 | + |
| 26 | +```bash |
| 27 | +npx wrangler d1 migrations apply cloudcore-db --file=src/db/migrations/002_ecom.sql |
| 28 | +``` |
| 29 | + |
| 30 | +### 3. Mount the routes in your CMS |
| 31 | + |
| 32 | +```typescript |
| 33 | +// src/index.ts |
| 34 | +import { ecomRoutes, ecomPublicRoutes, ecomWebhookRoutes } from './ecom'; |
| 35 | + |
| 36 | +// Admin routes (behind auth) |
| 37 | +app.route('/api/v1/shop', ecomRoutes); |
| 38 | + |
| 39 | +// Public product listing (no auth) |
| 40 | +app.route('/api/v1/public/shop', ecomPublicRoutes); |
| 41 | + |
| 42 | +// Payment webhooks (verified by provider signatures, no auth) |
| 43 | +app.route('/api/v1/webhooks', ecomWebhookRoutes); |
| 44 | +``` |
| 45 | + |
| 46 | +### 4. Add environment variables |
| 47 | + |
| 48 | +```toml |
| 49 | +# wrangler.toml [vars] or use wrangler secret put |
| 50 | +STRIPE_SECRET_KEY = "" |
| 51 | +STRIPE_WEBHOOK_SECRET = "" |
| 52 | + |
| 53 | +# PayPal (optional) |
| 54 | +PAYPAL_CLIENT_ID = "" |
| 55 | +PAYPAL_CLIENT_SECRET = "" |
| 56 | +PAYPAL_WEBHOOK_ID = "" |
| 57 | +PAYPAL_MODE = "sandbox" # or "live" |
| 58 | +``` |
| 59 | + |
| 60 | +## API Endpoints |
| 61 | + |
| 62 | +### Admin (requires CMS auth) |
| 63 | + |
| 64 | +| Method | Endpoint | Description | |
| 65 | +|---|---|---| |
| 66 | +| `GET` | `/shop/products` | List products | |
| 67 | +| `GET` | `/shop/products/:id` | Get product | |
| 68 | +| `POST` | `/shop/products` | Create product | |
| 69 | +| `PATCH` | `/shop/products/:id` | Update product | |
| 70 | +| `DELETE` | `/shop/products/:id` | Delete product | |
| 71 | +| `POST` | `/shop/checkout` | Create checkout session | |
| 72 | +| `GET` | `/shop/subscriptions` | List all subscriptions | |
| 73 | +| `GET` | `/shop/subscriptions/my` | List current user's subscriptions | |
| 74 | +| `POST` | `/shop/subscriptions/:id/cancel` | Cancel subscription | |
| 75 | + |
| 76 | +### Public (no auth) |
| 77 | + |
| 78 | +| Method | Endpoint | Description | |
| 79 | +|---|---|---| |
| 80 | +| `GET` | `/public/shop/products` | List active products | |
| 81 | +| `GET` | `/public/shop/products/:slug` | Get product by slug | |
| 82 | + |
| 83 | +### Webhooks (verified by provider) |
| 84 | + |
| 85 | +| Method | Endpoint | Description | |
| 86 | +|---|---|---| |
| 87 | +| `POST` | `/webhooks/stripe` | Stripe webhook | |
| 88 | +| `POST` | `/webhooks/paypal` | PayPal webhook | |
| 89 | + |
| 90 | +## Product Types |
| 91 | + |
| 92 | +**One-time purchase:** |
| 93 | +```json |
| 94 | +{ |
| 95 | + "name": "Pro Template", |
| 96 | + "type": "one_time", |
| 97 | + "price": 4900, |
| 98 | + "currency": "usd" |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +**Subscription:** |
| 103 | +```json |
| 104 | +{ |
| 105 | + "name": "Pro Plan", |
| 106 | + "type": "subscription", |
| 107 | + "price": 1900, |
| 108 | + "currency": "usd", |
| 109 | + "intervalType": "month", |
| 110 | + "intervalCount": 1, |
| 111 | + "trialDays": 14 |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +Prices are in cents (4900 = $49.00). |
| 116 | + |
| 117 | +## Database Tables |
| 118 | + |
| 119 | +All tables prefixed with `cc_ecom_` to avoid conflicts with CMS tables: |
| 120 | + |
| 121 | +- `cc_ecom_products` — products and subscription plans |
| 122 | +- `cc_ecom_orders` — one-time purchase records |
| 123 | +- `cc_ecom_subscriptions` — active/canceled subscriptions |
| 124 | +- `cc_ecom_webhook_events` — idempotency log for payment webhooks |
| 125 | + |
| 126 | +## How It Works |
| 127 | + |
| 128 | +1. Admin creates products in the CMS (or via API) |
| 129 | +2. Frontend lists active products via the public endpoint |
| 130 | +3. User clicks "Buy" — frontend calls `/shop/checkout` with product ID and payment provider |
| 131 | +4. User is redirected to Stripe/PayPal hosted checkout |
| 132 | +5. On success, webhook creates order/subscription records in D1 |
| 133 | +6. Frontend checks subscription status via `/shop/subscriptions/my` |
| 134 | + |
| 135 | +No PCI compliance needed — all card handling is done by Stripe/PayPal. |
| 136 | + |
| 137 | +## License |
| 138 | + |
| 139 | +MIT |
0 commit comments