2019-09-05 09:00:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers;
|
|
|
|
|
2019-09-17 07:42:10 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
2019-09-08 14:13:55 +02:00
|
|
|
use App\Models\GatewayType;
|
2019-09-13 00:33:48 +02:00
|
|
|
use Stripe\PaymentIntent;
|
2019-09-14 14:34:05 +02:00
|
|
|
use Stripe\SetupIntent;
|
2019-09-05 14:42:26 +02:00
|
|
|
use Stripe\Stripe;
|
|
|
|
|
2019-09-05 09:00:12 +02:00
|
|
|
class StripePaymentDriver extends BasePaymentDriver
|
|
|
|
{
|
2019-09-06 07:22:05 +02:00
|
|
|
protected $refundable = true;
|
2019-09-05 09:00:12 +02:00
|
|
|
|
2019-09-06 07:22:05 +02:00
|
|
|
protected $token_billing = true;
|
|
|
|
|
2019-09-15 13:40:46 +02:00
|
|
|
protected $can_authorise_credit_card = true;
|
|
|
|
|
2019-09-06 07:22:05 +02:00
|
|
|
protected $customer_reference = 'customerReferenceParam';
|
|
|
|
|
2019-09-16 13:16:20 +02:00
|
|
|
/**
|
|
|
|
* Payments
|
|
|
|
* \Stripe\PaymentIntent::create([
|
|
|
|
'payment_method_types' => ['card'],
|
|
|
|
'amount' => 1099,
|
|
|
|
'currency' => 'aud',
|
|
|
|
'customer' => 'cus_Fow2nmVJX1EsQw',
|
|
|
|
'payment_method' => 'card_1FJIAjKmol8YQE9DxWb9kMpR',
|
|
|
|
]);
|
|
|
|
*/
|
|
|
|
|
2019-09-08 14:13:55 +02:00
|
|
|
|
2019-09-05 14:42:26 +02:00
|
|
|
/**
|
|
|
|
* Methods in this class are divided into
|
|
|
|
* two separate streams
|
|
|
|
*
|
|
|
|
* 1. Omnipay Specific
|
|
|
|
* 2. Stripe Specific
|
|
|
|
*
|
|
|
|
* Our Stripe integration is deeper than
|
|
|
|
* other gateways and therefore
|
|
|
|
* relies on direct calls to the API
|
|
|
|
*/
|
2019-09-08 14:13:55 +02:00
|
|
|
/************************************** Stripe API methods **********************************************************/
|
2019-09-05 14:42:26 +02:00
|
|
|
|
2019-09-16 06:59:59 +02:00
|
|
|
public function init()
|
2019-09-08 14:13:55 +02:00
|
|
|
{
|
2019-09-16 06:59:59 +02:00
|
|
|
Stripe::setApiKey($this->company_gateway->getConfigField('23_apiKey'));
|
2019-09-08 14:13:55 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the gateway types
|
|
|
|
*/
|
|
|
|
public function gatewayTypes() :array
|
|
|
|
{
|
|
|
|
$types = [
|
|
|
|
GatewayType::CREDIT_CARD,
|
|
|
|
GatewayType::TOKEN,
|
|
|
|
];
|
|
|
|
|
|
|
|
if($this->company_gateway->getSofortEnabled() && $this->invitation && $this->client() && isset($this->client()->country) && in_array($this->client()->country, ['AUT', 'BEL', 'DEU', 'ITA', 'NLD', 'ESP']))
|
|
|
|
$types[] = GatewayType::SOFORT;
|
|
|
|
|
|
|
|
if($this->company_gateway->getAchEnabled())
|
|
|
|
$types[] = GatewayType::BANK_TRANSFER;
|
|
|
|
|
|
|
|
if ($this->company_gateway->getSepaEnabled())
|
|
|
|
$types[] = GatewayType::SEPA;
|
|
|
|
|
|
|
|
if ($this->company_gateway->getBitcoinEnabled())
|
|
|
|
$types[] = GatewayType::BITCOIN;
|
|
|
|
|
|
|
|
if ($this->company_gateway->getAlipayEnabled())
|
|
|
|
$types[] = GatewayType::ALIPAY;
|
|
|
|
|
|
|
|
if ($this->company_gateway->getApplePayEnabled())
|
|
|
|
$types[] = GatewayType::APPLE_PAY;
|
|
|
|
|
|
|
|
|
|
|
|
return $types;
|
|
|
|
|
2019-09-12 13:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function viewForType($payment_type)
|
|
|
|
{
|
|
|
|
switch ($payment_type) {
|
|
|
|
case GatewayType::CREDIT_CARD:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.credit_card';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
case GatewayType::TOKEN:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.credit_card';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
case GatewayType::SOFORT:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.sofort';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
case GatewayType::BANK_TRANSFER:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.ach';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
case GatewayType::SEPA:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.sepa';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
case GatewayType::BITCOIN:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.other';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
case GatewayType::ALIPAY:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.other';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
case GatewayType::APPLE_PAY:
|
2019-09-14 14:34:05 +02:00
|
|
|
return 'portal.default.gateways.stripe.other';
|
2019-09-12 13:46:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
}
|
2019-09-08 14:13:55 +02:00
|
|
|
}
|
2019-09-13 00:33:48 +02:00
|
|
|
|
2019-09-14 14:34:05 +02:00
|
|
|
public function authorizeCreditCardView($data)
|
|
|
|
{
|
2019-09-16 04:05:30 +02:00
|
|
|
$intent['intent'] = $this->getSetupIntent();
|
2019-09-14 14:34:05 +02:00
|
|
|
|
2019-09-16 13:03:25 +02:00
|
|
|
return view('portal.default.gateways.stripe.create_customer', array_merge($data, $intent));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeCreditCardResponse($request)
|
|
|
|
{
|
2019-09-17 07:42:10 +02:00
|
|
|
\Log::error($request->all());
|
2019-09-16 13:03:25 +02:00
|
|
|
|
2019-09-17 07:42:10 +02:00
|
|
|
$server_response = json_decode($request->input('gateway_response'));
|
2019-09-16 13:03:25 +02:00
|
|
|
|
2019-09-16 04:05:30 +02:00
|
|
|
|
2019-09-17 07:42:10 +02:00
|
|
|
$gateway_id = $request->input('gateway_id');
|
|
|
|
$gateway_type_id = $request->input('payment_method_id');
|
|
|
|
$is_default = $request->input('is_default') ?: 0;
|
|
|
|
|
|
|
|
$payment_method = $server_response->payment_method;
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
$customer = $this->findOrCreateCustomer();
|
|
|
|
|
|
|
|
$stripe_payment_method = \Stripe\PaymentMethod::retrieve($payment_method);
|
|
|
|
$stripe_payment_method->attach(['customer' => $customer->id]);
|
2019-09-16 04:05:30 +02:00
|
|
|
|
2019-09-17 07:42:10 +02:00
|
|
|
$cgt = new ClientGatewayToken;
|
|
|
|
$cgt->company_id = $this->client->company->id;
|
|
|
|
$cgt->client_id = $this->client->id;
|
|
|
|
$cgt->token = $payment_method;
|
|
|
|
$cgt->company_gateway_id = $this->company_gateway->id;
|
|
|
|
$cgt->payment_method_id = $gateway_type_id;
|
|
|
|
$cgt->gateway_customer_reference = $customer->id;
|
|
|
|
$cgt->save();
|
2019-09-14 14:34:05 +02:00
|
|
|
|
2019-09-17 07:42:10 +02:00
|
|
|
|
|
|
|
if($is_default)
|
|
|
|
{
|
2019-09-17 07:59:09 +02:00
|
|
|
$this->client->gateway_tokens()->update(['is_default'=>0]);
|
2019-09-17 07:42:10 +02:00
|
|
|
|
|
|
|
$cgt->is_default = 1;
|
|
|
|
$cgt->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('client.payment_methods.index');
|
2019-09-14 14:34:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 00:33:48 +02:00
|
|
|
/**
|
|
|
|
* Creates a new String Payment Intent
|
|
|
|
* @param array $data The data array to be passed to Stripe
|
|
|
|
* @return PaymentIntent The Stripe payment intent object
|
|
|
|
*/
|
|
|
|
public function createIntent($data)
|
|
|
|
{
|
2019-09-16 06:59:59 +02:00
|
|
|
$this->init();
|
2019-09-13 00:33:48 +02:00
|
|
|
return PaymentIntent::create($data);
|
|
|
|
}
|
2019-09-14 14:34:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a setup intent that allows the user to enter card details without initiating a transaction.
|
|
|
|
*
|
|
|
|
* @return \Stripe\SetupIntent
|
|
|
|
*/
|
|
|
|
public function getSetupIntent()
|
|
|
|
{
|
2019-09-16 06:59:59 +02:00
|
|
|
$this->init();
|
2019-09-14 14:34:05 +02:00
|
|
|
return SetupIntent::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPublishableKey()
|
|
|
|
{
|
|
|
|
return $this->company_gateway->getPublishableKey();
|
|
|
|
}
|
2019-09-16 06:59:59 +02:00
|
|
|
|
|
|
|
public function findOrCreateCustomer() :?\Stripe\Customer
|
|
|
|
{
|
|
|
|
|
|
|
|
$customer = null;
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
2019-09-17 07:59:09 +02:00
|
|
|
$client_gateway_token = ClientGatewayToken::whereClientId($this->client->id)->whereCompanyGatewayId($this->company_gateway->id)->first();
|
2019-09-16 06:59:59 +02:00
|
|
|
|
2019-09-17 07:42:10 +02:00
|
|
|
if($client_gateway_token && $client_gateway_token->gateway_customer_reference)
|
2019-09-16 06:59:59 +02:00
|
|
|
$customer = \Stripe\Customer::retrieve($client_gateway_token->gateway_customer_reference);
|
|
|
|
else{
|
2019-09-17 07:59:09 +02:00
|
|
|
|
|
|
|
$data['name'] = $this->client->present()->name();
|
|
|
|
$data['phone'] = $this->client->present()->phone();
|
|
|
|
|
|
|
|
if(filter_var($this->client->present()->email(), FILTER_VALIDATE_EMAIL))
|
|
|
|
$data['email'] = $this->client->present()->email();
|
|
|
|
|
|
|
|
$customer = \Stripe\Customer::create($data);
|
|
|
|
|
2019-09-16 06:59:59 +02:00
|
|
|
}
|
2019-09-17 07:42:10 +02:00
|
|
|
|
2019-09-16 06:59:59 +02:00
|
|
|
return $customer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-05 14:42:26 +02:00
|
|
|
/************************************** Omnipay API methods **********************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-05 09:00:12 +02:00
|
|
|
}
|