From 6cd6c218dec47babee163e18e3dfe1ebc2fb2449 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 6 Oct 2022 11:00:39 +1100 Subject: [PATCH] Improve ACH flow --- .../Stripe/Jobs/PaymentIntentWebhook.php | 46 ++++++++++++++++++- .../Stripe/UpdatePaymentMethods.php | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php b/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php index 800f13f9f86b..6b00a1221a16 100644 --- a/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php +++ b/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php @@ -13,6 +13,7 @@ namespace App\PaymentDrivers\Stripe\Jobs; use App\Jobs\Util\SystemLogger; use App\Libraries\MultiDB; +use App\Models\ClientGatewayToken; use App\Models\Company; use App\Models\CompanyGateway; use App\Models\GatewayType; @@ -21,6 +22,7 @@ use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; +use App\PaymentDrivers\Stripe\UpdatePaymentMethods; use App\PaymentDrivers\Stripe\Utilities; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -53,7 +55,6 @@ class PaymentIntentWebhook implements ShouldQueue public function handle() { - nlog($this->stripe_request); MultiDB::findAndSetDbByCompanyKey($this->company_key); @@ -202,6 +203,49 @@ class PaymentIntentWebhook implements ShouldQueue $client->company, ); + try { + + $customer = $driver->getCustomer($this->stripe_request['object']['charges']['data'][0]['customer']); + $method = $driver->getStripePaymentMethod($this->stripe_request['object']['charges']['data'][0]['payment_method']); + $payment_method = $this->stripe_request['object']['charges']['data'][0]['payment_method']; + + $token_exists = ClientGatewayToken::where([ + 'gateway_customer_reference' => $customer->id, + 'token' => $payment_method, + 'client_id' => $client->id, + 'company_id' => $client->company_id, + ])->exists(); + + /* Already exists return */ + if ($token_exists) { + return; + } + + $payment_meta = new \stdClass; + $payment_meta->brand = (string) \sprintf('%s (%s)', $method->us_bank_account['bank_name'], ctrans('texts.ach')); + $payment_meta->last4 = (string) $method->us_bank_account['last4']; + $payment_meta->type = GatewayType::BANK_TRANSFER; + $payment_meta->state = 'verified'; + + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $payment_method, + '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]; + } + + $driver->storeGatewayToken($data, $additional_data); + + } + catch(\Exception $e){ + nlog("failed to import payment methods"); + nlog($e->getMessage()); + } } diff --git a/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php b/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php index 4424ca40eee7..ed333de662f2 100644 --- a/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php +++ b/app/PaymentDrivers/Stripe/UpdatePaymentMethods.php @@ -70,7 +70,7 @@ class UpdatePaymentMethods $this->importBankAccounts($customer, $client); } - private function importBankAccounts($customer, $client) + public function importBankAccounts($customer, $client) { $sources = $customer->sources;