feat(purchase-status): enhance purchase status handling with polling and improved UI feedback
This commit is contained in:
@@ -12,6 +12,11 @@ export interface CreatePurchasePayload {
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface PurchaseStatusResponse {
|
||||
payment_status: string | null;
|
||||
status: string | null;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -20,12 +25,16 @@ export class CheckoutService {
|
||||
|
||||
async createPurchase(tenantCode: string, payload: CreatePurchasePayload): Promise<{ id: number }> {
|
||||
const response = await firstValueFrom(
|
||||
this.http.post<{ data: { id: number } }>(`${environment.url}tenants/${tenantCode}/compras`, payload)
|
||||
this.http.post<{ data?: { id: number }; id?: number }>(`${environment.url}tenants/${tenantCode}/compras`, payload)
|
||||
);
|
||||
if (!response?.data) {
|
||||
|
||||
const purchase = this.extractResponseData<{ id: number }>(response);
|
||||
|
||||
if (!purchase?.id) {
|
||||
throw new Error('Error al crear la compra.');
|
||||
}
|
||||
return response.data;
|
||||
|
||||
return purchase;
|
||||
}
|
||||
|
||||
async generatePaymentIntent(tenantCode: string, purchaseId: number, method: 'qr' | 'transfer' | 'telepagos'): Promise<any> {
|
||||
@@ -38,23 +47,49 @@ export class CheckoutService {
|
||||
return response;
|
||||
}
|
||||
|
||||
async finalizePurchase(tenantCode: string, purchaseId: number): Promise<{ payment_status: string; status: string }> {
|
||||
async finalizePurchase(tenantCode: string, purchaseId: number): Promise<PurchaseStatusResponse> {
|
||||
const response = await firstValueFrom(
|
||||
this.http.post<{ data: { payment_status: string; status: string } }>(`${environment.url}tenants/${tenantCode}/compras/${purchaseId}/finalize`, {})
|
||||
this.http.post<{ data?: PurchaseStatusResponse } | PurchaseStatusResponse>(`${environment.url}tenants/${tenantCode}/compras/${purchaseId}/finalize`, {})
|
||||
);
|
||||
if (!response?.data) {
|
||||
|
||||
const purchase = this.extractResponseData<PurchaseStatusResponse>(response);
|
||||
|
||||
if (!purchase) {
|
||||
throw new Error('Error al finalizar la compra.');
|
||||
}
|
||||
return response.data;
|
||||
|
||||
return {
|
||||
payment_status: purchase.payment_status ?? null,
|
||||
status: purchase.status ?? null
|
||||
};
|
||||
}
|
||||
|
||||
async getPurchase(tenantCode: string, purchaseId: string | number): Promise<{ payment_status: string }> {
|
||||
async getPurchase(tenantCode: string, purchaseId: string | number): Promise<PurchaseStatusResponse> {
|
||||
const response = await firstValueFrom(
|
||||
this.http.get<{ data: { payment_status: string } }>(`${environment.url}tenants/${tenantCode}/compras/${purchaseId}`)
|
||||
this.http.get<{ data?: PurchaseStatusResponse } | PurchaseStatusResponse>(`${environment.url}tenants/${tenantCode}/compras/${purchaseId}`)
|
||||
);
|
||||
if (!response?.data) {
|
||||
|
||||
const purchase = this.extractResponseData<PurchaseStatusResponse>(response);
|
||||
|
||||
if (!purchase) {
|
||||
throw new Error('Error al obtener la compra.');
|
||||
}
|
||||
return response.data;
|
||||
|
||||
return {
|
||||
payment_status: purchase.payment_status ?? null,
|
||||
status: purchase.status ?? null
|
||||
};
|
||||
}
|
||||
|
||||
private extractResponseData<T>(response: T | { data?: T } | null | undefined): T | null {
|
||||
if (!response) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof response === 'object' && 'data' in response && response.data) {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
return response as T;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user