1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Working on card authorisation for Stripe

This commit is contained in:
David Bomba 2019-09-16 14:59:59 +10:00
parent 6b8cbe4e7c
commit 0331575197
9 changed files with 85 additions and 39 deletions

View File

@ -42,7 +42,7 @@ class PaymentMethodController extends Controller
'token' => false, 'token' => false,
]; ];
return $gateway->driver()->authorizeCreditCardView($data); return $gateway->driver(auth()->user()->client)->authorizeCreditCardView($data);
} }
/** /**

View File

@ -246,7 +246,7 @@ class Client extends BaseModel
foreach($gateways as $gateway) 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; return $gateway;
} }

View File

@ -11,6 +11,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Models\Gateway; use App\Models\Gateway;
use App\Models\GatewayType; use App\Models\GatewayType;
@ -43,11 +44,11 @@ class CompanyGateway extends BaseModel
} }
/* This is the public entry point into the payment superclass */ /* This is the public entry point into the payment superclass */
public function driver() public function driver(Client $client)
{ {
$class = static::driver_class(); $class = static::driver_class();
return new $class($this); return new $class($this, $client);
} }
private function driver_class() private function driver_class()

View File

@ -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'; 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() public function address()
{ {
$str = ''; $str = '';

View File

@ -11,6 +11,7 @@
namespace App\PaymentDrivers; namespace App\PaymentDrivers;
use App\Models\Client;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
use App\Models\GatewayType; use App\Models\GatewayType;
use Omnipay\Omnipay; use Omnipay\Omnipay;
@ -39,10 +40,11 @@ class BasePaymentDriver
protected $can_authorise_credit_card = false; 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->company_gateway = $company_gateway;
$this->invitation = $invitation; $this->invitation = $invitation;
$this->client = $client;
//$this->gatewayType = $gatewayType ?: $this->gatewayTypes()[0]; //$this->gatewayType = $gatewayType ?: $this->gatewayTypes()[0];
} }
@ -70,26 +72,6 @@ class BasePaymentDriver
return $this->gateway->getDefaultParameters(); 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 * Returns the default gateway type
*/ */
@ -118,6 +100,11 @@ class BasePaymentDriver
return $this->token_billing; return $this->token_billing;
} }
public function canAuthoriseCreditCard()
{
return $this->can_authorise_credit_card;
}
/** /**
* Refunds a given payment * Refunds a given payment
* @return void * @return void

View File

@ -40,9 +40,9 @@ class StripePaymentDriver extends BasePaymentDriver
*/ */
/************************************** Stripe API methods **********************************************************/ /************************************** 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 * Returns the gateway types
@ -128,6 +128,7 @@ class StripePaymentDriver extends BasePaymentDriver
*/ */
public function createIntent($data) public function createIntent($data)
{ {
$this->init();
return PaymentIntent::create($data); return PaymentIntent::create($data);
} }
@ -138,8 +139,7 @@ class StripePaymentDriver extends BasePaymentDriver
*/ */
public function getSetupIntent() public function getSetupIntent()
{ {
Stripe::setApiKey($this->company_gateway->getConfigField('23_apiKey')); $this->init();
return SetupIntent::create(); return SetupIntent::create();
} }
@ -147,6 +147,29 @@ class StripePaymentDriver extends BasePaymentDriver
{ {
return $this->company_gateway->getPublishableKey(); 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 **********************************************************/ /************************************** Omnipay API methods **********************************************************/

View File

@ -906,10 +906,11 @@ class CreateUsersTable extends Migration
$table->increments('id'); $table->increments('id');
$table->unsignedInteger('company_id'); $table->unsignedInteger('company_id');
$table->unsignedInteger('client_id')->nullable(); $table->unsignedInteger('client_id')->nullable();
$table->text('token'); $table->text('token')->default('');
$table->unsignedInteger('company_gateway_id'); $table->unsignedInteger('company_gateway_id');
$table->string('gateway_customer_reference')->default('');
$table->unsignedInteger('payment_method_id'); $table->unsignedInteger('payment_method_id');
$table->boolean('is_default'); $table->boolean('is_default')->default(0);
$table->timestamps(6); $table->timestamps(6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
@ -919,7 +920,7 @@ class CreateUsersTable extends Migration
$table->increments('id'); $table->increments('id');
$table->unsignedInteger('company_id'); $table->unsignedInteger('company_id');
$table->unsignedInteger('user_id')->nullable(); $table->unsignedInteger('user_id')->nullable();
$table->string('name'); $table->string('name')->default('');
$table->text('settings'); $table->text('settings');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');

View File

@ -49,7 +49,7 @@
</ul> </ul>
</div> </div>
@include($gateway->driver()->viewForType($payment_method_id)) @include($gateway->driver(auth()->user()->client)->viewForType($payment_method_id))
</div> </div>
</div> </div>

View File

@ -3,12 +3,15 @@
@section('credit_card') @section('credit_card')
<div class="py-md-5 ninja stripe"> <div class="py-md-5 ninja stripe">
<input id="cardholder-name" type="text"> <div class="form-group">
<!-- placeholder for Elements -->
<div id="card-element"></div> <input class="form-control" id="cardholder-name" type="text" placeholder="{{ ctrans('texts.name') }}">
<button id="card-button" data-secret="<?= $intent->client_secret ?>"> <!-- placeholder for Elements -->
Save Card <div id="card-element" class="form-control"></div>
</button> <button id="card-button" class="btn btn-primary pull-right" data-secret="{{ $intent->client_secret }}">
{{ ctrans('texts.save') }}
</button>
</div>
</div> </div>
@endsection @endsection
@ -48,7 +51,33 @@
@push('css') @push('css')
<style type="text/css"> <style type="text/css">
.StripeElement {
box-sizing: border-box;
height: 40px;
padding: 10px 12px;
border: 1px solid transparent;
border-radius: 4px;
background-color: white;
box-shadow: 0 1px 3px 0 #e6ebf1;
-webkit-transition: box-shadow 150ms ease;
transition: box-shadow 150ms ease;
}
.StripeElement--focus {
box-shadow: 0 1px 3px 0 #cfd7df;
}
.StripeElement--invalid {
border-color: #fa755a;
}
.StripeElement--webkit-autofill {
background-color: #fefde5 !important;
}
</style> </style>