Implemented coin purchase-flow #89
@@ -15,7 +15,7 @@ class CoinPurchaseController extends Controller
|
|||||||
{
|
{
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'euros' => 'required|numeric|min:1|max:100',
|
'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' => [
|
'payment_reference' => [
|
||||||
'required',
|
'required',
|
||||||
function ($attribute, $value, $fail) use ($request) {
|
function ($attribute, $value, $fail) use ($request) {
|
||||||
@@ -24,7 +24,6 @@ class CoinPurchaseController extends Controller
|
|||||||
|
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case 'MBWAY': $regex = '/^9[0-9]{8}$/'; break;
|
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 'MB': $regex = '/^[0-9]{5}-[0-9]{9}$/'; break;
|
||||||
case 'VISA': $regex = '/^4[0-9]{15}$/'; break;
|
case 'VISA': $regex = '/^4[0-9]{15}$/'; break;
|
||||||
case 'PAYPAL':
|
case 'PAYPAL':
|
||||||
|
|||||||
@@ -25,6 +25,12 @@ Content-Type: application/json
|
|||||||
Accept: application/json
|
Accept: application/json
|
||||||
Authorization: Bearer {{token}}
|
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 me matches
|
||||||
GET http://localhost:8000/api/v1/users/{{id}}/matches
|
GET http://localhost:8000/api/v1/users/{{id}}/matches
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
@@ -36,3 +42,15 @@ GET http://localhost:8000/api/v1/users/{{id}}/transactions?type=C
|
|||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
Accept: 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>
|
||||||
|
|
||||||
<NavigationMenuItem v-else class="flex flex-row gap-4">
|
<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">
|
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">
|
<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>
|
<span class="text-[10px] font-bold text-purple-900">$</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<span class="text-sm font-medium tabular-nums">
|
<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>
|
</span>
|
||||||
</NavigationMenuLink>
|
</NavigationMenuLink>
|
||||||
<NavigationMenuLink>
|
<NavigationMenuLink>
|
||||||
@@ -56,13 +56,12 @@ import {
|
|||||||
NavigationMenuTrigger,
|
NavigationMenuTrigger,
|
||||||
} from '@/components/ui/navigation-menu'
|
} from '@/components/ui/navigation-menu'
|
||||||
import router from '@/router';
|
import router from '@/router';
|
||||||
import { ref, watch } from 'vue';
|
import { watch } from 'vue';
|
||||||
import { useUserStore } from '@/stores/user';
|
import { useUserStore } from '@/stores/user';
|
||||||
|
|
||||||
|
|
||||||
const emits = defineEmits(['logout'])
|
const emits = defineEmits(['logout'])
|
||||||
const { userLoggedIn } = defineProps(['userLoggedIn'])
|
const { userLoggedIn } = defineProps(['userLoggedIn'])
|
||||||
const coins = ref(0);
|
|
||||||
|
|
||||||
const biscaStore = useBiscaStore()
|
const biscaStore = useBiscaStore()
|
||||||
|
|
||||||
@@ -84,22 +83,13 @@ const logoutClickHandler = () => {
|
|||||||
router.push('/login')
|
router.push('/login')
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchUserCoins = async () => {
|
const userStore = useUserStore()
|
||||||
if (userLoggedIn) {
|
|
||||||
try {
|
|
||||||
const response = await useUserStore().getCoins()
|
|
||||||
coins.value = response.data?.coins || 0
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching user coins', error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(() => userLoggedIn, async (isLoggedIn) => {
|
watch(() => userLoggedIn, async (isLoggedIn) => {
|
||||||
if (isLoggedIn) {
|
if (isLoggedIn) {
|
||||||
await fetchUserCoins()
|
await userStore.fetchCoins()
|
||||||
} else {
|
} else {
|
||||||
coins.value = 0
|
userStore.coins = 0
|
||||||
}
|
}
|
||||||
}, { immediate: true })
|
}, { immediate: true })
|
||||||
</script>
|
</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 TestGameBoard from '@/pages/TestGameBoard.vue'
|
||||||
import SinglePlayerGamePage from '@/pages/game/SinglePlayerGamePage.vue'
|
import SinglePlayerGamePage from '@/pages/game/SinglePlayerGamePage.vue'
|
||||||
import RegisterPage from '@/pages/register/RegisterPage.vue'
|
import RegisterPage from '@/pages/register/RegisterPage.vue'
|
||||||
|
import CoinsPurchasePage from '@/pages/purchase/CoinPurchasePage.vue'
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
@@ -46,6 +47,12 @@ const router = createRouter({
|
|||||||
{
|
{
|
||||||
path: '/user',
|
path: '/user',
|
||||||
component: UserPage,
|
component: UserPage,
|
||||||
|
meta: { requiresAuth: true },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/coins-purchase',
|
||||||
|
component: CoinsPurchasePage,
|
||||||
|
meta: { requiresAuth: true },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/testing',
|
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 { defineStore } from 'pinia'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { inject } from 'vue'
|
import { inject, ref } from 'vue'
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', () => {
|
export const useUserStore = defineStore('user', () => {
|
||||||
|
|
||||||
const API_BASE_URL = inject('apiBaseURL')
|
const API_BASE_URL = inject('apiBaseURL')
|
||||||
|
const coins = ref(0)
|
||||||
|
|
||||||
const getCoins = async () => {
|
const fetchCoins = async () => {
|
||||||
return await axios.get(`${API_BASE_URL}/users/me/coins`)
|
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 {
|
return {
|
||||||
getCoins,
|
coins,
|
||||||
|
fetchCoins,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user