major claude changes
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user