|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Commands |
| 6 | + |
| 7 | +### Development |
| 8 | + |
| 9 | +- `npm run build` - Build all components |
| 10 | +- `npm run build <component>` - Build a single component (e.g., `npm run build client`) |
| 11 | +- `npm run watch:integration` - Watch for changes and rebuild automatically |
| 12 | + |
| 13 | +### Testing |
| 14 | + |
| 15 | +- `npm test` - Run all tests |
| 16 | +- `npm test <component>` - Run tests for a single component (e.g., `npm test client`) |
| 17 | +- `npm test lib` - Run tests for the lib directory |
| 18 | +- `jest <path-to-file>` - Run a single test file (requires jest installed globally) |
| 19 | + |
| 20 | +### Linting and Formatting |
| 21 | + |
| 22 | +- `npm run lint` - Lint all code |
| 23 | +- `npm run lint <component>` - Lint a single component |
| 24 | +- `npm run prettier` - Format all code with Prettier |
| 25 | + |
| 26 | +### Documentation and Development Tools |
| 27 | + |
| 28 | +- `npm run jsdoc` - Generate JSDoc documentation |
| 29 | +- `npm run storybook:dev` - Start Storybook development server on port 6006 |
| 30 | +- `npm run storybook:build` - Build Storybook static files |
| 31 | +- `npm run test:integration` - Run Browserstack integration tests |
| 32 | + |
| 33 | +## Architecture |
| 34 | + |
| 35 | +### Project Structure |
| 36 | + |
| 37 | +- `src/` - Source code organized by payment method components |
| 38 | +- `src/lib/` - Shared utilities and core functionality |
| 39 | +- `src/index.js` - Main entry point that exports all payment method modules |
| 40 | +- `tasks/` - Gulp build tasks, one per component |
| 41 | +- `test/` - Test files mirroring the src/ structure |
| 42 | +- `components.json` - List of all payment method components |
| 43 | + |
| 44 | +### Component Architecture |
| 45 | + |
| 46 | +This is a modular payment SDK with components for different payment methods: |
| 47 | + |
| 48 | +**Core Components:** |
| 49 | + |
| 50 | +- `client` - Base client for Braintree API communication |
| 51 | +- `hosted-fields` - Secure credit card input fields |
| 52 | + |
| 53 | +**Payment Methods:** |
| 54 | + |
| 55 | +- `paypal`, `apple-pay`, `google-payment` - Digital wallets |
| 56 | +- `venmo`, `us-bank-account`, `sepa` - Alternative payment methods |
| 57 | +- `three-d-secure` - Authentication component |
| 58 | +- `data-collector` - Fraud protection |
| 59 | + |
| 60 | +Each component follows the pattern: |
| 61 | + |
| 62 | +- `src/<component>/index.js` - Main module export with `create()` function |
| 63 | +- `src/<component>/<component>.js` - Core implementation class |
| 64 | +- Shared utilities from `src/lib/` for common functionality |
| 65 | + |
| 66 | +### Build System |
| 67 | + |
| 68 | +- Uses Gulp with component-specific tasks in `tasks/` directory |
| 69 | +- Each component defined in `components.json` gets its own build task |
| 70 | +- Browserify for bundling with environment variable transforms |
| 71 | +- Outputs to `dist/` with separate npm, bower, and hosted builds |
| 72 | + |
| 73 | +### Testing |
| 74 | + |
| 75 | +- Jest for unit testing with component-specific configurations |
| 76 | +- Tests mirror src/ structure in test/ directory |
| 77 | +- Integration tests using WebDriver with Browserstack |
| 78 | +- Storybook for component development and testing |
| 79 | + |
| 80 | +## Component Implementation Patterns |
| 81 | + |
| 82 | +### Standard Component Creation |
| 83 | + |
| 84 | +All components follow this lifecycle pattern: |
| 85 | + |
| 86 | +1. **Basic Verification** - Using `basicComponentVerification.verify()` |
| 87 | +2. **Client Creation** - Either use provided client or create deferred client |
| 88 | +3. **Configuration Check** - Verify component is enabled in gateway config |
| 89 | +4. **Component Initialization** - Create and return component instance |
| 90 | + |
| 91 | +### Component Structure |
| 92 | + |
| 93 | +```javascript |
| 94 | +// src/<component>/index.js |
| 95 | +function create(options) { |
| 96 | + return basicComponentVerification |
| 97 | + .verify({ |
| 98 | + name: "ComponentName", |
| 99 | + client: options.client, |
| 100 | + authorization: options.authorization, |
| 101 | + }) |
| 102 | + .then(function () { |
| 103 | + return createDeferredClient.create({ |
| 104 | + authorization: options.authorization, |
| 105 | + client: options.client, |
| 106 | + assetsUrl: createAssetsUrl.create(options.authorization), |
| 107 | + name: "ComponentName", |
| 108 | + }); |
| 109 | + }) |
| 110 | + .then(function (client) { |
| 111 | + // Check gateway configuration |
| 112 | + // Initialize component |
| 113 | + // Return component instance |
| 114 | + }); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Error Handling |
| 119 | + |
| 120 | +Use `BraintreeError` for all error conditions: |
| 121 | + |
| 122 | +```javascript |
| 123 | +var BraintreeError = require("../lib/braintree-error"); |
| 124 | + |
| 125 | +// Create error with proper type classification |
| 126 | +new BraintreeError({ |
| 127 | + type: BraintreeError.types.MERCHANT, // CUSTOMER, MERCHANT, NETWORK, INTERNAL, UNKNOWN |
| 128 | + code: "COMPONENT_SPECIFIC_ERROR_CODE", |
| 129 | + message: "User-friendly error message", |
| 130 | + details: { originalError: err }, // Optional additional info |
| 131 | +}); |
| 132 | +``` |
| 133 | + |
| 134 | +## Frame Service Architecture |
| 135 | + |
| 136 | +### Iframe Communication |
| 137 | + |
| 138 | +The SDK uses framebus for secure iframe communication: |
| 139 | + |
| 140 | +- **Dispatch Frame** - Hidden iframe for coordinating communication |
| 141 | +- **Open Frame** - Visible iframe containing UI (popup, modal, etc.) |
| 142 | +- **Bus Events** - Structured messaging between frames |
| 143 | + |
| 144 | +### Frame Strategies |
| 145 | + |
| 146 | +1. **Popup** - Opens payment UI in popup window |
| 147 | +2. **Modal** - Opens payment UI in modal overlay |
| 148 | +3. **PopupBridge** - Mobile app integration strategy |
| 149 | + |
| 150 | +### Frame Service Usage |
| 151 | + |
| 152 | +```javascript |
| 153 | +var FrameService = require("../lib/frame-service/external/frame-service"); |
| 154 | + |
| 155 | +var frameService = new FrameService({ |
| 156 | + name: "component_name", |
| 157 | + dispatchFrameUrl: "https://assets.braintreegateway.com/...", |
| 158 | + openFrameUrl: "https://assets.braintreegateway.com/...", |
| 159 | +}); |
| 160 | + |
| 161 | +frameService.initialize(function () { |
| 162 | + // Frame service ready |
| 163 | +}); |
| 164 | +``` |
| 165 | + |
| 166 | +## Build Process Details |
| 167 | + |
| 168 | +### Browserify Pipeline |
| 169 | + |
| 170 | +1. **Environment Transform** - `envify` replaces `process.env` variables |
| 171 | +2. **Code Removal** - `removeIf(production)` blocks stripped in production |
| 172 | +3. **Derequire** - Prevents global require conflicts |
| 173 | +4. **Minification** - UglifyJS for production builds |
| 174 | + |
| 175 | +### Build Outputs |
| 176 | + |
| 177 | +- `dist/npm/` - CommonJS modules for npm package |
| 178 | +- `dist/bower/` - Standalone files for bower package |
| 179 | +- `dist/hosted/` - CDN-ready files with versioned paths |
| 180 | + |
| 181 | +### Component Build Tasks |
| 182 | + |
| 183 | +Each component in `components.json` gets: |
| 184 | + |
| 185 | +- `build:<component>` - Build single component |
| 186 | +- `lint:<component>` - Lint single component |
| 187 | +- Individual gulp task in `tasks/<component>.js` |
| 188 | + |
| 189 | +## Testing Patterns |
| 190 | + |
| 191 | +### Test Structure |
| 192 | + |
| 193 | +- Tests mirror `src/` structure in `test/` directory |
| 194 | +- Each component has its own Jest configuration |
| 195 | +- Global test timeout: 4 seconds |
| 196 | + |
| 197 | +### Test Helpers |
| 198 | + |
| 199 | +- `test/helpers/promise-helper.js` - Promise testing utilities |
| 200 | +- `test/helpers/components.js` - Component-specific helpers |
| 201 | +- `rejectIfResolves()` - Helper for testing promise rejections |
| 202 | +- `wait(time)` - Helper for async testing delays |
| 203 | + |
| 204 | +### Running Tests |
| 205 | + |
| 206 | +```bash |
| 207 | +# All tests for a component |
| 208 | +npm test client |
| 209 | + |
| 210 | +# Single test file |
| 211 | +jest test/client/unit/client.js |
| 212 | + |
| 213 | +# With coverage |
| 214 | +BRAINTREE_JS_ENV=development jest --coverage |
| 215 | +``` |
| 216 | + |
| 217 | +## Debugging and Troubleshooting |
| 218 | + |
| 219 | +### Environment Setup |
| 220 | + |
| 221 | +Create `.env` file with development settings: |
| 222 | + |
| 223 | +```bash |
| 224 | +BRAINTREE_JS_API_HOST=development.gateway.hostname |
| 225 | +BRAINTREE_JS_API_PORT=443 |
| 226 | +BRAINTREE_JS_API_PROTOCOL=https |
| 227 | +STORYBOOK_BRAINTREE_TOKENIZATION_KEY=your_sandbox_key |
| 228 | +``` |
| 229 | + |
| 230 | +### Analytics and Logging |
| 231 | + |
| 232 | +Components send analytics events for debugging: |
| 233 | + |
| 234 | +```javascript |
| 235 | +var analytics = require("../lib/analytics"); |
| 236 | + |
| 237 | +// Send event for debugging |
| 238 | +analytics.sendEvent(client, "component.action.started"); |
| 239 | +``` |
| 240 | + |
| 241 | +### Common Error Codes |
| 242 | + |
| 243 | +- `CLIENT_AUTHORIZATION_INVALID` - Invalid/expired authorization |
| 244 | +- `INCOMPATIBLE_VERSIONS` - Version mismatch between components |
| 245 | +- `COMPONENT_NOT_ENABLED` - Component disabled in gateway config |
| 246 | +- `CLIENT_SCRIPT_FAILED_TO_LOAD` - Network/CDN loading issues |
| 247 | + |
| 248 | +### Frame Communication Issues |
| 249 | + |
| 250 | +1. Check browser console for framebus errors |
| 251 | +2. Verify iframe URLs are loading correctly |
| 252 | +3. Ensure proper domain configuration for cross-origin |
| 253 | +4. Test with `BRAINTREE_JS_ENV=development` for detailed logging |
| 254 | + |
| 255 | +## Important Notes |
| 256 | + |
| 257 | +### Environment Variables |
| 258 | + |
| 259 | +- Create `.env` file for local development (not committed) |
| 260 | +- Use `BRAINTREE_JS_ENV=development` for development builds |
| 261 | +- Storybook requires `STORYBOOK_BRAINTREE_TOKENIZATION_KEY` in `.env` |
| 262 | + |
| 263 | +### Code Style |
| 264 | + |
| 265 | +- 2-space indentation |
| 266 | +- ES5 syntax (for browser compatibility) |
| 267 | +- JSDoc comments for public APIs |
| 268 | +- Prettier for consistent formatting |
| 269 | +- ESLint with Braintree configuration |
| 270 | + |
| 271 | +### Dependencies |
| 272 | + |
| 273 | +- Core dependencies are minimal browser-compatible libraries |
| 274 | +- Custom Braintree packages prefixed with `@braintree/` |
| 275 | +- Uses framebus for iframe communication |
| 276 | +- SJCL crypto library (custom build) for data-collector component |
| 277 | + |
| 278 | +### Development Workflow |
| 279 | + |
| 280 | +1. Run `npm test` after making changes (includes linting) |
| 281 | +2. Use `npm run build <component>` to build specific components during development |
| 282 | +3. Test integration scenarios in Storybook |
| 283 | +4. Follow existing patterns when adding new components or features |
| 284 | +5. Use `scripts/npm-to-gulp` wrapper for component-specific commands |
| 285 | +6. Check version compatibility with `basicComponentVerification` |
| 286 | +7. Verify environment variables are set correctly for local development |
| 287 | +8. Use frame service for any secure UI components requiring iframes |
0 commit comments