152 lines
4.4 KiB
TypeScript
152 lines
4.4 KiB
TypeScript
// src/stores/payment.js
|
|
import { defineStore } from 'pinia';
|
|
|
|
export const usePaymentStore = defineStore('payment', {
|
|
state: () => ({
|
|
currentStep: 1,
|
|
patientInfo: {
|
|
name: '',
|
|
amount: '',
|
|
expiry: '',
|
|
},
|
|
qrData: {
|
|
qrvalue: null,
|
|
display_nobill: null,
|
|
display_name: null,
|
|
display_amount: null,
|
|
status: null, // Tambahkan status untuk tracking
|
|
ip: null, // Tambahkan IP untuk reference
|
|
},
|
|
}),
|
|
|
|
getters: {
|
|
hasQrData: (state) => state.qrData && state.qrData.qrvalue,
|
|
safeQrValue: (state) => state.qrData?.qrvalue || 'https://www.google.com',
|
|
|
|
// Getter untuk cek apakah data siap untuk polling status
|
|
isReadyForStatusCheck: (state) => {
|
|
return state.qrData.display_nobill && state.qrData.display_name;
|
|
},
|
|
|
|
// Getter untuk informasi debug
|
|
debugInfo: (state) => ({
|
|
step: state.currentStep,
|
|
hasIdentifiers: !!(state.qrData.display_nobill && state.qrData.display_name),
|
|
qrData: state.qrData,
|
|
patientInfo: state.patientInfo,
|
|
})
|
|
},
|
|
|
|
actions: {
|
|
nextStep() {
|
|
this.currentStep++;
|
|
},
|
|
prevStep() {
|
|
this.currentStep--;
|
|
},
|
|
|
|
setStep(step) {
|
|
console.log(`Setting step from ${this.currentStep} to ${step}`);
|
|
this.currentStep = step;
|
|
},
|
|
|
|
reset() {
|
|
console.log('Resetting payment store');
|
|
this.currentStep = 1;
|
|
this.patientInfo = {
|
|
name: '',
|
|
amount: '',
|
|
expiry: '',
|
|
};
|
|
this.qrData = {
|
|
qrvalue: null,
|
|
display_nobill: null,
|
|
display_name: null,
|
|
display_amount: null,
|
|
status: null,
|
|
ip: null,
|
|
};
|
|
},
|
|
|
|
updatePayment(apiResponse) {
|
|
console.log("=== UPDATE PAYMENT ===");
|
|
console.log("API Response:", apiResponse);
|
|
|
|
// Validasi response
|
|
if (!apiResponse?.data || !Array.isArray(apiResponse.data) || apiResponse.data.length === 0) {
|
|
console.error("Invalid API response structure");
|
|
return;
|
|
}
|
|
|
|
const apiData = apiResponse.data[0];
|
|
console.log("API Data:", apiData);
|
|
|
|
// Simpan data lama untuk comparison
|
|
const oldQrData = { ...this.qrData };
|
|
|
|
// Update qrData dengan semua properti dari API
|
|
this.qrData = {
|
|
qrvalue: apiData.qrvalue || apiData.qr_code || this.qrData.qrvalue,
|
|
display_nobill: apiData.display_nobill || this.qrData.display_nobill,
|
|
display_name: apiData.display_name || this.qrData.display_name,
|
|
display_amount: apiData.display_amount || apiData.nominal || this.qrData.display_amount,
|
|
status: apiData.status || this.qrData.status,
|
|
ip: apiData.ip || this.qrData.ip,
|
|
// Spread untuk properti lain yang mungkin ada
|
|
...apiData
|
|
};
|
|
|
|
// Update patientInfo
|
|
this.patientInfo = {
|
|
name: apiData.display_name || this.patientInfo.name || 'Unknown',
|
|
amount: apiData.display_amount || apiData.nominal || this.patientInfo.amount || '0',
|
|
expiry: this.patientInfo.expiry,
|
|
};
|
|
|
|
// Log changes
|
|
if (oldQrData.status !== this.qrData.status) {
|
|
console.log(`Status changed: ${oldQrData.status} -> ${this.qrData.status}`);
|
|
}
|
|
|
|
console.log("Updated qrData:", this.qrData);
|
|
console.log("Updated patientInfo:", this.patientInfo);
|
|
console.log("===================");
|
|
},
|
|
|
|
// Method untuk debug
|
|
debugCurrentState() {
|
|
console.log("=== PAYMENT STORE DEBUG ===");
|
|
console.log("Current Step:", this.currentStep);
|
|
console.log("Patient Info:", this.patientInfo);
|
|
console.log("QR Data:", this.qrData);
|
|
console.log("Has QR Data:", this.hasQrData);
|
|
console.log("Ready for Status Check:", this.isReadyForStatusCheck);
|
|
console.log("Safe QR Value:", this.safeQrValue);
|
|
console.log("==========================");
|
|
},
|
|
|
|
// Method untuk validasi data
|
|
validateData() {
|
|
const issues = [];
|
|
|
|
if (!this.qrData.display_nobill) {
|
|
issues.push('display_nobill missing');
|
|
}
|
|
|
|
if (!this.qrData.display_name) {
|
|
issues.push('display_name missing');
|
|
}
|
|
|
|
if (!this.qrData.qrvalue && !this.qrData.qr_code) {
|
|
issues.push('QR code missing');
|
|
}
|
|
|
|
if (issues.length > 0) {
|
|
console.warn('Data validation issues:', issues);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
},
|
|
}); |