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

171 lines
5.2 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
$card_methods = PaymentMethod::all([
'customer' => $customer->id,
'type' => 'card',
],
2021-05-17 06:02:43 +02:00
$this->stripe->stripe_connect_auth);
foreach ($card_methods as $method) {
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::CREDIT_CARD);
}
2021-05-17 06:02:43 +02:00
$alipay_methods = PaymentMethod::all([
'customer' => $customer->id,
'type' => 'alipay',
],
2021-05-17 06:02:43 +02:00
$this->stripe->stripe_connect_auth);
foreach ($alipay_methods as $method) {
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::ALIPAY);
}
2021-05-17 06:02:43 +02:00
$sofort_methods = PaymentMethod::all([
'customer' => $customer->id,
'type' => 'sofort',
],
2021-05-17 06:02:43 +02:00
$this->stripe->stripe_connect_auth);
foreach ($sofort_methods as $method) {
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::SOFORT);
}
2021-05-17 06:02:43 +02:00
$this->importBankAccounts($customer, $client);
}
2022-10-06 02:00:39 +02:00
public function importBankAccounts($customer, $client)
{
$sources = $customer->sources;
2021-05-17 06:02:43 +02:00
if(!$customer || is_null($sources) || !property_exists($sources, 'data'))
return;
foreach ($sources->data as $method) {
$token_exists = ClientGatewayToken::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;
}
$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
{
$token_exists = ClientGatewayToken::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->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:
$payment_meta = new \stdClass;
$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;
2021-05-17 06:02:43 +02:00
return $payment_meta;
break;
2021-05-17 06:02:43 +02:00
case GatewayType::ALIPAY:
case GatewayType::SOFORT:
return new \stdClass;
default:
break;
}
}
}