From af78a3dd2df9aed9610330016a8d1f31cfecccb2 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 29 Jul 2016 14:58:26 +0300 Subject: [PATCH] Test Stripe credentials when adding gateway --- app/Http/Controllers/AccountGatewayController.php | 14 +++++++++++++- app/Ninja/PaymentDrivers/BasePaymentDriver.php | 5 +++++ app/Ninja/PaymentDrivers/StripePaymentDriver.php | 15 +++++++++++++++ resources/lang/en/texts.php | 2 ++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/AccountGatewayController.php b/app/Http/Controllers/AccountGatewayController.php index 740e78246f23..d0ce1b6f62e0 100644 --- a/app/Http/Controllers/AccountGatewayController.php +++ b/app/Http/Controllers/AccountGatewayController.php @@ -60,6 +60,8 @@ class AccountGatewayController extends BaseController $data['hiddenFields'] = Gateway::$hiddenFields; $data['selectGateways'] = Gateway::where('id', '=', $accountGateway->gateway_id)->get(); + $this->testGateway($accountGateway); + return View::make('accounts.account_gateway', $data); } @@ -307,7 +309,7 @@ class AccountGatewayController extends BaseController $account->account_gateways()->save($accountGateway); } - if(isset($wepayResponse)) { + if (isset($wepayResponse)) { return $wepayResponse; } else { if ($accountGatewayPublicId) { @@ -322,6 +324,16 @@ class AccountGatewayController extends BaseController } } + private function testGateway($accountGateway) + { + $paymentDriver = $accountGateway->paymentDriver(); + $result = $paymentDriver->isValid(); + + if ($result !== true) { + Session::flash('error', $result . ' - ' . trans('texts.gateway_config_error')); + } + } + protected function getWePayUpdateUri($accountGateway) { if ($accountGateway->gateway_id != GATEWAY_WEPAY) { diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index c6b13d2d8227..7c9c5c2ea710 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -44,6 +44,11 @@ class BasePaymentDriver return $this->accountGateway->gateway_id == $gatewayId; } + public function isValid() + { + return true; + } + // optionally pass a paymentMethod to determine the type from the token protected function isGatewayType($gatewayType, $paymentMethod = false) { diff --git a/app/Ninja/PaymentDrivers/StripePaymentDriver.php b/app/Ninja/PaymentDrivers/StripePaymentDriver.php index d2da7bab42ee..088f09c19208 100644 --- a/app/Ninja/PaymentDrivers/StripePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/StripePaymentDriver.php @@ -39,6 +39,21 @@ class StripePaymentDriver extends BasePaymentDriver return $rules; } + public function isValid() + { + $result = $this->makeStripeCall( + 'GET', + 'charges', + 'limit=1' + ); + + if (array_get($result, 'object') == 'list') { + return true; + } else { + return $result; + } + } + protected function checkCustomerExists($customer) { $response = $this->gateway() diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 405148a08ccb..230ceb74ca52 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2049,6 +2049,8 @@ $LANG = array( 'no_contact_selected' => 'Please select a contact', 'no_client_selected' => 'Please select a client', + 'gateway_config_error' => 'It may help to set new passwords or generate new API keys.', + ); return $LANG;