|
| 1 | +# commercetools Checkout Example |
| 2 | + |
| 3 | +A complete React-based e-commerce application demonstrating commercetools Checkout integration with embedded payment processing, cart management, and order completion. |
| 4 | + |
| 5 | +## 🚀 Features |
| 6 | + |
| 7 | +- **Product Catalog**: Browse products from your commercetools project |
| 8 | +- **Shopping Cart**: Add, update, and remove items |
| 9 | +- **Embedded Checkout**: Secure payment processing with commercetools Checkout |
| 10 | +- **Order Completion**: Success page with order confirmation |
| 11 | +- **Responsive Design**: Works on desktop and mobile devices |
| 12 | +- **Real-time Updates**: Cart and checkout state management |
| 13 | + |
| 14 | +## 📋 Prerequisites |
| 15 | + |
| 16 | +Before running this example, you need: |
| 17 | + |
| 18 | +### 1. commercetools Project Setup |
| 19 | +- commercetools account and project |
| 20 | +- API client credentials with required scopes |
| 21 | +- Sample products in your project (or import using the import example) |
| 22 | +- Shipping methods configured with rates |
| 23 | +- Configure taxes with rates for address being used for testing |
| 24 | + |
| 25 | +### 2. commercetools Checkout Application Setup |
| 26 | +This is the **critical step** - you must set up a Checkout application in Merchant Center: |
| 27 | + |
| 28 | +#### Step-by-Step Checkout Setup: |
| 29 | + |
| 30 | +1. **Login to Merchant Center** |
| 31 | + ``` |
| 32 | + https://mc.europe-west1.gcp.commercetools.com |
| 33 | + ``` |
| 34 | + |
| 35 | +2. **Navigate to Settings > Checkout** |
| 36 | + - In the left sidebar: Settings → Checkout |
| 37 | + - If you don't see "Checkout", ensure your project has Checkout enabled |
| 38 | + |
| 39 | +3. **Configure Checkout Application** |
| 40 | + - Use sample Checkout application |
| 41 | + - Change Origin URLs to "Allow all URLs to communicate to the checkout application" for testing purposes |
| 42 | + |
| 43 | +4. **Configure Payment Methods** |
| 44 | + - In the Checkout application settings |
| 45 | + - Configure Payment Integrations , install Sample payment connector |
| 46 | + - Add payment methods (card etc.) and enable them |
| 47 | + |
| 48 | +5. **Copy Application ID** |
| 49 | + - After configuration, copy the **Application Key** |
| 50 | + - You'll need this for your `.env` file |
| 51 | + |
| 52 | +## 🛠 Installation & Setup |
| 53 | + |
| 54 | +### 1. Clone and Install |
| 55 | + |
| 56 | +```bash |
| 57 | +# From the hackathon-starter-kit root |
| 58 | +cd checkout |
| 59 | + |
| 60 | +# Install server & client dependencies |
| 61 | +npm run setup |
| 62 | + |
| 63 | + |
| 64 | +### 2. Environment Configuration |
| 65 | + |
| 66 | +```bash |
| 67 | +# Copy environment template |
| 68 | +cp .env.example .env |
| 69 | +cp /client/.env.example /client/.env |
| 70 | +``` |
| 71 | + |
| 72 | +Edit `.env` with your credentials: |
| 73 | + |
| 74 | +```env |
| 75 | +# commercetools Platform Configuration |
| 76 | +CTP_PROJECT_KEY=your-project-key |
| 77 | +CTP_CLIENT_ID=your-client-id |
| 78 | +CTP_CLIENT_SECRET=your-client-secret |
| 79 | +CTP_AUTH_URL=https://auth.europe-west1.gcp.commercetools.com |
| 80 | +CTP_API_URL=https://api.europe-west1.gcp.commercetools.com |
| 81 | +CTP_SCOPES=manage_orders:your-project-key manage_customers:your-project-key view_shipping_methods:your-project-key view_cart_discounts:your-project-key view_discount_codes:your-project-key view_tax_categories:your-project-key view_sessions:your-project-key view_categories:your-project-key view_products:your-project-key view_published_products:your-project-key view_project_settings:your-project-key create_anonymous_token:your-project-key view_standalone_prices:your-project-key manage_shopping_lists:your-project-key manage_order_edits:your-project-key view_product_selections:your-project-key view_types:your-project-key manage_sessions:your-project-key |
| 82 | +
|
| 83 | +# commercetools Checkout Configuration |
| 84 | +CHECKOUT_APPLICATION_ID=your-checkout-application-id-from-step-5-above |
| 85 | +CHECKOUT_APPLICATION_URL=https://checkout.europe-west1.gcp.commercetools.com |
| 86 | +
|
| 87 | +# Server Configuration |
| 88 | +PORT=3001 |
| 89 | +CLIENT_PORT=3000 |
| 90 | +``` |
| 91 | + |
| 92 | +Edit client `.env` with your project: |
| 93 | + |
| 94 | +```env |
| 95 | +REACT_APP_CTP_PROJECT_KEY=your-project-key |
| 96 | +``` |
| 97 | + |
| 98 | + |
| 99 | +## 🎯 Running the Application |
| 100 | + |
| 101 | +### Development Mode |
| 102 | + |
| 103 | +```bash |
| 104 | +# Start server |
| 105 | +npm run start-server |
| 106 | +``` |
| 107 | + |
| 108 | +```bash |
| 109 | +# Start client |
| 110 | +npm run build-client |
| 111 | +npm run start-client |
| 112 | +``` |
| 113 | + |
| 114 | +This starts: |
| 115 | +- **Backend server**: http://localhost:3001 |
| 116 | +- **React client**: http://localhost:3000 |
| 117 | + |
| 118 | + |
| 119 | +## 🧪 Testing the Checkout Flow |
| 120 | + |
| 121 | +### 1. Browse Products |
| 122 | +- Navigate to http://localhost:3000 |
| 123 | +- Products should load from your commercetools project |
| 124 | +- Add items to cart |
| 125 | + |
| 126 | +### 2. View Cart |
| 127 | +- Click the cart icon (should show item count) |
| 128 | +- Verify items, quantities, and pricing |
| 129 | +- Proceed to checkout |
| 130 | + |
| 131 | +### 3. Complete Checkout |
| 132 | +- Fill in shipping/billing information |
| 133 | +- Use test card numbers for testing: |
| 134 | + - **Visa**: 4111111111111111 |
| 135 | + - **Expiry**: Any future date |
| 136 | + - **CVV**: Any 3-digit number |
| 137 | + |
| 138 | +### 4. Order Completion |
| 139 | +- Should redirect to success page |
| 140 | +- Order ID should be displayed |
| 141 | +- Cart should be cleared |
| 142 | + |
| 143 | +## 🚨 Troubleshooting |
| 144 | + |
| 145 | +### Common Issues |
| 146 | + |
| 147 | +#### 1. "CHECKOUT_APPLICATION_ID not set" |
| 148 | +**Cause**: Missing or incorrect checkout application ID |
| 149 | +**Solution**: |
| 150 | +- Verify the Application ID in Merchant Center → Settings → Checkout |
| 151 | +- Ensure `.env` file has correct `CHECKOUT_APPLICATION_ID` |
| 152 | + |
| 153 | +#### 2. "Failed to create checkout session" |
| 154 | +**Causes**: |
| 155 | +- Incorrect API credentials |
| 156 | +- Missing cart or invalid cart ID |
| 157 | +- Checkout application not properly configured |
| 158 | + |
| 159 | +**Solutions**: |
| 160 | +- Check API client scopes include `manage_payments` |
| 161 | +- Verify cart exists and has items |
| 162 | +- Confirm checkout application is active in Merchant Center |
| 163 | + |
| 164 | +#### 3. "No products found" |
| 165 | +**Cause**: Empty commercetools project |
| 166 | +**Solution**: Import sample products using the import example |
| 167 | + |
| 168 | +#### 4. Checkout SDK fails to load |
| 169 | +**Causes**: |
| 170 | +- CORS issues |
| 171 | +- Invalid session URL |
| 172 | +- Network connectivity |
| 173 | + |
| 174 | +**Solutions**: |
| 175 | +- Check browser console for CORS errors |
| 176 | +- Verify allowed origins in checkout application settings |
| 177 | +- Use fallback checkout URL provided in the UI |
| 178 | + |
| 179 | +#### 5. Payments fail in test mode |
| 180 | +**Cause**: Test payment processor not configured |
| 181 | +**Solution**: |
| 182 | +- Enable test mode in checkout application |
| 183 | +- Use valid test card numbers |
| 184 | +- Check payment processor logs in Merchant Center |
| 185 | + |
| 186 | + |
| 187 | +## 📖 Additional Resources |
| 188 | + |
| 189 | +### commercetools Documentation |
| 190 | +- [Checkout Documentation](https://docs.commercetools.com/checkout) |
| 191 | +- [Checkout Browser SDK](https://docs.commercetools.com/checkout/browser-sdk) |
| 192 | +- [Payment Connectors](https://docs.commercetools.com/checkout/connectors-and-applications) |
0 commit comments