diff --git a/app/PaymentDrivers/GoCardless/ACH.php b/app/PaymentDrivers/GoCardless/ACH.php index e620db4d56ba..055a313563f9 100644 --- a/app/PaymentDrivers/GoCardless/ACH.php +++ b/app/PaymentDrivers/GoCardless/ACH.php @@ -164,6 +164,8 @@ class ACH implements MethodInterface $this->decodePrimaryKey($request->source) )->firstOrFail(); + $this->go_cardless->ensureMandateIsReady($token); + try { $payment = $this->go_cardless->gateway->payments()->create([ 'params' => [ diff --git a/app/PaymentDrivers/GoCardless/DirectDebit.php b/app/PaymentDrivers/GoCardless/DirectDebit.php index 5b9207ad1205..fae242949242 100644 --- a/app/PaymentDrivers/GoCardless/DirectDebit.php +++ b/app/PaymentDrivers/GoCardless/DirectDebit.php @@ -156,6 +156,8 @@ class DirectDebit implements MethodInterface $this->decodePrimaryKey($request->source) )->firstOrFail(); + $this->go_cardless->ensureMandateIsReady($token); + try { $payment = $this->go_cardless->gateway->payments()->create([ 'params' => [ diff --git a/app/PaymentDrivers/GoCardless/SEPA.php b/app/PaymentDrivers/GoCardless/SEPA.php index 9eef98e8fde7..35ffef5b91a8 100644 --- a/app/PaymentDrivers/GoCardless/SEPA.php +++ b/app/PaymentDrivers/GoCardless/SEPA.php @@ -164,6 +164,8 @@ class SEPA implements MethodInterface $this->decodePrimaryKey($request->source) )->firstOrFail(); + $this->go_cardless->ensureMandateIsReady($token); + try { $payment = $this->go_cardless->gateway->payments()->create([ 'params' => [ diff --git a/app/PaymentDrivers/GoCardlessPaymentDriver.php b/app/PaymentDrivers/GoCardlessPaymentDriver.php index b2d298c42319..db2dd56d155b 100644 --- a/app/PaymentDrivers/GoCardlessPaymentDriver.php +++ b/app/PaymentDrivers/GoCardlessPaymentDriver.php @@ -11,6 +11,7 @@ namespace App\PaymentDrivers; +use App\Events\Payment\PaymentFailed; use App\Http\Requests\Payments\PaymentWebhookRequest; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; @@ -258,4 +259,17 @@ class GoCardlessPaymentDriver extends BaseDriver return response()->json([], 200); } + + public function ensureMandateIsReady(ClientGatewayToken $cgt) + { + try { + $mandate = $this->gateway->mandates()->get($cgt->token); + + if ($mandate->status !== 'active') { + throw new \Exception(ctrans('texts.gocardless_mandate_not_ready')); + } + } catch (\Exception $exception) { + throw new \App\Exceptions\PaymentFailed($exception->getMessage()); + } + } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index d211e72d2c29..14af04fc3f09 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4337,7 +4337,8 @@ $LANG = array( 'invalid_amount' => 'Invalid amount. Number/Decimal values only.', 'client_payment_failure_body' => 'Payment for Invoice :invoice for amount :amount failed.', 'browser_pay' => 'Google Pay, Apple Pay, Microsoft Pay', - 'no_available_methods' => 'We can\'t find any credit cards on your device. Read more about this.' + 'no_available_methods' => 'We can\'t find any credit cards on your device. Read more about this.', + 'gocardless_mandate_not_ready' => 'Payment mandate is not ready. Please try again later.', ); return $LANG;