From 87646d43355f2a0403e2b2ec85af8564222d93a5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 11 Mar 2022 10:19:22 +1100 Subject: [PATCH] Fixes for importing ACH bank accoun tokens --- app/PaymentDrivers/Stripe/ImportCustomers.php | 7 +- .../Stripe/UpdatePaymentMethods.php | 85 ++++++++----------- 2 files changed, 37 insertions(+), 55 deletions(-) diff --git a/app/PaymentDrivers/Stripe/ImportCustomers.php b/app/PaymentDrivers/Stripe/ImportCustomers.php index 38690c8aa2..8f32238992 100644 --- a/app/PaymentDrivers/Stripe/ImportCustomers.php +++ b/app/PaymentDrivers/Stripe/ImportCustomers.php @@ -76,11 +76,9 @@ class ImportCustomers $account = $this->stripe->company_gateway->company->account; - if(!$account->isPaidHostedClient() && Client::where('company_id', $this->stripe->company_gateway->company_id)->count() > config('ninja.quotas.free.clients')) + if(Ninja::isHosted() && !$account->isPaidHostedClient() && Client::where('company_id', $this->stripe->company_gateway->company_id)->count() > config('ninja.quotas.free.clients')) return; - // nlog("search Stripe for {$customer->id}"); - $existing_customer_token = $this->stripe ->company_gateway ->client_gateway_tokens() @@ -104,9 +102,6 @@ class ImportCustomers return; } - // nlog("inserting a customer"); - //nlog($customer); - $client = ClientFactory::create($this->stripe->company_gateway->company_id, $this->stripe->company_gateway->user_id); if($customer->address) diff --git a/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php b/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php index f585ada7b8..b89a9bc1a7 100644 --- a/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php +++ b/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php @@ -33,33 +33,10 @@ class UpdatePaymentMethods $this->stripe = $stripe; } - // 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); - - // }); - - // } - public function updateMethods(Customer $customer, Client $client) { + $this->stripe->client = $client; + $card_methods = PaymentMethod::all([ 'customer' => $customer->id, 'type' => 'card', @@ -93,37 +70,47 @@ class UpdatePaymentMethods $this->addOrUpdateCard($method, $customer->id, $client, GatewayType::SOFORT); } - //$this->importBankAccounts($customer, $client); + $this->importBankAccounts($customer, $client); } private function importBankAccounts($customer, $client) { + $sources = $customer->sources; + + 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; + + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $method->id, + 'payment_method_id' => GatewayType::BANK_TRANSFER, + ]; + + $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]); + + } + + } - // 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) {