diff --git a/app/Http/Controllers/ClientPortal/PaymentMethodController.php b/app/Http/Controllers/ClientPortal/PaymentMethodController.php index 629076c6ba..ddc2b5a93c 100644 --- a/app/Http/Controllers/ClientPortal/PaymentMethodController.php +++ b/app/Http/Controllers/ClientPortal/PaymentMethodController.php @@ -42,7 +42,7 @@ class PaymentMethodController extends Controller 'token' => false, ]; - return $gateway->driver()->authorizeCreditCardView($data); + return $gateway->driver(auth()->user()->client)->authorizeCreditCardView($data); } /** diff --git a/app/Models/Client.php b/app/Models/Client.php index c07d9af6ea..6e7d970f44 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -246,7 +246,7 @@ class Client extends BaseModel foreach($gateways as $gateway) { - if(in_array(GatewayType::CREDIT_CARD, $gateway->driver()->gatewayTypes())) + if(in_array(GatewayType::CREDIT_CARD, $gateway->driver($this)->gatewayTypes())) return $gateway; } diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 46d4ac4f7a..9a091ff65d 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -11,6 +11,7 @@ namespace App\Models; +use App\Models\Client; use App\Models\Company; use App\Models\Gateway; use App\Models\GatewayType; @@ -43,11 +44,11 @@ class CompanyGateway extends BaseModel } /* This is the public entry point into the payment superclass */ - public function driver() + public function driver(Client $client) { $class = static::driver_class(); - return new $class($this); + return new $class($this, $client); } private function driver_class() diff --git a/app/Models/Presenters/ClientPresenter.php b/app/Models/Presenters/ClientPresenter.php index 5f35521b97..8e32801a49 100644 --- a/app/Models/Presenters/ClientPresenter.php +++ b/app/Models/Presenters/ClientPresenter.php @@ -31,6 +31,11 @@ class ClientPresenter extends EntityPresenter return $this->entity->primary_contact->first() !== null ? $this->entity->primary_contact->first()->first_name . ' '. $this->entity->primary_contact->first()->last_name : 'No primary contact set'; } + public function email() + { + return $this->entity->primary_contact->first() !== null ? $this->entity->primary_contact->first()->email : 'No Email Set'; + } + public function address() { $str = ''; diff --git a/app/PaymentDrivers/BasePaymentDriver.php b/app/PaymentDrivers/BasePaymentDriver.php index 7638104528..67ab746482 100644 --- a/app/PaymentDrivers/BasePaymentDriver.php +++ b/app/PaymentDrivers/BasePaymentDriver.php @@ -11,6 +11,7 @@ namespace App\PaymentDrivers; +use App\Models\Client; use App\Models\CompanyGateway; use App\Models\GatewayType; use Omnipay\Omnipay; @@ -39,10 +40,11 @@ class BasePaymentDriver protected $can_authorise_credit_card = false; - public function __construct(CompanyGateway $company_gateway, $invitation = false) + public function __construct(CompanyGateway $company_gateway, Client $client, $invitation = false) { $this->company_gateway = $company_gateway; $this->invitation = $invitation; + $this->client = $client; //$this->gatewayType = $gatewayType ?: $this->gatewayTypes()[0]; } @@ -70,26 +72,6 @@ class BasePaymentDriver return $this->gateway->getDefaultParameters(); } - public function invoice() - { - return $this->invitation->invoice; - } - - public function contact() - { - return $this->invitation->contact; - } - - public function client() - { - return $this->contact()->client; - } - - public function company() - { - return $this->invitation->company; - } - /** * Returns the default gateway type */ @@ -118,6 +100,11 @@ class BasePaymentDriver return $this->token_billing; } + public function canAuthoriseCreditCard() + { + return $this->can_authorise_credit_card; + } + /** * Refunds a given payment * @return void diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 1716da30ca..5bd6e8eaaf 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -40,9 +40,9 @@ class StripePaymentDriver extends BasePaymentDriver */ /************************************** Stripe API methods **********************************************************/ - public function init($api_key) + public function init() { - Stripe::setApiKey($api_key); + Stripe::setApiKey($this->company_gateway->getConfigField('23_apiKey')); } /** * Returns the gateway types @@ -128,6 +128,7 @@ class StripePaymentDriver extends BasePaymentDriver */ public function createIntent($data) { + $this->init(); return PaymentIntent::create($data); } @@ -138,8 +139,7 @@ class StripePaymentDriver extends BasePaymentDriver */ public function getSetupIntent() { - Stripe::setApiKey($this->company_gateway->getConfigField('23_apiKey')); - + $this->init(); return SetupIntent::create(); } @@ -147,6 +147,29 @@ class StripePaymentDriver extends BasePaymentDriver { return $this->company_gateway->getPublishableKey(); } + + public function findOrCreateCustomer() :?\Stripe\Customer + { + + $customer = null; + + $this->init(); + + $client_gateway_token = $this->client->gateway_tokens->whereGatewayId($this->company_gateway->gateway_id)->first(); + + if($client_gateway_token->gateway_customer_reference) + $customer = \Stripe\Customer::retrieve($client_gateway_token->gateway_customer_reference); + else{ + $customer = \Stripe\Customer::create([ + "email" => $this->client->present()->email(), + "name" => $this->client->present()->name(), + "phone" => $this->client->present()->phone(), + ]); + } + return $customer; + } + + /************************************** Omnipay API methods **********************************************************/ diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index d1c56b11da..ff76ffe43b 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -906,10 +906,11 @@ class CreateUsersTable extends Migration $table->increments('id'); $table->unsignedInteger('company_id'); $table->unsignedInteger('client_id')->nullable(); - $table->text('token'); + $table->text('token')->default(''); $table->unsignedInteger('company_gateway_id'); + $table->string('gateway_customer_reference')->default(''); $table->unsignedInteger('payment_method_id'); - $table->boolean('is_default'); + $table->boolean('is_default')->default(0); $table->timestamps(6); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); @@ -919,7 +920,7 @@ class CreateUsersTable extends Migration $table->increments('id'); $table->unsignedInteger('company_id'); $table->unsignedInteger('user_id')->nullable(); - $table->string('name'); + $table->string('name')->default(''); $table->text('settings'); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); diff --git a/resources/views/portal/default/gateways/pay_now.blade.php b/resources/views/portal/default/gateways/pay_now.blade.php index 0d34108f77..ac14ee0f06 100644 --- a/resources/views/portal/default/gateways/pay_now.blade.php +++ b/resources/views/portal/default/gateways/pay_now.blade.php @@ -49,7 +49,7 @@ - @include($gateway->driver()->viewForType($payment_method_id)) + @include($gateway->driver(auth()->user()->client)->viewForType($payment_method_id)) diff --git a/resources/views/portal/default/gateways/stripe/create_customer.blade.php b/resources/views/portal/default/gateways/stripe/create_customer.blade.php index 4f917578c0..2d7b12528e 100644 --- a/resources/views/portal/default/gateways/stripe/create_customer.blade.php +++ b/resources/views/portal/default/gateways/stripe/create_customer.blade.php @@ -3,12 +3,15 @@ @section('credit_card')
- - -
- +
+ + + +
+ +
@endsection @@ -48,7 +51,33 @@ @push('css')