diff --git a/app/Http/Controllers/StripeController.php b/app/Http/Controllers/StripeController.php index 2e4227d81a..61d412a1c2 100644 --- a/app/Http/Controllers/StripeController.php +++ b/app/Http/Controllers/StripeController.php @@ -14,6 +14,7 @@ namespace App\Http\Controllers; use App\Jobs\Util\ImportStripeCustomers; use App\Jobs\Util\StripeUpdatePaymentMethods; +use App\Libraries\MultiDB; use App\Models\Client; use App\Models\CompanyGateway; @@ -60,6 +61,8 @@ class StripeController extends BaseController if(auth()->user()->isAdmin()) { + MultiDB::findAndSetDbByCompanyKey(auth()->user()->company()->company_key); + $company_gateway = CompanyGateway::where('company_id', auth()->user()->company()->id) ->where('is_deleted',0) ->whereIn('gateway_key', $this->stripe_keys) diff --git a/app/Jobs/Cron/RecurringInvoicesCron.php b/app/Jobs/Cron/RecurringInvoicesCron.php index ae993e180e..51b8bb874e 100644 --- a/app/Jobs/Cron/RecurringInvoicesCron.php +++ b/app/Jobs/Cron/RecurringInvoicesCron.php @@ -46,6 +46,7 @@ class RecurringInvoicesCron $recurring_invoices = RecurringInvoice::where('next_send_date', '<=', now()->toDateTimeString()) ->whereNotNull('next_send_date') ->whereNull('deleted_at') + ->where('is_deleted', false) ->where('status_id', RecurringInvoice::STATUS_ACTIVE) ->where('remaining_cycles', '!=', '0') ->whereHas('client', function ($query) { @@ -61,7 +62,13 @@ class RecurringInvoicesCron nlog("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date); if (!$recurring_invoice->company->is_disabled) { - SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db); + + try{ + SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db); + } + catch(\Exception $e){ + nlog("Unable to sending recurring invoice {$recurring_invoice->id}"); + } } }); } else { @@ -72,6 +79,7 @@ class RecurringInvoicesCron $recurring_invoices = RecurringInvoice::where('next_send_date', '<=', now()->toDateTimeString()) ->whereNotNull('next_send_date') ->whereNull('deleted_at') + ->where('is_deleted', false) ->where('status_id', RecurringInvoice::STATUS_ACTIVE) ->where('remaining_cycles', '!=', '0') ->whereHas('client', function ($query) { @@ -87,7 +95,13 @@ class RecurringInvoicesCron nlog("Current date = " . now()->format("Y-m-d") . " Recurring date = " .$recurring_invoice->next_send_date ." Recurring #id = ". $recurring_invoice->id); if (!$recurring_invoice->company->is_disabled) { - SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db); + + try{ + SendRecurring::dispatchNow($recurring_invoice, $recurring_invoice->company->db); + } + catch(\Exception $e){ + nlog("Unable to sending recurring invoice {$recurring_invoice->id} on db {$db}"); + } } }); } diff --git a/app/PaymentDrivers/Stripe/ImportCustomers.php b/app/PaymentDrivers/Stripe/ImportCustomers.php index 2baf26d5bb..88c1372cbf 100644 --- a/app/PaymentDrivers/Stripe/ImportCustomers.php +++ b/app/PaymentDrivers/Stripe/ImportCustomers.php @@ -52,7 +52,7 @@ class ImportCustomers $this->update_payment_methods = new UpdatePaymentMethods($this->stripe); if(strlen($this->stripe->company_gateway->getConfigField('account_id')) < 1) - throw new StripeConnectFailure('Stripe Connect has not been configured'); + throw new StripeConnectFailure('Stripe Connect has not been configured'); $customers = Customer::all([], $this->stripe->stripe_connect_auth); @@ -83,7 +83,14 @@ class ImportCustomers ->exists(); if($existing_customer){ - nlog("Skipping - Customer exists: {$customer->email}"); + nlog("Skipping - Customer exists: {$customer->email} just updating payment methods"); + $this->update_payment_methods->updateMethods($customer, $contact->client); + return; + } + + if($customer->email && $contact = $this->stripe->company_gateway->company->client_contacts()->where('email', $customer->email)->first()){ + nlog("Customer exists: {$customer->email} just updating payment methods"); + $this->update_payment_methods->updateMethods($customer, $contact->client); return; } @@ -92,15 +99,15 @@ class ImportCustomers $client = ClientFactory::create($this->stripe->company_gateway->company_id, $this->stripe->company_gateway->user_id); - if(property_exists($customer, 'address')) + if($customer->address) { - $client->address1 = property_exists($customer->address, 'line1') ? $customer->address->line1 : ''; - $client->address2 = property_exists($customer->address, 'line2') ? $customer->address->line2 : ''; - $client->city = property_exists($customer->address, 'city') ? $customer->address->city : ''; - $client->state = property_exists($customer->address, 'state') ? $customer->address->state : ''; - $client->phone = property_exists($customer->address, 'phone') ? $customer->phone : ''; + $client->address1 = $customer->address->line1 ? $customer->address->line1 : ''; + $client->address2 = $customer->address->line2 ? $customer->address->line2 : ''; + $client->city = $customer->address->city ? $customer->address->city : ''; + $client->state = $customer->address->state ? $customer->address->state : ''; + $client->phone = $customer->address->phone ? $customer->phone : ''; - if(property_exists($customer->address, 'country')){ + if($customer->address->country){ $country = Country::where('iso_3166_2', $customer->address->country)->first(); @@ -124,7 +131,7 @@ class ImportCustomers } - $client->name = property_exists($customer, 'name') ? $customer->name : $customer->email; + $client->name = $customer->name ? $customer->name : $customer->email; if (!isset($client->number) || empty($client->number)) { $client->number = $this->getNextClientNumber($client);