major claude changes

This commit is contained in:
2026-01-28 21:55:14 -05:00
parent f9d0e4c0fd
commit f9b5364c53
81 changed files with 11155 additions and 10086 deletions

View File

@@ -202,463 +202,468 @@
</div>
</template>
<script lang="ts">
import { defineComponent, watch } from 'vue'
import axios from 'axios'
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios, { AxiosResponse, AxiosError } from 'axios'
import authHeader from '../../../services/auth.header'
import { notify } from "@kyvg/vue3-notification"
import type {
CustomerFormData,
CreditCardFormData,
PricingData,
OilPricingResponse,
WhoAmIResponse,
AutoDeliveryData
} from '../../../types/models'
export default defineComponent({
name: 'AuthorizePrechargeAutho',
const route = useRoute()
const router = useRouter()
data() {
return {
deliveryId: this.$route.params.id as string,
loaded: false,
chargeAmount: 0,
quickGallonAmounts: [100, 125, 150, 175, 200, 220],
loading: false,
action: '', // 'preauthorize' or 'charge'
error: '',
success: '',
isChargeConfirmationModalVisible: false,
transactionId: 0,
user: {
user_id: 0,
},
autoDelivery: {
id: 0,
customer_id: 0,
customer_full_name: '',
customer_address: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
tank_size: 0,
estimated_gallons_left: 0,
house_factor: 0,
auto_status: 0,
},
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: "",
},
currentOilPrice: 0,
pricingTiers: [] as { gallons: number; price: number }[],
}
},
computed: {
selectedCard(): any {
return this.credit_cards.find((card: any) => card.main_card) || this.credit_cards[0]
},
customerStateName(): string {
const states: Record<number, string> = { 0: 'Massachusetts', 1: 'Rhode Island', 2: 'New Hampshire', 3: 'Maine', 4: 'Vermont', 5: 'Connecticut', 6: 'New York' };
return states[this.customer.customer_state] || 'Unknown state';
},
selectedGallonAmount(): number | null {
return this.quickGallonAmounts.find(gal => this.calculatePriceForGallons(gal).toFixed(2) === this.chargeAmount.toFixed(2)) || null;
}
},
mounted() {
this.loadData(this.deliveryId)
this.getPricingTiers()
},
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
},
loadData(deliveryId: string) {
this.userStatus()
this.getAutoDelivery(deliveryId)
this.getOilPricing()
this.getCurrentOilPrice()
},
getCurrentOilPrice() {
let path = import.meta.env.VITE_BASE_URL + '/info/price/oil'
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: any) => {
this.currentOilPrice = response.data.price_for_customer;
this.calculateDefaultChargeAmount()
})
.catch(() => {
notify({
title: "Error",
text: "Could not get oil pricing",
type: "error",
});
});
},
calculateDefaultChargeAmount() {
this.chargeAmount = this.calculateTotalAsNumber()
},
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;
this.calculateDefaultChargeAmount()
})
.catch(() => {
notify({
title: "Error",
text: "Could not get oil pricing",
type: "error",
});
});
},
getAutoDelivery(delivery_id: any) {
let path = import.meta.env.VITE_AUTO_URL + "/delivery/delivery/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: any) => {
if (response.data && response.data.customer_id) {
this.autoDelivery = response.data;
this.getCustomer(this.autoDelivery.customer_id)
this.getCreditCards(this.autoDelivery.customer_id)
} else {
console.error("API Error:", response.data.error || "Failed to fetch auto delivery data.");
}
})
.catch((error: any) => {
console.error("API Error in getAutoDelivery:", error);
notify({
title: "Error",
text: "Could not get automatic 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
})
},
getPricingTiers() {
let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/tiers";
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader()
})
.then((response: any) => {
this.pricingTiers = Object.entries(response.data).map(([gallons, price]) => ({ gallons: parseInt(gallons, 10), price: Number(price) }));
})
.catch(() => {
notify({
title: "Pricing Error",
text: "Could not retrieve today's pricing.",
type: "error"
});
});
},
isPricingTierSelected(tierGallons: number | string): boolean {
const calculated = this.calculateGallonsToFill()
return calculated == Number(tierGallons)
},
calculatePriceForGallons(gallons: number): number {
let priceForGallons = 0;
const sortedTiers = [...this.pricingTiers].sort((a, b) => Number(a.gallons) - Number(b.gallons));
// Find the highest tier that is less than or equal to the gallons ordered
let applicableTier = sortedTiers.filter(t => gallons >= Number(t.gallons)).pop();
if (applicableTier) {
const pricePerGallon = Number(applicableTier.price) / Number(applicableTier.gallons);
priceForGallons = gallons * pricePerGallon;
} else if (sortedTiers.length > 0) {
// Fallback to the lowest tier's price/gallon if no tier is met (e.g., ordering 50 gallons when lowest tier is 100)
const lowestTier = sortedTiers[0];
const pricePerGallon = Number(lowestTier.price) / Number(lowestTier.gallons);
priceForGallons = gallons * pricePerGallon;
}
return priceForGallons;
},
setGallons(gallons: number) {
this.chargeAmount = this.calculatePriceForGallons(gallons);
},
calculateGallonsToFill() {
return this.autoDelivery.tank_size - this.autoDelivery.estimated_gallons_left
},
calculateSubtotal() {
const gallons = this.calculateGallonsToFill()
const pricePerGallon = this.pricing.price_for_customer || this.currentOilPrice
return (gallons * pricePerGallon).toFixed(2)
},
calculateTotalAmount() {
const subtotal = parseFloat(this.calculateSubtotal())
let total = subtotal
// No additional fees for auto preauthorization
return total.toFixed(2)
},
calculateTotalAsNumber() {
return parseFloat(this.calculateTotalAmount())
},
async handlePreauthorize() {
await this.processPayment('preauthorize')
},
async handleChargeNow() {
this.loading = true
this.isChargeConfirmationModalVisible = true
},
async proceedWithCharge() {
this.isChargeConfirmationModalVisible = false
await this.processPayment('charge')
},
cancelCharge() {
this.isChargeConfirmationModalVisible = false
},
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 {
// Step 2: If payment method is credit, perform the pre-authorization
if (actionType === 'preauthorize') {
if (!this.chargeAmount || this.chargeAmount <= 0) {
throw new Error("Pre-authorization amount must be greater than zero.");
}
const authPayload = {
card_id: (this.selectedCard as any).id,
preauthorize_amount: this.chargeAmount.toFixed(2),
delivery_id: null,
auto_id: this.deliveryId,
};
const authPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/authorize/saved-card/${this.customer.id}`;
const response = await axios.post(authPath, authPayload, { withCredentials: true, headers: authHeader() });
if (!response.data.auth_net_transaction_id || response.data.auth_net_transaction_id.trim() === '') {
throw new Error("Failed transaction: No Authorize.net transaction ID received");
}
this.transactionId = response.data.id;
// Update auto_delivery status to 3
await axios.put(`${import.meta.env.VITE_AUTO_URL}/delivery/update_status/${this.deliveryId}`, {}, { withCredentials: true, headers: authHeader() });
// Create Tickets_Auto_Delivery after successful preauthorize
const ticketPayload = {
gallons_delivered: 0,
payment_type: 11, // 11 for preauthorize, 1 for charge
payment_card_id: this.selectedCard.id,
payment_status: 1 // Pre-authorized status (ready for capture)
};
const ticketUrl = `${import.meta.env.VITE_AUTO_URL}/confirm/auto/create/${this.deliveryId}`;
const ticketResponse = await axios.post(ticketUrl, ticketPayload, { withCredentials: true, headers: authHeader() });
console.log('Ticket response data:', ticketResponse.data, 'type:', typeof ticketResponse.data, 'keys:', ticketResponse.data ? Object.keys(ticketResponse.data) : 'no data');
// Update transaction auto_id to ticket ID
if (this.transactionId && ticketResponse.data) {
const data = Array.isArray(ticketResponse.data) ? ticketResponse.data[0] : ticketResponse.data;
if (data && data.auto_ticket_id !== undefined) {
await axios.put(`${import.meta.env.VITE_AUTHORIZE_URL}/api/transaction/${this.transactionId}/update_auto_id/${data.auto_ticket_id}`, {}, { withCredentials: true, headers: authHeader() });
} else {
console.error('auto_ticket_id is undefined in ticket response');
}
}
// On successful authorization, show success and redirect
this.success = `Preauthorization successful! Transaction ID: ${response.data.auth_net_transaction_id}`;
setTimeout(() => {
this.$router.push({ name: "auto" });
}, 2000);
}
else { // Handle 'charge' action
if (!this.chargeAmount || this.chargeAmount <= 0) {
throw new Error("Charge amount must be greater than zero.");
}
const chargePayload = {
card_id: (this.selectedCard as any).id,
charge_amount: this.chargeAmount.toFixed(2),
delivery_id: this.deliveryId,
};
const chargePath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/charge/saved-card/${this.customer.id}`;
console.log('=== DEBUG: Charge payload ===');
console.log('Calling endpoint:', chargePath);
console.log('Final payload being sent:', chargePayload);
const response = await axios.post(chargePath, chargePayload, { withCredentials: true, headers: authHeader() });
// Update auto_delivery status to 3
await axios.put(`${import.meta.env.VITE_AUTO_URL}/delivery/update_status/${this.deliveryId}`, {}, { withCredentials: true, headers: authHeader() });
// Status codes: 0 = APPROVED, 1 = DECLINED (based on backend TransactionStatus enum)
if (response.data && response.data.status === 0) { // 0 = APPROVED
if (!response.data.auth_net_transaction_id || response.data.auth_net_transaction_id.trim() === '') {
throw new Error("Failed transaction: No Authorize.net transaction ID received");
}
this.transactionId = response.data.id;
this.success = `Charge successful! Transaction ID: ${response.data.auth_net_transaction_id}`;
// Create Tickets_Auto_Delivery after successful charge
const ticketPayload = {
gallons_delivered: 0,
payment_type: 11, // 11 for Authorize charge
payment_card_id: this.selectedCard.id,
payment_status: response.data.status // 0 = APPROVED
};
const ticketUrl = `${import.meta.env.VITE_AUTO_URL}/confirm/auto/create/${this.deliveryId}`;
console.log("POOOPP!")
await axios.post(ticketUrl, ticketPayload, { withCredentials: true, headers: authHeader() });
setTimeout(() => {
this.$router.push({ name: "auto" });
}, 2000);
} else {
// The error message from your backend will be more specific now
throw new Error(`Payment charge failed: ${response.data?.rejection_reason || 'Unknown error'}`);
}
}
} catch (error: any) {
console.log(error)
this.error = error.response?.data?.detail || `Failed to ${actionType} payment`
notify({
title: "Error",
text: this.error,
type: "error",
})
} finally {
this.loading = false
this.action = ''
}
}
},
// Reactive data
const deliveryId = ref(route.params.id as string)
const loaded = ref(false)
const chargeAmount = ref(0)
const quickGallonAmounts = ref([100, 125, 150, 175, 200, 220])
const loading = ref(false)
const action = ref('') // 'preauthorize' or 'charge'
const error = ref('')
const success = ref('')
const isChargeConfirmationModalVisible = ref(false)
const transactionId = ref(0)
const user = ref({
user_id: 0,
})
const autoDelivery = ref<AutoDeliveryData>({
id: 0,
customer_id: 0,
customer_full_name: '',
customer_address: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
tank_size: 0,
estimated_gallons_left: 0,
estimated_gallons_left_prev_day: 0,
house_factor: 0,
auto_status: 0,
account_number: '',
last_fill: '',
days_since_last_fill: 0,
last_updated: '',
tank_height: '',
open_ticket_id: null,
})
const credit_cards = ref<CreditCardFormData[]>([
{
id: 0,
name_on_card: '',
main_card: false,
card_number: '',
expiration_month: '',
type_of_card: '',
last_four_digits: '',
expiration_year: '',
security_number: '',
}
])
const customer = ref<CustomerFormData>({
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: '',
})
const pricing = ref<PricingData>({
price_from_supplier: 0,
price_for_customer: 0,
price_for_employee: 0,
price_same_day: 0,
price_prime: 0,
price_emergency: 0,
date: "",
})
const currentOilPrice = ref(0)
const pricingTiers = ref([] as { gallons: number; price: number }[])
// Computed
const selectedCard = computed(() => {
return credit_cards.value.find((card) => card.main_card) || credit_cards.value[0]
})
const customerStateName = computed(() => {
const states: Record<number, string> = { 0: 'Massachusetts', 1: 'Rhode Island', 2: 'New Hampshire', 3: 'Maine', 4: 'Vermont', 5: 'Connecticut', 6: 'New York' };
return states[customer.value.customer_state] || 'Unknown state';
})
const selectedGallonAmount = computed(() => {
return quickGallonAmounts.value.find(gal => calculatePriceForGallons(gal).toFixed(2) === chargeAmount.value.toFixed(2)) || null;
})
// Watchers
watch(() => route.params.id, (newId) => {
if (newId !== deliveryId.value) {
resetState()
deliveryId.value = newId as string
loadData(newId as string)
}
})
// Lifecycle
onMounted(() => {
loadData(deliveryId.value)
getPricingTiers()
})
// Functions
const resetState = () => {
loading.value = false
action.value = ''
error.value = ''
success.value = ''
chargeAmount.value = 0
}
const loadData = (deliveryId: string) => {
userStatus()
getAutoDelivery(deliveryId)
getOilPricing()
getCurrentOilPrice()
}
const getCurrentOilPrice = () => {
let path = import.meta.env.VITE_BASE_URL + '/info/price/oil'
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: AxiosResponse<OilPricingResponse>) => {
currentOilPrice.value = response.data.price_for_customer;
calculateDefaultChargeAmount()
})
.catch(() => {
notify({
title: "Error",
text: "Could not get oil pricing",
type: "error",
});
});
}
const calculateDefaultChargeAmount = () => {
chargeAmount.value = calculateTotalAsNumber()
}
const userStatus = () => {
let path = import.meta.env.VITE_BASE_URL + '/auth/whoami';
axios({
method: 'get',
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: AxiosResponse<WhoAmIResponse>) => {
if (response.data.ok) {
user.value = response.data.user;
}
})
}
const getOilPricing = () => {
let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/table";
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<OilPricingResponse>) => {
pricing.value = response.data;
calculateDefaultChargeAmount()
})
.catch(() => {
notify({
title: "Error",
text: "Could not get oil pricing",
type: "error",
});
});
}
const getAutoDelivery = (delivery_id: number | string) => {
let path = import.meta.env.VITE_AUTO_URL + "/delivery/delivery/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<AutoDeliveryData>) => {
if (response.data && response.data.customer_id) {
autoDelivery.value = response.data;
getCustomer(autoDelivery.value.customer_id)
getCreditCards(autoDelivery.value.customer_id)
} else {
console.error("API Error: Failed to fetch auto delivery data.");
}
})
.catch((err: Error) => {
console.error("API Error in getAutoDelivery:", err);
notify({
title: "Error",
text: "Could not get automatic delivery",
type: "error",
});
});
}
const getCreditCards = (user_id: number) => {
let path = import.meta.env.VITE_BASE_URL + '/payment/cards/' + user_id;
axios({
method: 'get',
url: path,
headers: authHeader(),
}).then((response: AxiosResponse<CreditCardFormData[]>) => {
credit_cards.value = response.data
})
}
const getCustomer = (userid: number) => {
let path = import.meta.env.VITE_BASE_URL + '/customer/' + userid;
axios({
method: 'get',
url: path,
headers: authHeader(),
}).then((response: AxiosResponse<CustomerFormData>) => {
customer.value = response.data
})
}
const getPricingTiers = () => {
let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/tiers";
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader()
})
.then((response: AxiosResponse<Record<string, number>>) => {
pricingTiers.value = Object.entries(response.data).map(([gallons, price]) => ({ gallons: parseInt(gallons, 10), price: Number(price) }));
})
.catch(() => {
notify({
title: "Pricing Error",
text: "Could not retrieve today's pricing.",
type: "error"
});
});
}
const isPricingTierSelected = (tierGallons: number | string): boolean => {
const calculated = calculateGallonsToFill()
return calculated == Number(tierGallons)
}
const calculatePriceForGallons = (gallons: number): number => {
let priceForGallons = 0;
const sortedTiers = [...pricingTiers.value].sort((a, b) => Number(a.gallons) - Number(b.gallons));
// Find the highest tier that is less than or equal to the gallons ordered
let applicableTier = sortedTiers.filter(t => gallons >= Number(t.gallons)).pop();
if (applicableTier) {
const pricePerGallon = Number(applicableTier.price) / Number(applicableTier.gallons);
priceForGallons = gallons * pricePerGallon;
} else if (sortedTiers.length > 0) {
// Fallback to the lowest tier's price/gallon if no tier is met (e.g., ordering 50 gallons when lowest tier is 100)
const lowestTier = sortedTiers[0];
const pricePerGallon = Number(lowestTier.price) / Number(lowestTier.gallons);
priceForGallons = gallons * pricePerGallon;
}
return priceForGallons;
}
const setGallons = (gallons: number) => {
chargeAmount.value = calculatePriceForGallons(gallons);
}
const calculateGallonsToFill = () => {
return autoDelivery.value.tank_size - autoDelivery.value.estimated_gallons_left
}
const calculateSubtotal = () => {
const gallons = calculateGallonsToFill()
const pricePerGallon = pricing.value.price_for_customer || currentOilPrice.value
return (gallons * pricePerGallon).toFixed(2)
}
const calculateTotalAmount = () => {
const subtotal = parseFloat(calculateSubtotal())
let total = subtotal
// No additional fees for auto preauthorization
return total.toFixed(2)
}
const calculateTotalAsNumber = () => {
return parseFloat(calculateTotalAmount())
}
const handlePreauthorize = async () => {
await processPayment('preauthorize')
}
const handleChargeNow = async () => {
loading.value = true
isChargeConfirmationModalVisible.value = true
}
const proceedWithCharge = async () => {
isChargeConfirmationModalVisible.value = false
await processPayment('charge')
}
const cancelCharge = () => {
isChargeConfirmationModalVisible.value = false
}
const processPayment = async (actionType: string) => {
if (!selectedCard.value) {
error.value = 'No credit card found for this customer'
return
}
loading.value = true
action.value = actionType
error.value = ''
success.value = ''
try {
// Step 2: If payment method is credit, perform the pre-authorization
if (actionType === 'preauthorize') {
if (!chargeAmount.value || chargeAmount.value <= 0) {
throw new Error("Pre-authorization amount must be greater than zero.");
}
const authPayload = {
card_id: selectedCard.value!.id,
preauthorize_amount: chargeAmount.value.toFixed(2),
delivery_id: null,
auto_id: deliveryId.value,
};
const authPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/authorize/saved-card/${customer.value.id}`;
const response = await axios.post(authPath, authPayload, { withCredentials: true, headers: authHeader() });
if (!response.data.auth_net_transaction_id || response.data.auth_net_transaction_id.trim() === '') {
throw new Error("Failed transaction: No Authorize.net transaction ID received");
}
transactionId.value = response.data.id;
// Update auto_delivery status to 3
await axios.put(`${import.meta.env.VITE_AUTO_URL}/delivery/update_status/${deliveryId.value}`, {}, { withCredentials: true, headers: authHeader() });
// Create Tickets_Auto_Delivery after successful preauthorize
const ticketPayload = {
gallons_delivered: 0,
payment_type: 11, // 11 for preauthorize, 1 for charge
payment_card_id: selectedCard.value!.id,
payment_status: 1 // Pre-authorized status (ready for capture)
};
const ticketUrl = `${import.meta.env.VITE_AUTO_URL}/confirm/auto/create/${deliveryId.value}`;
const ticketResponse = await axios.post(ticketUrl, ticketPayload, { withCredentials: true, headers: authHeader() });
console.log('Ticket response data:', ticketResponse.data, 'type:', typeof ticketResponse.data, 'keys:', ticketResponse.data ? Object.keys(ticketResponse.data) : 'no data');
// Update transaction auto_id to ticket ID
if (transactionId.value && ticketResponse.data) {
const data = Array.isArray(ticketResponse.data) ? ticketResponse.data[0] : ticketResponse.data;
if (data && data.auto_ticket_id !== undefined) {
await axios.put(`${import.meta.env.VITE_AUTHORIZE_URL}/api/transaction/${transactionId.value}/update_auto_id/${data.auto_ticket_id}`, {}, { withCredentials: true, headers: authHeader() });
} else {
console.error('auto_ticket_id is undefined in ticket response');
}
}
// On successful authorization, show success and redirect
success.value = `Preauthorization successful! Transaction ID: ${response.data.auth_net_transaction_id}`;
setTimeout(() => {
router.push({ name: "auto" });
}, 2000);
}
else { // Handle 'charge' action
if (!chargeAmount.value || chargeAmount.value <= 0) {
throw new Error("Charge amount must be greater than zero.");
}
const chargePayload = {
card_id: selectedCard.value!.id,
charge_amount: chargeAmount.value.toFixed(2),
delivery_id: deliveryId.value,
};
const chargePath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/charge/saved-card/${customer.value.id}`;
console.log('=== DEBUG: Charge payload ===');
console.log('Calling endpoint:', chargePath);
console.log('Final payload being sent:', chargePayload);
const response = await axios.post(chargePath, chargePayload, { withCredentials: true, headers: authHeader() });
// Update auto_delivery status to 3
await axios.put(`${import.meta.env.VITE_AUTO_URL}/delivery/update_status/${deliveryId.value}`, {}, { withCredentials: true, headers: authHeader() });
// Status codes: 0 = APPROVED, 1 = DECLINED (based on backend TransactionStatus enum)
if (response.data && response.data.status === 0) { // 0 = APPROVED
if (!response.data.auth_net_transaction_id || response.data.auth_net_transaction_id.trim() === '') {
throw new Error("Failed transaction: No Authorize.net transaction ID received");
}
transactionId.value = response.data.id;
success.value = `Charge successful! Transaction ID: ${response.data.auth_net_transaction_id}`;
// Create Tickets_Auto_Delivery after successful charge
const ticketPayload = {
gallons_delivered: 0,
payment_type: 11, // 11 for Authorize charge
payment_card_id: selectedCard.value!.id,
payment_status: response.data.status // 0 = APPROVED
};
const ticketUrl = `${import.meta.env.VITE_AUTO_URL}/confirm/auto/create/${deliveryId.value}`;
console.log("POOOPP!")
await axios.post(ticketUrl, ticketPayload, { withCredentials: true, headers: authHeader() });
setTimeout(() => {
router.push({ name: "auto" });
}, 2000);
} else {
// The error message from your backend will be more specific now
throw new Error(`Payment charge failed: ${response.data?.rejection_reason || 'Unknown error'}`);
}
}
} catch (err: unknown) {
const axiosErr = err as AxiosError<{ detail?: string }>;
console.log(err)
error.value = axiosErr.response?.data?.detail || `Failed to ${actionType} payment`
notify({
title: "Error",
text: error.value,
type: "error",
})
} finally {
loading.value = false
action.value = ''
}
}
</script>
<style scoped></style>

View File

@@ -237,341 +237,349 @@
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import axios from 'axios'
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios, { AxiosResponse, AxiosError } from 'axios'
import authHeader from '../../../services/auth.header'
import { notify } from "@kyvg/vue3-notification"
import type {
CustomerFormData,
CreditCardFormData,
PricingData,
CustomerDescriptionData,
AutoTicketData,
AutoDeliveryData,
PaymentCardResponse,
AuthorizeNetTransactionResponse
} from '../../../types/models'
export default defineComponent({
name: 'captureAuthorizeAuto',
const route = useRoute()
const router = useRouter()
data() {
return {
loading: false,
userCardfound: false,
gallonsDelivered: '',
captureAmount: 0,
preAuthAmount: 0,
transaction: null as any,
showPaymentModal: false,
modalStep: 0,
modalCapturedAmount: 0,
// Reactive data
const loading = ref(false)
const userCardfound = ref(false)
const gallonsDelivered = ref('')
const captureAmount = ref(0)
const preAuthAmount = ref(0)
const transaction = ref<AuthorizeNetTransactionResponse | null>(null)
const showPaymentModal = ref(false)
const modalStep = ref(0)
const modalCapturedAmount = ref(0)
userCard: {
date_added: '',
user_id: '',
card_number: '',
last_four_digits: '',
name_on_card: '',
expiration_month: '',
expiration_year: '',
type_of_card: '',
security_number: '',
accepted_or_declined: '',
main_card: '',
},
customerDescription: {
customer_id: 0,
account_number: '',
company_id: 0,
fill_location: 0,
description: '',
},
customer: {
id: 0,
user_id: 0,
customer_address: '',
customer_first_name: '',
customer_last_name: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
customer_apt: '',
customer_home_type: 0,
customer_phone_number: '',
},
autoDelivery: {
id: 0,
customer_id: 0,
account_number: '',
customer_town: '',
customer_state: 0,
customer_address: '',
customer_zip: '',
customer_full_name: '',
last_fill: '',
days_since_last_fill: 0,
last_updated: '',
estimated_gallons_left: 0,
estimated_gallons_left_prev_day: 0,
tank_height: '',
tank_size: '',
house_factor: 0,
auto_status: 0,
open_ticket_id: null,
},
autoTicket: {
id: 0,
customer_id: '',
account_number: '',
customer_town : '',
customer_state : '',
customer_address : '',
customer_zip: '',
customer_full_name : '',
oil_prices_id : '',
fill_date : '',
gallons_delivered : '',
price_per_gallon : '',
total_amount_customer : '',
payment_type : '',
payment_card_id : '',
payment_status : 0,
open_ticket_id: 0
},
pricing: {
price_from_supplier: 0,
price_for_customer: 0,
price_for_employee: 0,
price_same_day: 0,
price_prime: 0,
price_emergency: 0,
date: "",
},
total_amount: 0,
discount: 0,
total_amount_after_discount: 0,
}
},
mounted() {
this.getAutoTicket(this.$route.params.id)
this.getTransaction()
},
methods: {
getAutoTicket(delivery_id: any) {
let path = import.meta.env.VITE_AUTO_URL + "/delivery/autoticket/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: any) => {
this.autoTicket = response.data;
console.log(this.autoTicket)
this.gallonsDelivered = this.autoTicket.gallons_delivered;
this.captureAmount = parseFloat(this.autoTicket.total_amount_customer || '0');
this.getCustomer(this.autoTicket.customer_id)
this.getAutoDelivery(this.autoTicket.id)
this.getCustomerDescription(this.autoTicket.customer_id)
if (this.autoTicket.payment_card_id) {
this.getPaymentCard(this.autoTicket.payment_card_id)
}
})
.catch(() => {
notify({
title: "Error",
text: "Could not get automatic",
type: "error",
});
});
},
getAutoDelivery(delivery_id: any) {
let path = import.meta.env.VITE_AUTO_URL + "/delivery/finddelivery/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: any) => {
this.autoDelivery = response.data;
this.getCustomer(this.autoDelivery.customer_id)
this.getCustomerDescription(this.autoDelivery.customer_id)
})
.catch(() => {
notify({
title: "Error",
text: "Could not get automatic",
type: "error",
});
});
},
getPaymentCard(card_id: any) {
let path = import.meta.env.VITE_BASE_URL + "/payment/card/" + card_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: any) => {
if (response.data.userCard.card_number === ''){
this.userCardfound = false;
}
else{
this.userCard = response.data;
this.userCardfound = true;
}
})
.catch(() => {
});
},
getCustomer(user_id: any) {
const path = `${import.meta.env.VITE_BASE_URL}/customer/${user_id}`;
axios.get(path, { withCredentials: true })
.then((response: any) => {
this.customer = response.data;
})
.catch((error: any) => {
notify({ title: "Error", text: "Could not find customer", type: "error" });
console.error("Error fetching customer:", error);
});
},
getCustomerDescription(user_id: any) {
let path = import.meta.env.VITE_BASE_URL + "/customer/description/" + user_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: any) => {
this.customerDescription = response.data;
this.loading = false
})
.catch(() => {
notify({
title: "Error",
text: "Could not find customer",
type: "error",
});
});
},
getTransaction() {
const path = `${import.meta.env.VITE_AUTHORIZE_URL}/api/auto/transaction/delivery/${this.$route.params.id}`;
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) => {
if (error.response && error.response.status === 404) {
notify({ title: "Info", text: "No pre-authorization found. Redirecting to customer profile to update payment method.", type: "info" });
console.log("No transaction found for Automatic - redirecting to customer profile");
this.$router.push({ name: 'customerProfile', params: { id: this.customer.id } });
} else {
notify({ title: "Error", text: "No pre-authorized transaction found", type: "error" });
this.$router.push({ name: 'finalizeTicketAuto', params: { id: this.$route.params.id } });
}
});
},
async capturePayment() {
if (this.autoTicket.payment_status !== 1) {
notify({ title: "Error", text: "Payment already captured or not ready for capture", type: "error" });
return;
}
if (!this.transaction || !this.captureAmount) {
notify({ title: "Error", text: "Invalid capture amount or transaction data", type: "error" });
return;
}
this.loading = true;
try {
const payload = {
charge_amount: this.captureAmount,
auth_net_transaction_id: this.transaction.auth_net_transaction_id
};
const url = `${import.meta.env.VITE_AUTHORIZE_URL}/api/capture/`;
const response = await axios.post(
url,
payload,
{ withCredentials: true, headers: authHeader() }
);
if (response.data && response.data.status === 0) {
this.autoTicket.payment_status = 3; // Update local status immediately
this.modalCapturedAmount = this.captureAmount;
this.showPaymentModal = true;
// Close the ticket and unassign from delivery
this.closeTicket(this.autoTicket.id);
setTimeout(() => { this.modalStep = 1 }, 2000);
setTimeout(() => { this.showPaymentModal = false; this.$router.push({ name: 'auto' }) }, 4000);
} else if (response.data && response.data.status === 1) {
const reason = response.data.rejection_reason || "The payment was declined by the gateway.";
notify({
title: "Payment Declined",
text: reason,
type: "warn",
});
} else {
throw new Error("Invalid response from server during capture.");
}
} catch (error: any) {
const detail = error.response?.data?.detail || "Failed to capture payment due to a server error.";
notify({
title: "Error",
text: detail,
type: "error",
});
console.error("Capture Payment Error:", error);
} finally {
this.loading = false;
}
},
cancelCapture() {
this.$router.push({ name: 'finalizeTicketAuto', params: { id: this.$route.params.id } });
},
async closeTicket(ticket_id: number) {
const path = `${import.meta.env.VITE_AUTO_URL}/confirm/auto/close_ticket/${ticket_id}`;
axios.put(path, {}, { withCredentials: true })
.then(() => {
console.log("Ticket closed successfully");
})
.catch((error: any) => {
notify({
title: "Warning",
text: "Payment captured, but failed to close ticket. Check manually.",
type: "warn",
});
console.error("Error closing ticket:", error);
});
},
getTypeColor(transactionType: number) {
switch (transactionType) {
case 1: return 'text-blue-600'; // Auth
case 0: return 'text-orange-600'; // Charge
case 2: return 'text-purple-600'; // Capture
case 3: return 'text-green-600'; // Delivery/Other
default: return 'text-gray-600';
}
},
},
const userCard = ref<CreditCardFormData>({
id: 0,
date_added: '',
user_id: '',
card_number: '',
last_four_digits: '',
name_on_card: '',
expiration_month: '',
expiration_year: '',
type_of_card: '',
security_number: '',
accepted_or_declined: '',
main_card: false,
})
const customerDescription = ref<CustomerDescriptionData>({
customer_id: 0,
account_number: '',
company_id: 0,
fill_location: 0,
description: '',
})
const customer = ref<CustomerFormData>({
id: 0,
user_id: 0,
customer_address: '',
customer_first_name: '',
customer_last_name: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
customer_apt: '',
customer_home_type: 0,
customer_phone_number: '',
account_number: '',
})
const autoDelivery = ref<AutoDeliveryData>({
id: 0,
customer_id: 0,
account_number: '',
customer_town: '',
customer_state: 0,
customer_address: '',
customer_zip: '',
customer_full_name: '',
last_fill: '',
days_since_last_fill: 0,
last_updated: '',
estimated_gallons_left: 0,
estimated_gallons_left_prev_day: 0,
tank_height: '',
tank_size: '',
house_factor: 0,
auto_status: 0,
open_ticket_id: null,
})
const autoTicket = ref<AutoTicketData>({
id: 0,
customer_id: '',
account_number: '',
customer_town: '',
customer_state: '',
customer_address: '',
customer_zip: '',
customer_full_name: '',
oil_prices_id: '',
fill_date: '',
gallons_delivered: '',
price_per_gallon: '',
total_amount_customer: '',
payment_type: '',
payment_card_id: '',
payment_status: 0,
open_ticket_id: 0
})
const pricing = ref<PricingData>({
price_from_supplier: 0,
price_for_customer: 0,
price_for_employee: 0,
price_same_day: 0,
price_prime: 0,
price_emergency: 0,
date: "",
})
const total_amount = ref(0)
const discount = ref(0)
const total_amount_after_discount = ref(0)
// Lifecycle
onMounted(() => {
getAutoTicket(route.params.id)
getTransaction()
})
// Functions
const getAutoTicket = (delivery_id: number | string) => {
let path = import.meta.env.VITE_AUTO_URL + "/delivery/autoticket/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<AutoTicketData>) => {
autoTicket.value = response.data;
console.log(autoTicket.value)
gallonsDelivered.value = autoTicket.value.gallons_delivered;
captureAmount.value = parseFloat(autoTicket.value.total_amount_customer || '0');
getCustomer(autoTicket.value.customer_id)
getAutoDelivery(autoTicket.value.id)
getCustomerDescription(autoTicket.value.customer_id)
if (autoTicket.value.payment_card_id) {
getPaymentCard(autoTicket.value.payment_card_id)
}
})
.catch(() => {
notify({
title: "Error",
text: "Could not get automatic",
type: "error",
});
});
}
const getAutoDelivery = (delivery_id: number) => {
let path = import.meta.env.VITE_AUTO_URL + "/delivery/finddelivery/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<AutoDeliveryData>) => {
autoDelivery.value = response.data;
getCustomer(autoDelivery.value.customer_id)
getCustomerDescription(autoDelivery.value.customer_id)
})
.catch(() => {
notify({
title: "Error",
text: "Could not get automatic",
type: "error",
});
});
}
const getPaymentCard = (card_id: number | string) => {
let path = import.meta.env.VITE_BASE_URL + "/payment/card/" + card_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<PaymentCardResponse>) => {
if (response.data.userCard.card_number === ''){
userCardfound.value = false;
}
else{
userCard.value = response.data.userCard as CreditCardFormData;
userCardfound.value = true;
}
})
.catch(() => {
});
}
const getCustomer = (user_id: number | string) => {
const path = `${import.meta.env.VITE_BASE_URL}/customer/${user_id}`;
axios.get<CustomerFormData>(path, { withCredentials: true })
.then((response) => {
customer.value = response.data;
})
.catch((error: Error) => {
notify({ title: "Error", text: "Could not find customer", type: "error" });
console.error("Error fetching customer:", error);
});
}
const getCustomerDescription = (user_id: number | string) => {
let path = import.meta.env.VITE_BASE_URL + "/customer/description/" + user_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<CustomerDescriptionData>) => {
customerDescription.value = response.data;
loading.value = false
})
.catch(() => {
notify({
title: "Error",
text: "Could not find customer",
type: "error",
});
});
}
const getTransaction = () => {
const path = `${import.meta.env.VITE_AUTHORIZE_URL}/api/auto/transaction/delivery/${route.params.id}`;
axios.get<AuthorizeNetTransactionResponse>(path, { withCredentials: true, headers: authHeader() })
.then((response) => {
transaction.value = response.data;
preAuthAmount.value = parseFloat(String(response.data.preauthorize_amount) || '0');
if (response.data.status !== 0) { // Not approved
preAuthAmount.value = 0;
}
})
.catch((error: AxiosError) => {
if (error.response && error.response.status === 404) {
notify({ title: "Info", text: "No pre-authorization found. Redirecting to customer profile to update payment method.", type: "info" });
console.log("No transaction found for Automatic - redirecting to customer profile");
router.push({ name: 'customerProfile', params: { id: customer.value.id } });
} else {
notify({ title: "Error", text: "No pre-authorized transaction found", type: "error" });
router.push({ name: 'finalizeTicketAuto', params: { id: route.params.id } });
}
});
}
const capturePayment = async () => {
if (autoTicket.value.payment_status !== 1) {
notify({ title: "Error", text: "Payment already captured or not ready for capture", type: "error" });
return;
}
if (!transaction.value || !captureAmount.value) {
notify({ title: "Error", text: "Invalid capture amount or transaction data", type: "error" });
return;
}
loading.value = true;
try {
const payload = {
charge_amount: captureAmount.value,
auth_net_transaction_id: transaction.value.auth_net_transaction_id
};
const url = `${import.meta.env.VITE_AUTHORIZE_URL}/api/capture/`;
const response = await axios.post(
url,
payload,
{ withCredentials: true, headers: authHeader() }
);
if (response.data && response.data.status === 0) {
autoTicket.value.payment_status = 3; // Update local status immediately
modalCapturedAmount.value = captureAmount.value;
showPaymentModal.value = true;
// Close the ticket and unassign from delivery
closeTicket(autoTicket.value.id);
setTimeout(() => { modalStep.value = 1 }, 2000);
setTimeout(() => { showPaymentModal.value = false; router.push({ name: 'auto' }) }, 4000);
} else if (response.data && response.data.status === 1) {
const reason = response.data.rejection_reason || "The payment was declined by the gateway.";
notify({
title: "Payment Declined",
text: reason,
type: "warn",
});
} else {
throw new Error("Invalid response from server during capture.");
}
} catch (err: unknown) {
const error = err as AxiosError<{ detail?: string }>;
const detail = error.response?.data?.detail || "Failed to capture payment due to a server error.";
notify({
title: "Error",
text: detail,
type: "error",
});
console.error("Capture Payment Error:", error);
} finally {
loading.value = false;
}
}
const cancelCapture = () => {
router.push({ name: 'finalizeTicketAuto', params: { id: route.params.id } });
}
const closeTicket = async (ticket_id: number) => {
const path = `${import.meta.env.VITE_AUTO_URL}/confirm/auto/close_ticket/${ticket_id}`;
axios.put(path, {}, { withCredentials: true })
.then(() => {
console.log("Ticket closed successfully");
})
.catch((error: Error) => {
notify({
title: "Warning",
text: "Payment captured, but failed to close ticket. Check manually.",
type: "warn",
});
console.error("Error closing ticket:", error);
});
}
const getTypeColor = (transactionType: number) => {
switch (transactionType) {
case 1: return 'text-blue-600'; // Auth
case 0: return 'text-orange-600'; // Charge
case 2: return 'text-purple-600'; // Capture
case 3: return 'text-green-600'; // Delivery/Other
default: return 'text-gray-600';
}
}
</script>
<style scoped></style>

View File

@@ -172,501 +172,503 @@
</div>
</template>
<script lang="ts">
import { defineComponent, watch } from 'vue'
import axios from 'axios'
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios, { AxiosResponse, AxiosError } from 'axios'
import authHeader from '../../../services/auth.header'
import { notify } from "@kyvg/vue3-notification"
import type {
DeliveryFormData,
CustomerFormData,
CreditCardFormData,
PricingData,
PromoData,
DeliveryOrderResponse,
DeliveryTotalResponse,
OilPricingResponse,
PromoResponse,
WhoAmIResponse,
UpdateStatusResponse
} from '../../../types/models'
export default defineComponent({
name: 'AuthorizePreauthCharge',
// Router and route
const route = useRoute()
const router = useRouter()
data() {
return {
deliveryId: this.$route.params.id as string,
loaded: false,
chargeAmount: 0,
loading: false,
action: '', // 'preauthorize' or 'charge'
error: '',
success: '',
isChargeConfirmationModalVisible: false,
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")
})
},
updateChargeAmount() {
// Only update if we have all necessary data
if (this.total_amount_after_discount > 0 &&
this.pricing.price_prime !== undefined &&
this.pricing.price_same_day !== undefined &&
this.pricing.price_emergency !== undefined) {
this.chargeAmount = this.calculateTotalAsNumber();
return true;
}
return false;
},
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 = parseFloat(response.data.total_amount) || 0;
this.discount = parseFloat(response.data.discount) || 0;
this.total_amount_after_discount = parseFloat(response.data.total_amount_after_discount) || 0;
// Try to update charge amount with complete pricing
const updated = this.updateChargeAmount();
// Fallback only if pricing not loaded yet and calculation didn't run
if (!updated) {
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
// Trigger a charge amount update if all data is available
this.updateChargeAmount();
}
})
},
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;
// Try to update charge amount when pricing is loaded
this.updateChargeAmount();
})
.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)
},
calculateTotalAmount() {
if (this.total_amount_after_discount == null || this.total_amount_after_discount === undefined) {
return '0.00';
}
let totalNum = Number(this.total_amount_after_discount);
if (isNaN(totalNum)) {
return '0.00';
}
if (this.delivery && this.delivery.prime == 1 && this.pricing && this.pricing.price_prime) {
totalNum += Number(this.pricing.price_prime) || 0;
}
if (this.delivery && this.delivery.same_day == 1 && this.pricing && this.pricing.price_same_day) {
totalNum += Number(this.pricing.price_same_day) || 0;
}
if (this.delivery && this.delivery.emergency == 1 && this.pricing && this.pricing.price_emergency) {
totalNum += Number(this.pricing.price_emergency) || 0;
}
return totalNum.toFixed(2);
},
calculateTotalAsNumber() {
if (this.total_amount_after_discount == null || this.total_amount_after_discount === undefined) {
return 0;
}
let totalNum = Number(this.total_amount_after_discount);
if (isNaN(totalNum)) {
return 0;
}
if (this.delivery && this.delivery.prime == 1 && this.pricing && this.pricing.price_prime) {
totalNum += Number(this.pricing.price_prime) || 0;
}
if (this.delivery && this.delivery.same_day == 1 && this.pricing && this.pricing.price_same_day) {
totalNum += Number(this.pricing.price_same_day) || 0;
}
if (this.delivery && this.delivery.emergency == 1 && this.pricing && this.pricing.price_emergency) {
totalNum += Number(this.pricing.price_emergency) || 0;
}
return totalNum;
},
async handlePreauthorize() {
await this.processPayment('preauthorize')
},
async handleChargeNow() {
if (!this.selectedCard) {
this.error = 'No credit card found for this customer'
return
}
if (!this.chargeAmount || this.chargeAmount <= 0) {
this.error = 'Please enter a valid charge amount'
return
}
this.isChargeConfirmationModalVisible = true
},
async proceedWithCharge() {
this.isChargeConfirmationModalVisible = false
await this.processPayment('charge')
},
cancelCharge() {
this.isChargeConfirmationModalVisible = false
},
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 {
// Step 2: If payment method is credit, perform the pre-authorization
if (actionType === 'preauthorize') {
if (!this.chargeAmount || this.chargeAmount <= 0) {
throw new Error("Pre-authorization amount must be greater than zero.");
}
const authPayload = {
card_id: (this.selectedCard as any).id,
preauthorize_amount: this.chargeAmount.toFixed(2),
delivery_id: this.delivery.id,
};
const authPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/authorize/saved-card/${this.customer.id}`;
const response = await axios.post(authPath, authPayload, { withCredentials: true, headers: authHeader() });
// Update payment type to 11 after successful preauthorization
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/authorize/${this.delivery.id}`, {}, { headers: authHeader() });
} catch (updateError) {
console.error('Failed to update payment type after preauthorization:', updateError);
}
// On successful authorization, show success and redirect
this.success = `Preauthorization successful! Transaction ID: ${response.data?.auth_net_transaction_id || 'N/A'}`;
setTimeout(() => {
this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
}, 2000);
} else { // Handle 'charge' action
if (!this.chargeAmount || this.chargeAmount <= 0) {
throw new Error("Charge amount must be greater than zero.");
}
// Create a payload that matches the backend's TransactionCreateByCardID schema
const chargePayload = {
card_id: (this.selectedCard as any).id,
charge_amount: this.chargeAmount.toFixed(2),
delivery_id: this.delivery.id,
service_id: this.delivery.service_id || null,
// You can add other fields here if your schema requires them
};
// Use the correct endpoint for charging a saved card
const chargePath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/charge/saved-card/${this.customer.id}`;
console.log('=== DEBUG: Charge payload ===');
console.log('Calling endpoint:', chargePath);
console.log('Final payload being sent:', chargePayload);
const response = await axios.post(chargePath, chargePayload, { withCredentials: true, headers: authHeader() });
// Update payment type to 11 after successful charge
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/authorize/${this.delivery.id}`, {}, { headers: authHeader() });
} catch (updateError) {
console.error('Failed to update payment type after charge:', updateError);
}
// Status codes: 0 = APPROVED, 1 = DECLINED (based on backend TransactionStatus enum)
if (response.data && response.data.status === 0) { // 0 = APPROVED
this.success = `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 {
// The error message from your backend will be more specific now
throw new Error(`Payment charge failed: ${response.data?.rejection_reason || 'Unknown error'}`);
}
}
} catch (error: any) {
console.log(error)
this.error = error.response?.data?.detail || `Failed to ${actionType} payment`
notify({
title: "Error",
text: this.error,
type: "error",
})
} finally {
this.loading = false
this.action = ''
}
}
},
// Reactive data
const deliveryId = ref(route.params.id as string)
const loaded = ref(false)
const chargeAmount = ref(0)
const loading = ref(false)
const action = ref('') // 'preauthorize' or 'charge'
const error = ref('')
const success = ref('')
const isChargeConfirmationModalVisible = ref(false)
const user = ref({
user_id: 0,
})
const delivery = ref<DeliveryFormData>({
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,
})
const credit_cards = ref<CreditCardFormData[]>([
{
id: 0,
name_on_card: '',
main_card: false,
card_number: '',
expiration_month: '',
type_of_card: '',
last_four_digits: '',
expiration_year: '',
security_number: '',
}
])
const customer = ref<CustomerFormData>({
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: '',
})
const pricing = ref<PricingData>({
price_from_supplier: 0,
price_for_customer: 0,
price_for_employee: 0,
price_same_day: 0,
price_prime: 0,
price_emergency: 0,
date: "",
})
const promo_active = ref(false)
const promo = ref<PromoData>({
name_of_promotion: '',
description: '',
money_off_delivery: 0,
text_on_ticket: ''
})
const total_amount = ref(0)
const discount = ref(0)
const total_amount_after_discount = ref(0)
// Computed properties
const selectedCard = computed(() => {
return credit_cards.value.find((card) => card.id === delivery.value.payment_card_id)
})
// Lifecycle
onMounted(() => {
loadData(deliveryId.value)
})
// Watchers
watch(() => route.params.id, (newId) => {
if (newId !== deliveryId.value) {
resetState()
deliveryId.value = newId as string
loadData(newId as string)
}
})
// Functions
const resetState = () => {
loading.value = false
action.value = ''
error.value = ''
success.value = ''
chargeAmount.value = 0
promo_active.value = false
total_amount.value = 0
discount.value = 0
total_amount_after_discount.value = 0
deliveryId.value = route.params.id as string
}
const loadData = (deliveryId: string) => {
userStatus()
getOilOrder(deliveryId)
sumdelivery(deliveryId)
getOilPricing()
updatestatus()
}
const updatestatus = () => {
let path = import.meta.env.VITE_BASE_URL + '/delivery/updatestatus';
axios({
method: 'get',
url: path,
headers: authHeader(),
}).then((response: AxiosResponse<UpdateStatusResponse>) => {
if (response.data.update)
console.log("Updated Status of Deliveries")
})
}
const updateChargeAmount = () => {
// Only update if we have all necessary data
if (total_amount_after_discount.value > 0 &&
pricing.value.price_prime !== undefined &&
pricing.value.price_same_day !== undefined &&
pricing.value.price_emergency !== undefined) {
chargeAmount.value = calculateTotalAsNumber();
return true;
}
return false;
}
const sumdelivery = (delivery_id: number | string) => {
let path = import.meta.env.VITE_BASE_URL + "/delivery/total/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<DeliveryTotalResponse>) => {
if (response.data.ok) {
total_amount.value = parseFloat(String(response.data.total_amount)) || 0;
discount.value = parseFloat(String(response.data.discount)) || 0;
total_amount_after_discount.value = parseFloat(String(response.data.total_amount_after_discount)) || 0;
// Try to update charge amount with complete pricing
const updated = updateChargeAmount();
// Fallback only if pricing not loaded yet and calculation didn't run
if (!updated) {
if (promo_active.value) {
chargeAmount.value = total_amount_after_discount.value;
} else {
chargeAmount.value = total_amount.value;
}
}
}
})
.catch(() => {
notify({
title: "Error",
text: "Could not get oil pricing",
type: "error",
});
});
}
const getPromo = (promo_id: number) => {
let path = import.meta.env.VITE_BASE_URL + "/promo/" + promo_id;
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: AxiosResponse<PromoResponse>) => {
if (response.data) {
promo.value = response.data
promo_active.value = true
// Trigger a charge amount update if all data is available
updateChargeAmount();
}
})
}
const userStatus = () => {
let path = import.meta.env.VITE_BASE_URL + '/auth/whoami';
axios({
method: 'get',
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: AxiosResponse<WhoAmIResponse>) => {
if (response.data.ok) {
user.value = response.data.user;
}
})
}
const getOilPricing = () => {
let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/table";
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<OilPricingResponse>) => {
pricing.value = response.data;
// Try to update charge amount when pricing is loaded
updateChargeAmount();
})
.catch(() => {
notify({
title: "Error",
text: "Could not get oil pricing",
type: "error",
});
});
}
const getOilOrder = (delivery_id: number | string) => {
let path = import.meta.env.VITE_BASE_URL + "/delivery/order/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<DeliveryOrderResponse>) => {
if (response.data && response.data.ok) {
delivery.value = response.data.delivery as DeliveryFormData;
getCustomer(delivery.value.customer_id)
getCreditCards(delivery.value.customer_id)
if (delivery.value.promo_id != null) {
getPromo(delivery.value.promo_id);
promo_active.value = true;
}
} else {
console.error("API Error:", response.data.error || "Failed to fetch delivery data.");
}
})
.catch((error: Error) => {
console.error("API Error in getOilOrder:", error);
notify({
title: "Error",
text: "Could not get delivery",
type: "error",
});
});
}
const getCreditCards = (user_id: number) => {
let path = import.meta.env.VITE_BASE_URL + '/payment/cards/' + user_id;
axios({
method: 'get',
url: path,
headers: authHeader(),
}).then((response: AxiosResponse<CreditCardFormData[]>) => {
credit_cards.value = response.data
})
}
const getCustomer = (userid: number) => {
let path = import.meta.env.VITE_BASE_URL + '/customer/' + userid;
axios({
method: 'get',
url: path,
headers: authHeader(),
}).then((response: AxiosResponse<CustomerFormData>) => {
customer.value = response.data
})
}
const calculateSubtotal = () => {
const gallons = delivery.value.gallons_ordered || 0
const pricePerGallon = delivery.value.customer_price || 0
return (gallons * pricePerGallon).toFixed(2)
}
const calculateTotalAmount = () => {
if (total_amount_after_discount.value == null || total_amount_after_discount.value === undefined) {
return '0.00';
}
let totalNum = Number(total_amount_after_discount.value);
if (isNaN(totalNum)) {
return '0.00';
}
if (delivery.value && delivery.value.prime == 1 && pricing.value && pricing.value.price_prime) {
totalNum += Number(pricing.value.price_prime) || 0;
}
if (delivery.value && delivery.value.same_day == 1 && pricing.value && pricing.value.price_same_day) {
totalNum += Number(pricing.value.price_same_day) || 0;
}
if (delivery.value && delivery.value.emergency == 1 && pricing.value && pricing.value.price_emergency) {
totalNum += Number(pricing.value.price_emergency) || 0;
}
return totalNum.toFixed(2);
}
const calculateTotalAsNumber = () => {
if (total_amount_after_discount.value == null || total_amount_after_discount.value === undefined) {
return 0;
}
let totalNum = Number(total_amount_after_discount.value);
if (isNaN(totalNum)) {
return 0;
}
if (delivery.value && delivery.value.prime == 1 && pricing.value && pricing.value.price_prime) {
totalNum += Number(pricing.value.price_prime) || 0;
}
if (delivery.value && delivery.value.same_day == 1 && pricing.value && pricing.value.price_same_day) {
totalNum += Number(pricing.value.price_same_day) || 0;
}
if (delivery.value && delivery.value.emergency == 1 && pricing.value && pricing.value.price_emergency) {
totalNum += Number(pricing.value.price_emergency) || 0;
}
return totalNum;
}
const handlePreauthorize = async () => {
await processPayment('preauthorize')
}
const handleChargeNow = async () => {
if (!selectedCard.value) {
error.value = 'No credit card found for this customer'
return
}
if (!chargeAmount.value || chargeAmount.value <= 0) {
error.value = 'Please enter a valid charge amount'
return
}
isChargeConfirmationModalVisible.value = true
}
const proceedWithCharge = async () => {
isChargeConfirmationModalVisible.value = false
await processPayment('charge')
}
const cancelCharge = () => {
isChargeConfirmationModalVisible.value = false
}
const processPayment = async (actionType: string) => {
if (!selectedCard.value) {
error.value = 'No credit card found for this customer'
return
}
loading.value = true
action.value = actionType
error.value = ''
success.value = ''
try {
// Step 2: If payment method is credit, perform the pre-authorization
if (actionType === 'preauthorize') {
if (!chargeAmount.value || chargeAmount.value <= 0) {
throw new Error("Pre-authorization amount must be greater than zero.");
}
const authPayload = {
card_id: selectedCard.value!.id,
preauthorize_amount: chargeAmount.value.toFixed(2),
delivery_id: delivery.value.id,
};
const authPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/authorize/saved-card/${customer.value.id}`;
const response = await axios.post(authPath, authPayload, { withCredentials: true, headers: authHeader() });
// Update payment type to 11 after successful preauthorization
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/authorize/${delivery.value.id}`, {}, { headers: authHeader() });
} catch (updateError) {
console.error('Failed to update payment type after preauthorization:', updateError);
}
// On successful authorization, show success and redirect
success.value = `Preauthorization successful! Transaction ID: ${response.data?.auth_net_transaction_id || 'N/A'}`;
setTimeout(() => {
router.push({ name: "customerProfile", params: { id: customer.value.id } });
}, 2000);
} else { // Handle 'charge' action
if (!chargeAmount.value || chargeAmount.value <= 0) {
throw new Error("Charge amount must be greater than zero.");
}
// Create a payload that matches the backend's TransactionCreateByCardID schema
const chargePayload = {
card_id: selectedCard.value!.id,
charge_amount: chargeAmount.value.toFixed(2),
delivery_id: delivery.value.id,
service_id: delivery.value.service_id || null,
// You can add other fields here if your schema requires them
};
// Use the correct endpoint for charging a saved card
const chargePath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/charge/saved-card/${customer.value.id}`;
console.log('=== DEBUG: Charge payload ===');
console.log('Calling endpoint:', chargePath);
console.log('Final payload being sent:', chargePayload);
const response = await axios.post(chargePath, chargePayload, { withCredentials: true, headers: authHeader() });
// Update payment type to 11 after successful charge
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/authorize/${delivery.value.id}`, {}, { headers: authHeader() });
} catch (updateError) {
console.error('Failed to update payment type after charge:', updateError);
}
// Status codes: 0 = APPROVED, 1 = DECLINED (based on backend TransactionStatus enum)
if (response.data && response.data.status === 0) { // 0 = APPROVED
success.value = `Charge successful! Transaction ID: ${response.data.auth_net_transaction_id || 'N/A'}`;
setTimeout(() => {
router.push({ name: "customerProfile", params: { id: customer.value.id } });
}, 2000);
} else {
// The error message from your backend will be more specific now
throw new Error(`Payment charge failed: ${response.data?.rejection_reason || 'Unknown error'}`);
}
}
} catch (err: unknown) {
const axiosErr = err as AxiosError<{ detail?: string }>;
console.log(err)
error.value = axiosErr.response?.data?.detail || `Failed to ${actionType} payment`
notify({
title: "Error",
text: error.value,
type: "error",
})
} finally {
loading.value = false
action.value = ''
}
}
</script>
<style scoped></style>

View File

@@ -278,403 +278,409 @@
<Footer />
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import axios from 'axios'
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios, { AxiosResponse, AxiosError } 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 { notify } from "@kyvg/vue3-notification"
import type {
CustomerFormData,
CreditCardFormData,
PricingData,
PromoData,
DeliveryOrderResponse,
DeliveryTotalResponse,
OilPricingResponse,
PromoResponse,
PaymentCardResponse,
AuthorizeNetTransactionResponse
} from '../../../types/models'
export default defineComponent({
name: 'captureAuthorize',
const route = useRoute()
const router = useRouter()
components: {
Header,
SideBar,
Footer,
},
// Reactive data
const loading = ref(false)
const userCardfound = ref(false)
const gallonsDelivered = ref('')
const captureAmount = ref(0)
const preAuthAmount = ref(0)
const transaction = ref<AuthorizeNetTransactionResponse | null>(null)
const showPaymentModal = ref(false)
const modalStep = ref(0)
const modalCapturedAmount = ref(0)
data() {
return {
loading: false,
userCardfound: false,
gallonsDelivered: '',
captureAmount: 0,
preAuthAmount: 0,
transaction: null as any,
showPaymentModal: false,
modalStep: 0,
modalCapturedAmount: 0,
userCard: {
date_added: '',
user_id: '',
card_number: '',
last_four_digits: '',
name_on_card: '',
expiration_month: '',
expiration_year: '',
type_of_card: '',
security_number: '',
accepted_or_declined: '',
main_card: '',
},
customer: {
id: 0,
user_id: 0,
customer_address: '',
customer_first_name: '',
customer_last_name: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
customer_apt: '',
customer_home_type: 0,
customer_phone_number: '',
},
deliveryOrder: {
id: '',
customer_id: 0,
customer_name: '',
customer_address: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
gallons_ordered: 0,
customer_asked_for_fill: 0,
gallons_delivered: '',
customer_filled: 0,
delivery_status: 0,
when_ordered: '',
when_delivered: '',
expected_delivery_date: '',
automatic: 0,
oil_id: 0,
supplier_price: '',
customer_price: '',
customer_temperature: '',
dispatcher_notes: '',
prime: 0,
same_day: 0,
emergency: 0,
promo_id: 0,
payment_type: 0,
payment_card_id: '',
driver_employee_id: 0,
driver_first_name: '',
driver_last_name: '',
total_price: 0,
},
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,
}
},
mounted() {
this.getOilOrder(this.$route.params.id)
this.getOilPricing()
this.getTransaction()
},
methods: {
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 = parseFloat(response.data.total_amount) || 0;
this.discount = parseFloat(response.data.discount) || 0;
this.total_amount_after_discount = parseFloat(response.data.total_amount_after_discount) || 0;
// Set capture amount to the calculated total including fees and discount
this.captureAmount = this.calculateTotalAsNumber();
}
})
.catch(() => {
notify({
title: "Error",
text: "Could not get totals",
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
}
})
},
getOilOrder(delivery_id: any) {
const path = `${import.meta.env.VITE_BASE_URL}/delivery/order/${delivery_id}`;
axios.get(path, { withCredentials: true, headers: authHeader() })
.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);
this.sumdelivery(delivery_id);
if ([1, 2, 3].includes(this.deliveryOrder.payment_type)) {
this.getPaymentCard(this.deliveryOrder.payment_card_id);
}
if (this.deliveryOrder.promo_id != null) {
this.getPromo(this.deliveryOrder.promo_id);
this.promo_active = true;
}
} else {
console.error("API Error:", response.data.error || "Failed to fetch delivery data.");
}
})
.catch((error: any) => console.error("Error fetching oil order:", error));
},
getPaymentCard(card_id: any) {
const path = `${import.meta.env.VITE_BASE_URL}/payment/card/${card_id}`;
axios.get(path, { withCredentials: true })
.then((response: any) => {
if (response.data.userCard && response.data.userCard.card_number !== '') {
this.userCard = response.data;
this.userCardfound = true;
}
})
.catch((error: any) => {
this.userCardfound = false;
console.error("Error fetching payment card:", error);
});
},
getCustomer(user_id: any) {
const path = `${import.meta.env.VITE_BASE_URL}/customer/${user_id}`;
axios.get(path, { withCredentials: true })
.then((response: any) => {
this.customer = response.data;
})
.catch((error: any) => {
notify({ title: "Error", text: "Could not find customer", type: "error" });
console.error("Error fetching customer:", error);
});
},
getOilPricing() {
const path = `${import.meta.env.VITE_BASE_URL}/info/price/oil/table`;
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" });
console.error("Error fetching oil pricing:", error);
});
},
getTransaction() {
const path = `${import.meta.env.VITE_AUTHORIZE_URL}/api/transaction/delivery/${this.$route.params.id}`;
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) => {
if (error.response && error.response.status === 404) {
notify({ title: "Info", text: "No pre-authorization found. Redirecting to customer profile to update payment method.", type: "info" });
console.log("No transaction found for delivery - redirecting to customer profile");
this.$router.push({ name: 'customerProfile', params: { id: this.customer.id } });
} else {
notify({ title: "Error", text: "No pre-authorized transaction found", type: "error" });
this.$router.push({ name: 'finalizeTicket', params: { id: this.$route.params.id } });
}
});
},
async capturePayment() {
if (!this.transaction || !this.captureAmount) {
notify({ title: "Error", text: "Invalid capture amount or transaction data", type: "error" });
return;
}
this.loading = true;
try {
const payload = {
charge_amount: this.captureAmount, // FastAPI handles string/number conversion
auth_net_transaction_id: this.transaction.auth_net_transaction_id
};
// ✅ FIX: Cleaned up URL, removing the unnecessary customer_id query parameter.
const url = `${import.meta.env.VITE_AUTHORIZE_URL}/api/capture/`;
const response = await axios.post(
url,
payload,
{ withCredentials: true, headers: authHeader() }
);
// ✅ FIX: Improved logic to handle both success and declines properly.
if (response.data && response.data.status === 0) {
// This is the APPROVED case
this.modalCapturedAmount = this.captureAmount;
this.showPaymentModal = true;
setTimeout(() => { this.modalStep = 1 }, 2000);
setTimeout(() => { this.showPaymentModal = false; this.$router.push({ name: 'deliveryOrder', params: { id: this.$route.params.id } }) }, 4000);
} else if (response.data && response.data.status === 1) {
// This is the DECLINED case
const reason = response.data.rejection_reason || "The payment was declined by the gateway.";
notify({
title: "Payment Declined",
text: reason,
type: "warn", // Use 'warn' for declines instead of 'error'
});
} else {
// This handles unexpected responses from the backend.
throw new Error("Invalid response from server during capture.");
}
} catch (error: any) {
// This 'catch' block now only handles network errors or server crashes (500 errors).
const detail = error.response?.data?.detail || "Failed to capture payment due to a server error.";
notify({
title: "Error",
text: detail,
type: "error",
});
console.error("Capture Payment Error:", error);
} finally {
this.loading = false;
}
},
calculateSubtotal() {
const gallons = parseFloat(this.gallonsDelivered || '0') || 0;
const pricePerGallon = typeof this.deliveryOrder.customer_price === 'string' ? parseFloat(this.deliveryOrder.customer_price) : Number(this.deliveryOrder.customer_price) || 0;
return (gallons * pricePerGallon).toFixed(2);
},
calculateTotalAmount() {
if (this.total_amount_after_discount == null || this.total_amount_after_discount === undefined) {
return '0.00';
}
let totalNum = Number(this.total_amount_after_discount);
if (isNaN(totalNum)) {
return '0.00';
}
if (this.deliveryOrder && this.deliveryOrder.prime == 1 && this.pricing && this.pricing.price_prime) {
totalNum += Number(this.pricing.price_prime) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.same_day == 1 && this.pricing && this.pricing.price_same_day) {
totalNum += Number(this.pricing.price_same_day) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.emergency == 1 && this.pricing && this.pricing.price_emergency) {
totalNum += Number(this.pricing.price_emergency) || 0;
}
return totalNum.toFixed(2);
},
calculateTotalAsNumber() {
if (this.total_amount_after_discount == null || this.total_amount_after_discount === undefined) {
return 0;
}
let totalNum = Number(this.total_amount_after_discount);
if (isNaN(totalNum)) {
return 0;
}
if (this.deliveryOrder && this.deliveryOrder.prime == 1 && this.pricing && this.pricing.price_prime) {
totalNum += Number(this.pricing.price_prime) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.same_day == 1 && this.pricing && this.pricing.price_same_day) {
totalNum += Number(this.pricing.price_same_day) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.emergency == 1 && this.pricing && this.pricing.price_emergency) {
totalNum += Number(this.pricing.price_emergency) || 0;
}
return totalNum;
},
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 } });
},
getTypeColor(transactionType: number) {
switch (transactionType) {
case 1: return 'text-blue-600'; // Auth
case 0: return 'text-orange-600'; // Charge
case 2: return 'text-purple-600'; // Capture
case 3: return 'text-green-600'; // Delivery/Other
default: return 'text-gray-600';
}
},
},
const userCard = ref<CreditCardFormData>({
id: 0,
date_added: '',
user_id: '',
card_number: '',
last_four_digits: '',
name_on_card: '',
expiration_month: '',
expiration_year: '',
type_of_card: '',
security_number: '',
accepted_or_declined: '',
main_card: false,
})
const customer = ref<CustomerFormData>({
id: 0,
user_id: 0,
customer_address: '',
customer_first_name: '',
customer_last_name: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
customer_apt: '',
customer_home_type: 0,
customer_phone_number: '',
account_number: '',
})
const deliveryOrder = ref({
id: '',
customer_id: 0,
customer_name: '',
customer_address: '',
customer_town: '',
customer_state: 0,
customer_zip: '',
gallons_ordered: 0,
customer_asked_for_fill: 0,
gallons_delivered: '',
customer_filled: 0,
delivery_status: 0,
when_ordered: '',
when_delivered: '',
expected_delivery_date: '',
automatic: 0,
oil_id: 0,
supplier_price: '',
customer_price: '',
customer_temperature: '',
dispatcher_notes: '',
prime: 0,
same_day: 0,
emergency: 0,
promo_id: 0,
payment_type: 0,
payment_card_id: '',
driver_employee_id: 0,
driver_first_name: '',
driver_last_name: '',
total_price: 0,
})
const pricing = ref<PricingData>({
price_from_supplier: 0,
price_for_customer: 0,
price_for_employee: 0,
price_same_day: 0,
price_prime: 0,
price_emergency: 0,
date: "",
})
const promo_active = ref(false)
const promo = ref<PromoData>({
name_of_promotion: '',
description: '',
money_off_delivery: 0,
text_on_ticket: ''
})
const total_amount = ref(0)
const discount = ref(0)
const total_amount_after_discount = ref(0)
// Lifecycle
onMounted(() => {
getOilOrder(route.params.id)
getOilPricing()
getTransaction()
})
// Functions
const sumdelivery = (delivery_id: number | string) => {
let path = import.meta.env.VITE_BASE_URL + "/delivery/total/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: AxiosResponse<DeliveryTotalResponse>) => {
if (response.data.ok) {
total_amount.value = parseFloat(String(response.data.total_amount)) || 0;
discount.value = parseFloat(String(response.data.discount)) || 0;
total_amount_after_discount.value = parseFloat(String(response.data.total_amount_after_discount)) || 0;
// Set capture amount to the calculated total including fees and discount
captureAmount.value = calculateTotalAsNumber();
}
})
.catch(() => {
notify({
title: "Error",
text: "Could not get totals",
type: "error",
});
});
}
const getPromo = (promo_id: number) => {
let path = import.meta.env.VITE_BASE_URL + "/promo/" + promo_id;
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: AxiosResponse<PromoResponse>) => {
if (response.data) {
promo.value = response.data
promo_active.value = true
}
})
}
const getOilOrder = (delivery_id: number | string) => {
const path = `${import.meta.env.VITE_BASE_URL}/delivery/order/${delivery_id}`;
axios.get<DeliveryOrderResponse>(path, { withCredentials: true, headers: authHeader() })
.then((response) => {
if (response.data && response.data.ok) {
deliveryOrder.value = response.data.delivery as typeof deliveryOrder.value;
gallonsDelivered.value = deliveryOrder.value.gallons_delivered;
getCustomer(deliveryOrder.value.customer_id);
sumdelivery(delivery_id);
if ([1, 2, 3].includes(deliveryOrder.value.payment_type)) {
getPaymentCard(deliveryOrder.value.payment_card_id);
}
if (deliveryOrder.value.promo_id != null) {
getPromo(deliveryOrder.value.promo_id);
promo_active.value = true;
}
} else {
console.error("API Error:", response.data.error || "Failed to fetch delivery data.");
}
})
.catch((error: Error) => console.error("Error fetching oil order:", error));
}
const getPaymentCard = (card_id: number | string) => {
const path = `${import.meta.env.VITE_BASE_URL}/payment/card/${card_id}`;
axios.get<PaymentCardResponse>(path, { withCredentials: true })
.then((response) => {
if (response.data.userCard && response.data.userCard.card_number !== '') {
userCard.value = response.data.userCard as CreditCardFormData;
userCardfound.value = true;
}
})
.catch((error: Error) => {
userCardfound.value = false;
console.error("Error fetching payment card:", error);
});
}
const getCustomer = (user_id: number) => {
const path = `${import.meta.env.VITE_BASE_URL}/customer/${user_id}`;
axios.get<CustomerFormData>(path, { withCredentials: true })
.then((response) => {
customer.value = response.data;
})
.catch((error: Error) => {
notify({ title: "Error", text: "Could not find customer", type: "error" });
console.error("Error fetching customer:", error);
});
}
const getOilPricing = () => {
const path = `${import.meta.env.VITE_BASE_URL}/info/price/oil/table`;
axios.get<OilPricingResponse>(path, { withCredentials: true })
.then((response) => {
pricing.value = response.data;
// Calculate capture amount if delivery order is already loaded
calculateCaptureAmount();
})
.catch((error: Error) => {
notify({ title: "Error", text: "Could not get oil pricing", type: "error" });
console.error("Error fetching oil pricing:", error);
});
}
const getTransaction = () => {
const path = `${import.meta.env.VITE_AUTHORIZE_URL}/api/transaction/delivery/${route.params.id}`;
axios.get<AuthorizeNetTransactionResponse>(path, { withCredentials: true, headers: authHeader() })
.then((response) => {
transaction.value = response.data;
preAuthAmount.value = parseFloat(String(response.data.preauthorize_amount) || '0');
if (response.data.status !== 0) { // Not approved
preAuthAmount.value = 0;
}
})
.catch((error: AxiosError) => {
if (error.response && error.response.status === 404) {
notify({ title: "Info", text: "No pre-authorization found. Redirecting to customer profile to update payment method.", type: "info" });
console.log("No transaction found for delivery - redirecting to customer profile");
router.push({ name: 'customerProfile', params: { id: customer.value.id } });
} else {
notify({ title: "Error", text: "No pre-authorized transaction found", type: "error" });
router.push({ name: 'finalizeTicket', params: { id: route.params.id } });
}
});
}
const capturePayment = async () => {
if (!transaction.value || !captureAmount.value) {
notify({ title: "Error", text: "Invalid capture amount or transaction data", type: "error" });
return;
}
loading.value = true;
try {
const payload = {
charge_amount: captureAmount.value, // FastAPI handles string/number conversion
auth_net_transaction_id: transaction.value.auth_net_transaction_id
};
// ✅ FIX: Cleaned up URL, removing the unnecessary customer_id query parameter.
const url = `${import.meta.env.VITE_AUTHORIZE_URL}/api/capture/`;
const response = await axios.post(
url,
payload,
{ withCredentials: true, headers: authHeader() }
);
// ✅ FIX: Improved logic to handle both success and declines properly.
if (response.data && response.data.status === 0) {
// This is the APPROVED case
modalCapturedAmount.value = captureAmount.value;
showPaymentModal.value = true;
setTimeout(() => { modalStep.value = 1 }, 2000);
setTimeout(() => { showPaymentModal.value = false; router.push({ name: 'deliveryOrder', params: { id: route.params.id } }) }, 4000);
} else if (response.data && response.data.status === 1) {
// This is the DECLINED case
const reason = response.data.rejection_reason || "The payment was declined by the gateway.";
notify({
title: "Payment Declined",
text: reason,
type: "warn", // Use 'warn' for declines instead of 'error'
});
} else {
// This handles unexpected responses from the backend.
throw new Error("Invalid response from server during capture.");
}
} catch (err: unknown) {
// This 'catch' block now only handles network errors or server crashes (500 errors).
const error = err as AxiosError<{ detail?: string }>;
const detail = error.response?.data?.detail || "Failed to capture payment due to a server error.";
notify({
title: "Error",
text: detail,
type: "error",
});
console.error("Capture Payment Error:", error);
} finally {
loading.value = false;
}
}
const calculateSubtotal = () => {
const gallons = parseFloat(gallonsDelivered.value || '0') || 0;
const pricePerGallon = typeof deliveryOrder.value.customer_price === 'string' ? parseFloat(deliveryOrder.value.customer_price) : Number(deliveryOrder.value.customer_price) || 0;
return (gallons * pricePerGallon).toFixed(2);
}
const calculateTotalAmount = () => {
if (total_amount_after_discount.value == null || total_amount_after_discount.value === undefined) {
return '0.00';
}
let totalNum = Number(total_amount_after_discount.value);
if (isNaN(totalNum)) {
return '0.00';
}
if (deliveryOrder.value && deliveryOrder.value.prime == 1 && pricing.value && pricing.value.price_prime) {
totalNum += Number(pricing.value.price_prime) || 0;
}
if (deliveryOrder.value && deliveryOrder.value.same_day == 1 && pricing.value && pricing.value.price_same_day) {
totalNum += Number(pricing.value.price_same_day) || 0;
}
if (deliveryOrder.value && deliveryOrder.value.emergency == 1 && pricing.value && pricing.value.price_emergency) {
totalNum += Number(pricing.value.price_emergency) || 0;
}
return totalNum.toFixed(2);
}
const calculateTotalAsNumber = () => {
if (total_amount_after_discount.value == null || total_amount_after_discount.value === undefined) {
return 0;
}
let totalNum = Number(total_amount_after_discount.value);
if (isNaN(totalNum)) {
return 0;
}
if (deliveryOrder.value && deliveryOrder.value.prime == 1 && pricing.value && pricing.value.price_prime) {
totalNum += Number(pricing.value.price_prime) || 0;
}
if (deliveryOrder.value && deliveryOrder.value.same_day == 1 && pricing.value && pricing.value.price_same_day) {
totalNum += Number(pricing.value.price_same_day) || 0;
}
if (deliveryOrder.value && deliveryOrder.value.emergency == 1 && pricing.value && pricing.value.price_emergency) {
totalNum += Number(pricing.value.price_emergency) || 0;
}
return totalNum;
}
const calculateCaptureAmount = () => {
// Only calculate if we have both delivery order and pricing data
if (deliveryOrder.value.id && pricing.value.price_for_customer) {
const gallons = typeof gallonsDelivered.value === 'string' ? parseFloat(gallonsDelivered.value) : Number(gallonsDelivered.value) || 0;
const pricePerGallon = typeof deliveryOrder.value.customer_price === 'string' ? parseFloat(deliveryOrder.value.customer_price) : Number(deliveryOrder.value.customer_price) || 0;
let total = gallons * pricePerGallon;
// Add prime fee if applicable
if (deliveryOrder.value.prime == 1) {
const primeFee = typeof pricing.value.price_prime === 'string' ? parseFloat(pricing.value.price_prime) : Number(pricing.value.price_prime) || 0;
total += primeFee;
}
// Add same day fee if applicable
if (deliveryOrder.value.same_day === 1) {
const sameDayFee = typeof pricing.value.price_same_day === 'string' ? parseFloat(pricing.value.price_same_day) : Number(pricing.value.price_same_day) || 0;
total += sameDayFee;
}
captureAmount.value = total;
}
}
const cancelCapture = () => {
router.push({ name: 'finalizeTicket', params: { id: route.params.id } });
}
const getTypeColor = (transactionType: number) => {
switch (transactionType) {
case 1: return 'text-blue-600'; // Auth
case 0: return 'text-orange-600'; // Charge
case 2: return 'text-purple-600'; // Capture
case 3: return 'text-green-600'; // Delivery/Other
default: return 'text-gray-600';
}
}
</script>
<style scoped></style>

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +1,13 @@
import PayOil from './oil/pay_oil.vue';
import AuthorizePreauthCharge from './oil/authorize_preauthcharge.vue';
import CaptureAuthorize from './oil/capture_authorize.vue';
import PayService from './service/pay_service.vue';
import AuthorizeServicePreauthCharge from './service/authorize_preauthcharge.vue';
import ChargeServiceAuthorize from './service/capture_authorize.vue';
import AuthorizePrechargeAutho from './auto/authorize_precharge_autho.vue';
import CaptureAuthorizeAutho from './auto/capture_authorize_autho.vue';
const PayOil = () => import('./oil/pay_oil.vue');
const AuthorizePreauthCharge = () => import('./oil/authorize_preauthcharge.vue');
const CaptureAuthorize = () => import('./oil/capture_authorize.vue');
const PayService = () => import('./service/pay_service.vue');
const AuthorizeServicePreauthCharge = () => import('./service/authorize_preauthcharge.vue');
const ChargeServiceAuthorize = () => import('./service/capture_authorize.vue');
const AuthorizePrechargeAutho = () => import('./auto/authorize_precharge_autho.vue');
const CaptureAuthorizeAutho = () => import('./auto/capture_authorize_autho.vue');
const payRoutes = [
// This is for oil delivery

View File

@@ -154,383 +154,382 @@
</div>
</template>
<script lang="ts">
import { defineComponent, watch } from 'vue'
import axios from 'axios'
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios, { AxiosResponse, AxiosError } from 'axios'
import authHeader from '../../../services/auth.header'
import { notify } from "@kyvg/vue3-notification"
import type {
CustomerFormData,
CreditCardFormData,
WhoAmIResponse
} from '../../../types/models'
export default defineComponent({
name: 'AuthorizeServicePreauthCharge',
const route = useRoute()
const router = useRouter()
data() {
return {
serviceId: this.$route.params.id as string,
loaded: false,
chargeAmount: 0,
loading: false,
action: '', // 'preauthorize' or 'charge'
error: '',
success: '',
selectedCardId: null as number | null, // Track which card is selected
user: {
user_id: 0,
},
service: {
id: 0,
scheduled_date: '',
customer_id: 0,
customer_name: '',
customer_address: '',
customer_town: '',
type_service_call: 0,
description: '',
service_cost: '',
payment_card_id: 0,
},
credit_cards: [
{
id: 0,
name_on_card: '',
main_card: false,
card_number: '',
expiration_month: '',
type_of_card: '',
last_four_digits: '',
expiration_year: '',
security_number: '',
interface ServiceFormData {
id: number;
scheduled_date: string;
customer_id: number;
customer_name: string;
customer_address: string;
customer_town: string;
type_service_call: number;
description: string;
service_cost: string;
payment_card_id: 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: '',
},
// Reactive data
const serviceId = ref(route.params.id as string)
const loaded = ref(false)
const chargeAmount = ref(0)
const loading = ref(false)
const action = ref('') // 'preauthorize' or 'charge'
const error = ref('')
const success = ref('')
const selectedCardId = ref(null as number | null) // Track which card is selected
const user = ref({
user_id: 0,
})
const service = ref<ServiceFormData>({
id: 0,
scheduled_date: '',
customer_id: 0,
customer_name: '',
customer_address: '',
customer_town: '',
type_service_call: 0,
description: '',
service_cost: '',
payment_card_id: 0,
})
const credit_cards = ref<CreditCardFormData[]>([
{
id: 0,
name_on_card: '',
main_card: false,
card_number: '',
expiration_month: '',
type_of_card: '',
last_four_digits: '',
expiration_year: '',
security_number: '',
}
])
const customer = ref<CustomerFormData>({
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: '',
})
}
},
// Computed
const selectedCard = computed(() => {
// If user has selected a card manually, use that
if (selectedCardId.value) {
return credit_cards.value.find((card) => card.id === selectedCardId.value)
}
// Otherwise use automatic selection logic
// First try to find payment_card_id from service
if (service.value.payment_card_id && service.value.payment_card_id > 0) {
return credit_cards.value.find((card) => card.id === service.value.payment_card_id)
}
// Otherwise return the primary card (main_card = true)
return credit_cards.value.find((card) => card.main_card === true) ||
credit_cards.value.find((card) => card.id > 0) || // Any card if no primary
null
})
computed: {
selectedCard(): any {
// If user has selected a card manually, use that
if (this.selectedCardId) {
return this.credit_cards.find((card: any) => card.id === this.selectedCardId)
// Watchers
watch(() => route.params.id, (newId) => {
if (newId !== serviceId.value) {
resetState()
serviceId.value = newId as string
loadData(newId as string)
}
})
// Lifecycle
onMounted(() => {
loadData(serviceId.value)
})
// Functions
const resetState = () => {
loading.value = false
action.value = ''
error.value = ''
success.value = ''
chargeAmount.value = 0
serviceId.value = route.params.id as string
}
const loadData = (serviceId: string) => {
userStatus()
getServiceOrder(serviceId)
}
const userStatus = () => {
let path = import.meta.env.VITE_BASE_URL + '/auth/whoami';
axios({
method: 'get',
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: AxiosResponse<WhoAmIResponse>) => {
if (response.data.ok) {
user.value = response.data.user;
}
// Otherwise use automatic selection logic
// First try to find payment_card_id from service
if (this.service.payment_card_id && this.service.payment_card_id > 0) {
return this.credit_cards.find((card: any) => card.id === this.service.payment_card_id)
}
// Otherwise return the primary card (main_card = true)
return this.credit_cards.find((card: any) => card.main_card === true) ||
this.credit_cards.find((card: any) => card.id > 0) || // Any card if no primary
null
}
},
})
}
mounted() {
this.loadData(this.serviceId)
},
created() {
this.watchRoute()
},
methods: {
watchRoute() {
watch(
() => this.$route.params.id,
(newId) => {
if (newId !== this.serviceId) {
this.resetState()
this.serviceId = newId as string
this.loadData(newId as string)
}
const getServiceOrder = (serviceId: number | string) => {
let path = import.meta.env.VITE_BASE_URL + "/service/" + serviceId;
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: AxiosResponse<{ ok?: boolean; service?: ServiceFormData } | ServiceFormData[] | ServiceFormData>) => {
let serviceData: ServiceFormData | undefined;
if (response.data) {
// Handle different API response structures
if ('service' in response.data && response.data.service) {
// API returns {ok: true, service: {...}} structure
serviceData = response.data.service;
} else if (Array.isArray(response.data)) {
serviceData = response.data[0]; // Array response
} else if ('id' in response.data) {
serviceData = response.data as ServiceFormData; // Direct object response
}
)
},
resetState() {
this.loading = false
this.action = ''
this.error = ''
this.success = ''
this.chargeAmount = 0
this.serviceId = this.$route.params.id as string
},
if (serviceData && serviceData.id) {
service.value = {
id: serviceData.id,
scheduled_date: serviceData.scheduled_date,
customer_id: serviceData.customer_id,
customer_name: serviceData.customer_name,
customer_address: serviceData.customer_address,
customer_town: serviceData.customer_town,
type_service_call: serviceData.type_service_call,
description: serviceData.description,
service_cost: serviceData.service_cost,
payment_card_id: serviceData.payment_card_id || 0,
};
loadData(serviceId: string) {
this.userStatus()
this.getServiceOrder(serviceId)
},
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;
}
})
},
getServiceOrder(serviceId: any) {
let path = import.meta.env.VITE_BASE_URL + "/service/" + serviceId;
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: any) => {
let serviceData;
if (response.data) {
// Handle different API response structures
if (response.data.service) {
// API returns {ok: true, service: {...}} structure
serviceData = response.data.service;
} else if (Array.isArray(response.data)) {
serviceData = response.data[0]; // Array response
} else {
serviceData = response.data; // Direct object response
}
if (serviceData && serviceData.id) {
this.service = {
id: serviceData.id,
scheduled_date: serviceData.scheduled_date,
customer_id: serviceData.customer_id,
customer_name: serviceData.customer_name,
customer_address: serviceData.customer_address,
customer_town: serviceData.customer_town,
type_service_call: serviceData.type_service_call,
description: serviceData.description,
service_cost: serviceData.service_cost,
payment_card_id: serviceData.payment_card_id || 0,
};
// Fetch related data
this.getCustomer(this.service.customer_id);
this.getCreditCards(this.service.customer_id);
} else {
console.error("API Error: Invalid service data received:", serviceData);
notify({
title: "Error",
text: "Invalid service data received",
type: "error",
});
}
} else {
console.error("API Error: No response data received");
notify({
title: "Error",
text: "Could not get service data",
type: "error",
});
}
})
.catch((error: any) => {
console.error("API Error in getServiceOrder:", error);
// Fetch related data
getCustomer(service.value.customer_id);
getCreditCards(service.value.customer_id);
} else {
console.error("API Error: Invalid service data received:", serviceData);
notify({
title: "Error",
text: "Could not get service data",
text: "Invalid service data received",
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) => {
console.log('Credit cards loaded:', response.data?.length || 0, 'cards');
this.credit_cards = response.data || [];
}).catch((error: any) => {
console.error('Failed to load credit cards:', error);
this.credit_cards = [];
});
},
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
})
},
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 {
// Step 2: If payment method is credit, perform the pre-authorization
if (actionType === 'preauthorize') {
if (!this.chargeAmount || this.chargeAmount <= 0) {
throw new Error("Pre-authorization amount must be greater than zero.");
}
const authPayload = {
card_id: (this.selectedCard as any).id,
preauthorize_amount: this.chargeAmount.toFixed(2),
service_id: this.service.id,
delivery_id: null, // No delivery for services
};
const authPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/authorize/saved-card/${this.customer.id}`;
const response = await axios.post(authPath, authPayload, { withCredentials: true, headers: authHeader() });
// Update payment type to 11 after successful preauthorization
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/authorize/service/${this.service.id}`, {
card_id: (this.selectedCard as any).id,
status: actionType === 'preauthorize' ? 1 : 3
}, { headers: authHeader() });
} catch (updateError) {
console.error('Failed to update payment type after preauthorization:', updateError);
}
// On successful authorization, show success and redirect
this.success = `Preauthorization successful! Transaction ID: ${response.data?.auth_net_transaction_id || 'N/A'}`;
setTimeout(() => {
this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
}, 2000);
} else { // Handle 'charge' action
if (!this.chargeAmount || this.chargeAmount <= 0) {
throw new Error("Charge amount must be greater than zero.");
}
// Create a payload that matches the backend's TransactionCreateByCardID schema
const chargePayload = {
card_id: (this.selectedCard as any).id,
charge_amount: this.chargeAmount.toFixed(2),
service_id: this.service.id,
delivery_id: null, // No delivery for services
// You can add other fields here if your schema requires them
};
// Use the correct endpoint for charging a saved card
const chargePath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/charge/saved-card/${this.customer.id}`;
console.log('=== DEBUG: Charge payload ===');
console.log('Calling endpoint:', chargePath);
console.log('Final payload being sent:', chargePayload);
const response = await axios.post(chargePath, chargePayload, { withCredentials: true, headers: authHeader() });
// Update service cost to the charged amount using new dedicated API
try {
await axios.put(
`${import.meta.env.VITE_BASE_URL}/service/update-cost/${this.service.id}`,
{ service_cost: this.chargeAmount },
{ headers: authHeader(), withCredentials: true }
);
console.log(`✅ Updated service cost to ${this.chargeAmount} for service ${this.service.id}`);
} catch (costError) {
console.error('❌ Failed to update service cost:', costError);
}
// Update payment status after successful charge
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/capture/service/${this.service.id}`, {
card_id: (this.selectedCard as any).id,
status: 3 // Approved status
}, {
headers: authHeader(),
withCredentials: true
});
} catch (updateError) {
console.error('Failed to update payment status after charge:', updateError);
}
// Status codes: 0 = APPROVED, 1 = DECLINED (based on backend TransactionStatus enum)
if (response.data && response.data.status === 0) { // 0 = APPROVED
this.success = `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 {
// The error message from your backend will be more specific now
throw new Error(`Payment charge failed: ${response.data?.rejection_reason || 'Unknown error'}`);
}
}
} catch (error: any) {
console.log(error)
this.error = error.response?.data?.detail || `Failed to ${actionType} payment`
} else {
console.error("API Error: No response data received");
notify({
title: "Error",
text: this.error,
text: "Could not get service data",
type: "error",
})
} finally {
this.loading = false
this.action = ''
});
}
},
})
.catch((err: Error) => {
console.error("API Error in getServiceOrder:", err);
notify({
title: "Error",
text: "Could not get service data",
type: "error",
});
});
}
getServiceTypeName(typeId: number): string {
const typeMap: { [key: number]: string } = { 0: 'Tune-up', 1: 'No Heat', 2: 'Fix', 3: 'Tank Install', 4: 'Other' };
return typeMap[typeId] || 'Unknown';
},
const getCreditCards = (user_id: number) => {
let path = import.meta.env.VITE_BASE_URL + '/payment/cards/' + user_id;
axios({
method: 'get',
url: path,
headers: authHeader(),
}).then((response: AxiosResponse<CreditCardFormData[]>) => {
console.log('Credit cards loaded:', response.data?.length || 0, 'cards');
credit_cards.value = response.data || [];
}).catch((err: Error) => {
console.error('Failed to load credit cards:', err);
credit_cards.value = [];
});
}
getServiceTypeColor(typeId: number): string {
const colorMap: { [key: number]: string } = { 0: 'primary', 1: 'error', 2: 'warning', 3: 'info', 4: 'neutral' };
return `badge-${colorMap[typeId] || 'neutral'}`;
},
const getCustomer = (userid: number) => {
let path = import.meta.env.VITE_BASE_URL + '/customer/' + userid;
axios({
method: 'get',
url: path,
headers: authHeader(),
}).then((response: AxiosResponse<CustomerFormData>) => {
customer.value = response.data
})
}
selectCard(cardId: number) {
this.selectedCardId = cardId;
},
const handlePreauthorize = async () => {
await processPayment('preauthorize')
}
const handleChargeNow = async () => {
await processPayment('charge')
}
const processPayment = async (actionType: string) => {
if (!selectedCard.value) {
error.value = 'No credit card found for this customer'
return
}
formatScheduledDate(dateString: string): string {
if (!dateString) return 'Not scheduled';
return dateString; // Could format with dayjs if needed
loading.value = true
action.value = actionType
error.value = ''
success.value = ''
try {
// Step 2: If payment method is credit, perform the pre-authorization
if (actionType === 'preauthorize') {
if (!chargeAmount.value || chargeAmount.value <= 0) {
throw new Error("Pre-authorization amount must be greater than zero.");
}
const authPayload = {
card_id: selectedCard.value!.id,
preauthorize_amount: chargeAmount.value.toFixed(2),
service_id: service.value.id,
delivery_id: null, // No delivery for services
};
const authPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/authorize/saved-card/${customer.value.id}`;
const response = await axios.post(authPath, authPayload, { withCredentials: true, headers: authHeader() });
// Update payment type to 11 after successful preauthorization
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/authorize/service/${service.value.id}`, {
card_id: selectedCard.value!.id,
status: actionType === 'preauthorize' ? 1 : 3
}, { headers: authHeader() });
} catch (updateError) {
console.error('Failed to update payment type after preauthorization:', updateError);
}
// On successful authorization, show success and redirect
success.value = `Preauthorization successful! Transaction ID: ${response.data?.auth_net_transaction_id || 'N/A'}`;
setTimeout(() => {
router.push({ name: "customerProfile", params: { id: customer.value.id } });
}, 2000);
} else { // Handle 'charge' action
if (!chargeAmount.value || chargeAmount.value <= 0) {
throw new Error("Charge amount must be greater than zero.");
}
// Create a payload that matches the backend's TransactionCreateByCardID schema
const chargePayload = {
card_id: selectedCard.value!.id,
charge_amount: chargeAmount.value.toFixed(2),
service_id: service.value.id,
delivery_id: null, // No delivery for services
// You can add other fields here if your schema requires them
};
// Use the correct endpoint for charging a saved card
const chargePath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/charge/saved-card/${customer.value.id}`;
console.log('=== DEBUG: Charge payload ===');
console.log('Calling endpoint:', chargePath);
console.log('Final payload being sent:', chargePayload);
const response = await axios.post(chargePath, chargePayload, { withCredentials: true, headers: authHeader() });
// Update service cost to the charged amount using new dedicated API
try {
await axios.put(
`${import.meta.env.VITE_BASE_URL}/service/update-cost/${service.value.id}`,
{ service_cost: chargeAmount.value },
{ headers: authHeader(), withCredentials: true }
);
console.log(`✅ Updated service cost to ${chargeAmount.value} for service ${service.value.id}`);
} catch (costError) {
console.error('❌ Failed to update service cost:', costError);
}
// Update payment status after successful charge
try {
await axios.put(`${import.meta.env.VITE_BASE_URL}/payment/capture/service/${service.value.id}`, {
card_id: selectedCard.value!.id,
status: 3 // Approved status
}, {
headers: authHeader(),
withCredentials: true
});
} catch (updateError) {
console.error('Failed to update payment status after charge:', updateError);
}
// Status codes: 0 = APPROVED, 1 = DECLINED (based on backend TransactionStatus enum)
if (response.data && response.data.status === 0) { // 0 = APPROVED
success.value = `Charge successful! Transaction ID: ${response.data.auth_net_transaction_id || 'N/A'}`;
setTimeout(() => {
router.push({ name: "customerProfile", params: { id: customer.value.id } });
}, 2000);
} else {
// The error message from your backend will be more specific now
throw new Error(`Payment charge failed: ${response.data?.rejection_reason || 'Unknown error'}`);
}
}
},
})
} catch (err: unknown) {
const axiosErr = err as AxiosError<{ detail?: string }>;
console.log(err)
error.value = axiosErr.response?.data?.detail || `Failed to ${actionType} payment`
notify({
title: "Error",
text: error.value,
type: "error",
})
} finally {
loading.value = false
action.value = ''
}
}
const getServiceTypeName = (typeId: number): string => {
const typeMap: { [key: number]: string } = { 0: 'Tune-up', 1: 'No Heat', 2: 'Fix', 3: 'Tank Install', 4: 'Other' };
return typeMap[typeId] || 'Unknown';
}
const getServiceTypeColor = (typeId: number): string => {
const colorMap: { [key: number]: string } = { 0: 'primary', 1: 'error', 2: 'warning', 3: 'info', 4: 'neutral' };
return `badge-${colorMap[typeId] || 'neutral'}`;
}
const selectCard = (cardId: number) => {
selectedCardId.value = cardId;
}
const formatScheduledDate = (dateString: string): string => {
if (!dateString) return 'Not scheduled';
return dateString; // Could format with dayjs if needed
}
</script>
<style scoped></style>

View File

@@ -264,7 +264,7 @@ const service = ref<Service | null>(null);
const preAuthCard = ref<UserCard | null>(null);
const selectedCard = ref<UserCard | null>(null);
const chargeAmount = ref<number>(0);
const transaction = ref(null as any);
const transaction = ref<ServiceTransaction | null>(null);
const preAuthAmount = ref<number>(0);
const serviceTransactions = ref<ServiceTransaction[]>([]);
const showPaymentModal = ref(false);
@@ -356,7 +356,8 @@ const updateServiceCost = async (serviceId: number, newCost: number): Promise<bo
console.error(`❌ SERVICE COST UPDATE FAILED:`, response.data);
return false;
}
} catch (error: any) {
} catch (err: unknown) {
const error = err as { response?: { data?: unknown }; message?: string };
console.error(`💥 ERROR UPDATING SERVICE COST:`, error.response?.data || error.message);
return false;
}
@@ -459,7 +460,8 @@ const chargeService = async () => {
throw new Error("Invalid response from server during capture.");
}
} catch (error: any) {
} catch (err: unknown) {
const error = err as { response?: { data?: { detail?: string } }; message?: string };
const detail = error.response?.data?.detail || "Failed to process payment due to a server error.";
notify({ title: "Error", text: detail, type: "error" });
console.error("Charge/Capture Service Error:", error);
@@ -489,7 +491,7 @@ const getTransaction = async () => {
selectedCard.value = cardResponse.data;
chargeAmount.value = preAuthAmount.value;
}
} catch (error: any) {
} catch (error: unknown) {
console.error("No pre-authorized transaction found for service:", error);
preAuthAmount.value = service.value?.service_cost || 0;
}

File diff suppressed because it is too large Load Diff