mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-09-21 08:51:34 +02:00
Merge pull request #6852 from beganovich/gocardless-direct-debit
GoCardless: Direct Debit
This commit is contained in:
commit
2c0cef9b15
@ -149,11 +149,11 @@ class PaymentMethodController extends Controller
|
||||
private function getClientGateway()
|
||||
{
|
||||
if (request()->query('method') == GatewayType::CREDIT_CARD) {
|
||||
return $gateway = auth()->user()->client->getCreditCardGateway();
|
||||
return auth()->user()->client->getCreditCardGateway();
|
||||
}
|
||||
|
||||
if (in_array(request()->query('method'), [GatewayType::BANK_TRANSFER, GatewayType::SEPA])) {
|
||||
return $gateway = auth()->user()->client->getBankTransferGateway();
|
||||
if (in_array(request()->query('method'), [GatewayType::BANK_TRANSFER, GatewayType::DIRECT_DEBIT, GatewayType::SEPA])) {
|
||||
return auth()->user()->client->getBankTransferGateway();
|
||||
}
|
||||
|
||||
abort(404, 'Gateway not found.');
|
||||
|
@ -530,6 +530,18 @@ class Client extends BaseModel implements HasLocalePreference
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->country->iso_3166_3 == 'GBR' && in_array(GatewayType::DIRECT_DEBIT, array_column($pms, 'gateway_type_id'))) {
|
||||
foreach ($pms as $pm) {
|
||||
if ($pm['gateway_type_id'] == GatewayType::DIRECT_DEBIT) {
|
||||
$cg = CompanyGateway::find($pm['company_gateway_id']);
|
||||
|
||||
if ($cg && $cg->fees_and_limits->{GatewayType::DIRECT_DEBIT}->is_enabled) {
|
||||
return $cg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
@ -544,6 +556,10 @@ class Client extends BaseModel implements HasLocalePreference
|
||||
if ($this->currency()->code == 'EUR') {
|
||||
return GatewayType::SEPA;
|
||||
}
|
||||
|
||||
if ($this->currency()->code == 'GBP') {
|
||||
return GatewayType::DIRECT_DEBIT;
|
||||
}
|
||||
}
|
||||
|
||||
public function getCurrencyCode()
|
||||
|
@ -155,7 +155,8 @@ class Gateway extends StaticModel
|
||||
break;
|
||||
case 52:
|
||||
return [
|
||||
GatewayType::BANK_TRANSFER => ['refund' => true, 'token_billing' => true, 'webhooks' => [' ']], // GoCardless,
|
||||
GatewayType::BANK_TRANSFER => ['refund' => true, 'token_billing' => true, 'webhooks' => [' ']], // GoCardless
|
||||
GatewayType::DIRECT_DEBIT => ['refund' => false, 'token_billing' => true, 'webhooks' => [' ']],
|
||||
GatewayType::SEPA => ['refund' => false, 'token_billing' => true, 'webhooks' => [' ']]
|
||||
];
|
||||
break;
|
||||
|
@ -32,6 +32,7 @@ class GatewayType extends StaticModel
|
||||
const GIROPAY = 15;
|
||||
const PRZELEWY24 = 16;
|
||||
const EPS = 17;
|
||||
const DIRECT_DEBIT = 18;
|
||||
|
||||
public function gateway()
|
||||
{
|
||||
@ -78,6 +79,8 @@ class GatewayType extends StaticModel
|
||||
return ctrans('texts.giropay');
|
||||
case self::EPS:
|
||||
return ctrans('texts.eps');
|
||||
case self::DIRECT_DEBIT:
|
||||
return ctrans('texts.payment_type_direct_debit');
|
||||
default:
|
||||
return 'Undefined.';
|
||||
break;
|
||||
|
@ -50,6 +50,7 @@ class PaymentType extends StaticModel
|
||||
const GIROPAY = 39;
|
||||
const PRZELEWY24 = 40;
|
||||
const EPS = 41;
|
||||
const DIRECT_DEBIT = 42;
|
||||
|
||||
public static function parseCardType($cardName)
|
||||
{
|
||||
|
@ -56,6 +56,7 @@ class ACH implements MethodInterface
|
||||
try {
|
||||
$redirect = $this->go_cardless->gateway->redirectFlows()->create([
|
||||
"params" => [
|
||||
"scheme" => "ach",
|
||||
"session_token" => $session_token,
|
||||
"success_redirect_url" => route('client.payment_methods.confirm', [
|
||||
'method' => GatewayType::BANK_TRANSFER,
|
||||
|
248
app/PaymentDrivers/GoCardless/DirectDebit.php
Normal file
248
app/PaymentDrivers/GoCardless/DirectDebit.php
Normal file
@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\PaymentDrivers\GoCardless;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use App\Jobs\Mail\PaymentFailureMailer;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\Payment;
|
||||
use App\Models\PaymentType;
|
||||
use App\Models\SystemLog;
|
||||
use App\PaymentDrivers\Common\MethodInterface;
|
||||
use App\PaymentDrivers\GoCardlessPaymentDriver;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Routing\Redirector;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class DirectDebit implements MethodInterface
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
protected GoCardlessPaymentDriver $go_cardless;
|
||||
|
||||
public function __construct(GoCardlessPaymentDriver $go_cardless)
|
||||
{
|
||||
$this->go_cardless = $go_cardless;
|
||||
|
||||
$this->go_cardless->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle authorization for Direct Debit.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Redirector|RedirectResponse|void
|
||||
*/
|
||||
public function authorizeView(array $data)
|
||||
{
|
||||
$session_token = \Illuminate\Support\Str::uuid()->toString();
|
||||
|
||||
try {
|
||||
$redirect = $this->go_cardless->gateway->redirectFlows()->create([
|
||||
'params' => [
|
||||
'session_token' => $session_token,
|
||||
'success_redirect_url' => route('client.payment_methods.confirm', [
|
||||
'method' => GatewayType::DIRECT_DEBIT,
|
||||
'session_token' => $session_token,
|
||||
]),
|
||||
'prefilled_customer' => [
|
||||
'given_name' => auth('contact')->user()->first_name,
|
||||
'family_name' => auth('contact')->user()->last_name,
|
||||
'email' => auth('contact')->user()->email,
|
||||
'address_line1' => auth('contact')->user()->client->address1,
|
||||
'city' => auth('contact')->user()->client->city,
|
||||
'postal_code' => auth('contact')->user()->client->postal_code,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
return redirect(
|
||||
$redirect->redirect_url
|
||||
);
|
||||
} catch (\Exception $exception) {
|
||||
return $this->processUnsuccessfulAuthorization($exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle unsuccessful authorization.
|
||||
*
|
||||
* @param Exception $exception
|
||||
* @throws PaymentFailed
|
||||
* @return void
|
||||
*/
|
||||
public function processUnsuccessfulAuthorization(\Exception $exception): void
|
||||
{
|
||||
SystemLogger::dispatch(
|
||||
$exception->getMessage(),
|
||||
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||
SystemLog::EVENT_GATEWAY_FAILURE,
|
||||
SystemLog::TYPE_GOCARDLESS,
|
||||
$this->go_cardless->client,
|
||||
$this->go_cardless->client->company,
|
||||
);
|
||||
|
||||
throw new PaymentFailed($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle authorization response for Direct Debit.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return RedirectResponse|void
|
||||
*/
|
||||
public function authorizeResponse(Request $request)
|
||||
{
|
||||
try {
|
||||
$redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete(
|
||||
$request->redirect_flow_id,
|
||||
['params' => [
|
||||
'session_token' => $request->session_token
|
||||
]],
|
||||
);
|
||||
|
||||
$payment_meta = new \stdClass;
|
||||
$payment_meta->brand = ctrans('texts.payment_type_direct_debit');
|
||||
$payment_meta->type = GatewayType::DIRECT_DEBIT;
|
||||
$payment_meta->state = 'authorized';
|
||||
|
||||
$data = [
|
||||
'payment_meta' => $payment_meta,
|
||||
'token' => $redirect_flow->links->mandate,
|
||||
'payment_method_id' => GatewayType::DIRECT_DEBIT,
|
||||
];
|
||||
|
||||
$payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $redirect_flow->links->customer]);
|
||||
|
||||
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
|
||||
} catch (\Exception $exception) {
|
||||
return $this->processUnsuccessfulAuthorization($exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Payment view for Direct Debit.
|
||||
*
|
||||
* @param array $data
|
||||
* @return View
|
||||
*/
|
||||
public function paymentView(array $data): View
|
||||
{
|
||||
$data['gateway'] = $this->go_cardless;
|
||||
$data['amount'] = $this->go_cardless->convertToGoCardlessAmount($data['total']['amount_with_fee'], $this->go_cardless->client->currency()->precision);
|
||||
$data['currency'] = $this->go_cardless->client->getCurrencyCode();
|
||||
|
||||
return render('gateways.gocardless.direct_debit.pay', $data);
|
||||
}
|
||||
|
||||
public function paymentResponse(PaymentResponseRequest $request)
|
||||
{
|
||||
$token = ClientGatewayToken::find(
|
||||
$this->decodePrimaryKey($request->source)
|
||||
)->firstOrFail();
|
||||
|
||||
try {
|
||||
$payment = $this->go_cardless->gateway->payments()->create([
|
||||
'params' => [
|
||||
'amount' => $request->amount,
|
||||
'currency' => $request->currency,
|
||||
'metadata' => [
|
||||
'payment_hash' => $this->go_cardless->payment_hash->hash,
|
||||
],
|
||||
'links' => [
|
||||
'mandate' => $token->token,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
|
||||
if ($payment->status === 'pending_submission') {
|
||||
return $this->processPendingPayment($payment, ['token' => $token->hashed_id]);
|
||||
}
|
||||
|
||||
return $this->processUnsuccessfulPayment($payment);
|
||||
} catch (\Exception $exception) {
|
||||
throw new PaymentFailed($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle pending payments for Direct Debit.
|
||||
*
|
||||
* @param ResourcesPayment $payment
|
||||
* @param array $data
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function processPendingPayment(\GoCardlessPro\Resources\Payment $payment, array $data = [])
|
||||
{
|
||||
$data = [
|
||||
'payment_method' => $data['token'],
|
||||
'payment_type' => PaymentType::DIRECT_DEBIT,
|
||||
'amount' => $this->go_cardless->payment_hash->data->amount_with_fee,
|
||||
'transaction_reference' => $payment->id,
|
||||
'gateway_type_id' => GatewayType::DIRECT_DEBIT,
|
||||
];
|
||||
|
||||
$payment = $this->go_cardless->createPayment($data, Payment::STATUS_PENDING);
|
||||
|
||||
SystemLogger::dispatch(
|
||||
['response' => $payment, 'data' => $data],
|
||||
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||
SystemLog::EVENT_GATEWAY_SUCCESS,
|
||||
SystemLog::TYPE_GOCARDLESS,
|
||||
$this->go_cardless->client,
|
||||
$this->go_cardless->client->company,
|
||||
);
|
||||
|
||||
return redirect()->route('client.payments.show', ['payment' => $this->go_cardless->encodePrimaryKey($payment->id)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process unsuccessful payments for Direct Debit.
|
||||
*
|
||||
* @param ResourcesPayment $payment
|
||||
* @return never
|
||||
*/
|
||||
public function processUnsuccessfulPayment(\GoCardlessPro\Resources\Payment $payment)
|
||||
{
|
||||
PaymentFailureMailer::dispatch($this->go_cardless->client, $payment->status, $this->go_cardless->client->company, $this->go_cardless->payment_hash->data->amount_with_fee);
|
||||
|
||||
PaymentFailureMailer::dispatch(
|
||||
$this->go_cardless->client,
|
||||
$payment,
|
||||
$this->go_cardless->client->company,
|
||||
$payment->amount
|
||||
);
|
||||
|
||||
$message = [
|
||||
'server_response' => $payment,
|
||||
'data' => $this->go_cardless->payment_hash->data,
|
||||
];
|
||||
|
||||
SystemLogger::dispatch(
|
||||
$message,
|
||||
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||
SystemLog::EVENT_GATEWAY_FAILURE,
|
||||
SystemLog::TYPE_GOCARDLESS,
|
||||
$this->go_cardless->client,
|
||||
$this->go_cardless->client->company,
|
||||
);
|
||||
|
||||
throw new PaymentFailed('Failed to process the payment.', 500);
|
||||
}
|
||||
}
|
@ -37,6 +37,7 @@ class GoCardlessPaymentDriver extends BaseDriver
|
||||
|
||||
public static $methods = [
|
||||
GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\ACH::class,
|
||||
GatewayType::DIRECT_DEBIT => \App\PaymentDrivers\GoCardless\DirectDebit::class,
|
||||
GatewayType::SEPA => \App\PaymentDrivers\GoCardless\SEPA::class,
|
||||
];
|
||||
|
||||
@ -63,6 +64,14 @@ class GoCardlessPaymentDriver extends BaseDriver
|
||||
$types[] = GatewayType::BANK_TRANSFER;
|
||||
}
|
||||
|
||||
if (
|
||||
$this->client
|
||||
&& isset($this->client->country)
|
||||
&& in_array($this->client->country->iso_3166_3, ['GBR'])
|
||||
) {
|
||||
$types[] = GatewayType::DIRECT_DEBIT;
|
||||
}
|
||||
|
||||
if ($this->client->currency()->code === 'EUR') {
|
||||
$types[] = GatewayType::SEPA;
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\PaymentType;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddDirectDebitToPaymentTypes extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('payment_types', function (Blueprint $table) {
|
||||
$type = new PaymentType();
|
||||
|
||||
$type->id = 42;
|
||||
$type->name = 'Direct Debit';
|
||||
$type->gateway_type_id = GatewayType::DIRECT_DEBIT;
|
||||
|
||||
$type->save();
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use App\Models\GatewayType;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AddGatewayTypeForDirectDebit extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$type = new GatewayType();
|
||||
|
||||
$type->id = 18;
|
||||
$type->alias = 'direct_debit';
|
||||
$type->name = 'Direct Debit';
|
||||
|
||||
$type->save();
|
||||
}
|
||||
}
|
@ -4328,9 +4328,9 @@ $LANG = array(
|
||||
'giropay_law' => 'By entering your Customer information (such as name, sort code and account number) you (the Customer) agree that this information is given voluntarily.',
|
||||
'eps' => 'EPS',
|
||||
'you_need_to_accept_the_terms_before_proceeding' => 'You need to accept the terms before proceeding.',
|
||||
'direct_debit' => 'Direct Debit',
|
||||
'clone_to_expense' => 'Clone to expense',
|
||||
'checkout' => 'Checkout',
|
||||
|
||||
);
|
||||
|
||||
return $LANG;
|
||||
|
@ -0,0 +1,56 @@
|
||||
@extends('portal.ninja2020.layout.payments', ['gateway_title' => 'Direct Debit', 'card_title' => 'Direct Debit'])
|
||||
|
||||
@section('gateway_content')
|
||||
@if (count($tokens) > 0)
|
||||
<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
|
||||
<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="">
|
||||
<input type="hidden" name="amount" value="{{ $amount }}">
|
||||
<input type="hidden" name="currency" value="{{ $currency }}">
|
||||
<input type="hidden" name="payment_hash" value="{{ $payment_hash }}">
|
||||
</form>
|
||||
|
||||
@component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.pay_with')])
|
||||
@if (count($tokens) > 0)
|
||||
@foreach ($tokens as $token)
|
||||
<label class="mr-4">
|
||||
<input type="radio" data-token="{{ $token->hashed_id }}" name="payment-type"
|
||||
class="form-radio cursor-pointer toggle-payment-with-token" />
|
||||
<span class="ml-1 cursor-pointer">{{ ctrans('texts.payment_type_direct_debit') }}
|
||||
(#{{ $token->hashed_id }})</span>
|
||||
</label>
|
||||
@endforeach
|
||||
@endisset
|
||||
@endcomponent
|
||||
|
||||
@else
|
||||
@component('portal.ninja2020.components.general.card-element-single', ['title' => 'Direct Debit', '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
|
||||
|
||||
@include('portal.ninja2020.gateways.includes.pay_now')
|
||||
@endsection
|
||||
|
||||
@push('footer')
|
||||
<script>
|
||||
Array
|
||||
.from(document.getElementsByClassName('toggle-payment-with-token'))
|
||||
.forEach((element) => element.addEventListener('click', (element) => {
|
||||
document.querySelector('input[name=source]').value = element.target.dataset.token;
|
||||
}));
|
||||
|
||||
document.getElementById('pay-now').addEventListener('click', function() {
|
||||
document.getElementById('server-response').submit();
|
||||
});
|
||||
</script>
|
||||
@endpush
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\ClientPortal\Gateways\GoCardless;
|
||||
|
||||
use App\Models\CompanyGateway;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\Browser\Pages\ClientPortal\Login;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class DirectDebitTest extends DuskTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
foreach (static::$browsers as $browser) {
|
||||
$browser->driver->manage()->deleteAllCookies();
|
||||
}
|
||||
|
||||
$this->disableCompanyGateways();
|
||||
|
||||
CompanyGateway::where('gateway_key', 'b9886f9257f0c6ee7c302f1c74475f6c')->restore();
|
||||
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser
|
||||
->visit(new Login())
|
||||
->auth();
|
||||
});
|
||||
}
|
||||
|
||||
public function testPayingWithNoPreauthorizedIsntPossible()
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser
|
||||
->visitRoute('client.invoices.index')
|
||||
->click('@pay-now')
|
||||
->press('Pay Now')
|
||||
->clickLink('Direct Debit')
|
||||
->assertSee('To pay with a bank account, first you have to add it as payment method.');
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user