1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php

261 lines
8.3 KiB
PHP
Raw Normal View History

2021-05-17 06:02:43 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-05-17 06:02:43 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-05-17 06:02:43 +02:00
*/
namespace App\PaymentDrivers\Stripe;
use App\Factory\ClientGatewayTokenFactory;
2021-05-21 12:08:48 +02:00
use App\Models\Client;
2021-05-17 06:02:43 +02:00
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\PaymentDrivers\StripePaymentDriver;
use App\Utils\Traits\MakesHash;
use Stripe\Customer;
use Stripe\PaymentMethod;
class UpdatePaymentMethods
{
use MakesHash;
/** @var StripePaymentDriver */
public $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
2021-06-15 00:19:23 +02:00
public function updateMethods(Customer $customer, Client $client)
2021-05-21 12:08:48 +02:00
{
$this->stripe->client = $client;
2023-02-16 02:36:09 +01:00
$card_methods = PaymentMethod::all(
[
'customer' => $customer->id,
'type' => 'card',
],
2023-02-16 02:36:09 +01:00
$this->stripe->stripe_connect_auth
);
2021-05-17 06:02:43 +02:00
foreach ($card_methods as $method) {
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::CREDIT_CARD);
}
2021-05-17 06:02:43 +02:00
2023-02-16 02:36:09 +01:00
$alipay_methods = PaymentMethod::all(
[
'customer' => $customer->id,
'type' => 'alipay',
],
2023-02-16 02:36:09 +01:00
$this->stripe->stripe_connect_auth
);
2021-05-17 06:02:43 +02:00
foreach ($alipay_methods as $method) {
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::ALIPAY);
}
2021-05-17 06:02:43 +02:00
2023-02-16 02:36:09 +01:00
$sofort_methods = PaymentMethod::all(
[
'customer' => $customer->id,
'type' => 'sofort',
],
2023-02-16 02:36:09 +01:00
$this->stripe->stripe_connect_auth
);
2021-05-17 06:02:43 +02:00
foreach ($sofort_methods as $method) {
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::SOFORT);
}
2021-05-17 06:02:43 +02:00
$sepa_methods = PaymentMethod::all(
[
'customer' => $customer->id,
'type' => 'sepa_debit',
],
$this->stripe->stripe_connect_auth
);
foreach ($sepa_methods as $method) {
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::SEPA);
}
$this->importBankAccounts($customer, $client);
2024-01-14 05:05:00 +01:00
2023-02-19 01:32:40 +01:00
$this->importPMBankAccounts($customer, $client);
}
2023-02-22 07:38:14 +01:00
/* ACH may also be nested inside Payment Methods.*/
2023-02-19 01:32:40 +01:00
public function importPMBankAccounts($customer, $client)
{
$bank_methods = \Stripe\PaymentMethod::all(
[
'customer' => $customer->id,
'type' => 'us_bank_account',
2023-02-22 07:38:14 +01:00
],
2023-02-19 01:32:40 +01:00
$this->stripe->stripe_connect_auth
);
2023-02-22 07:38:14 +01:00
foreach ($bank_methods->data as $method) {
2023-08-08 12:39:46 +02:00
$token = ClientGatewayToken::query()->where([
2023-02-19 01:32:40 +01:00
'gateway_customer_reference' => $customer->id,
'token' => $method->id,
'client_id' => $client->id,
'company_id' => $client->company_id,
2023-03-09 07:33:10 +01:00
])->first();
2023-02-19 01:32:40 +01:00
/* Already exists return */
2023-03-09 07:33:10 +01:00
if ($token) {
$meta = $token->meta;
$meta->state = 'authorized';
$token->meta = $meta;
$token->save();
2023-02-19 01:32:40 +01:00
continue;
}
$bank_account = $method['us_bank_account'];
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
2023-02-19 01:32:40 +01:00
$payment_meta->brand = (string) \sprintf('%s (%s)', $bank_account->bank_name, ctrans('texts.ach'));
$payment_meta->last4 = (string) $bank_account->last4;
$payment_meta->type = GatewayType::BANK_TRANSFER;
$payment_meta->state = 'authorized';
$data = [
'payment_meta' => $payment_meta,
'token' => $method->id,
'payment_method_id' => GatewayType::BANK_TRANSFER,
];
$additional_data = ['gateway_customer_reference' => $customer->id];
if ($customer->default_source === $method->id) {
$additional_data = ['gateway_customer_reference' => $customer->id, 'is_default' => 1];
}
$this->stripe->storeGatewayToken($data, $additional_data);
}
}
2022-10-06 02:00:39 +02:00
public function importBankAccounts($customer, $client)
{
$sources = $customer->sources;
2021-05-17 06:02:43 +02:00
2023-02-16 02:36:09 +01:00
if (!$customer || is_null($sources) || !property_exists($sources, 'data')) {
return;
2023-02-16 02:36:09 +01:00
}
foreach ($sources->data as $method) {
2023-08-08 12:39:46 +02:00
$token_exists = ClientGatewayToken::query()->where([
'gateway_customer_reference' => $customer->id,
'token' => $method->id,
'client_id' => $client->id,
'company_id' => $client->company_id,
])->exists();
/* Already exists return */
if ($token_exists) {
continue;
}
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
$payment_meta->brand = (string) \sprintf('%s (%s)', $method->bank_name, ctrans('texts.ach'));
$payment_meta->last4 = (string) $method->last4;
$payment_meta->type = GatewayType::BANK_TRANSFER;
$payment_meta->state = $method->status;
2022-03-11 02:01:59 +01:00
$data = [
'payment_meta' => $payment_meta,
'token' => $method->id,
'payment_method_id' => GatewayType::BANK_TRANSFER,
];
2022-03-11 02:01:59 +01:00
$additional_data = ['gateway_customer_reference' => $customer->id];
if ($customer->default_source === $method->id) {
$additional_data = ['gateway_customer_reference' => $customer->id, 'is_default' => 1];
}
$this->stripe->storeGatewayToken($data, $additional_data);
}
}
2021-05-21 12:08:48 +02:00
private function addOrUpdateCard(PaymentMethod $method, $customer_reference, Client $client, $type_id)
2021-05-17 06:02:43 +02:00
{
2023-08-08 12:39:46 +02:00
$token_exists = ClientGatewayToken::query()->where([
2021-05-21 12:08:48 +02:00
'gateway_customer_reference' => $customer_reference,
2021-05-17 06:02:43 +02:00
'token' => $method->id,
2021-09-04 05:00:05 +02:00
'client_id' => $client->id,
2021-09-02 04:20:03 +02:00
'company_id' => $client->company_id,
2021-05-17 06:02:43 +02:00
])->exists();
/* Already exists return */
if ($token_exists) {
2021-05-17 06:02:43 +02:00
return;
}
2021-05-17 06:02:43 +02:00
/* Ignore Expired cards */
if ($method->card && $method->card->exp_year <= date('Y') && $method->card->exp_month < date('m')) {
2021-05-17 06:02:43 +02:00
return;
}
2021-05-17 06:02:43 +02:00
2021-05-21 12:08:48 +02:00
$cgt = ClientGatewayTokenFactory::create($client->company_id);
$cgt->client_id = $client->id;
2021-05-17 06:02:43 +02:00
$cgt->token = $method->id;
2021-05-21 12:08:48 +02:00
$cgt->gateway_customer_reference = $customer_reference;
$cgt->company_gateway_id = $this->stripe->company_gateway->id;
2021-05-17 06:02:43 +02:00
$cgt->gateway_type_id = $type_id;
$cgt->meta = $this->buildPaymentMethodMeta($method, $type_id);
$cgt->save();
}
2021-06-15 00:19:23 +02:00
private function buildPaymentMethodMeta(PaymentMethod $method, $type_id)
2021-05-17 06:02:43 +02:00
{
switch ($type_id) {
case GatewayType::CREDIT_CARD:
2023-10-26 04:57:44 +02:00
/**
* @class \Stripe\PaymentMethod $method
* @property \Stripe\StripeObject $card
* @class \Stripe\StripeObject $card
* @property string $exp_year
* @property string $exp_month
* @property string $brand
* @property string $last4
*/
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
2023-10-26 04:57:44 +02:00
$payment_meta->exp_month = (string) $method->card->exp_month;
$payment_meta->exp_year = (string) $method->card->exp_year;
$payment_meta->brand = (string) $method->card->brand;
$payment_meta->last4 = (string) $method->card->last4;
$payment_meta->type = GatewayType::CREDIT_CARD;
return $payment_meta;
2021-05-17 06:02:43 +02:00
case GatewayType::ALIPAY:
case GatewayType::SOFORT:
2024-01-14 05:05:00 +01:00
return new \stdClass();
2021-05-17 06:02:43 +02:00
case GatewayType::SEPA:
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
$payment_meta->brand = (string) \sprintf('%s (%s)', $method->sepa_debit->bank_code, ctrans('texts.sepa'));
$payment_meta->last4 = (string) $method->sepa_debit->last4;
$payment_meta->state = 'authorized';
$payment_meta->type = GatewayType::SEPA;
return $payment_meta;
2021-05-17 06:02:43 +02:00
default:
break;
}
}
}