Adding authnet not tested
This commit is contained in:
@@ -469,35 +469,60 @@ export default defineComponent({
|
||||
this.success = ''
|
||||
|
||||
try {
|
||||
const endpoint = actionType === 'preauthorize' ? 'authorize' : 'charge'
|
||||
const payload = {
|
||||
card_number: (this.selectedCard as any).card_number,
|
||||
expiration_date: `${(this.selectedCard as any).expiration_month}${(this.selectedCard as any).expiration_year}`,
|
||||
cvv: (this.selectedCard as any).security_number,
|
||||
[actionType === 'preauthorize' ? 'preauthorize_amount' : 'charge_amount']: this.chargeAmount.toString(),
|
||||
transaction_type: actionType === 'preauthorize' ? 1 : 0,
|
||||
service_id: this.delivery.service_id || null,
|
||||
delivery_id: this.delivery.id,
|
||||
card_id: (this.selectedCard as any).id
|
||||
}
|
||||
// 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.");
|
||||
}
|
||||
|
||||
console.log('=== DEBUG: Payment payload ===')
|
||||
console.log('Delivery ID:', this.delivery.id)
|
||||
console.log('Final payload being sent:', payload)
|
||||
const authPayload = {
|
||||
card_id: (this.selectedCard as any).id,
|
||||
preauthorize_amount: this.chargeAmount.toFixed(2),
|
||||
delivery_id: this.delivery.id,
|
||||
};
|
||||
|
||||
const response = await axios.post(
|
||||
`${import.meta.env.VITE_AUTHORIZE_URL}/api/${endpoint}/?customer_id=${this.customer.id}`,
|
||||
payload,
|
||||
{ withCredentials: true }
|
||||
)
|
||||
const authPath = `${import.meta.env.VITE_AUTHORIZE_URL}/api/payments/authorize/saved-card/${this.customer.id}`;
|
||||
|
||||
if (response.data && response.data.status === 0) {
|
||||
this.success = `${actionType === 'preauthorize' ? 'Preauthorization' : 'Charge'} successful! Transaction ID: ${response.data.auth_net_transaction_id || 'N/A'}`
|
||||
const response = await axios.post(authPath, authPayload, { withCredentials: true, headers: authHeader() });
|
||||
|
||||
// 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 {
|
||||
throw new Error(`Payment ${actionType} failed: ${response.data?.status || 'Unknown error'}`)
|
||||
}, 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/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() });
|
||||
|
||||
// Assuming your backend charge response has the same structure
|
||||
if (response.data && response.data.status === 'APPROVED') { // Adjust based on your actual response
|
||||
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) {
|
||||
this.error = error.response?.data?.detail || `Failed to ${actionType} payment`
|
||||
|
||||
Reference in New Issue
Block a user