added popup

This commit is contained in:
2025-09-19 19:08:13 -04:00
parent 481420e795
commit ce86bab1dc
2 changed files with 164 additions and 11 deletions

View File

@@ -152,6 +152,8 @@ export default defineComponent({
user: null,
customer: {} as any,
isLoading: false,
isLoadingAuthorize: true,
authorizeCheck: { profile_exists: false, has_payment_methods: false, missing_components: [] as string[], valid_for_charging: false },
CardForm: {
main_card: false,
card_number: '',
@@ -190,10 +192,36 @@ export default defineComponent({
})
.catch(() => { this.user = null; });
},
async checkAuthorizeAccount() {
if (!this.customer.id) return;
this.isLoadingAuthorize = true;
try {
const path = `${import.meta.env.VITE_AUTHORIZE_URL}/user/check-authorize-account/${this.customer.id}`;
const response = await axios.get(path, { headers: authHeader() });
this.authorizeCheck = response.data;
} catch (error) {
console.error("Failed to check authorize account:", error);
notify({ title: "Error", text: "Could not check payment account status.", type: "error" });
// Set default error state
this.authorizeCheck = {
profile_exists: false,
has_payment_methods: false,
missing_components: ['api_error'],
valid_for_charging: false
};
} finally {
this.isLoadingAuthorize = false;
}
},
getCustomer(user_id: any) {
const path = `${import.meta.env.VITE_BASE_URL}/customer/${user_id}`;
axios.get(path, { withCredentials: true, headers: authHeader() })
.then((response: any) => { this.customer = response.data; })
.then((response: any) => {
this.customer = response.data;
this.checkAuthorizeAccount();
})
.catch(() => {
notify({ title: "Error", text: "Could not find customer", type: "error" });
});
@@ -249,7 +277,17 @@ export default defineComponent({
return; // End the function here
}
// --- STEP 3: BEST-EFFORT CALL - TOKENIZE CARD VIA FASTAPI AND UPDATE LOCAL CARD ---
// --- CHECK IF AUTHORIZE.NET PROFILE EXISTS ---
if (!this.authorizeCheck.profile_exists) {
console.log("Skipping Authorize.Net tokenization as no profile exists for customer.");
// Show success and redirect (card saved locally without tokenization)
notify({ title: "Success", text: "Credit card has been saved.", type: "success" });
this.isLoading = false;
this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
return;
}
// --- STEP 3: BEST-EFFORT CALL - TOKENIZE CARD VIA AUTHORIZE
try {
const fastapiPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/customers/${this.customer.id}/cards`;
console.log("Attempting to tokenize card with Authorize.Net via FastAPI:", fastapiPath);

View File

@@ -140,7 +140,7 @@
<CreditCards
:cards="credit_cards"
:count="credit_cards_count"
:user_id="customer.user_id"
:user_id="customer.user_id || 0"
:auth_net_profile_id="customer.auth_net_profile_id"
@edit-card="editCard"
@remove-card="removeCard"
@@ -207,7 +207,7 @@
<p class="text-lg font-semibold mb-2">Account Created Successfully!</p>
<div class="bg-base-200 p-3 rounded-lg mb-4">
<p class="text-sm mb-1">Authorize.net Profile ID:</p>
<p class="font-mono font-bold text-primary">{{ createdProfileId }}</p>
<p class="font-mono font-bold text-success">{{ createdProfileId }}</p>
</div>
<p class="text-sm text-gray-600">Your payment account is now ready for transactions.</p>
</div>
@@ -215,6 +215,51 @@
</div>
</div>
<!-- Duplicate Account Error Modal -->
<div class="modal" :class="{ 'modal-open': isDuplicateErrorModalVisible }">
<div class="modal-box">
<h3 class="font-bold text-lg text-error">⚠️ Duplicate Account Detected</h3>
<div class="py-4 space-y-4">
<div class="text-center">
<svg class="w-16 h-16 mx-auto mb-4 text-warning" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"/>
</svg>
<p class="text-lg font-semibold">Duplicate Account in Authorize.net</p>
<p class="text-sm text-gray-600 mt-2">
A duplicate account was found in your Authorize.net merchant account.
</p>
<p class="text-sm text-gray-600 mt-2">
Customer ID: <strong>{{ customer.id }}</strong>
</p>
</div>
<div class="bg-base-200 p-4 rounded-lg">
<h4 class="font-semibold mb-2 text-warning">Action Required:</h4>
<ul class="list-disc list-inside text-sm space-y-1">
<li>Manually check your Authorize.net merchant dashboard</li>
<li>Review existing customer profiles</li>
<li>Contact support for linkage if needed</li>
</ul>
<p class="text-xs text-gray-500 mt-2">
Inconsistency between your system and Authorize.net detected.
</p>
</div>
<div class="text-center pt-2">
<p class="text-xs text-gray-500">
This profile may have been created previously and needs manual linking.
</p>
</div>
</div>
<div class="modal-action">
<button class="btn btn-primary" @click="hideDuplicateErrorModal()">
Acknowledge
</button>
</div>
</div>
</div>
</template>
<script lang="ts">
@@ -352,7 +397,7 @@ export default defineComponent({
// --- END OF UPDATES ---
automatic_response: 0,
credit_cards_count: 0,
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: '', customer_latitude: 0, customer_longitude: 0, correct_address: true, account_number: '', auth_net_profile_id: null },
customer: { id: 0, user_id: null as number | null, customer_first_name: '', customer_last_name: '', customer_town: '', customer_address: '', customer_state: 0, customer_zip: '', customer_apt: '', customer_home_type: 0, customer_phone_number: '', customer_latitude: 0, customer_longitude: 0, correct_address: true, account_number: '', auth_net_profile_id: null },
customer_description: { id: 0, customer_id: 0, account_number: '', company_id: '', fill_location: 0, description: '' },
customer_tank: { id: 0, last_tank_inspection: null, tank_status: false, outside_or_inside: false, tank_size: 0 },
customer_stats: { id: 0, customer_id: 0, total_calls: 0, service_calls_total: 0, service_calls_total_spent: 0, service_calls_total_profit: 0, oil_deliveries: 0, oil_total_gallons: 0, oil_total_spent: 0, oil_total_profit: 0 },
@@ -367,6 +412,7 @@ export default defineComponent({
isCreateAccountModalVisible: false,
isCreatingAccount: false,
createdProfileId: '',
isDuplicateErrorModalVisible: false, // Add for duplicate detection popup
}
},
computed: {
@@ -860,6 +906,13 @@ onSubmitSocial(commentText: string) {
const path = `${import.meta.env.VITE_AUTHORIZE_URL}/user/check-authorize-account/${this.customer.id}`;
const response = await axios.get(path, { headers: authHeader() });
this.authorizeCheck = response.data;
// Check if the API returned an error in the response body
if (this.authorizeCheck.missing_components && this.authorizeCheck.missing_components.includes('api_error')) {
console.log("API error detected in response, calling cleanup for customer:", this.customer.id);
this.cleanupAuthorizeData();
return; // Don't set loading to false yet, let cleanup handle it
}
} catch (error) {
console.error("Failed to check authorize account:", error);
notify({ title: "Error", text: "Could not check payment account status.", type: "error" });
@@ -870,6 +923,9 @@ onSubmitSocial(commentText: string) {
missing_components: ['api_error'],
valid_for_charging: false
};
// Automatically cleanup the local Authorize.Net data on API error
console.log("Calling cleanupAuthorurizedData for customer:", this.customer.id);
this.cleanupAuthorizeData();
} finally {
this.isLoadingAuthorize = false;
}
@@ -913,24 +969,60 @@ onSubmitSocial(commentText: string) {
} else {
// Hide modal on error
this.isCreateAccountModalVisible = false;
notify({
title: "Error",
text: response.data.message || "Failed to create Authorize.net account",
type: "error"
});
// Check for E00039 duplicate error
const errorMessage = response.data.message || response.data.error_detail || "Failed to create Authorize.net account";
if (response.data.is_duplicate || errorMessage.includes("E00039")) {
// Show duplicate account popup
setTimeout(() => {
this.showDuplicateErrorModal();
}, 300);
return;
} else {
// Normal error notification
notify({
title: "Error",
text: errorMessage,
type: "error"
});
}
}
} catch (error: any) {
console.error("Failed to create account:", error);
this.isCreateAccountModalVisible = false;
this.isCreatingAccount = false;
// Check for E00039 duplicate error
const errorMessage = error.response?.data?.error_detail ||
error.response?.data?.detail ||
error.response?.data?.message ||
error.message || "Failed to create Authorize.net account";
if (error.response?.data?.is_duplicate || errorMessage.includes("E00039")) {
// Show duplicate account popup
setTimeout(() => {
this.showDuplicateErrorModal();
}, 300);
return;
}
// Normal error notification
notify({
title: "Error",
text: error.response?.data?.detail || "Failed to create Authorize.net account",
text: errorMessage,
type: "error"
});
}
},
// Duplicate Account Error Modal Methods
showDuplicateErrorModal() {
this.isDuplicateErrorModalVisible = true;
},
hideDuplicateErrorModal() {
this.isDuplicateErrorModalVisible = false;
},
addCreditCard() {
// Redirect to add card page
this.$router.push({ name: 'cardadd', params: { customerId: this.customer.id } });
@@ -976,6 +1068,29 @@ onSubmitSocial(commentText: string) {
});
}
},
async cleanupAuthorizeData() {
try {
const path = `${import.meta.env.VITE_BASE_URL}/payment/authorize/cleanup/${this.customer.id}`;
const response = await axios.post(path, {}, { headers: authHeader() });
if (response.data.ok) {
// Update local state to reflect cleanup
this.customer.auth_net_profile_id = null;
this.authorizeCheck.valid_for_charging = false;
this.authorizeCheck.profile_exists = false;
this.authorizeCheck.has_payment_methods = false;
// Refresh credit cards to reflect null payment profile IDs
this.getCreditCards(this.customer.id);
console.log("Successfully cleaned up Authorize.Net data:", response.data.message);
} else {
console.error("Failed to cleanup Authorize.Net data:", response.data.error);
}
} catch (error) {
console.error("Error during cleanup:", error);
}
},
getAccountStatusMessage(): string {
if (!this.authorizeCheck || !this.authorizeCheck.missing_components) {
return 'Account setup incomplete';