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

176 lines
5.4 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
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-05-21 12:08:48 +02:00
// public function run()
// {
// $this->stripe->init();
// $this->stripe
// ->company_gateway
// ->client_gateway_tokens
// ->each(function ($token){
// // $bank_accounts = Customer::allSources(
// // $token->gateway_customer_reference,
// // ['object' => 'bank_account', 'limit' => 300]
// // );
// // foreach($bank_accounts as $bank_account)
// // {
// // $this->addOrUpdateBankAccount($bank_account, $token);
// // }
// // $this->processCustomer($token->gateway_customer_reference);
// });
2021-05-17 06:02:43 +02:00
2021-05-21 12:08:48 +02:00
// }
2021-05-17 06:02:43 +02:00
2021-06-15 00:19:23 +02:00
public function updateMethods(Customer $customer, Client $client)
2021-05-21 12:08:48 +02:00
{
2021-05-17 06:02:43 +02:00
$card_methods = PaymentMethod::all([
2021-05-21 12:08:48 +02:00
'customer' => $customer->id,
2021-05-17 06:02:43 +02:00
'type' => 'card',
],
$this->stripe->stripe_connect_auth);
foreach($card_methods as $method)
{
2021-05-21 12:08:48 +02:00
$this->addOrUpdateCard($method, $customer->id, $client,GatewayType::CREDIT_CARD);
2021-05-17 06:02:43 +02:00
}
$alipay_methods = PaymentMethod::all([
2021-05-21 12:08:48 +02:00
'customer' => $customer->id,
2021-05-17 06:02:43 +02:00
'type' => 'alipay',
],
$this->stripe->stripe_connect_auth);
foreach($alipay_methods as $method)
{
2021-05-21 12:08:48 +02:00
$this->addOrUpdateCard($method, $customer->id, $client,GatewayType::ALIPAY);
2021-05-17 06:02:43 +02:00
}
$sofort_methods = PaymentMethod::all([
2021-05-21 12:08:48 +02:00
'customer' => $customer->id,
2021-05-17 06:02:43 +02:00
'type' => 'sofort',
],
$this->stripe->stripe_connect_auth);
foreach($alipay_methods as $method)
{
2021-05-21 12:08:48 +02:00
$this->addOrUpdateCard($method, $customer->id, $client, GatewayType::SOFORT);
2021-05-17 06:02:43 +02:00
}
}
2021-05-21 12:08:48 +02:00
// private function addOrUpdateBankAccount($bank_account, $customer_reference, Client $client)
// {
// $token_exists = ClientGatewayToken::where([
// 'gateway_customer_reference' => $customer_reference,
// 'token' => $bank_account->id,
// ])->exists();
// /* Already exists return */
// if($token_exists)
// return;
// $cgt = ClientGatewayTokenFactory::create($client->company_id);
// $cgt->client_id = $client->id;
// $cgt->token = $bank_account->id;
// $cgt->gateway_customer_reference = $customer_reference;
// $cgt->company_gateway_id = $this->stripe->company_gateway->id;
// $cgt->gateway_type_id = GatewayType::BANK_TRANSFER;
// $cgt->meta = new \stdClass;
// $cgt->routing_number = $bank_account->routing_number;
// $cgt->save();
// }
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,
])->exists();
/* Already exists return */
if($token_exists)
return;
/* Ignore Expired cards */
if($method->card->exp_year <= date('Y') && $method->card->exp_month < date('m'))
return;
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;
return $payment_meta;
break;
case GatewayType::ALIPAY:
case GatewayType::SOFORT:
return new \stdClass;
default:
break;
}
}
}