Integrate authentication checks and order placement logic in CheckoutPage
- Inject AuthService to verify user authentication before placing orders. - Enhance order creation process by ensuring required data is present, including checkout cart and stall information. - Update order data structure to include detailed buyer and seller information, contact details, and shipping zone data. - Implement error handling for order placement failures, improving user feedback during the checkout process.
This commit is contained in:
parent
143c8afcc3
commit
fec577ba39
1 changed files with 53 additions and 14 deletions
|
|
@ -272,6 +272,7 @@
|
|||
import { ref, computed, onMounted } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useMarketStore } from '@/stores/market'
|
||||
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
|
||||
import {
|
||||
Card,
|
||||
CardHeader,
|
||||
|
|
@ -291,6 +292,7 @@ import {
|
|||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const marketStore = useMarketStore()
|
||||
const authService = injectService(SERVICE_TOKENS.AUTH_SERVICE) as any
|
||||
|
||||
// State
|
||||
const isLoading = ref(true)
|
||||
|
|
@ -402,24 +404,61 @@ const placeOrder = async () => {
|
|||
error.value = null
|
||||
|
||||
try {
|
||||
// Mock order placement
|
||||
await new Promise(resolve => setTimeout(resolve, 2000))
|
||||
// Ensure we have the required data
|
||||
if (!checkoutCart.value) {
|
||||
throw new Error('No checkout cart found')
|
||||
}
|
||||
if (!currentStall.value) {
|
||||
throw new Error('Stall information not available')
|
||||
}
|
||||
if (!authService.isAuthenticated.value || !authService.user.value?.pubkey) {
|
||||
throw new Error('You must be logged in to place an order')
|
||||
}
|
||||
|
||||
// In real app, would create Nostr order event and handle payment
|
||||
console.log('Placing order:', {
|
||||
// Create the order using the market store's order placement functionality
|
||||
const orderData = {
|
||||
stallId: stallId.value,
|
||||
cart: checkoutCart.value,
|
||||
shipping: selectedShippingZone.value,
|
||||
contact: contactData.value,
|
||||
paymentMethod: paymentMethod.value,
|
||||
total: orderTotal.value
|
||||
})
|
||||
buyerPubkey: authService.user.value?.pubkey || '',
|
||||
sellerPubkey: currentStall.value.pubkey,
|
||||
status: 'pending' as const,
|
||||
items: checkoutCart.value.products.map(item => ({
|
||||
productId: item.product.id,
|
||||
productName: item.product.name,
|
||||
quantity: item.quantity,
|
||||
price: item.product.price,
|
||||
currency: item.product.currency
|
||||
})),
|
||||
contactInfo: {
|
||||
address: contactData.value.address || undefined,
|
||||
email: contactData.value.email || undefined,
|
||||
message: contactData.value.message || undefined,
|
||||
npub: contactData.value.npub || undefined
|
||||
},
|
||||
shippingZone: selectedShippingZone.value || {
|
||||
id: 'default',
|
||||
name: 'Default Shipping',
|
||||
cost: 0,
|
||||
currency: checkoutCart.value.currency,
|
||||
requiresPhysicalShipping: false
|
||||
},
|
||||
paymentMethod: paymentMethod.value === 'ln' ? 'lightning' as const : 'btc_onchain' as const,
|
||||
subtotal: orderSubtotal.value,
|
||||
shippingCost: selectedShippingZone.value?.cost || 0,
|
||||
total: orderTotal.value,
|
||||
currency: checkoutCart.value.currency,
|
||||
cartId: checkoutCart.value.id
|
||||
}
|
||||
|
||||
// Clear the checkout cart
|
||||
marketStore.clearCheckoutCart()
|
||||
console.log('Creating order:', orderData)
|
||||
|
||||
// Create and place the order via the market store
|
||||
const order = await marketStore.createAndPlaceOrder(orderData)
|
||||
|
||||
console.log('Order placed successfully:', order)
|
||||
orderPlaced.value = true
|
||||
|
||||
} catch (err) {
|
||||
console.error('Failed to place order:', err)
|
||||
error.value = err instanceof Error ? err.message : 'Failed to place order'
|
||||
} finally {
|
||||
isPlacingOrder.value = false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue