diff --git a/app/Http/Livewire/RequiredClientInfo.php b/app/Http/Livewire/RequiredClientInfo.php index 349c839154..3778834bb6 100644 --- a/app/Http/Livewire/RequiredClientInfo.php +++ b/app/Http/Livewire/RequiredClientInfo.php @@ -14,6 +14,7 @@ namespace App\Http\Livewire; use App\Libraries\MultiDB; use App\Models\ClientContact; +use App\Models\CompanyGateway; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; use Livewire\Component; @@ -105,6 +106,8 @@ class RequiredClientInfo extends Component public $company; + public $company_gateway_id; + public function mount() { MultiDB::setDb($this->company->db); @@ -141,6 +144,8 @@ class RequiredClientInfo extends Component 'client_postal_code' => $this->contact->client->postal_code, ]); + //if stripe is enabled, we want to update the customer at this point. + return true; } @@ -150,6 +155,7 @@ class RequiredClientInfo extends Component private function updateClientDetails(array $data): bool { + nlog($this->company->id); $client = []; $contact = []; @@ -172,6 +178,16 @@ class RequiredClientInfo extends Component ->push(); if ($contact_update && $client_update) { + + $cg = CompanyGateway::find($this->company_gateway_id); + + if($cg && $cg->update_details){ + $payment_gateway = $cg->driver($this->client)->init(); + + if(method_exists($payment_gateway, "updateCustomer")) + $payment_gateway->updateCustomer(); + } + return true; } diff --git a/app/PaymentDrivers/Stripe/Jobs/UpdateCustomer.php b/app/PaymentDrivers/Stripe/Jobs/UpdateCustomer.php index a9aadefde5..add84804a9 100644 --- a/app/PaymentDrivers/Stripe/Jobs/UpdateCustomer.php +++ b/app/PaymentDrivers/Stripe/Jobs/UpdateCustomer.php @@ -58,12 +58,12 @@ class UpdateCustomer implements ShouldQueue $company = Company::where('company_key', $this->company_key)->first(); - if($company->id !== config('ninja.ninja_default_company_id')) - return; - $company_gateway = CompanyGateway::find($this->company_gateway_id); $client = Client::withTrashed()->find($this->client_id); + if(!$company_gateway->update_details) + return; + $stripe = $company_gateway->driver($client)->init(); $customer = $stripe->findOrCreateCustomer(); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 4c3303f98b..a46d17ace9 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -511,6 +511,36 @@ class StripePaymentDriver extends BaseDriver } } + public function updateCustomer() + { + if($this->client) + { + + $customer = $this->findOrCreateCustomer(); + //Else create a new record + $data['name'] = $this->client->present()->name(); + $data['phone'] = substr($this->client->present()->phone(), 0, 20); + + $data['address']['line1'] = $this->client->address1; + $data['address']['line2'] = $this->client->address2; + $data['address']['city'] = $this->client->city; + $data['address']['postal_code'] = $this->client->postal_code; + $data['address']['state'] = $this->client->state; + $data['address']['country'] = $this->client->country ? $this->client->country->iso_3166_2 : ''; + + $data['shipping']['name'] = $this->client->present()->name(); + $data['shipping']['address']['line1'] = $this->client->shipping_address1; + $data['shipping']['address']['line2'] = $this->client->shipping_address2; + $data['shipping']['address']['city'] = $this->client->shipping_city; + $data['shipping']['address']['postal_code'] = $this->client->shipping_postal_code; + $data['shipping']['address']['state'] = $this->client->shipping_state; + $data['shipping']['address']['country'] = $this->client->shipping_country ? $this->client->shipping_country->iso_3166_2 : ''; + + \Stripe\Customer::update($customer->id, $data, $this->stripe_connect_auth); + + } + } + public function refund(Payment $payment, $amount, $return_client_response = false) { $this->init(); diff --git a/resources/views/portal/ninja2020/layout/payments.blade.php b/resources/views/portal/ninja2020/layout/payments.blade.php index b21a08abc7..9101bff6c5 100644 --- a/resources/views/portal/ninja2020/layout/payments.blade.php +++ b/resources/views/portal/ninja2020/layout/payments.blade.php @@ -11,7 +11,7 @@ @endpush @section('body') - @livewire('required-client-info', ['fields' => method_exists($gateway, 'getClientRequiredFields') ? $gateway->getClientRequiredFields() : [], 'contact' => auth()->guard('contact')->user(), 'countries' => $countries, 'company' => $company]) + @livewire('required-client-info', ['fields' => method_exists($gateway, 'getClientRequiredFields') ? $gateway->getClientRequiredFields() : [], 'contact' => auth()->guard('contact')->user(), 'countries' => $countries, 'company' => $company, 'company_gateway_id' => $gateway->company_gateway->id])