diff --git a/NOSTR_ARCHITECTURE.md b/NOSTR_ARCHITECTURE.md index 4ff0c7f..550eb22 100644 --- a/NOSTR_ARCHITECTURE.md +++ b/NOSTR_ARCHITECTURE.md @@ -780,3 +780,330 @@ This architecture makes it easy to add new Nostr functionality: 8. **Metrics and Monitoring**: Add comprehensive metrics for relay performance This architecture makes the app more maintainable, performant, and user-friendly while providing a solid foundation for future features. + +## Market Integration Roadmap + +### Overview +This document outlines the roadmap for integrating the nostr-market-app purchasing functionality into the web-app, creating a seamless e-commerce experience while maintaining the decentralized, Nostr-based architecture. + +### Analysis of nostr-market-app Purchasing Flow + +The nostr-market-app has a sophisticated purchasing system with the following key components: + +#### 1. Shopping Cart System +- **Cart Management**: Products are added to stall-specific carts +- **Cart State**: Each stall has its own cart with products and quantities +- **Cart Persistence**: Cart data is stored locally and synced across sessions + +#### 2. Checkout Process +- **Order Confirmation**: Users provide contact information (address, email, message) +- **Shipping Selection**: Multiple shipping zones with different costs +- **Payment Options**: Lightning Network, BTC Onchain, and Cashu support + +#### 3. Order Placement +- **Encrypted Communication**: Orders are encrypted using NIP-04 and sent as direct messages +- **Order Structure**: Includes product details, quantities, shipping, and contact info +- **Payment Integration**: Lightning invoice generation and QR code display + +#### 4. Key Components +- `ShoppingCartCheckout.vue` - Main checkout interface +- `useShoppingCart.js` - Cart management logic +- `useOrders.js` - Order placement and management +- `marketStore.js` - Central state management + +### Implementation Roadmap + +#### Phase 1: Enhanced Shopping Cart System (High Priority) + +**1.1 Extend Market Store** +- Add cart management with stall-specific carts +- Implement cart persistence and synchronization +- Add shipping zone support +- Extend existing `useMarketStore` with cart functionality + +**1.2 Create Cart Components** +- `ShoppingCart.vue` - Cart overview and management +- `CartItem.vue` - Individual cart item with quantity controls +- `CartSummary.vue` - Cart totals and checkout button +- Integrate cart icon in header with item count + +**1.3 Cart State Management** +- Implement stall-specific cart structure +- Add cart persistence to local storage +- Sync cart state across components +- Handle cart updates and real-time synchronization + +#### Phase 2: Checkout System (High Priority) + +**2.1 Checkout Flow** +- `CheckoutPage.vue` - Main checkout interface +- Contact information form (address, email, message) +- Shipping zone selection with cost calculation +- Order summary and confirmation + +**2.2 Payment Integration** +- Lightning Network invoice generation +- QR code display for payments +- Payment status tracking +- Integration with existing payment infrastructure + +**2.3 Checkout State Management** +- Form validation and error handling +- Multi-step checkout process +- Order confirmation and review + +#### Phase 3: Order Management (Medium Priority) + +**3.1 Order Processing** +- Encrypted order creation using NIP-04 +- Direct message sending to merchants +- Order status tracking and updates +- Integration with existing Nostr messaging system + +**3.2 Order History** +- `OrdersPage.vue` - User's order history +- Order status updates and notifications +- Communication with merchants +- Order filtering and search + +**3.3 Order Communication** +- Encrypted messaging between buyers and sellers +- Order status notifications +- Shipping updates and tracking + +#### Phase 4: Enhanced User Experience (Medium Priority) + +**4.1 Streamlined Navigation** +- Integrated cart icon in header +- Quick checkout from product cards +- Seamless flow between browsing and purchasing +- Breadcrumb navigation for checkout process + +**4.2 Real-time Updates** +- Live inventory updates +- Order status notifications +- Chat integration with merchants +- WebSocket connections for live updates + +#### Phase 5: Advanced Features (Low Priority) + +**5.1 Multiple Payment Methods** +- BTC Onchain payments +- Cashu integration +- Payment method selection +- Payment preference storage + +**5.2 Advanced Filtering and Search** +- Enhanced product search +- Advanced filtering options +- Saved search preferences +- Product recommendations + +**5.3 Merchant Tools** +- Merchant dashboard +- Inventory management +- Order fulfillment tools +- Analytics and reporting + +### Technical Implementation Details + +#### State Management Architecture + +**Extended Market Store Structure** +```typescript +interface CartItem { + product: Product + quantity: number + stallId: string +} + +interface StallCart { + id: string + merchant: string + products: CartItem[] + subtotal: number + shippingZone?: ShippingZone +} + +interface Order { + id: string + cartId: string + status: OrderStatus + contactInfo: ContactInfo + shippingZone: ShippingZone + paymentRequest?: string + createdAt: number + updatedAt: number +} +``` + +#### Component Architecture + +**New Components to Create** +1. `ShoppingCart.vue` - Main cart interface +2. `CartItem.vue` - Individual cart item +3. `CartSummary.vue` - Cart totals and checkout +4. `CheckoutPage.vue` - Complete checkout flow +5. `OrderSummary.vue` - Order review and confirmation +6. `PaymentModal.vue` - Payment processing interface +7. `OrdersPage.vue` - Order history and management + +**Enhanced Existing Components** +1. `ProductCard.vue` - Add quick add to cart +2. `Market.vue` - Integrate cart functionality +3. Header navigation - Add cart icon and count + +#### Data Flow + +**Cart Management Flow** +1. User adds product to cart +2. Cart state updated in store +3. Cart persisted to local storage +4. Cart UI components updated +5. Real-time sync across components + +**Checkout Flow** +1. User initiates checkout from cart +2. Contact information collected +3. Shipping zone selected +4. Order summary displayed +5. Payment method selected +6. Order encrypted and sent +7. Payment processed +8. Order confirmed + +#### Integration Points + +**Existing Systems** +1. **Authentication**: Integrate with existing auth system +2. **Nostr Store**: Extend existing Nostr functionality +3. **Relay Hub**: Use existing relay connections +4. **Notifications**: Leverage existing notification system +5. **Storage**: Extend existing storage mechanisms + +**New Systems** +1. **Payment Gateway**: Lightning Network integration +2. **Order Management**: Encrypted order processing +3. **Cart Persistence**: Local storage with sync +4. **Real-time Updates**: WebSocket connections + +### Security Considerations + +#### Data Encryption +- All order data encrypted using NIP-04 +- Private keys never stored in plain text +- Secure communication channels +- Payment information protection + +#### Privacy Protection +- Minimal data collection +- User consent for data sharing +- Anonymity options for users +- Secure storage practices + +#### Payment Security +- Lightning Network security +- Payment verification +- Fraud prevention measures +- Secure key management + +### Performance Considerations + +#### Optimization Strategies +- Lazy loading of cart components +- Efficient state management +- Minimal re-renders +- Optimized storage operations + +#### Scalability +- Modular component architecture +- Efficient data structures +- Caching strategies +- Performance monitoring + +### Testing Strategy + +#### Unit Testing +- Component functionality +- Store actions and mutations +- Utility functions +- Integration points + +#### Integration Testing +- End-to-end checkout flow +- Payment processing +- Order management +- Real-time updates + +#### User Testing +- Usability testing +- Performance testing +- Security testing +- Accessibility testing + +### Deployment and Rollout + +#### Phase 1 Deployment +- Enhanced shopping cart +- Basic checkout functionality +- Order placement system + +#### Phase 2 Deployment +- Payment integration +- Order tracking +- Enhanced user experience + +#### Phase 3 Deployment +- Advanced features +- Performance optimizations +- Full feature set + +### Success Metrics + +#### User Experience Metrics +- Cart abandonment rate +- Checkout completion rate +- Time to complete purchase +- User satisfaction scores + +#### Technical Metrics +- Page load times +- Cart sync performance +- Order processing speed +- Error rates + +#### Business Metrics +- Conversion rates +- Average order value +- Repeat purchase rate +- Customer retention + +### Future Enhancements + +#### Long-term Vision +- Multi-currency support +- Advanced analytics +- AI-powered recommendations +- Mobile app development +- API for third-party integrations + +#### Scalability Plans +- Microservices architecture +- Distributed storage +- Global relay network +- Cross-platform support + +### Conclusion + +This roadmap provides a comprehensive plan for integrating the nostr-market-app purchasing functionality into the web-app. The phased approach ensures core functionality is delivered first while building toward a full-featured e-commerce platform. + +The integration will maintain the decentralized, Nostr-based architecture while providing a professional, user-friendly shopping experience. Each phase builds upon the previous one, ensuring a smooth development process and consistent user experience. + +Key success factors include: +- Maintaining the existing architecture and design patterns +- Ensuring seamless integration with current systems +- Prioritizing user experience and performance +- Implementing robust security measures +- Creating a scalable and maintainable codebase + +This roadmap serves as a living document that should be updated as development progresses and new requirements emerge. diff --git a/ORDER_MANAGEMENT_FULFILLMENT.md b/ORDER_MANAGEMENT_FULFILLMENT.md new file mode 100644 index 0000000..af79620 --- /dev/null +++ b/ORDER_MANAGEMENT_FULFILLMENT.md @@ -0,0 +1,921 @@ +# Order Management & Fulfillment Workflows + +This document provides comprehensive coverage of the complete order lifecycle, from initial placement through payment processing to final fulfillment and shipping management. It includes detailed analysis of both merchant and customer interfaces, database operations, and automated fulfillment processes. + +## Overview: Order Lifecycle Management + +The marketplace implements a **comprehensive order management system** with dual interfaces for merchants and customers, supporting complete order tracking from placement to fulfillment with automated inventory management and payment processing. + +### Order States and Transitions + +```mermaid +graph LR + A[Order Created] --> B[Invoice Generated] + B --> C[Payment Request Sent] + C --> D{Payment Status} + D -->|Paid| E[Payment Confirmed] + D -->|Unpaid| F[Invoice Expired/Reissued] + F --> C + E --> G[Inventory Updated] + G --> H{Fulfillment} + H -->|Ready| I[Shipped Status] + H -->|Issue| J[Error/Refund] + I --> K[Order Complete] +``` + +## Core Order Data Models + +### 1. Order Schema (`models.py:400-485`) + +#### Order Structure Hierarchy +```python +class OrderItem(BaseModel): + product_id: str # Product identifier from order + quantity: int # Quantity ordered + +class OrderContact(BaseModel): + nostr: Optional[str] = None # Customer's nostr pubkey + phone: Optional[str] = None # Customer phone number + email: Optional[str] = None # Customer email address + +class OrderExtra(BaseModel): + products: List[ProductOverview] # Snapshot of products at time of order + currency: str # Pricing currency (USD, EUR, sat, etc.) + btc_price: str # Exchange rate at time of order + shipping_cost: float = 0 # Shipping cost in currency + shipping_cost_sat: float = 0 # Shipping cost in satoshis + fail_message: Optional[str] = None # Error message if order failed +``` + +#### Complete Order Model +```python +class Order(PartialOrder): + stall_id: str # Associated stall identifier + invoice_id: str # Lightning invoice payment hash + total: float # Total amount in satoshis + paid: bool = False # Payment status + shipped: bool = False # Shipping/fulfillment status + time: Optional[int] = None # Completion timestamp + extra: OrderExtra # Additional order metadata +``` + +### 2. Order Status Models (`models.py:467-485`) + +#### Status Update Structure +```python +class OrderStatusUpdate(BaseModel): + id: str # Order identifier + message: Optional[str] = None # Status update message + paid: Optional[bool] = False # Payment status + shipped: Optional[bool] = None # Shipping status + +class OrderReissue(BaseModel): + id: str # Order identifier to reissue + shipping_id: Optional[str] = None # Updated shipping zone + +class PaymentRequest(BaseModel): + id: str # Order identifier + message: Optional[str] = None # Response message + payment_options: List[PaymentOption] # Available payment methods +``` + +### 3. Database Schema (`migrations.py:110-130`) + +#### Order Table Structure +```sql +CREATE TABLE nostrmarket.orders ( + merchant_id TEXT NOT NULL, -- Merchant who owns this order + id TEXT PRIMARY KEY, -- Unique order identifier (UUID) + event_id TEXT, -- Nostr event ID for order placement + event_created_at INTEGER NOT NULL, -- Unix timestamp of order creation + public_key TEXT NOT NULL, -- Customer's public key + merchant_public_key TEXT NOT NULL, -- Merchant's public key + contact_data TEXT NOT NULL DEFAULT '{}', -- JSON contact information + extra_data TEXT NOT NULL DEFAULT '{}', -- JSON extra metadata + order_items TEXT NOT NULL, -- JSON array of ordered items + address TEXT, -- Shipping address (deprecated) + total REAL NOT NULL, -- Total amount in satoshis + shipping_id TEXT NOT NULL, -- Shipping zone identifier + stall_id TEXT NOT NULL, -- Associated stall identifier + invoice_id TEXT NOT NULL, -- Lightning invoice payment hash + paid BOOLEAN NOT NULL DEFAULT false, -- Payment confirmation + shipped BOOLEAN NOT NULL DEFAULT false, -- Fulfillment status + time INTEGER -- Completion timestamp +); +``` + +## Merchant Order Management Interface + +### 1. Order List Component (`order-list.js`) + +#### Component Structure and Properties +```javascript +window.app.component('order-list', { + name: 'order-list', + props: ['stall-id', 'customer-pubkey-filter', 'adminkey', 'inkey'], + template: '#order-list', + delimiters: ['${', '}'], +``` + +#### Advanced Search and Filtering (`order-list.js:15-49`) +```javascript +data: function () { + return { + orders: [], + selectedOrder: null, + search: { + publicKey: null, // Filter by customer public key + isPaid: {label: 'All', id: null}, // Payment status filter + isShipped: {label: 'All', id: null}, // Shipping status filter + }, + ternaryOptions: [ + {label: 'All', id: null}, // Show all orders + {label: 'Yes', id: 'true'}, // Filter for paid/shipped = true + {label: 'No', id: 'false'} // Filter for paid/shipped = false + ] + } +} +``` + +#### Dynamic Order Fetching (`order-list.js:156-181`) +```javascript +getOrders: async function () { + try { + // Support both stall-specific and merchant-wide queries + const ordersPath = this.stallId + ? `stall/order/${this.stallId}` // Orders for specific stall + : 'order' // All orders for merchant + + // Build query parameters for filtering + const query = [] + if (this.search.publicKey) { + query.push(`pubkey=${this.search.publicKey}`) + } + if (this.search.isPaid.id) { + query.push(`paid=${this.search.isPaid.id}`) + } + if (this.search.isShipped.id) { + query.push(`shipped=${this.search.isShipped.id}`) + } + + const {data} = await LNbits.api.request( + 'GET', + `/nostrmarket/api/v1/${ordersPath}?${query.join('&')}`, + this.inkey + ) + this.orders = data.map(s => ({...s, expanded: false})) + } catch (error) { + LNbits.utils.notifyApiError(error) + } +} +``` + +### 2. Order Display and Calculations (`order-list.js:119-155`) + +#### Product Information Retrieval +```javascript +productName: function (order, productId) { + product = order.extra.products.find(p => p.id === productId) + if (product) { + return product.name + } + return '' +}, + +productPrice: function (order, productId) { + product = order.extra.products.find(p => p.id === productId) + if (product) { + return `${product.price} ${order.extra.currency}` + } + return '' +}, + +orderTotal: function (order) { + // Calculate total from individual product costs + shipping + const productCost = order.items.reduce((t, item) => { + product = order.extra.products.find(p => p.id === item.product_id) + return t + item.quantity * product.price + }, 0) + return productCost + order.extra.shipping_cost +} +``` + +### 3. Shipping Status Management (`order-list.js:259-280`) + +#### Shipping Status Updates +```javascript +updateOrderShipped: async function () { + this.selectedOrder.shipped = !this.selectedOrder.shipped + try { + await LNbits.api.request( + 'PATCH', + `/nostrmarket/api/v1/order/${this.selectedOrder.id}`, + this.adminkey, + { + id: this.selectedOrder.id, + message: this.shippingMessage, // Custom message to customer + shipped: this.selectedOrder.shipped // New shipping status + } + ) + this.$q.notify({ + type: 'positive', + message: 'Order updated!' + }) + } catch (error) { + LNbits.utils.notifyApiError(error) + } + this.showShipDialog = false +} +``` + +#### Shipping Dialog Interface (`order-list.js:356-365`) +```javascript +showShipOrderDialog: function (order) { + this.selectedOrder = order + this.shippingMessage = order.shipped + ? 'The order has been shipped!' + : 'The order has NOT yet been shipped!' + + // Toggle status (will be confirmed on dialog submit) + this.selectedOrder.shipped = !order.shipped + this.showShipDialog = true +} +``` + +### 4. Order Recovery and Restoration (`order-list.js:194-233`) + +#### Individual Order Restoration +```javascript +restoreOrder: async function (eventId) { + try { + this.search.restoring = true + const {data} = await LNbits.api.request( + 'PUT', + `/nostrmarket/api/v1/order/restore/${eventId}`, // Restore from DM event + this.adminkey + ) + await this.getOrders() // Refresh order list + this.$q.notify({ + type: 'positive', + message: 'Order restored!' + }) + return data + } catch (error) { + LNbits.utils.notifyApiError(error) + } finally { + this.search.restoring = false + } +} +``` + +#### Bulk Order Restoration +```javascript +restoreOrders: async function () { + try { + this.search.restoring = true + await LNbits.api.request( + 'PUT', + `/nostrmarket/api/v1/orders/restore`, // Restore all from DMs + this.adminkey + ) + await this.getOrders() + this.$q.notify({ + type: 'positive', + message: 'Orders restored!' + }) + } catch (error) { + LNbits.utils.notifyApiError(error) + } +} +``` + +### 5. Invoice Management (`order-list.js:234-258`) + +#### Invoice Reissuance +```javascript +reissueOrderInvoice: async function (order) { + try { + const {data} = await LNbits.api.request( + 'PUT', + `/nostrmarket/api/v1/order/reissue`, + this.adminkey, + { + id: order.id, + shipping_id: order.shipping_id // Optional shipping zone update + } + ) + this.$q.notify({ + type: 'positive', + message: 'Order invoice reissued!' + }) + + // Update order in local state + data.expanded = order.expanded + const i = this.orders.map(o => o.id).indexOf(order.id) + if (i !== -1) { + this.orders[i] = {...this.orders[i], ...data} + } + } catch (error) { + LNbits.utils.notifyApiError(error) + } +} +``` + +## Customer Order Interface + +### 1. Customer Orders Component (`CustomerOrders.vue`) + +#### Order Display Structure +```vue +
Publish your stalls and products to the nostrmarket network
+