Implemented coin purchase-flow
This commit is contained in:
@@ -15,7 +15,7 @@ class CoinPurchaseController extends Controller
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'euros' => 'required|numeric|min:1|max:100',
|
||||
'payment_type' => 'required|in:MBWAY,IBAN,MB,VISA,PAYPAL',
|
||||
'payment_type' => 'required|in:MBWAY,MB,VISA,PAYPAL',
|
||||
'payment_reference' => [
|
||||
'required',
|
||||
function ($attribute, $value, $fail) use ($request) {
|
||||
@@ -24,7 +24,6 @@ class CoinPurchaseController extends Controller
|
||||
|
||||
switch ($type) {
|
||||
case 'MBWAY': $regex = '/^9[0-9]{8}$/'; break;
|
||||
case 'IBAN': $regex = '/^[A-Z]{2}[0-9]{23}$/'; break;
|
||||
case 'MB': $regex = '/^[0-9]{5}-[0-9]{9}$/'; break;
|
||||
case 'VISA': $regex = '/^4[0-9]{15}$/'; break;
|
||||
case 'PAYPAL':
|
||||
|
||||
@@ -25,6 +25,12 @@ Content-Type: application/json
|
||||
Accept: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Get me coins
|
||||
GET http://localhost:8000/api/v1/users/me/coins
|
||||
Content-Type: application/json
|
||||
Accept: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### Get me matches
|
||||
GET http://localhost:8000/api/v1/users/{{id}}/matches
|
||||
Content-Type: application/json
|
||||
@@ -35,4 +41,16 @@ Authorization: Bearer {{token}}
|
||||
GET http://localhost:8000/api/v1/users/{{id}}/transactions?type=C
|
||||
Content-Type: application/json
|
||||
Accept: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### post coin-purchases
|
||||
POST http://localhost:8000/api/v1/coin-purchases
|
||||
Content-Type: application/json
|
||||
Accept: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
{
|
||||
"euros": 1,
|
||||
"payment_type": "MBWAY",
|
||||
"payment_reference": "912345678"
|
||||
}
|
||||
@@ -23,14 +23,14 @@
|
||||
</NavigationMenuItem>
|
||||
|
||||
<NavigationMenuItem v-else class="flex flex-row gap-4">
|
||||
<NavigationMenuLink v-if="coins > 0" href="/user?tab=transaction-history"
|
||||
<NavigationMenuLink v-if="userStore.coins > 0" href="/coins-purchase"
|
||||
class="flex flex-row items-center gap-1">
|
||||
<div class="flex items-center justify-center w-5 h-5 bg-purple-400 rounded-full shadow-sm">
|
||||
<span class="text-[10px] font-bold text-purple-900">$</span>
|
||||
</div>
|
||||
|
||||
<span class="text-sm font-medium tabular-nums">
|
||||
{{ coins }} <span class="text-muted-foreground text-xs">Coins</span>
|
||||
{{ userStore.coins }} <span class="text-muted-foreground text-xs">Coins</span>
|
||||
</span>
|
||||
</NavigationMenuLink>
|
||||
<NavigationMenuLink>
|
||||
@@ -56,13 +56,12 @@ import {
|
||||
NavigationMenuTrigger,
|
||||
} from '@/components/ui/navigation-menu'
|
||||
import router from '@/router';
|
||||
import { ref, watch } from 'vue';
|
||||
import { watch } from 'vue';
|
||||
import { useUserStore } from '@/stores/user';
|
||||
|
||||
|
||||
const emits = defineEmits(['logout'])
|
||||
const { userLoggedIn } = defineProps(['userLoggedIn'])
|
||||
const coins = ref(0);
|
||||
|
||||
const biscaStore = useBiscaStore()
|
||||
|
||||
@@ -84,22 +83,13 @@ const logoutClickHandler = () => {
|
||||
router.push('/login')
|
||||
}
|
||||
|
||||
const fetchUserCoins = async () => {
|
||||
if (userLoggedIn) {
|
||||
try {
|
||||
const response = await useUserStore().getCoins()
|
||||
coins.value = response.data?.coins || 0
|
||||
} catch (error) {
|
||||
console.error('Error fetching user coins', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
const userStore = useUserStore()
|
||||
|
||||
watch(() => userLoggedIn, async (isLoggedIn) => {
|
||||
if (isLoggedIn) {
|
||||
await fetchUserCoins()
|
||||
await userStore.fetchCoins()
|
||||
} else {
|
||||
coins.value = 0
|
||||
userStore.coins = 0
|
||||
}
|
||||
}, { immediate: true })
|
||||
</script>
|
||||
@@ -0,0 +1,139 @@
|
||||
<template>
|
||||
<div class="flex min-h-screen items-center justify-center px-4 py-12 sm:px-6 lg:px-8">
|
||||
<div class="w-full max-w-md space-y-8">
|
||||
<div>
|
||||
<h2 class="mt-6 text-center text-3xl font-bold tracking-tight text-gray-900">Buy Coins</h2>
|
||||
<p class="mt-2 text-center text-sm text-gray-600">€1.00 = 10 Coins</p>
|
||||
</div>
|
||||
|
||||
<form class="mt-8 space-y-4" @submit.prevent="handleSubmit">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Amount (Euros)</label>
|
||||
<Input v-model.number="formData.euros" type="number" min="1" max="100" placeholder="Ex: 10"
|
||||
required />
|
||||
<p class="mt-1 text-xs text-blue-600 font-medium">
|
||||
You will receive: {{ formData.euros * 10 }} coins
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Payment Method</label>
|
||||
<select v-model="formData.payment_type"
|
||||
class="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2">
|
||||
<option value="MBWAY">MB WAY</option>
|
||||
<option value="MB">Multibanco (MB)</option>
|
||||
<option value="VISA">VISA</option>
|
||||
<option value="PAYPAL">PayPal</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">
|
||||
{{ referenceLabel }}
|
||||
</label>
|
||||
<Input v-model="formData.payment_reference" type="text" :placeholder="referencePlaceholder"
|
||||
required />
|
||||
<p v-if="errors.payment_reference" class="mt-1 text-xs text-red-500">
|
||||
{{ errors.payment_reference }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="pt-4">
|
||||
<Button type="submit" class="w-full">
|
||||
Confirm Purchase (€{{ formData.euros }})
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed } from 'vue'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { usePurchaseStore } from '@/stores/purchase'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { toast } from 'vue-sonner'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
|
||||
const purchaseStore = usePurchaseStore()
|
||||
const router = useRouter()
|
||||
|
||||
const formData = ref({
|
||||
euros: 1,
|
||||
payment_type: 'MBWAY',
|
||||
payment_reference: ''
|
||||
})
|
||||
|
||||
const errors = ref({})
|
||||
|
||||
const referenceLabel = computed(() => {
|
||||
const labels = {
|
||||
MBWAY: 'Phone Number',
|
||||
MB: 'Entity-Reference',
|
||||
VISA: 'Card Number',
|
||||
PAYPAL: 'PayPal Email'
|
||||
}
|
||||
return labels[formData.value.payment_type]
|
||||
})
|
||||
|
||||
const referencePlaceholder = computed(() => {
|
||||
const placeholders = {
|
||||
MBWAY: '912345678',
|
||||
MB: '12345-123456789',
|
||||
VISA: '1234 5678 9012 3456',
|
||||
PAYPAL: '[email protected]'
|
||||
}
|
||||
return placeholders[formData.value.payment_type]
|
||||
})
|
||||
|
||||
const validateForm = () => {
|
||||
errors.value = {}
|
||||
const type = formData.value.payment_type
|
||||
const refValue = formData.value.payment_reference
|
||||
|
||||
const patterns = {
|
||||
MBWAY: /^9[0-9]{8}$/,
|
||||
MB: /^[0-9]{5}-[0-9]{9}$/,
|
||||
VISA: /^4[0-9]{15}$/,
|
||||
PAYPAL: /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
}
|
||||
|
||||
if (!patterns[type].test(refValue)) {
|
||||
errors.value.payment_reference = `Invalid format for ${type}`
|
||||
return false
|
||||
}
|
||||
|
||||
if (formData.value.euros < 1 || formData.value.euros > 100) {
|
||||
toast.error("Amount must be between 1 and 100")
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
const userStore = useUserStore()
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!validateForm()) return
|
||||
|
||||
const promise = purchaseStore.purchase(formData.value)
|
||||
|
||||
toast.promise(promise, {
|
||||
loading: 'Processing transaction...',
|
||||
success: (res) => {
|
||||
userStore.fetchCoins()
|
||||
router.push('/')
|
||||
return `Purchase successful! New balance: ${res.data.balance} coins`
|
||||
},
|
||||
error: (error) => {
|
||||
if (error.response?.status === 422) {
|
||||
errors.value = error.response.data.errors
|
||||
return "Please check the highlighted fields"
|
||||
}
|
||||
return error.response?.data?.message || "Transaction failed"
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
@@ -10,6 +10,7 @@ import TestAllAnimations from '@/pages/TestAllAnimations.vue'
|
||||
import TestGameBoard from '@/pages/TestGameBoard.vue'
|
||||
import SinglePlayerGamePage from '@/pages/game/SinglePlayerGamePage.vue'
|
||||
import RegisterPage from '@/pages/register/RegisterPage.vue'
|
||||
import CoinsPurchasePage from '@/pages/purchase/CoinPurchasePage.vue'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
@@ -46,6 +47,12 @@ const router = createRouter({
|
||||
{
|
||||
path: '/user',
|
||||
component: UserPage,
|
||||
meta: { requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: '/coins-purchase',
|
||||
component: CoinsPurchasePage,
|
||||
meta: { requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: '/testing',
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import axios from 'axios'
|
||||
import { defineStore } from 'pinia'
|
||||
import { inject } from 'vue'
|
||||
|
||||
export const usePurchaseStore = defineStore('purchase', () => {
|
||||
|
||||
const API_BASE_URL = inject('apiBaseURL')
|
||||
|
||||
const purchase = async (data) => {
|
||||
return await axios.post(`${API_BASE_URL}/coin-purchases`, data)
|
||||
}
|
||||
|
||||
return {
|
||||
purchase,
|
||||
}
|
||||
})
|
||||
@@ -1,16 +1,24 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import axios from 'axios'
|
||||
import { inject } from 'vue'
|
||||
import { inject, ref } from 'vue'
|
||||
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
|
||||
const API_BASE_URL = inject('apiBaseURL')
|
||||
const coins = ref(0)
|
||||
|
||||
const getCoins = async () => {
|
||||
return await axios.get(`${API_BASE_URL}/users/me/coins`)
|
||||
const fetchCoins = async () => {
|
||||
try {
|
||||
const response = await axios.get(`${API_BASE_URL}/users/me/coins`)
|
||||
coins.value = response.data?.coins || 0
|
||||
return response
|
||||
} catch (error) {
|
||||
console.error('Error fetching coins', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
getCoins,
|
||||
coins,
|
||||
fetchCoins,
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user