ACH payments

This commit is contained in:
Benjamin Beganović 2020-11-01 19:21:31 +01:00
parent 1ae2649be6
commit 4c27652545
2 changed files with 70 additions and 99 deletions

View File

@ -12,7 +12,6 @@
namespace App\PaymentDrivers\Stripe;
use App\Events\Payment\PaymentWasCreated;
use App\Exceptions\PaymentFailed;
use App\Http\Requests\Request;
use App\Jobs\Mail\PaymentFailureMailer;
@ -23,7 +22,6 @@ use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\StripePaymentDriver;
use App\Utils\Ninja;
use Stripe\Exception\InvalidRequestException;
class ACH
@ -87,38 +85,33 @@ class ACH
public function paymentView(array $data)
{
$state = [
'amount' => $data['amount_with_fee'],
'currency' => $this->stripe->client->getCurrencyCode(),
'invoices' => $data['invoices'],
'gateway' => $this->stripe,
'payment_method_id' => GatewayType::BANK_TRANSFER,
'token' => $data['token'],
'customer' => $this->stripe->findOrCreateCustomer(),
];
$data['gateway'] = $this->stripe;
$data['currency'] = $this->stripe->client->getCurrencyCode();
$data['payment_method_id'] = GatewayType::BANK_TRANSFER;
$data['customer'] = $this->stripe->findOrCreateCustomer();
$data['amount'] = $this->stripe->convertToStripeAmount($data['amount_with_fee'], $this->stripe->client->currency()->precision);
return render('gateways.stripe.ach.pay', $state);
return render('gateways.stripe.ach.pay', $data);
}
public function paymentResponse($request)
{
$this->stripe->init();
$state = [
'payment_method' => $request->payment_method_id,
'gateway_type_id' => $request->company_gateway_id,
'hashed_ids' => $request->hashed_ids,
'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision),
'currency' => $request->currency,
'source' => $request->source,
'customer' => $request->customer,
];
if ($this->stripe->getContact()) {
$state['client_contact'] = $this->stripe->getContact();
} else {
$state['client_contact'] = $state['invoices']->first()->invitations->first()->contact;
}
$state = array_merge($state, $request->all());
$this->stripe->init();
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state);
$this->stripe->payment_hash->save();
try {
$state['charge'] = \Stripe\Charge::create([
@ -128,6 +121,11 @@ class ACH
'source' => $state['source'],
]);
$state = array_merge($state, $request->all());
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state);
$this->stripe->payment_hash->save();
if ($state['charge']->status === 'pending' && is_null($state['charge']->failure_message)) {
return $this->processPendingPayment($state);
}
@ -142,33 +140,24 @@ class ACH
public function processPendingPayment($state)
{
$state['charge_id'] = $state['charge']->id;
$this->stripe->init();
$state['payment_type'] = PaymentType::ACH;
$data = [
'payment_method' => $state['charge_id'],
'payment_type' => $state['payment_type'],
'amount' => $state['charge']->amount,
'gateway_type_id' => GatewayType::BANK_TRANSFER,
'payment_method' => $state['source'],
'payment_type' => PaymentType::ACH,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->amount, $this->stripe->client->currency()->precision),
'transaction_reference' => $state['charge']->id,
];
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
$this->stripe->attachInvoices($payment, $state['hashed_ids']); //todo remove hashed_ids
$payment->service()->updateInvoicePayment(); //inject payment_hash
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
$logger_message = [
'server_response' => $state['charge'],
'data' => $data,
];
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client);
SystemLogger::dispatch(
['response' => $state['charge'], 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_STRIPE,
$this->stripe->client
);
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
}
@ -177,14 +166,27 @@ class ACH
{
PaymentFailureMailer::dispatch($this->stripe->client, $state['charge']->failure_message, $this->stripe->client->company, $state['amount']);
PaymentFailureMailer::dispatch(
$this->stripe->client,
$state['charge'],
$this->stripe->client->company,
$state['amount']
);
$message = [
'server_response' => $state['charge'],
'data' => $state,
'data' => $this->stripe->payment_hash->data,
];
SystemLogger::dispatch($message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client);
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->stripe->client
);
throw new \Exception('Failed to process the payment.', 1);
throw new PaymentFailed('Failed to process the payment.', 500);
}
private function storePaymentMethod($method, $payment_method_id, $customer)

View File

@ -1,69 +1,38 @@
@extends('portal.ninja2020.layout.app')
@section('meta_title', ctrans('texts.ach'))
@extends('portal.ninja2020.layout.payments', ['gateway_title' => 'ACH (Stripe)', 'card_title' => 'ACH (Stripe)'])
@section('body')
@section('gateway_content')
@if($token)
<div class="alert alert-failure mb-4" hidden id="errors"></div>
@include('portal.ninja2020.gateways.includes.payment_details')
<form action="{{ route('client.payments.response') }}" method="post" id="server-response">
@csrf
@foreach($invoices as $invoice)
<input type="hidden" name="hashed_ids[]" value="{{ $invoice->hashed_id }}">
@endforeach
<input type="hidden" name="company_gateway_id" value="{{ $gateway->getCompanyGatewayId() }}">
<input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}">
<input type="hidden" name="source" value="{{ $token->meta->id }}">
<input type="hidden" name="source" value="{{ $token->token }}">
<input type="hidden" name="amount" value="{{ $amount }}">
<input type="hidden" name="currency" value="{{ $currency }}">
<input type="hidden" name="customer" value="{{ $customer->id }}">
</form>
@component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.payment_type')])
{{ ctrans('texts.ach') }} ({{ ctrans('texts.bank_transfer') }}) (**** {{ $token->meta->last4 }})
@endcomponent
@else
@component('portal.ninja2020.components.general.card-element-single', ['title' => 'ACH', 'show_title' => false])
<span>{{ ctrans('texts.bank_account_not_linked') }}</span>
<a class="button button-link text-primary" href="{{ route('client.payment_methods.index') }}">{{ ctrans('texts.add_payment_method') }}</a>
@endcomponent
@endif
<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">
<div class="alert alert-failure mb-4" hidden id="errors"></div>
<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.pay_now') }}
</h3>
<p class="mt-1 max-w-2xl text-sm leading-5 text-gray-500">
{{ ctrans('texts.complete_your_payment') }}
</p>
</div>
<div>
@if($token)
<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">
{{ ctrans('texts.payment_type') }}
</dt>
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
{{ ctrans('texts.ach') }} ({{ ctrans('texts.bank_transfer') }}) (****{{ $token->meta->last4 }})
</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">
{{ ctrans('texts.amount') }}
</dt>
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<span class="font-bold">{{ App\Utils\Number::formatMoney($amount, $client) }}</span>
</dd>
</div>
<div class="bg-gray-50 px-4 py-5 flex justify-end">
<button type="button" id="pay-now" class="button button-primary bg-primary" onclick="document.getElementById('server-response').submit()">
{{ ctrans('texts.pay_now') }}
</button>
</div>
@else
<div class="bg-gray-50 px-4 py-5 sm:px-6 flex items-center">
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
<span>{{ ctrans('texts.bank_account_not_linked') }}</span>
<a class="button button-link text-primary" href="{{ route('client.payment_methods.index') }}">{{ ctrans('texts.add_payment_method') }}</a>
</dd>
</div>
@endif
</div>
</div>
</div>
</div>
</div>
@endsection
@include('portal.ninja2020.gateways.includes.pay_now')
@endsection
@push('footer')
<script>
document.getElementById('pay-now').addEventListener('click', function() {
document.getElementById('server-response').submit();
});
</script>
@endpush