1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/PaymentDrivers/StripePaymentDriver.php

531 lines
17 KiB
PHP
Raw Normal View History

2019-09-05 09:00:12 +02:00
<?php
2020-06-24 16:07:12 +02:00
2019-09-05 09:00:12 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-09-05 09:00:12 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-09-05 09:00:12 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-09-05 09:00:12 +02:00
*/
namespace App\PaymentDrivers;
use App\Exceptions\PaymentFailed;
use App\Factory\PaymentFactory;
2020-06-27 17:39:28 +02:00
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Http\Requests\Request;
use App\Jobs\Util\SystemLogger;
2019-09-17 07:42:10 +02:00
use App\Models\ClientGatewayToken;
2019-09-08 14:13:55 +02:00
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\SystemLog;
2021-01-19 16:07:58 +01:00
use App\PaymentDrivers\Stripe\ACH;
use App\PaymentDrivers\Stripe\Alipay;
2020-07-14 14:50:16 +02:00
use App\PaymentDrivers\Stripe\Charge;
use App\PaymentDrivers\Stripe\CreditCard;
use App\PaymentDrivers\Stripe\ImportCustomers;
use App\PaymentDrivers\Stripe\SOFORT;
2021-05-17 06:02:43 +02:00
use App\PaymentDrivers\Stripe\UpdatePaymentMethods;
2020-06-01 14:17:29 +02:00
use App\PaymentDrivers\Stripe\Utilities;
use App\Utils\Traits\MakesHash;
use Exception;
use Illuminate\Support\Carbon;
2021-05-12 05:00:46 +02:00
use Stripe\Account;
2020-10-28 11:10:49 +01:00
use Stripe\Customer;
use Stripe\Exception\ApiErrorException;
2019-09-13 00:33:48 +02:00
use Stripe\PaymentIntent;
use Stripe\PaymentMethod;
2019-09-14 14:34:05 +02:00
use Stripe\SetupIntent;
2019-09-05 14:42:26 +02:00
use Stripe\Stripe;
2020-10-28 11:10:49 +01:00
use Stripe\StripeClient;
2019-09-05 14:42:26 +02:00
class StripePaymentDriver extends BaseDriver
2019-09-05 09:00:12 +02:00
{
2020-06-01 14:17:29 +02:00
use MakesHash, Utilities;
2020-07-15 07:05:02 +02:00
public $refundable = true;
2019-09-05 09:00:12 +02:00
2020-07-15 07:05:02 +02:00
public $token_billing = true;
2020-07-15 07:05:02 +02:00
public $can_authorise_credit_card = true;
2019-09-15 13:40:46 +02:00
2020-09-18 14:35:53 +02:00
/** @var \Stripe\StripeClient */
public $stripe;
2020-09-18 14:35:53 +02:00
protected $customer_reference = 'customerReferenceParam';
public $payment_method;
2020-06-01 14:03:18 +02:00
2021-04-20 13:30:52 +02:00
public $stripe_connect = false;
public $stripe_connect_auth = [];
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class,
2021-01-19 16:07:58 +01:00
GatewayType::BANK_TRANSFER => ACH::class,
GatewayType::ALIPAY => Alipay::class,
GatewayType::SOFORT => SOFORT::class,
GatewayType::APPLE_PAY => 1, // TODO
GatewayType::SEPA => 1, // TODO
];
const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE;
2019-09-17 12:27:48 +02:00
/**
* Initializes the Stripe API.
2019-09-17 12:27:48 +02:00
* @return void
*/
2020-06-24 16:07:12 +02:00
public function init(): void
{
2021-04-20 13:30:52 +02:00
if($this->stripe_connect)
{
Stripe::setApiKey(config('ninja.ninja_stripe_key'));
2021-04-20 13:30:52 +02:00
$this->stripe_connect_auth = ["stripe_account" => $this->company_gateway->getConfigField('account_id')];
}
else
{
$this->stripe = new StripeClient(
$this->company_gateway->getConfigField('apiKey')
);
2020-09-18 14:35:53 +02:00
2021-04-20 13:30:52 +02:00
Stripe::setApiKey($this->company_gateway->getConfigField('apiKey'));
}
}
2019-09-17 12:27:48 +02:00
public function setPaymentMethod($payment_method_id)
2020-06-01 14:03:18 +02:00
{
$class = self::$methods[$payment_method_id];
2020-06-24 16:07:12 +02:00
$this->payment_method = new $class($this);
2020-06-01 14:03:18 +02:00
return $this;
}
/**
* Returns the gateway types.
*/
2020-06-24 16:07:12 +02:00
public function gatewayTypes(): array
2019-09-08 14:13:55 +02:00
{
$types = [
GatewayType::CREDIT_CARD,
GatewayType::CRYPTO,
2021-01-27 12:00:42 +01:00
// GatewayType::SEPA, // TODO: Missing implementation
// GatewayType::APPLE_PAY, // TODO:: Missing implementation
2019-09-08 14:13:55 +02:00
];
2020-03-23 18:10:42 +01:00
2021-03-22 12:18:57 +01:00
if ($this->client
&& isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['AUT', 'BEL', 'DEU', 'ITA', 'NLD', 'ESP'])) {
2019-09-08 14:13:55 +02:00
$types[] = GatewayType::SOFORT;
}
2019-09-08 14:13:55 +02:00
2021-03-22 12:18:57 +01:00
if ($this->client
&& isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['USA'])) {
$types[] = GatewayType::BANK_TRANSFER;
}
2021-03-22 12:18:57 +01:00
if ($this->client
&& isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['AUS', 'DNK', 'DEU', 'ITA', 'LUX', 'NOR', 'SVN', 'GBR', 'AUT', 'EST', 'GRC', 'JPN', 'MYS', 'PRT', 'ESP', 'USA', 'BEL', 'FIN', 'HKG', 'LVA', 'NLD', 'SGP', 'SWE', 'CAN', 'FRA', 'IRL', 'LTU', 'NZL', 'SVK', 'CHE'])) {
$types[] = GatewayType::ALIPAY;
}
2019-09-08 14:13:55 +02:00
return $types;
2019-09-12 13:46:09 +02:00
}
2019-09-18 04:39:53 +02:00
public function viewForType($gateway_type_id)
2019-09-12 13:46:09 +02:00
{
switch ($gateway_type_id) {
case GatewayType::CREDIT_CARD:
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.credit_card';
break;
case GatewayType::SOFORT:
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.sofort';
break;
case GatewayType::BANK_TRANSFER:
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.ach';
break;
case GatewayType::SEPA:
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.sepa';
break;
case GatewayType::CRYPTO:
case GatewayType::ALIPAY:
case GatewayType::APPLE_PAY:
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.other';
break;
default:
break;
}
2019-09-08 14:13:55 +02:00
}
2019-09-13 00:33:48 +02:00
public function getClientRequiredFields(): array
{
$fields = [
['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'],
];
if ($this->company_gateway->require_client_name) {
$fields[] = ['name' => 'client_name', 'label' => ctrans('texts.client_name'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_client_phone) {
$fields[] = ['name' => 'client_phone', 'label' => ctrans('texts.client_phone'), 'type' => 'tel', 'validation' => 'required'];
}
if ($this->company_gateway->require_contact_name) {
$fields[] = ['name' => 'contact_first_name', 'label' => ctrans('texts.first_name'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'contact_last_name', 'label' => ctrans('texts.last_name'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_contact_email) {
$fields[] = ['name' => 'contact_email', 'label' => ctrans('texts.email'), 'type' => 'text', 'validation' => 'required,email:rfc'];
}
if ($this->company_gateway->require_billing_address) {
$fields[] = ['name' => 'client_address_line_1', 'label' => ctrans('texts.address1'), 'type' => 'text', 'validation' => 'required'];
// $fields[] = ['name' => 'client_address_line_2', 'label' => ctrans('texts.address2'), 'type' => 'text', 'validation' => 'nullable'];
$fields[] = ['name' => 'client_city', 'label' => ctrans('texts.city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_state', 'label' => ctrans('texts.state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_shipping_address) {
$fields[] = ['name' => 'client_shipping_address_line_1', 'label' => ctrans('texts.shipping_address1'), 'type' => 'text', 'validation' => 'required'];
// $fields[] = ['name' => 'client_shipping_address_line_2', 'label' => ctrans('texts.shipping_address2'), 'type' => 'text', 'validation' => 'sometimes'];
$fields[] = ['name' => 'client_shipping_city', 'label' => ctrans('texts.shipping_city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_state', 'label' => ctrans('texts.shipping_state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_postal_code', 'label' => ctrans('texts.shipping_postal_code'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_country_id', 'label' => ctrans('texts.shipping_country'), 'type' => 'text', 'validation' => 'required'];
}
return $fields;
}
/**
2020-06-01 14:03:18 +02:00
* Proxy method to pass the data into payment method authorizeView().
*
* @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed
*/
2020-06-01 14:03:18 +02:00
public function authorizeView(array $data)
2019-09-14 14:34:05 +02:00
{
2020-06-01 14:03:18 +02:00
return $this->payment_method->authorizeView($data);
2019-09-16 13:03:25 +02:00
}
/**
2020-03-23 18:10:42 +01:00
* Processes the gateway response for credit card authorization.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|mixed
*/
2020-06-26 13:25:58 +02:00
public function authorizeResponse($request)
2019-09-16 13:03:25 +02:00
{
2020-06-01 14:14:41 +02:00
return $this->payment_method->authorizeResponse($request);
2019-09-14 14:34:05 +02:00
}
2019-09-25 04:07:33 +02:00
/**
2020-03-23 18:10:42 +01:00
* Process the payment with gateway.
*
2020-03-23 18:10:42 +01:00
* @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed
2019-09-25 04:07:33 +02:00
*/
public function processPaymentView(array $data)
2019-09-25 04:07:33 +02:00
{
2020-06-01 14:29:41 +02:00
return $this->payment_method->paymentView($data);
2019-09-25 04:07:33 +02:00
}
public function processPaymentResponse($request)
{
2020-06-01 16:19:03 +02:00
return $this->payment_method->paymentResponse($request);
2019-10-01 11:59:32 +02:00
}
2019-09-13 00:33:48 +02:00
/**
* Creates a new String Payment Intent.
*
2020-10-28 11:10:49 +01:00
* @param array $data The data array to be passed to Stripe
2019-09-13 00:33:48 +02:00
* @return PaymentIntent The Stripe payment intent object
* @throws ApiErrorException
2019-09-13 00:33:48 +02:00
*/
public function createPaymentIntent($data): ?PaymentIntent
2019-09-13 00:33:48 +02:00
{
$this->init();
2021-05-11 23:58:18 +02:00
$meta = $this->stripe_connect_auth;
2019-09-17 13:54:14 +02:00
2021-05-11 23:58:18 +02:00
return PaymentIntent::create($data, $meta);
2019-09-13 00:33:48 +02:00
}
2019-09-14 14:34:05 +02:00
/**
* Returns a setup intent that allows the user
2019-09-17 13:54:14 +02:00
* to enter card details without initiating a transaction.
2019-09-14 14:34:05 +02:00
*
2020-10-28 11:10:49 +01:00
* @return SetupIntent
* @throws ApiErrorException
2019-09-14 14:34:05 +02:00
*/
public function getSetupIntent(): SetupIntent
2019-09-14 14:34:05 +02:00
{
$this->init();
2019-09-17 13:54:14 +02:00
2021-05-11 23:58:18 +02:00
$params = [];
$meta = $this->stripe_connect_auth;
return SetupIntent::create($params, $meta);
2019-09-14 14:34:05 +02:00
}
2019-09-17 13:54:14 +02:00
/**
* Returns the Stripe publishable key.
* @return null|string The stripe publishable key
2019-09-17 13:54:14 +02:00
*/
2020-06-24 16:07:12 +02:00
public function getPublishableKey(): ?string
2019-09-14 14:34:05 +02:00
{
return $this->company_gateway->getPublishableKey();
}
2019-09-17 13:54:14 +02:00
/**
* Finds or creates a Stripe Customer object.
*
2020-10-28 11:10:49 +01:00
* @return null|Customer A Stripe customer object
* @throws \Laracasts\Presenter\Exceptions\PresenterException
* @throws ApiErrorException
2019-09-17 13:54:14 +02:00
*/
2020-10-28 11:10:49 +01:00
public function findOrCreateCustomer(): ?Customer
{
$customer = null;
$this->init();
$client_gateway_token = ClientGatewayToken::whereClientId($this->client->id)->whereCompanyGatewayId($this->company_gateway->id)->first();
if ($client_gateway_token && $client_gateway_token->gateway_customer_reference) {
2021-05-12 00:04:55 +02:00
$customer = Customer::retrieve($client_gateway_token->gateway_customer_reference, $this->stripe_connect_auth);
} else {
2021-05-11 23:58:18 +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();
}
2021-05-11 23:58:18 +02:00
$customer = Customer::create($data, $this->stripe_connect_auth);
}
2019-09-17 07:42:10 +02:00
2020-09-18 09:48:53 +02:00
if (!$customer) {
throw new Exception('Unable to create gateway customer');
}
2019-09-25 04:07:33 +02:00
return $customer;
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
2020-09-18 14:35:53 +02:00
$this->init();
2021-05-11 23:58:18 +02:00
$meta = $this->stripe_connect_auth;
/** Response from Stripe SDK/API. */
$response = null;
2020-09-18 14:35:53 +02:00
try {
$response = $this->stripe
->refunds
2021-05-11 23:58:18 +02:00
->create(['charge' => $payment->transaction_reference, 'amount' => $this->convertToStripeAmount($amount, $this->client->currency()->precision)], $meta);
if ($response->status == $response::STATUS_SUCCEEDED) {
SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(),], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->client, $this->client->company);
return [
'transaction_reference' => $response->charge,
'transaction_response' => json_encode($response),
'success' => $response->status == $response::STATUS_SUCCEEDED ? true : false,
'description' => $response->metadata,
'code' => $response,
];
}
SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(),], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client, $this->client->company);
2020-06-24 16:07:12 +02:00
return [
'transaction_reference' => null,
2020-09-18 14:35:53 +02:00
'transaction_response' => json_encode($response),
'success' => false,
'description' => $response->failure_reason,
'code' => 422,
2020-06-24 16:07:12 +02:00
];
} catch (Exception $e) {
SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(),], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client, $this->client->company);
2020-12-29 22:10:03 +01:00
nlog($e->getMessage());
2020-06-24 16:07:12 +02:00
return [
'transaction_reference' => null,
'transaction_response' => json_encode($response),
'success' => false,
'description' => $e->getMessage(),
'code' => 422,
];
}
}
public function verificationView(ClientGatewayToken $payment_method)
{
return $this->payment_method->verificationView($payment_method);
}
public function processVerification(Request $request, ClientGatewayToken $payment_method)
{
return $this->payment_method->processVerification($request, $payment_method);
}
2020-12-16 15:25:42 +01:00
public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment)
2020-06-27 17:39:28 +02:00
{
2020-12-16 15:25:42 +01:00
if ($request->type == 'source.chargeable') {
2020-06-27 17:39:28 +02:00
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->save();
}
2021-05-29 12:57:39 +02:00
if ($request->type == 'charge.succeeded') {
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->save();
}
// charge.failed, charge.refunded
2020-06-27 17:39:28 +02:00
return response([], 200);
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
2020-07-14 14:50:16 +02:00
{
2021-01-27 11:38:28 +01:00
return (new Charge($this))->tokenBilling($cgt, $payment_hash);
2020-07-15 07:05:02 +02:00
}
2020-09-18 09:48:53 +02:00
/**
2020-11-25 15:19:52 +01:00
* Attach Stripe payment method to Stripe client.
*
* @param string $payment_method
* @param mixed $customer
*
* @return void
*/
public function attach(string $payment_method, $customer): void
{
2021-05-12 04:03:46 +02:00
$this->init();
try {
2021-05-12 04:03:46 +02:00
$stripe_payment_method = $this->getStripePaymentMethod($payment_method);
2021-05-12 04:03:46 +02:00
$stripe_payment_method->attach(['customer' => $customer->id], $this->stripe_connect_auth);
} catch (ApiErrorException | Exception $e) {
2021-05-12 04:03:46 +02:00
nlog($e->getMessage());
SystemLogger::dispatch([
'server_response' => $e->getMessage(),
2021-05-12 04:03:46 +02:00
'data' => request()->all(),
],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->client, $this->client->company);
2021-05-12 04:03:46 +02:00
}
}
2020-09-18 09:48:53 +02:00
/**
* Detach payment method from the Stripe.
* https://stripe.com/docs/api/payment_methods/detach
2020-10-28 11:10:49 +01:00
*
* @param ClientGatewayToken $token
* @return void
*/
2020-09-18 09:48:53 +02:00
public function detach(ClientGatewayToken $token)
{
2021-05-12 04:03:46 +02:00
$this->init();
try{
2021-05-12 04:03:46 +02:00
$pm = $this->getStripePaymentMethod($token->token);
$pm->detach([], $this->stripe_connect_auth);
} catch (ApiErrorException | Exception $e) {
nlog($e->getMessage());
2020-09-18 09:48:53 +02:00
SystemLogger::dispatch([
'server_response' => $e->getMessage(),
2021-05-12 04:03:46 +02:00
'data' => request()->all(),
],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->client, $this->client->company);
2021-05-12 04:03:46 +02:00
2020-09-18 09:48:53 +02:00
}
}
public function getCompanyGatewayId(): int
{
return $this->company_gateway->id;
}
/**
* Retrieve payment method from Stripe.
*
2020-11-25 15:19:52 +01:00
* @param string $source
*
* @return PaymentMethod|void
*/
public function getStripePaymentMethod(string $source)
{
try {
2021-05-12 04:03:46 +02:00
2021-05-11 23:58:18 +02:00
return PaymentMethod::retrieve($source, $this->stripe_connect_auth);
2021-05-12 04:03:46 +02:00
} catch (ApiErrorException | Exception $e) {
throw new PaymentFailed($e->getMessage(), $e->getCode());
}
}
2021-05-12 05:00:46 +02:00
public function getAllConnectedAccounts()
{
$this->init();
return Account::all();
}
2021-05-17 06:02:43 +02:00
/**
* Pull all client payment methods and update
* the respective tokens in the system.
*
2021-05-17 06:02:43 +02:00
*/
2021-06-15 03:09:12 +02:00
public function updateAllPaymentMethods()
{
return (new UpdatePaymentMethods($this))->run();
}
2021-05-17 06:02:43 +02:00
/**
* Imports stripe customers and their payment methods
* Matches users in the system based on the $match_on_record
2021-05-17 06:02:43 +02:00
* ie. email
*
2021-05-17 06:02:43 +02:00
* Phone
* Email
*/
public function importCustomers()
2021-05-17 06:02:43 +02:00
{
return (new ImportCustomers($this))->run();
2021-05-17 06:02:43 +02:00
//match clients based on the gateway_customer_reference column
}
}