Skip to content

Commit

Permalink
Add action to cancel a GoCardless payment
Browse files Browse the repository at this point in the history
  • Loading branch information
rjackson committed Apr 7, 2024
1 parent 46df151 commit c085e29
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/Helpers/GoCardlessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function getPayment($paymentId)
return $this->client->payments()->get($paymentId);
}

public function cancelPayment($paymentId)
{
return $this->client->payments()->cancel($paymentId);
}

public function newPreAuthUrl($user, $paymentDetails)
{
$redirectFlow = $this->client->redirectFlows()->create([
Expand Down
22 changes: 18 additions & 4 deletions app/Http/Controllers/GoCardlessPaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BB\Http\Controllers;

use BB\Entities\Payment;
use BB\Entities\User;
use Exception;
use GoCardlessPro\Core\Exception\InvalidStateException;
Expand Down Expand Up @@ -100,8 +101,7 @@ private function handleBill($amount, $reason, $user, $ref, $returnPath)
\FlashNotification::success("The payment was submitted successfully");

return \Redirect::to($returnPath);
}
catch (InvalidStateException | ValidationFailedException $e) {
} catch (InvalidStateException | ValidationFailedException $e) {
$status = 'failed';
$this->paymentRepository->recordPayment($reason, $user->id, 'gocardless-variable', null, $amount, $status, 0, $ref);

Expand All @@ -111,8 +111,7 @@ private function handleBill($amount, $reason, $user, $ref, $returnPath)

\FlashNotification::error("We were unable to take payment from your account. Please try again.");
return \Redirect::to($returnPath);
}
catch (Exception $e) {
} catch (Exception $e) {
// Genuine app exception... needs investigation
\Log::info($e);

Expand Down Expand Up @@ -173,4 +172,19 @@ private function getReference($reason)
}
return false;
}

public function cancel(Payment $payment)
{
if ($payment->status != 'pending') {
\FlashNotification::error("The payment could not be cancelled");
return \Redirect::to(\Request::get('return_path'));
}

$this->goCardless->cancelPayment($payment->source_id);
\FlashNotification::success("Cancellation request sent to GoCardless");

// The payment log will be updated from a webhook once GoCardless has actioned the cancellation

return \Redirect::to(\Request::get('return_path'));
}
}
12 changes: 12 additions & 0 deletions resources/views/payments/index-row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
{!! Form::close() !!}
</li>
@endif
@if ($payment->source == 'gocardless-variable' && $payment->status == \BB\Entities\Payment::STATUS_PENDING)
<li>
{!! Form::open([
'method' => 'POST',
'route' => ['payment.gocardless.cancel', $payment],
'class' => 'navbar-form navbar-left'
]) !!}
{!! Form::hidden('cancel', 'confirm-gocardless-variable') !!}
{!! Form::submit('Cancel payment', array('class'=>'btn btn-link')) !!}
{!! Form::close() !!}
</li>
@endif
</ul>
</div>
</td>
Expand Down
7 changes: 6 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
Route::post('account/{account}/payment/gocardless', ['as' => 'account.payment.gocardless.create', 'uses' => 'GoCardlessPaymentController@create']);
Route::post('account/{account}/payment/balance', ['as' => 'account.payment.balance.create', 'uses' => 'BalancePaymentController@store']);
Route::post('account/{account}/payment/cash2', ['as' => 'account.payment.cash2.create', 'uses' => 'CashPaymentController@store']);
Route::post('payment/gocardless/{payment}/cancel', [
'as' => 'payment.gocardless.cancel',
'uses' => 'GoCardlessPaymentController@cancel',
'middleware' => 'role:finance'
]);


//Cash
Expand Down Expand Up @@ -280,7 +285,7 @@
##########################

// Route::group(array('middleware' => 'role:finance'), function () {
// Route::resource('expenses', 'ExpensesController');
// Route::resource('expenses', 'ExpensesController');
// });


Expand Down

0 comments on commit c085e29

Please sign in to comment.