diff --git a/app/Http/Controllers/ClientPortal/PaymentMethodController.php b/app/Http/Controllers/ClientPortal/PaymentMethodController.php index b5182945be03..ff97ab85b672 100644 --- a/app/Http/Controllers/ClientPortal/PaymentMethodController.php +++ b/app/Http/Controllers/ClientPortal/PaymentMethodController.php @@ -15,13 +15,13 @@ namespace App\Http\Controllers\ClientPortal; use App\Events\Payment\Methods\MethodDeleted; use App\Http\Controllers\Controller; use App\Http\Requests\ClientPortal\CreatePaymentMethodRequest; +use App\Http\Requests\Request; use App\Models\ClientGatewayToken; use App\Models\CompanyGateway; use App\Models\GatewayType; use App\PaymentDrivers\AuthorizePaymentDriver; use App\Utils\Ninja; use App\Utils\Traits\MakesDates; -use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; class PaymentMethodController extends Controller @@ -117,14 +117,14 @@ class PaymentMethodController extends Controller ->verificationView($payment_method); } - public function processVerification(ClientGatewaytoken $payment_method) + public function processVerification(Request $request, ClientGatewaytoken $payment_method) { $gateway = $this->getClientGateway(); return $gateway ->driver(auth()->user()->client) ->setPaymentMethod(request()->query('method')) - ->processVerification($payment_method); + ->processVerification($request, $payment_method); } /** diff --git a/app/PaymentDrivers/Stripe/ACH.php b/app/PaymentDrivers/Stripe/ACH.php index 837f5a8f3cb5..b103a9f897e0 100644 --- a/app/PaymentDrivers/Stripe/ACH.php +++ b/app/PaymentDrivers/Stripe/ACH.php @@ -13,6 +13,8 @@ namespace App\PaymentDrivers\Stripe; use App\Events\Payment\PaymentWasCreated; +use App\Exceptions\PaymentFailed; +use App\Http\Requests\Request; use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; @@ -41,52 +43,19 @@ class ACH public function authorizeResponse($request) { - $state = [ - 'server_response' => json_decode($request->gateway_response), - 'gateway_id' => $request->company_gateway_id, - 'gateway_type_id' => $request->gateway_type_id, - 'is_default' => $request->is_default, - ]; + $this->stripe->init(); + + $stripe_response = json_decode($request->input('gateway_response')); $customer = $this->stripe->findOrCreateCustomer(); - $this->stripe->init(); - - $local_stripe = new \Stripe\StripeClient( - $this->stripe->company_gateway->getConfigField('apiKey') - ); - try { - $local_stripe->customers->createSource( - $customer->id, - ['source' => $state['server_response']->token->id] - ); + $source = $this->stripe->stripe->customers->createSource($customer->id, ['source' => $stripe_response->token->id]); } catch (InvalidRequestException $e) { - return back()->with('ach_error', $e->getMessage()); + throw new PaymentFailed($e->getMessage(), $e->getCode()); } - $payment_meta = $state['server_response']->token->bank_account; - $payment_meta->brand = ctrans('texts.ach'); - $payment_meta->type = ctrans('texts.bank_transfer'); - $payment_meta->verified_at = null; - $payment_meta->token = $state['server_response']->token->id; - - $client_gateway_token = new ClientGatewayToken(); - $client_gateway_token->company_id = $this->stripe->client->company->id; - $client_gateway_token->client_id = $this->stripe->client->id; - $client_gateway_token->token = $state['server_response']->token->bank_account->id; - $client_gateway_token->company_gateway_id = $this->stripe->company_gateway->id; - $client_gateway_token->gateway_type_id = $state['gateway_type_id']; - $client_gateway_token->gateway_customer_reference = $customer->id; - $client_gateway_token->meta = $payment_meta; - $client_gateway_token->save(); - - if ($state['is_default'] == 'true' || $this->stripe->client->gateway_tokens->count() == 1) { - $this->stripe->client->gateway_tokens()->update(['is_default' => 0]); - - $client_gateway_token->is_default = 1; - $client_gateway_token->save(); - } + $client_gateway_token = $this->storePaymentMethod($source, $request->input('method'), $customer); return redirect()->route('client.payment_methods.verification', ['payment_method' => $client_gateway_token->hashed_id, 'method' => GatewayType::BANK_TRANSFER]); } @@ -96,14 +65,11 @@ class ACH return render('gateways.stripe.ach.verify', compact('token')); } - public function processVerification(ClientGatewayToken $token) + public function processVerification(Request $request, ClientGatewayToken $token) { $this->stripe->init(); - $bank_account = \Stripe\Customer::retrieveSource( - request()->customer, - request()->source, - ); + $bank_account = \Stripe\Customer::retrieveSource($request->customer, $request->source); try { $status = $bank_account->verify(['amounts' => request()->transactions]); @@ -193,7 +159,7 @@ class ACH $this->stripe->attachInvoices($payment, $state['hashed_ids']); //todo remove hashed_ids - $payment->service()->updateInvoicePayment();//inject payment_hash + $payment->service()->updateInvoicePayment(); //inject payment_hash event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); @@ -220,4 +186,24 @@ class ACH throw new \Exception('Failed to process the payment.', 1); } + + private function storePaymentMethod($method, $payment_method_id, $customer) + { + try { + $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; + + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $method->id, + 'payment_method_id' => $payment_method_id, + ]; + + return $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]); + } catch (\Exception $e) { + return $this->stripe->processInternallyFailedPayment($this->stripe, $e); + } + } } diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index 196fc09f696c..d512f6a0d2db 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -102,17 +102,6 @@ class CreditCard $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state); $this->stripe->payment_hash->save(); - /*Hydrate the invoices from the payment hash*/ - // $invoices = Invoice::whereIn('id', $this->stripe->transformKeys(array_column($payment_hash->invoices(), 'invoice_id'))) - // ->whereClientId($this->stripe->client->id) - // ->get(); - - // if ($this->stripe->getContact()) { - // $client_contact = $this->stripe->getContact(); - // } else { - // $client_contact = $invoices->first()->invitations->first()->contact; - // } - $server_response = $this->stripe->payment_hash->data->server_response; if ($server_response->status == 'succeeded') { diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index da4e6e8db981..af9dfab04de8 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -15,6 +15,7 @@ namespace App\PaymentDrivers; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; use App\Http\Requests\Payments\PaymentWebhookRequest; +use App\Http\Requests\Request; use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; @@ -33,7 +34,6 @@ use App\PaymentDrivers\Stripe\CreditCard; use App\PaymentDrivers\Stripe\SOFORT; use App\PaymentDrivers\Stripe\Utilities; use App\Utils\Traits\MakesHash; -use Illuminate\Http\Request; use Illuminate\Support\Carbon; use Stripe\PaymentIntent; use Stripe\SetupIntent; @@ -50,7 +50,7 @@ class StripePaymentDriver extends BaseDriver public $can_authorise_credit_card = true; /** @var \Stripe\StripeClient */ - protected $stripe; + public $stripe; protected $customer_reference = 'customerReferenceParam'; @@ -328,9 +328,9 @@ class StripePaymentDriver extends BaseDriver return $this->payment_method->verificationView($payment_method); } - public function processVerification(ClientGatewayToken $payment_method) + public function processVerification(Request $request, ClientGatewayToken $payment_method) { - return $this->payment_method->processVerification($payment_method); + return $this->payment_method->processVerification($request, $payment_method); } public function processWebhookRequest(PaymentWebhookRequest $request, Company $company, CompanyGateway $company_gateway, Payment $payment) diff --git a/resources/views/portal/ninja2020/gateways/stripe/ach/verify.blade.php b/resources/views/portal/ninja2020/gateways/stripe/ach/verify.blade.php index 3b4375b502d1..380b98584fc7 100644 --- a/resources/views/portal/ninja2020/gateways/stripe/ach/verify.blade.php +++ b/resources/views/portal/ninja2020/gateways/stripe/ach/verify.blade.php @@ -1,58 +1,3 @@ -@extends('portal.ninja2020.layout.app') -@section('meta_title', ctrans('texts.verification')) - -@section('body') -
- {{ ctrans('texts.complete_your_bank_account_verification') }} ({{ ctrans('texts.ach') }}/{{ $token->meta->last4 }}) -
- {{ __('texts.learn_more') }} -