From a0395558f7fe9b79978c5fe48ddab13decea7d54 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 11 Jan 2023 11:12:40 +1100 Subject: [PATCH] Update bank accounts when they are verified offline --- app/Models/Gateway.php | 4 +- app/PaymentDrivers/Stripe/ACH.php | 59 ++++++++++++++++++---- app/PaymentDrivers/StripePaymentDriver.php | 5 ++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index fa610163097e..3e6df76d7e65 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -103,7 +103,7 @@ class Gateway extends StaticModel case 20: return [ GatewayType::CREDIT_CARD => ['refund' => true, 'token_billing' => true, 'webhooks' => ['payment_intent.succeeded']], - GatewayType::BANK_TRANSFER => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], + GatewayType::BANK_TRANSFER => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded', 'customer.source.updated']], GatewayType::ALIPAY => ['refund' => false, 'token_billing' => false], GatewayType::APPLE_PAY => ['refund' => false, 'token_billing' => false], GatewayType::SOFORT => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded', 'payment_intent.succeeded']], @@ -140,7 +140,7 @@ class Gateway extends StaticModel case 56: //Stripe return [ GatewayType::CREDIT_CARD => ['refund' => true, 'token_billing' => true, 'webhooks' => ['payment_intent.succeeded']], - GatewayType::BANK_TRANSFER => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], + GatewayType::BANK_TRANSFER => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded', 'customer.source.updated']], GatewayType::ALIPAY => ['refund' => false, 'token_billing' => false], GatewayType::APPLE_PAY => ['refund' => false, 'token_billing' => false], GatewayType::SOFORT => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded', 'payment_intent.succeeded']], diff --git a/app/PaymentDrivers/Stripe/ACH.php b/app/PaymentDrivers/Stripe/ACH.php index eccdc0b70494..5167dc47744e 100644 --- a/app/PaymentDrivers/Stripe/ACH.php +++ b/app/PaymentDrivers/Stripe/ACH.php @@ -94,6 +94,26 @@ class ACH return redirect()->route('client.payment_methods.verification', ['payment_method' => $client_gateway_token->hashed_id, 'method' => GatewayType::BANK_TRANSFER]); } + public function updateBankAccount(array $event) + { + + $stripe_event = $event['data']['object']; + + $token = ClientGatewayToken::where('token', $stripe_event['id']) + ->where('gateway_customer_reference', $stripe_event['customer']) + ->first(); + + if($token && isset($stripe_event['object']) && $stripe_event['object'] == 'bank_account' && isset($stripe_event['status']) && $stripe_event['status'] == 'verified') { + + $meta = $token->meta; + $meta->state = 'authorized'; + $token->meta = $meta; + $token->save(); + + } + + } + public function verificationView(ClientGatewayToken $token) { if (isset($token->meta->state) && $token->meta->state === 'authorized') { @@ -102,6 +122,25 @@ class ACH ->with('message', __('texts.payment_method_verified')); } + //double check here if we need to show the verification view. + $this->stripe->init(); + + $bank_account = Customer::retrieveSource($token->gateway_customer_reference, $token->token, [], $this->stripe->stripe_connect_auth); + + /* Catch externally validated bank accounts and mark them as verified */ + if(isset($bank_account->status) && $bank_account->status == 'verified'){ + + $meta = $token->meta; + $meta->state = 'authorized'; + $token->meta = $meta; + $token->save(); + + return redirect() + ->route('client.payment_methods.show', $token->hashed_id) + ->with('message', __('texts.payment_method_verified')); + + } + $data = [ 'token' => $token, 'gateway' => $this->stripe, @@ -126,19 +165,19 @@ class ACH $bank_account = Customer::retrieveSource($request->customer, $request->source, [], $this->stripe->stripe_connect_auth); - /* Catch externally validated bank accounts and mark them as verified */ - if(property_exists($bank_account, 'status') && $bank_account->status == 'verified'){ + // /* Catch externally validated bank accounts and mark them as verified */ + // if(isset($bank_account->status) && $bank_account->status == 'verified'){ - $meta = $token->meta; - $meta->state = 'authorized'; - $token->meta = $meta; - $token->save(); + // $meta = $token->meta; + // $meta->state = 'authorized'; + // $token->meta = $meta; + // $token->save(); - return redirect() - ->route('client.payment_methods.show', $token->hashed_id) - ->with('message', __('texts.payment_method_verified')); + // return redirect() + // ->route('client.payment_methods.show', $token->hashed_id) + // ->with('message', __('texts.payment_method_verified')); - } + // } try { $bank_account->verify(['amounts' => request()->transactions]); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 369a0f259975..7b127cfa3b50 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -631,6 +631,11 @@ class StripePaymentDriver extends BaseDriver // nlog($request->all()); //payment_intent.succeeded - this will confirm or cancel the payment + if($request->type === 'customer.source.updated') { + $ach = new ACH($this); + $ach->updateBankAccount($request->all()); + } + if ($request->type === 'payment_intent.succeeded') { PaymentIntentWebhook::dispatch($request->data, $request->company_key, $this->company_gateway->id)->delay(now()->addSeconds(rand(5, 10)));