1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Stripe ACH authorization & verification

This commit is contained in:
Benjamin Beganović 2020-11-01 18:47:48 +01:00
parent 99c30846ad
commit 1ae2649be6
5 changed files with 39 additions and 119 deletions

View File

@ -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);
}
/**

View File

@ -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);
}
}
}

View File

@ -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') {

View File

@ -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)

View File

@ -1,58 +1,3 @@
@extends('portal.ninja2020.layout.app')
@section('meta_title', ctrans('texts.verification'))
@section('body')
<div class="container mx-auto">
<div class="grid grid-cols-6 gap-4">
<div class="col-span-6 md:col-start-2 md:col-span-4">
@if(session()->has('error'))
<div class="alert alert-failure mb-4">{{ session('error') }}</div>
@endif
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
<div class="px-4 py-5 border-b border-gray-200 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900">
{{ ctrans('texts.verification') }}
</h3>
<p class="mt-1 max-w-2xl text-sm leading-5 text-gray-500">
{{ ctrans('texts.complete_your_bank_account_verification') }} ({{ ctrans('texts.ach') }}/{{ $token->meta->last4 }})
</p>
<a href="#" class="button-link text-primary text-sm">{{ __('texts.learn_more') }}</a>
</div>
<div>
<form method="post">
@csrf
<input type="hidden" name="customer" value="{{ $token->gateway_customer_reference }}">
<input type="hidden" name="source" value="{{ $token->meta->id }}">
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 flex items-center">
<dt class="text-sm leading-5 font-medium text-gray-500 mr-4">
#1 {{ ctrans('texts.amount') }}
</dt>
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" name="transactions[]" class="w-full input" required>
</dd>
</div>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6 flex items-center">
<dt class="text-sm leading-5 font-medium text-gray-500 mr-4">
#2 {{ ctrans('texts.amount') }}
</dt>
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<input type="text" name="transactions[]" class="w-full input" required>
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 flex justify-end">
<button id="pay-now" class="button button-primary bg-primary">
{{ ctrans('texts.complete_verification') }}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@extends('portal.ninja2020.layout.payments', ['gateway_title' => 'ACH (Verification)', 'card_title' => 'ACH (Verification)'])
@section('gateway_content')
@ -63,7 +8,7 @@
<form method="POST">
@csrf
<input type="hidden" name="customer" value="{{ $token->gateway_customer_reference }}">
<input type="hidden" name="source" value="{{ $token->meta->id }}">
<input type="hidden" name="source" value="{{ $token->token }}">
@component('portal.ninja2020.components.general.card-element', ['title' => '#1 ' . ctrans('texts.amount')])
<input type="text" name="transactions[]" class="w-full input" required>