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 { ref, computed, onMounted } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import { useMarketStore } from '@/stores/market'
|
import { useMarketStore } from '@/stores/market'
|
||||||
|
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
CardHeader,
|
CardHeader,
|
||||||
|
|
@ -291,6 +292,7 @@ import {
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const marketStore = useMarketStore()
|
const marketStore = useMarketStore()
|
||||||
|
const authService = injectService(SERVICE_TOKENS.AUTH_SERVICE) as any
|
||||||
|
|
||||||
// State
|
// State
|
||||||
const isLoading = ref(true)
|
const isLoading = ref(true)
|
||||||
|
|
@ -402,24 +404,61 @@ const placeOrder = async () => {
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Mock order placement
|
// Ensure we have the required data
|
||||||
await new Promise(resolve => setTimeout(resolve, 2000))
|
if (!checkoutCart.value) {
|
||||||
|
throw new Error('No checkout cart found')
|
||||||
// In real app, would create Nostr order event and handle payment
|
}
|
||||||
console.log('Placing order:', {
|
if (!currentStall.value) {
|
||||||
stallId: stallId.value,
|
throw new Error('Stall information not available')
|
||||||
cart: checkoutCart.value,
|
}
|
||||||
shipping: selectedShippingZone.value,
|
if (!authService.isAuthenticated.value || !authService.user.value?.pubkey) {
|
||||||
contact: contactData.value,
|
throw new Error('You must be logged in to place an order')
|
||||||
paymentMethod: paymentMethod.value,
|
}
|
||||||
total: orderTotal.value
|
|
||||||
})
|
|
||||||
|
|
||||||
// Clear the checkout cart
|
// Create the order using the market store's order placement functionality
|
||||||
marketStore.clearCheckoutCart()
|
const orderData = {
|
||||||
|
stallId: stallId.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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
orderPlaced.value = true
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('Failed to place order:', err)
|
||||||
error.value = err instanceof Error ? err.message : 'Failed to place order'
|
error.value = err instanceof Error ? err.message : 'Failed to place order'
|
||||||
} finally {
|
} finally {
|
||||||
isPlacingOrder.value = false
|
isPlacingOrder.value = false
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue