Updated charge close to working
This commit is contained in:
518
src/pages/pay/oil/authorize_preauthcharge.vue
Normal file
518
src/pages/pay/oil/authorize_preauthcharge.vue
Normal file
@@ -0,0 +1,518 @@
|
||||
<!-- src/pages/pay/oil/authorize_preauthcharge.vue -->
|
||||
<template>
|
||||
<div class="flex">
|
||||
|
||||
|
||||
<!-- Main Content -->
|
||||
<div class="flex-1 px-8 py-6">
|
||||
|
||||
<!-- Breadcrumbs & Header -->
|
||||
<div class="text-sm breadcrumbs mb-6">
|
||||
<ul>
|
||||
<li><router-link :to="{ name: 'home' }">Home</router-link></li>
|
||||
<li><router-link :to="{ name: 'payOil', params: { id: deliveryId } }">Payment Confirmation</router-link></li>
|
||||
<li>Payment Authorization</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<!-- 2x2 Grid Layout -->
|
||||
<div class="max-w-6xl">
|
||||
<h1 class="text-3xl font-bold mb-8">Payment Authorization Authorize.net</h1>
|
||||
|
||||
<!-- Top Row: Charge Breakdown and Payment Method -->
|
||||
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
|
||||
<!-- Charge Breakdown -->
|
||||
<div class="bg-base-100 rounded-lg p-6">
|
||||
<h3 class="text-lg font-semibold mb-4">Charge Breakdown</h3>
|
||||
<div class="space-y-2">
|
||||
<div class="flex justify-between">
|
||||
<span>Gallons Ordered:</span>
|
||||
<span>{{ delivery.gallons_ordered || 0 }} gallons</span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span>Price per Gallon:</span>
|
||||
<span>${{ delivery.customer_price || 0 }}</span>
|
||||
</div>
|
||||
<div class="flex justify-between font-semibold">
|
||||
<span>Subtotal:</span>
|
||||
<span>${{ calculateSubtotal() }}</span>
|
||||
</div>
|
||||
<div v-if="delivery.prime == 1" class="flex justify-between text-sm">
|
||||
<span>Prime Fee:</span>
|
||||
<span>+ ${{ pricing.price_prime || 0 }}</span>
|
||||
</div>
|
||||
<div v-if="delivery.same_day == 1" class="flex justify-between text-sm">
|
||||
<span>Same Day Fee:</span>
|
||||
<span>+ ${{ pricing.price_same_day || 0 }}</span>
|
||||
</div>
|
||||
<div v-if="delivery.emergency == 1" class="flex justify-between text-sm">
|
||||
<span>Emergency Fee:</span>
|
||||
<span>+ ${{ pricing.price_emergency || 0 }}</span>
|
||||
</div>
|
||||
<div v-if="promo_active" class="flex justify-between text-success">
|
||||
<span>{{ promo.name_of_promotion }}:</span>
|
||||
<span>- ${{ discount }}</span>
|
||||
</div>
|
||||
<hr class="my-3">
|
||||
<div class="flex justify-between font-bold text-lg">
|
||||
<span>Total:</span>
|
||||
<span>${{ promo_active ? total_amount_after_discount : total_amount }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Credit Card Display -->
|
||||
<div class="bg-base-100 rounded-lg p-6">
|
||||
<h3 class="text-lg font-semibold mb-4">Payment Method</h3>
|
||||
<div v-if="selectedCard" class="bg-base-200 p-4 rounded-md">
|
||||
<div class="flex justify-between items-center mb-2">
|
||||
<div class="font-semibold">{{ selectedCard.type_of_card }}</div>
|
||||
<div v-if="selectedCard.main_card" class="badge badge-primary">Primary</div>
|
||||
</div>
|
||||
<div class="font-mono text-sm">
|
||||
<div> {{ selectedCard.card_number }}</div>
|
||||
<div>{{ selectedCard.name_on_card }}</div>
|
||||
<div>Expires: {{ selectedCard.expiration_month }}/{{ selectedCard.expiration_year }}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div v-else class="text-gray-500 p-4">
|
||||
No payment method selected
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Error/Success Messages -->
|
||||
<div class="mb-6">
|
||||
<div v-if="error" class="alert alert-error">
|
||||
<span>{{ error }}</span>
|
||||
</div>
|
||||
<div v-if="success" class="alert alert-success">
|
||||
<span>{{ success }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Bottom Section: Charge Amount Input and Action Buttons -->
|
||||
<div class="bg-base-100 rounded-lg p-6">
|
||||
<div class="flex flex-col lg:flex-row lg:items-center gap-6">
|
||||
<!-- Charge Amount Input - Compact -->
|
||||
<div class="flex-1">
|
||||
<h3 class="text-lg font-semibold mb-2">Charge Amount</h3>
|
||||
<div class="flex">
|
||||
<span class="inline-flex items-center px-3 text-sm bg-base-200 rounded-l-lg border border-r-0">$</span>
|
||||
<input
|
||||
v-model="chargeAmount"
|
||||
type="number"
|
||||
step="0.01"
|
||||
class="input input-bordered flex-1 rounded-l-none"
|
||||
placeholder="Enter amount"
|
||||
:disabled="loading"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Action Buttons -->
|
||||
<div class="flex gap-3 align-bottom">
|
||||
<router-link :to="{ name: 'payOil', params: { id: deliveryId } }">
|
||||
<button class="btn btn-ghost">Cancel</button>
|
||||
</router-link>
|
||||
<button
|
||||
@click="handlePreauthorize"
|
||||
class="btn btn-warning"
|
||||
:disabled="loading || !chargeAmount"
|
||||
>
|
||||
<span v-if="loading && action === 'preauthorize'" class="loading loading-spinner loading-sm"></span>
|
||||
Preauthorize
|
||||
</button>
|
||||
<button
|
||||
@click="handleChargeNow"
|
||||
class="btn btn-primary"
|
||||
:disabled="loading || !chargeAmount"
|
||||
>
|
||||
<span v-if="loading && action === 'charge'" class="loading loading-spinner loading-sm"></span>
|
||||
Charge Now
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, watch } from 'vue'
|
||||
import axios from 'axios'
|
||||
import authHeader from '../../../services/auth.header'
|
||||
import { notify } from "@kyvg/vue3-notification"
|
||||
|
||||
export default defineComponent({
|
||||
name: 'AuthorizePreauthCharge',
|
||||
|
||||
data() {
|
||||
return {
|
||||
deliveryId: this.$route.params.id as string,
|
||||
loaded: false,
|
||||
chargeAmount: 0,
|
||||
loading: false,
|
||||
action: '', // 'preauthorize' or 'charge'
|
||||
error: '',
|
||||
success: '',
|
||||
user: {
|
||||
user_id: 0,
|
||||
},
|
||||
delivery: {
|
||||
id: 0,
|
||||
customer_id: 0,
|
||||
customer_name: '',
|
||||
customer_address: '',
|
||||
customer_town: '',
|
||||
customer_state: 0,
|
||||
customer_zip: '',
|
||||
gallons_ordered: 0,
|
||||
customer_asked_for_fill: 0,
|
||||
gallons_delivered: 0,
|
||||
customer_filled: 0,
|
||||
delivery_status: 0,
|
||||
when_ordered: '',
|
||||
when_delivered: '',
|
||||
expected_delivery_date: '',
|
||||
automatic: 0,
|
||||
oil_id: 0,
|
||||
supplier_price: 0,
|
||||
customer_price: 0,
|
||||
customer_temperature: 0,
|
||||
dispatcher_notes: '',
|
||||
prime: 0,
|
||||
promo_id: 0,
|
||||
emergency: 0,
|
||||
same_day: 0,
|
||||
payment_type: 0,
|
||||
payment_card_id: 0,
|
||||
driver_employee_id: 0,
|
||||
driver_first_name: '',
|
||||
driver_last_name: '',
|
||||
pre_charge_amount: 0,
|
||||
total_price: 0,
|
||||
service_id: null,
|
||||
},
|
||||
credit_cards: [
|
||||
{
|
||||
id: 0,
|
||||
name_on_card: '',
|
||||
main_card: false,
|
||||
card_number: '',
|
||||
expiration_month: '',
|
||||
type_of_card: '',
|
||||
last_four_digits: '',
|
||||
expiration_year: '',
|
||||
security_number: '',
|
||||
|
||||
}
|
||||
],
|
||||
customer: {
|
||||
id: 0,
|
||||
user_id: 0,
|
||||
customer_first_name: '',
|
||||
customer_last_name: '',
|
||||
customer_town: '',
|
||||
customer_address: '',
|
||||
customer_state: 0,
|
||||
customer_zip: '',
|
||||
customer_apt: '',
|
||||
customer_home_type: 0,
|
||||
customer_phone_number: '',
|
||||
account_number: '',
|
||||
},
|
||||
pricing: {
|
||||
price_from_supplier: 0,
|
||||
price_for_customer: 0,
|
||||
price_for_employee: 0,
|
||||
price_same_day: 0,
|
||||
price_prime: 0,
|
||||
price_emergency: 0,
|
||||
date: "",
|
||||
},
|
||||
promo_active: false,
|
||||
promo: {
|
||||
name_of_promotion: '',
|
||||
description: '',
|
||||
money_off_delivery: 0,
|
||||
text_on_ticket: ''
|
||||
},
|
||||
total_amount: 0,
|
||||
discount: 0,
|
||||
total_amount_after_discount: 0,
|
||||
}
|
||||
},
|
||||
|
||||
computed: {
|
||||
selectedCard(): any {
|
||||
return this.credit_cards.find((card: any) => card.id === this.delivery.payment_card_id)
|
||||
}
|
||||
},
|
||||
|
||||
mounted() {
|
||||
this.loadData(this.deliveryId)
|
||||
},
|
||||
|
||||
created() {
|
||||
this.watchRoute()
|
||||
},
|
||||
|
||||
methods: {
|
||||
watchRoute() {
|
||||
watch(
|
||||
() => this.$route.params.id,
|
||||
(newId) => {
|
||||
if (newId !== this.deliveryId) {
|
||||
this.resetState()
|
||||
this.deliveryId = newId as string
|
||||
this.loadData(newId as string)
|
||||
}
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
resetState() {
|
||||
this.loading = false
|
||||
this.action = ''
|
||||
this.error = ''
|
||||
this.success = ''
|
||||
this.chargeAmount = 0
|
||||
this.promo_active = false
|
||||
this.total_amount = 0
|
||||
this.discount = 0
|
||||
this.total_amount_after_discount = 0
|
||||
this.deliveryId = this.$route.params.id as string
|
||||
},
|
||||
|
||||
loadData(deliveryId: string) {
|
||||
this.userStatus()
|
||||
this.getOilOrder(deliveryId)
|
||||
this.sumdelivery(deliveryId)
|
||||
this.getOilPricing()
|
||||
this.updatestatus()
|
||||
},
|
||||
|
||||
updatestatus() {
|
||||
let path = import.meta.env.VITE_BASE_URL + '/delivery/updatestatus';
|
||||
axios({
|
||||
method: 'get',
|
||||
url: path,
|
||||
headers: authHeader(),
|
||||
}).then((response: any) => {
|
||||
if (response.data.update)
|
||||
console.log("Updated Status of Deliveries")
|
||||
})
|
||||
},
|
||||
|
||||
sumdelivery(delivery_id: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + "/delivery/total/" + delivery_id;
|
||||
axios({
|
||||
method: "get",
|
||||
url: path,
|
||||
withCredentials: true,
|
||||
})
|
||||
.then((response: any) => {
|
||||
if (response.data.ok) {
|
||||
this.total_amount = response.data.total_amount;
|
||||
this.discount = response.data.discount;
|
||||
this.total_amount_after_discount = response.data.total_amount_after_discount;
|
||||
|
||||
// Auto-populate charge amount with the calculated total
|
||||
if (this.promo_active) {
|
||||
this.chargeAmount = this.total_amount_after_discount;
|
||||
} else {
|
||||
this.chargeAmount = this.total_amount;
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
notify({
|
||||
title: "Error",
|
||||
text: "Could not get oil pricing",
|
||||
type: "error",
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
getPromo(promo_id: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + "/promo/" + promo_id;
|
||||
axios({
|
||||
method: "get",
|
||||
url: path,
|
||||
withCredentials: true,
|
||||
headers: authHeader(),
|
||||
})
|
||||
.then((response: any) => {
|
||||
if (response.data) {
|
||||
this.promo = response.data
|
||||
this.promo_active = true
|
||||
|
||||
// Update charge amount when promo is applied
|
||||
if (this.total_amount_after_discount > 0) {
|
||||
this.chargeAmount = this.total_amount_after_discount
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
userStatus() {
|
||||
let path = import.meta.env.VITE_BASE_URL + '/auth/whoami';
|
||||
axios({
|
||||
method: 'get',
|
||||
url: path,
|
||||
withCredentials: true,
|
||||
headers: authHeader(),
|
||||
})
|
||||
.then((response: any) => {
|
||||
if (response.data.ok) {
|
||||
this.user = response.data.user;
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
getOilPricing() {
|
||||
let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/table";
|
||||
axios({
|
||||
method: "get",
|
||||
url: path,
|
||||
withCredentials: true,
|
||||
})
|
||||
.then((response: any) => {
|
||||
this.pricing = response.data;
|
||||
})
|
||||
.catch(() => {
|
||||
notify({
|
||||
title: "Error",
|
||||
text: "Could not get oil pricing",
|
||||
type: "error",
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
getOilOrder(delivery_id: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + "/delivery/order/" + delivery_id;
|
||||
axios({
|
||||
method: "get",
|
||||
url: path,
|
||||
withCredentials: true,
|
||||
})
|
||||
.then((response: any) => {
|
||||
if (response.data && response.data.ok) {
|
||||
this.delivery = response.data.delivery;
|
||||
this.getCustomer(this.delivery.customer_id)
|
||||
this.getCreditCards(this.delivery.customer_id)
|
||||
if (this.delivery.promo_id != null) {
|
||||
this.getPromo(this.delivery.promo_id);
|
||||
this.promo_active = true;
|
||||
}
|
||||
} else {
|
||||
console.error("API Error:", response.data.error || "Failed to fetch delivery data.");
|
||||
}
|
||||
})
|
||||
.catch((error: any) => {
|
||||
console.error("API Error in getOilOrder:", error);
|
||||
notify({
|
||||
title: "Error",
|
||||
text: "Could not get delivery",
|
||||
type: "error",
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
getCreditCards(user_id: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + '/payment/cards/' + user_id;
|
||||
axios({
|
||||
method: 'get',
|
||||
url: path,
|
||||
headers: authHeader(),
|
||||
}).then((response: any) => {
|
||||
this.credit_cards = response.data
|
||||
})
|
||||
},
|
||||
|
||||
getCustomer(userid: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + '/customer/' + userid;
|
||||
axios({
|
||||
method: 'get',
|
||||
url: path,
|
||||
headers: authHeader(),
|
||||
}).then((response: any) => {
|
||||
this.customer = response.data
|
||||
})
|
||||
},
|
||||
|
||||
calculateSubtotal() {
|
||||
const gallons = this.delivery.gallons_ordered || 0
|
||||
const pricePerGallon = this.delivery.customer_price || 0
|
||||
return (gallons * pricePerGallon).toFixed(2)
|
||||
},
|
||||
|
||||
async handlePreauthorize() {
|
||||
await this.processPayment('preauthorize')
|
||||
},
|
||||
|
||||
async handleChargeNow() {
|
||||
await this.processPayment('charge')
|
||||
},
|
||||
|
||||
async processPayment(actionType: string) {
|
||||
if (!this.selectedCard) {
|
||||
this.error = 'No credit card found for this customer'
|
||||
return
|
||||
}
|
||||
|
||||
this.loading = true
|
||||
this.action = actionType
|
||||
this.error = ''
|
||||
this.success = ''
|
||||
|
||||
try {
|
||||
const endpoint = actionType === 'preauthorize' ? 'authorize' : 'charge'
|
||||
const payload = {
|
||||
card_number: (this.selectedCard as any).card_number,
|
||||
expiration_date: `${(this.selectedCard as any).expiration_month}${(this.selectedCard as any).expiration_year}`,
|
||||
cvv: (this.selectedCard as any).security_number,
|
||||
[actionType === 'preauthorize' ? 'preauthorize_amount' : 'charge_amount']: this.chargeAmount.toString(),
|
||||
transaction_type: actionType === 'preauthorize' ? 1 : 0,
|
||||
service_id: this.delivery.service_id || null,
|
||||
delivery_id: this.delivery.id,
|
||||
card_id: (this.selectedCard as any).id
|
||||
}
|
||||
|
||||
console.log('=== DEBUG: Payment payload ===')
|
||||
console.log('Delivery ID:', this.delivery.id)
|
||||
console.log('Final payload being sent:', payload)
|
||||
|
||||
const response = await axios.post(
|
||||
`${import.meta.env.VITE_AUTHORIZE_URL}/api/${endpoint}/?customer_id=${this.customer.id}`,
|
||||
payload,
|
||||
{ withCredentials: true }
|
||||
)
|
||||
|
||||
if (response.data && response.data.status === 0) {
|
||||
this.success = `${actionType === 'preauthorize' ? 'Preauthorization' : 'Charge'} successful! Transaction ID: ${response.data.auth_net_transaction_id || 'N/A'}`
|
||||
setTimeout(() => {
|
||||
this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
|
||||
}, 2000)
|
||||
} else {
|
||||
throw new Error(`Payment ${actionType} failed: ${response.data?.status || 'Unknown error'}`)
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.error = error.response?.data?.detail || `Failed to ${actionType} payment`
|
||||
notify({
|
||||
title: "Error",
|
||||
text: this.error,
|
||||
type: "error",
|
||||
})
|
||||
} finally {
|
||||
this.loading = false
|
||||
this.action = ''
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
@@ -65,72 +65,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Delivery Details Card -->
|
||||
<div class="bg-neutral rounded-lg p-5">
|
||||
<h3 class="text-xl font-bold mb-4">Delivery Details</h3>
|
||||
<div class="grid grid-cols-2 gap-x-4 gap-y-3 text-sm">
|
||||
<div>
|
||||
<div class="font-bold">Status</div>
|
||||
<div class="opacity-80">
|
||||
<span v-if="deliveryOrder.delivery_status == 0">waiting</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 1">delivered</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 2">Today</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 3">Tomorrow</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 4">Partial Delivery</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 5">misdelivery</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 6">unknown</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 10">Finalized</span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">Scheduled Date</div>
|
||||
<div class="opacity-80">{{ deliveryOrder.expected_delivery_date }}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">When Ordered</div>
|
||||
<div class="opacity-80">{{ deliveryOrder.when_ordered }}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">When Delivered</div>
|
||||
<div class="opacity-80">{{ deliveryOrder.when_delivered }}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">Driver</div>
|
||||
<div class="opacity-80">{{ deliveryOrder.driver_first_name }} {{ deliveryOrder.driver_last_name }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Credit Card Details -->
|
||||
<div v-if="userCardfound" class="bg-neutral rounded-lg p-5">
|
||||
<h3 class="text-xl font-bold mb-4">Payment Method</h3>
|
||||
<div class="bg-base-100 p-4 rounded-md">
|
||||
<div class="flex justify-between items-center mb-2">
|
||||
<div class="font-semibold">{{ userCard.type_of_card }}</div>
|
||||
<div v-if="userCard.main_card" class="badge badge-primary">Primary</div>
|
||||
</div>
|
||||
<div class="font-mono text-sm">
|
||||
<div>**** **** **** {{ userCard.last_four_digits }}</div>
|
||||
<div>{{ userCard.name_on_card }}</div>
|
||||
<div>Expires: {{ userCard.expiration_month }}/{{ userCard.expiration_year }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- RIGHT COLUMN: Capture Form -->
|
||||
<div class="space-y-6">
|
||||
|
||||
<!-- Gallons Delivered Display -->
|
||||
<div class="bg-base-100 rounded-lg p-5">
|
||||
<h2 class="text-2xl font-bold mb-4">Gallons Delivered</h2>
|
||||
<div class="text-center">
|
||||
<div class="text-4xl font-bold text-primary">{{ gallonsDelivered || '0.00' }}</div>
|
||||
<div class="text-sm opacity-70">gallons</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Financial Summary Card -->
|
||||
<div class="bg-neutral rounded-lg p-5">
|
||||
<h3 class="text-xl font-bold mb-4">Financial Summary</h3>
|
||||
@@ -165,6 +99,71 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Delivery Details Card -->
|
||||
<div class="bg-neutral rounded-lg p-5">
|
||||
<h3 class="text-xl font-bold mb-4">Delivery Details</h3>
|
||||
<div class="grid grid-cols-2 gap-x-4 gap-y-3 text-sm">
|
||||
<div>
|
||||
<div class="font-bold">Status</div>
|
||||
<div class="opacity-80">
|
||||
<span v-if="deliveryOrder.delivery_status == 0">waiting</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 1">delivered</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 2">Today</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 3">Tomorrow</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 4">Partial Delivery</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 5">misdelivery</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 6">unknown</span>
|
||||
<span v-else-if="deliveryOrder.delivery_status == 10">Finalized</span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">Scheduled Date</div>
|
||||
<div class="opacity-80">{{ deliveryOrder.expected_delivery_date }}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">When Ordered</div>
|
||||
<div class="opacity-80">{{ deliveryOrder.when_ordered }}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">When Delivered</div>
|
||||
<div class="opacity-80">{{ deliveryOrder.when_delivered }}</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Credit Card Details -->
|
||||
<div v-if="userCardfound" class="bg-neutral rounded-lg p-5">
|
||||
<h3 class="text-xl font-bold mb-4">Payment Method</h3>
|
||||
<div class="p-2 rounded-lg border" :class="userCard.main_card ? 'bg-primary/10 border-primary' : 'bg-base-200 border-base-300'">
|
||||
<div class="flex justify-between items-start">
|
||||
<div>
|
||||
<div class="font-bold text-sm">{{ userCard.name_on_card }}</div>
|
||||
<div class="text-xs opacity-70">{{ userCard.type_of_card }}</div>
|
||||
</div>
|
||||
<div v-if="userCard.main_card" class="badge badge-primary badge-sm">Primary</div>
|
||||
</div>
|
||||
<div class="mt-1 text-sm font-mono tracking-wider">
|
||||
<p>**** **** **** {{ userCard.last_four_digits }}</p>
|
||||
<p>Exp: <span v-if="Number(userCard.expiration_month) < 10">0</span>{{ userCard.expiration_month }} / {{ userCard.expiration_year }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- RIGHT COLUMN: Capture Form -->
|
||||
<div class="space-y-6">
|
||||
|
||||
<!-- Gallons Delivered Display -->
|
||||
<div class="bg-base-100 rounded-lg p-5">
|
||||
<h2 class="text-2xl font-bold mb-4">Gallons Delivered</h2>
|
||||
<div class="text-center">
|
||||
<div class="text-4xl font-bold text-primary">{{ gallonsDelivered || '0.00' }}</div>
|
||||
<div class="text-sm opacity-70">gallons</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Capture Payment Form -->
|
||||
<div class="bg-base-100 rounded-lg p-5">
|
||||
<h2 class="text-2xl font-bold mb-4">Capture Payment</h2>
|
||||
@@ -173,24 +172,25 @@
|
||||
<label class="label">
|
||||
<span class="label-text font-bold">Capture Amount</span>
|
||||
</label>
|
||||
<div v-if="captureAmount > preAuthAmount" class="text-sm text-error mb-2">
|
||||
Cannot capture more than the preauthorization amount.
|
||||
</div>
|
||||
<input
|
||||
v-model="captureAmount"
|
||||
class="input input-bordered input-lg w-full"
|
||||
class="input input-bordered input-sm w-full"
|
||||
type="number"
|
||||
step="0.01"
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info">
|
||||
<span>Pre-authorized transaction found. Ready to capture payment.</span>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="flex gap-4">
|
||||
<button
|
||||
@click="capturePayment"
|
||||
class="btn btn-primary flex-1"
|
||||
:disabled="loading || !captureAmount"
|
||||
:class="`btn flex-1 ${captureAmount > 0 ? 'btn-success' : 'btn-error'}`"
|
||||
:disabled="loading || !captureAmount || captureAmount > preAuthAmount"
|
||||
>
|
||||
<span v-if="loading" class="loading loading-spinner loading-sm"></span>
|
||||
Capture Payment
|
||||
@@ -219,10 +219,10 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue'
|
||||
import axios from 'axios'
|
||||
import authHeader from '../../services/auth.header'
|
||||
import Header from '../../layouts/headers/headerauth.vue'
|
||||
import SideBar from '../../layouts/sidebar/sidebar.vue'
|
||||
import Footer from '../../layouts/footers/footer.vue'
|
||||
import authHeader from '../../../services/auth.header'
|
||||
import Header from '../../../layouts/headers/headerauth.vue'
|
||||
import SideBar from '../../../layouts/sidebar/sidebar.vue'
|
||||
import Footer from '../../../layouts/footers/footer.vue'
|
||||
import { notify } from "@kyvg/vue3-notification"
|
||||
|
||||
export default defineComponent({
|
||||
@@ -240,6 +240,7 @@ export default defineComponent({
|
||||
userCardfound: false,
|
||||
gallonsDelivered: '',
|
||||
captureAmount: 0,
|
||||
preAuthAmount: 0,
|
||||
transaction: null as any,
|
||||
|
||||
userCard: {
|
||||
@@ -311,10 +312,6 @@ export default defineComponent({
|
||||
},
|
||||
|
||||
mounted() {
|
||||
this.gallonsDelivered = this.$route.query.gallons as string || '';
|
||||
// Use the amount from the query params, passed from the finalize page
|
||||
this.captureAmount = parseFloat(this.$route.query.amount as string) || 0;
|
||||
|
||||
this.getOilOrder(this.$route.params.id)
|
||||
this.getOilPricing()
|
||||
this.getTransaction()
|
||||
@@ -327,11 +324,15 @@ export default defineComponent({
|
||||
.then((response: any) => {
|
||||
if (response.data && response.data.ok) {
|
||||
this.deliveryOrder = response.data.delivery;
|
||||
this.gallonsDelivered = this.deliveryOrder.gallons_delivered;
|
||||
this.getCustomer(this.deliveryOrder.customer_id);
|
||||
|
||||
if ([1, 2, 3].includes(this.deliveryOrder.payment_type)) {
|
||||
this.getPaymentCard(this.deliveryOrder.payment_card_id);
|
||||
}
|
||||
|
||||
// Calculate capture amount if pricing is already loaded
|
||||
this.calculateCaptureAmount();
|
||||
} else {
|
||||
console.error("API Error:", response.data.error || "Failed to fetch delivery data.");
|
||||
}
|
||||
@@ -371,6 +372,8 @@ export default defineComponent({
|
||||
axios.get(path, { withCredentials: true })
|
||||
.then((response: any) => {
|
||||
this.pricing = response.data;
|
||||
// Calculate capture amount if delivery order is already loaded
|
||||
this.calculateCaptureAmount();
|
||||
})
|
||||
.catch((error: any) => {
|
||||
notify({ title: "Error", text: "Could not get oil pricing", type: "error" });
|
||||
@@ -383,6 +386,10 @@ export default defineComponent({
|
||||
axios.get(path, { withCredentials: true, headers: authHeader() })
|
||||
.then((response: any) => {
|
||||
this.transaction = response.data;
|
||||
this.preAuthAmount = parseFloat(response.data.preauthorize_amount || 0);
|
||||
if (response.data.status !== 0) { // Not approved
|
||||
this.preAuthAmount = 0;
|
||||
}
|
||||
})
|
||||
.catch((error: any) => {
|
||||
notify({ title: "Error", text: "No pre-authorized transaction found", type: "error" });
|
||||
@@ -452,6 +459,29 @@ export default defineComponent({
|
||||
}
|
||||
},
|
||||
|
||||
calculateCaptureAmount() {
|
||||
// Only calculate if we have both delivery order and pricing data
|
||||
if (this.deliveryOrder.id && this.pricing.price_for_customer) {
|
||||
const gallons = typeof this.gallonsDelivered === 'string' ? parseFloat(this.gallonsDelivered) : Number(this.gallonsDelivered) || 0;
|
||||
const pricePerGallon = typeof this.deliveryOrder.customer_price === 'string' ? parseFloat(this.deliveryOrder.customer_price) : Number(this.deliveryOrder.customer_price) || 0;
|
||||
let total = gallons * pricePerGallon;
|
||||
|
||||
// Add prime fee if applicable
|
||||
if (this.deliveryOrder.prime == 1) {
|
||||
const primeFee = typeof this.pricing.price_prime === 'string' ? parseFloat(this.pricing.price_prime) : Number(this.pricing.price_prime) || 0;
|
||||
total += primeFee;
|
||||
}
|
||||
|
||||
// Add same day fee if applicable
|
||||
if (this.deliveryOrder.same_day === 1) {
|
||||
const sameDayFee = typeof this.pricing.price_same_day === 'string' ? parseFloat(this.pricing.price_same_day) : Number(this.pricing.price_same_day) || 0;
|
||||
total += sameDayFee;
|
||||
}
|
||||
|
||||
this.captureAmount = total;
|
||||
}
|
||||
},
|
||||
|
||||
cancelCapture() {
|
||||
this.$router.push({ name: 'finalizeTicket', params: { id: this.$route.params.id } });
|
||||
},
|
||||
@@ -459,4 +489,4 @@ export default defineComponent({
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
<style scoped></style>
|
||||
@@ -153,12 +153,12 @@
|
||||
<div class="bg-neutral rounded-lg p-5">
|
||||
<div class="flex flex-wrap gap-4 justify-between items-center">
|
||||
<!-- Pay Authorize Button -->
|
||||
<button class="btn btn-warning" @click="showPaymentPopup = true">
|
||||
Pay Authorize
|
||||
<button class="btn btn-warning" @click="$router.push({ name: 'authorizePreauthCharge', params: { id: $route.params.id } })">
|
||||
Pay Authorize.net
|
||||
</button>
|
||||
<!-- A single confirm button is cleaner -->
|
||||
<button class="btn btn-primary" @click="checkoutOilUpdatePayment(delivery.payment_type)">
|
||||
Confirm & Process Payment
|
||||
Pay Tiger
|
||||
</button>
|
||||
<router-link v-if="delivery && delivery.id" :to="{ name: 'deliveryEdit', params: { id: delivery.id } }">
|
||||
<button class="btn btn-sm btn-ghost">Edit Delivery</button>
|
||||
@@ -171,33 +171,17 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Payment Authorization Popup -->
|
||||
<PaymentAuthorizePopup
|
||||
:show="showPaymentPopup"
|
||||
:delivery="delivery"
|
||||
:customer="customer"
|
||||
:credit-cards="credit_cards"
|
||||
:pricing="pricing"
|
||||
:promo-active="promo_active"
|
||||
:promo="promo"
|
||||
:total-amount="total_amount"
|
||||
:total-amount-after-discount="total_amount_after_discount"
|
||||
:discount="discount"
|
||||
@close="showPaymentPopup = false"
|
||||
@payment-success="handlePaymentSuccess"
|
||||
/>
|
||||
|
||||
<Footer />
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue'
|
||||
import axios from 'axios'
|
||||
import authHeader from '../../services/auth.header'
|
||||
import Header from '../../layouts/headers/headerauth.vue'
|
||||
import SideBar from '../../layouts/sidebar/sidebar.vue'
|
||||
import Footer from '../../layouts/footers/footer.vue'
|
||||
import PaymentAuthorizePopup from '../../components/PaymentAuthorizePopup.vue'
|
||||
import authHeader from '../../../services/auth.header'
|
||||
import Header from '../../../layouts/headers/headerauth.vue'
|
||||
import SideBar from '../../../layouts/sidebar/sidebar.vue'
|
||||
import Footer from '../../../layouts/footers/footer.vue'
|
||||
|
||||
import useValidate from "@vuelidate/core";
|
||||
import { notify } from "@kyvg/vue3-notification"
|
||||
import { minLength, required } from "@vuelidate/validators";
|
||||
@@ -209,7 +193,6 @@ export default defineComponent({
|
||||
Header,
|
||||
SideBar,
|
||||
Footer,
|
||||
PaymentAuthorizePopup,
|
||||
},
|
||||
|
||||
data() {
|
||||
@@ -306,7 +289,6 @@ export default defineComponent({
|
||||
total_amount: 0,
|
||||
discount: 0,
|
||||
total_amount_after_discount: 0,
|
||||
showPaymentPopup: false,
|
||||
}
|
||||
},
|
||||
validations() {
|
||||
@@ -530,23 +512,7 @@ export default defineComponent({
|
||||
});
|
||||
},
|
||||
|
||||
handlePaymentSuccess(data: any) {
|
||||
// Handle successful payment
|
||||
console.log('Payment successful:', data)
|
||||
|
||||
// Show success notification
|
||||
notify({
|
||||
title: "Success",
|
||||
text: "Payment authorized successfully",
|
||||
type: "success",
|
||||
});
|
||||
|
||||
// Close the popup
|
||||
this.showPaymentPopup = false
|
||||
|
||||
// Redirect to customer profile
|
||||
this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
|
||||
},
|
||||
|
||||
},
|
||||
})
|
||||
@@ -1,7 +1,9 @@
|
||||
|
||||
|
||||
import PayOil from './pay_oil.vue';
|
||||
import CaptureAuthorize from './capture_authorize.vue';
|
||||
import PayOil from './oil/pay_oil.vue';
|
||||
import AuthorizePreauthCharge from './oil/authorize_preauthcharge.vue';
|
||||
import CaptureAuthorize from './oil/capture_authorize.vue';
|
||||
import ChargeServiceAuthorize from './service/capture_authorize.vue';
|
||||
|
||||
const payRoutes = [
|
||||
|
||||
@@ -10,11 +12,21 @@ const payRoutes = [
|
||||
name: 'payOil',
|
||||
component: PayOil,
|
||||
},
|
||||
{
|
||||
path: '/pay/oil/authorize/:id',
|
||||
name: 'authorizePreauthCharge',
|
||||
component: AuthorizePreauthCharge,
|
||||
},
|
||||
{
|
||||
path: '/pay/capture/authorize/:id',
|
||||
name: 'captureAuthorize',
|
||||
component: CaptureAuthorize,
|
||||
},
|
||||
{
|
||||
path: '/pay/service/capture/authorize/:id',
|
||||
name: 'chargeServiceAuthorize',
|
||||
component: ChargeServiceAuthorize,
|
||||
},
|
||||
|
||||
]
|
||||
|
||||
|
||||
344
src/pages/pay/service/capture_authorize.vue
Normal file
344
src/pages/pay/service/capture_authorize.vue
Normal file
@@ -0,0 +1,344 @@
|
||||
<template>
|
||||
<div class="flex">
|
||||
<!-- Main container with responsive horizontal padding -->
|
||||
<div class="w-full px-4 py-4 md:px-6">
|
||||
<div v-if="service" class="text-sm breadcrumbs">
|
||||
<ul>
|
||||
<li><router-link :to="{ name: 'home' }">Home</router-link></li>
|
||||
<li><router-link :to="{ name: 'customer' }">Customers</router-link></li>
|
||||
<li>
|
||||
<router-link :to="{ name: 'customerProfile', params: { id: service.customer_id } }">
|
||||
{{ service.customer_name }}
|
||||
</router-link>
|
||||
</li>
|
||||
<li>Charge Service #{{ service.id }}</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<!-- Page Header -->
|
||||
<div class="flex flex-wrap items-center justify-between gap-2 mt-4">
|
||||
<h1 class="text-3xl font-bold">
|
||||
Charge Service #{{ service?.id }}
|
||||
</h1>
|
||||
<router-link v-if="service" :to="{ name: 'customerProfile', params: { id: service.customer_id } }">
|
||||
<button class="btn btn-sm btn-secondary">Back to Customer</button>
|
||||
</router-link>
|
||||
</div>
|
||||
|
||||
<!-- Main Content Grid -->
|
||||
<div v-if="service" class="grid grid-cols-1 gap-6 mt-6 lg:grid-cols-2">
|
||||
<!-- LEFT COLUMN: Customer and Service Details -->
|
||||
<div class="space-y-6">
|
||||
<!-- Customer Info Card -->
|
||||
<div class="p-5 rounded-lg bg-neutral">
|
||||
<div class="mb-2 text-xl font-bold">Customer</div>
|
||||
<div>
|
||||
<div class="font-bold">{{ service.customer_name }}</div>
|
||||
<div>{{ service.customer_address }}</div>
|
||||
<div v-if="service.customer_apt && service.customer_apt !== 'None'">{{ service.customer_apt }}</div>
|
||||
<div>
|
||||
{{ service.customer_town }}, {{ stateName }} {{ service.customer_zip }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Service Details Card -->
|
||||
<div class="p-5 rounded-lg bg-neutral">
|
||||
<h3 class="mb-4 text-xl font-bold">Service Details</h3>
|
||||
<div class="grid grid-cols-1 gap-y-3 text-sm">
|
||||
<div>
|
||||
<div class="font-bold">Service Type</div>
|
||||
<div class="opacity-80">{{ serviceTypeName }}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-bold">Scheduled Date</div>
|
||||
<div class="opacity-80">{{ service.scheduled_date }}</div>
|
||||
</div>
|
||||
<div v-if="service.description">
|
||||
<div class="font-bold">Description</div>
|
||||
<div class="opacity-80">{{ service.description }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- RIGHT COLUMN: Cards and Payment Form -->
|
||||
<div class="space-y-6">
|
||||
<!-- Credit Cards Display -->
|
||||
<div class="p-5 rounded-lg bg-neutral">
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h3 class="text-xl font-bold">Credit Cards</h3>
|
||||
<router-link :to="{ name: 'cardadd', params: { id: service.customer_id } }">
|
||||
<button class="btn btn-xs btn-outline btn-success">Add New</button>
|
||||
</router-link>
|
||||
</div>
|
||||
<div v-if="userCards.length === 0" class="mt-2 text-sm opacity-70">
|
||||
<p class="font-semibold text-warning">No cards on file.</p>
|
||||
</div>
|
||||
<div class="mt-4 space-y-3">
|
||||
<div
|
||||
v-for="card in userCards"
|
||||
:key="card.id"
|
||||
class="p-2 border rounded-lg cursor-pointer transition-colors"
|
||||
:class="{
|
||||
'bg-blue-500 text-white border-blue-500': selectedCard?.id === card.id,
|
||||
'bg-primary/10 border-primary': selectedCard?.id !== card.id && card.main_card,
|
||||
'bg-base-200 border-base-300': selectedCard?.id !== card.id && !card.main_card,
|
||||
}"
|
||||
@click="selectCard(card)"
|
||||
>
|
||||
<div class="flex items-start justify-between">
|
||||
<div>
|
||||
<div class="text-sm font-bold">{{ card.name_on_card }}</div>
|
||||
<div class="text-xs opacity-70">{{ card.type_of_card }}</div>
|
||||
</div>
|
||||
<div v-if="card.main_card" class="badge badge-primary badge-sm">Primary</div>
|
||||
</div>
|
||||
<div class="mt-1 font-mono text-sm tracking-wider">
|
||||
<p>{{ card.card_number }}</p>
|
||||
<p>Exp: {{ formattedExpiration(card) }}</p>
|
||||
<p>{{ card.security_number }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Payment Form - always shown -->
|
||||
<div class="pt-4 mt-6 space-y-4 border-t border-base-300">
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="font-bold label-text">Charge Amount</span>
|
||||
</label>
|
||||
<input
|
||||
v-model="chargeAmount"
|
||||
class="w-full input input-bordered input-sm"
|
||||
type="number"
|
||||
step="0.01"
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-4">
|
||||
<button
|
||||
class="flex-1 btn btn-success"
|
||||
:disabled="isSubmitting || !chargeAmount || chargeAmount <= 0 || !selectedCard"
|
||||
@click="chargeService"
|
||||
>
|
||||
<span v-if="isSubmitting" class="loading loading-spinner loading-sm"></span>
|
||||
Charge Service
|
||||
</button>
|
||||
<button class="btn btn-ghost" :disabled="isSubmitting" @click="cancelCharge">
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Loading State -->
|
||||
<div v-else class="p-10 text-center">
|
||||
<span class="loading loading-spinner loading-lg"></span>
|
||||
<p class="mt-2">Loading service details...</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Footer />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, computed } from 'vue';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
import axios from 'axios';
|
||||
import authHeader from '../../../services/auth.header';
|
||||
import { notify } from "@kyvg/vue3-notification";
|
||||
import Footer from '../../../layouts/footers/footer.vue';
|
||||
|
||||
// --- Interfaces for Type Safety ---
|
||||
interface UserCard {
|
||||
id: number;
|
||||
name_on_card: string;
|
||||
type_of_card: string;
|
||||
main_card: boolean;
|
||||
card_number: string;
|
||||
expiration_month: number;
|
||||
expiration_year: number;
|
||||
security_number: string;
|
||||
}
|
||||
|
||||
interface Service {
|
||||
id: number;
|
||||
customer_id: number;
|
||||
customer_name: string;
|
||||
customer_address: string;
|
||||
customer_apt: string;
|
||||
customer_town: string;
|
||||
customer_state: number;
|
||||
customer_zip: string;
|
||||
type_service_call: number;
|
||||
scheduled_date: string;
|
||||
description: string;
|
||||
service_cost: number;
|
||||
}
|
||||
|
||||
// --- Component State ---
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
const isSubmitting = ref(false);
|
||||
const service = ref<Service | null>(null);
|
||||
const userCards = ref<UserCard[]>([]);
|
||||
const selectedCard = ref<UserCard | null>(null);
|
||||
const chargeAmount = ref<number>(0);
|
||||
const transaction = ref(null as any);
|
||||
const preAuthAmount = ref<number>(0);
|
||||
|
||||
// --- Computed Properties for Cleaner Template ---
|
||||
const stateName = computed(() => {
|
||||
const stateMap: Record<number, string> = {
|
||||
0: 'Massachusetts', 1: 'Rhode Island', 2: 'New Hampshire', 3: 'Maine',
|
||||
4: 'Vermont', 5: 'Maine', 6: 'New York',
|
||||
};
|
||||
return service.value ? stateMap[service.value.customer_state] || 'Unknown state' : '';
|
||||
});
|
||||
|
||||
const serviceTypeName = computed(() => {
|
||||
const typeMap: Record<number, string> = {
|
||||
0: 'Tune-up', 1: 'No Heat', 2: 'Fix', 3: 'Tank Install', 4: 'Other',
|
||||
};
|
||||
return service.value ? typeMap[service.value.type_service_call] || 'Unknown' : '';
|
||||
});
|
||||
|
||||
const formattedExpiration = (card: UserCard) => {
|
||||
const month = String(card.expiration_month).padStart(2, '0');
|
||||
return `${month} / ${card.expiration_year}`;
|
||||
};
|
||||
|
||||
|
||||
// --- Methods ---
|
||||
|
||||
/**
|
||||
* Toggles the selection of a credit card.
|
||||
* If the clicked card is already selected, it deselects it.
|
||||
* Otherwise, it selects the new card.
|
||||
*/
|
||||
const selectCard = (card: UserCard) => {
|
||||
if (selectedCard.value?.id === card.id) {
|
||||
selectedCard.value = null; // Deselect if clicked again
|
||||
} else {
|
||||
selectedCard.value = card; // Select the new card
|
||||
}
|
||||
};
|
||||
|
||||
const chargeService = async () => {
|
||||
if (!selectedCard.value || !chargeAmount.value || chargeAmount.value <= 0) {
|
||||
notify({ title: "Error", text: "Please select a card and enter a valid amount.", type: "error" });
|
||||
return;
|
||||
}
|
||||
|
||||
isSubmitting.value = true;
|
||||
try {
|
||||
const card = selectedCard.value;
|
||||
const expMonth = String(card.expiration_month).padStart(2, '0');
|
||||
const expYear = String(card.expiration_year).toString().slice(-2);
|
||||
|
||||
const payload = {
|
||||
charge_amount: chargeAmount.value,
|
||||
service_id: service.value!.id,
|
||||
delivery_id: null,
|
||||
transaction_type: 0,
|
||||
card_id: card.id,
|
||||
card_number: card.card_number,
|
||||
expiration_date: `${expMonth}${expYear}`,
|
||||
cvv: card.security_number,
|
||||
};
|
||||
console.log(payload)
|
||||
console.log(card)
|
||||
const response = await axios.post(
|
||||
`${import.meta.env.VITE_AUTHORIZE_URL}/api/charge/${service.value!.customer_id}`,
|
||||
payload,
|
||||
{ withCredentials: true, headers: authHeader() }
|
||||
);
|
||||
|
||||
if (response.data?.status === 0) {
|
||||
// Payment approved: now update the service cost in the database
|
||||
await axios.put(
|
||||
`${import.meta.env.VITE_BASE_URL}/service/update/${service.value!.id}`,
|
||||
{
|
||||
service_cost: chargeAmount.value
|
||||
},
|
||||
{ withCredentials: true, headers: authHeader() }
|
||||
);
|
||||
|
||||
notify({ title: "Success", text: "Service charged successfully!", type: "success" });
|
||||
router.push({ name: 'customerProfile', params: { id: service.value!.customer_id } });
|
||||
} else {
|
||||
const reason = response.data?.rejection_reason || "The charge was declined.";
|
||||
notify({ title: "Charge Declined", text: reason, type: "error" });
|
||||
}
|
||||
} catch (error: any) {
|
||||
const detail = error.response?.data?.detail || "Failed to charge service due to a server error.";
|
||||
notify({ title: "Error", text: detail, type: "error" });
|
||||
console.error("Charge Service Error:", error);
|
||||
} finally {
|
||||
isSubmitting.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const cancelCharge = () => {
|
||||
if (service.value) {
|
||||
router.push({ name: 'customerProfile', params: { id: service.value.customer_id } });
|
||||
}
|
||||
};
|
||||
|
||||
const getTransaction = () => {
|
||||
const serviceId = route.params.id;
|
||||
const path = `${import.meta.env.VITE_AUTHORIZE_URL}/api/transaction/service/${serviceId}`;
|
||||
axios.get(path, { withCredentials: true, headers: authHeader() })
|
||||
.then((response: any) => {
|
||||
transaction.value = response.data;
|
||||
preAuthAmount.value = parseFloat(response.data.preauthorize_amount || service.value?.service_cost || 0);
|
||||
if (response.data.status !== 0) { // Not approved
|
||||
preAuthAmount.value = 0;
|
||||
}
|
||||
})
|
||||
.catch((error: any) => {
|
||||
console.error("No pre-authorized transaction found for service:", error);
|
||||
preAuthAmount.value = service.value?.service_cost || 0; // fallback to service cost
|
||||
});
|
||||
};
|
||||
|
||||
// --- Lifecycle Hook ---
|
||||
onMounted(async () => {
|
||||
const serviceId = route.params.id;
|
||||
if (!serviceId) {
|
||||
notify({ title: "Error", text: "No service ID provided.", type: "error" });
|
||||
router.push({ name: 'customer' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Fetch Service Details
|
||||
const servicePath = `${import.meta.env.VITE_BASE_URL}/service/${serviceId}`;
|
||||
const serviceResponse = await axios.get(servicePath, { withCredentials: true, headers: authHeader() });
|
||||
|
||||
if (serviceResponse.data?.ok) {
|
||||
service.value = serviceResponse.data.service;
|
||||
chargeAmount.value = service.value?.service_cost || 0;
|
||||
|
||||
// Fetch Customer Cards
|
||||
const cardsPath = `${import.meta.env.VITE_BASE_URL}/payment/cards/${service.value!.customer_id}`;
|
||||
const cardsResponse = await axios.get(cardsPath, { withCredentials: true, headers: authHeader() });
|
||||
userCards.value = cardsResponse.data;
|
||||
|
||||
// Fetch pre-auth transaction
|
||||
getTransaction();
|
||||
} else {
|
||||
throw new Error(serviceResponse.data?.error || "Failed to fetch service data.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error loading page data:", error);
|
||||
notify({ title: "Error", text: "Service not found or could not be loaded.", type: "error" });
|
||||
router.push({ name: 'customer' });
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user