diff --git a/src/services/Customers.php b/src/services/Customers.php index 7631961..969e970 100644 --- a/src/services/Customers.php +++ b/src/services/Customers.php @@ -1,4 +1,5 @@ getApi(); $customers = $api->fetchAllCustomers(); + $stripeIds = []; + $count = 0; foreach ($customers as $customer) { + $stripeIds[] = $customer->id; if ($this->createOrUpdateCustomer($customer)) { $count++; } } + // Delete non existent customers + $customerDataRecord = CustomerDataRecord::find()->where(['not in', 'stripeId', $stripeIds])->all(); + + foreach ($customerDataRecord as $record) { + $record->delete(); + } + return $count; } diff --git a/src/services/Invoices.php b/src/services/Invoices.php index cffe86f..e793882 100644 --- a/src/services/Invoices.php +++ b/src/services/Invoices.php @@ -58,13 +58,23 @@ public function syncAllInvoices(): int $api = Plugin::getInstance()->getApi(); $invoices = $api->fetchAllInvoices(); + $stripeIds = []; + $count = 0; foreach ($invoices as $invoice) { + $stripeIds[] = $invoice->id; if ($this->createOrUpdateInvoice($invoice)) { $count++; } } + // Delete non existent invoices + $invoiceDataRecord = InvoiceDataRecord::find()->where(['not in', 'stripeId', $stripeIds])->all(); + + foreach ($invoiceDataRecord as $record) { + $record->delete(); + } + return $count; }