From a695cc8f4e460ff200a291cc3b3af3b98dbeb889 Mon Sep 17 00:00:00 2001 From: Cyril Joseph Date: Sun, 7 Sep 2025 21:53:04 -0300 Subject: [PATCH] feedback updates --- src/app/carnet/checkout/checkout.component.ts | 21 ++++++++---- src/app/core/models/carnet/payment-details.ts | 9 +++++ .../core/services/carnet/insurance.service.ts | 9 ----- .../core/services/carnet/payment.service.ts | 34 +++++++++++++++---- 4 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 src/app/core/models/carnet/payment-details.ts delete mode 100644 src/app/core/services/carnet/insurance.service.ts diff --git a/src/app/carnet/checkout/checkout.component.ts b/src/app/carnet/checkout/checkout.component.ts index f8e1926..085d7da 100644 --- a/src/app/carnet/checkout/checkout.component.ts +++ b/src/app/carnet/checkout/checkout.component.ts @@ -9,6 +9,7 @@ import { NotificationService } from '../../core/services/common/notification.ser import { StorageService } from '../../core/services/common/storage.service'; import { finalize } from 'rxjs'; import { AngularMaterialModule } from '../../shared/module/angular-material.module'; +import { PaymentDetail } from '../../core/models/carnet/payment-details'; // This is necessary to tell TypeScript that a global 'paypal' object exists. declare var paypal: any; @@ -30,10 +31,7 @@ export class CheckoutComponent { @ViewChild('paypalcontrol', { static: true }) paypalElement!: ElementRef; - orderDetails = { - price: '', - description: '', - }; + orderDetails: PaymentDetail = {}; orderTotal = 0.00; currency = 'USD'; @@ -47,6 +45,8 @@ export class CheckoutComponent { constructor() { this.currentApplicationDetails = this.storageService.get<{ headerid: number, applicationName: string, applicationType: string }>('currentapplication') + this.orderDetails.headerid = this.currentApplicationDetails?.headerid; + this.orderDetails.applicationName = this.currentApplicationDetails?.applicationName; this.orderDetails.description = ` 'Carnet Application - ${this.currentApplicationDetails?.headerid} - ${this.currentApplicationDetails?.applicationName}` } @@ -99,7 +99,7 @@ export class CheckoutComponent { try { // Call the createOrder method from your service const orderId = await this.paymentService.createOrder(this.orderDetails); - // console.log('Order ID created by server:', orderId); + this.orderDetails.orderId = orderId; return orderId; } catch (error) { this.notificationService.showError('Failed to initiate the payment'); @@ -112,7 +112,7 @@ export class CheckoutComponent { onApprove: async (data: any, actions: any) => { try { // data.orderID is the ID of the transaction from PayPal - const captureDetails = await this.paymentService.completePayment(data.orderID); + await this.paymentService.completePayment(this.orderDetails); // You can now redirect the user or update the UI this.changeInProgress = true; @@ -139,11 +139,20 @@ export class CheckoutComponent { // 3. Handle errors onError: (error: any) => { this.notificationService.showError('Payment transaction failed'); + + this.orderDetails.paymentStatus = 'Failed'; + this.orderDetails.paymentErrorDetails = JSON.stringify(error); + this.paymentService.logTransactionDetails(this.orderDetails); console.error('An error occurred during the transaction:', error); }, // 4. Handle cancellation onCancel: (data: any) => { + this.orderDetails.paymentStatus = 'Cancelled'; + if (this.orderDetails.orderId) { + this.paymentService.logTransactionDetails(this.orderDetails); + } + this.onCancel(); } }).render(this.paypalElement.nativeElement) diff --git a/src/app/core/models/carnet/payment-details.ts b/src/app/core/models/carnet/payment-details.ts new file mode 100644 index 0000000..a056935 --- /dev/null +++ b/src/app/core/models/carnet/payment-details.ts @@ -0,0 +1,9 @@ +export interface PaymentDetail { + headerid?: number; + applicationName?: string; + price?: string; + description?: string; + orderId?: string; + paymentStatus?: string; + paymentErrorDetails?: string; +} diff --git a/src/app/core/services/carnet/insurance.service.ts b/src/app/core/services/carnet/insurance.service.ts deleted file mode 100644 index 1162fe9..0000000 --- a/src/app/core/services/carnet/insurance.service.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Injectable } from '@angular/core'; - -@Injectable({ - providedIn: 'root' -}) -export class InsuranceService { - - constructor() { } -} diff --git a/src/app/core/services/carnet/payment.service.ts b/src/app/core/services/carnet/payment.service.ts index 0635983..6dad20b 100644 --- a/src/app/core/services/carnet/payment.service.ts +++ b/src/app/core/services/carnet/payment.service.ts @@ -2,6 +2,8 @@ import { inject, Injectable } from '@angular/core'; import { environment } from '../../../../environments/environment'; import { HttpClient } from '@angular/common/http'; import { firstValueFrom } from 'rxjs'; +import { PaymentDetail } from '../../models/carnet/payment-details'; +import { UserService } from '../common/user.service'; @Injectable({ providedIn: 'root' @@ -11,11 +13,14 @@ export class PaymentService { private apiDb = environment.apiDb; private http = inject(HttpClient); + private userService = inject(UserService); - createOrder(orderDetails: any): Promise { + createOrder(paymentDetails: PaymentDetail): Promise { const data = { - P_PRICE: orderDetails.price, - P_DESCRIPTION: orderDetails.description + P_APPLICATIONNAME: paymentDetails.applicationName, + P_PRICE: paymentDetails.price, + P_DESCRIPTION: paymentDetails.description, + P_USERID: this.userService.getUser() } return firstValueFrom( @@ -23,12 +28,29 @@ export class PaymentService { ).then(order => order.id); } - completePayment(orderId: string): Promise { + completePayment(paymentDetails: PaymentDetail): Promise { const data = { - P_ORDERID: orderId + P_ORDERID: paymentDetails.orderId, + P_APPLICATIONNAME: paymentDetails.applicationName, + P_PRICE: paymentDetails.price, + P_USERID: this.userService.getUser() } return firstValueFrom( - this.http.post(`${this.apiUrl}/oracle/CompletePayment`, data)); + this.http.post(`${this.apiUrl}/${this.apiDb}/CompletePayment`, data)); + } + + logTransactionDetails(paymentDetails: PaymentDetail): Promise { + const data = { + P_APPLICATIONNAME: paymentDetails.applicationName, + P_ORDERID: paymentDetails.orderId, + P_PAYMENTAMOUNT: paymentDetails.price, + P_STATUS: paymentDetails.paymentStatus, + P_USERID: this.userService.getUser(), + P_PAYMENTERROR: paymentDetails.paymentErrorDetails + } + + return firstValueFrom( + this.http.post(`${this.apiUrl}/${this.apiDb}/SaveHistory`, data)); } } \ No newline at end of file