From 5a3d5b8eea25c6016e73882f6b9653deac509499 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 5 Sep 2017 16:37:19 +0300 Subject: [PATCH] Add SOFORT support on Stripe #1338 --- .../Controllers/AccountGatewayController.php | 1 + app/Models/AccountGateway.php | 8 ++++ app/Models/Country.php | 1 + .../PaymentDrivers/BasePaymentDriver.php | 8 +++- .../PaymentDrivers/StripePaymentDriver.php | 41 ++++++++++++++++--- resources/lang/en/texts.php | 7 +++- .../views/accounts/account_gateway.blade.php | 21 ++++------ 7 files changed, 66 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/AccountGatewayController.php b/app/Http/Controllers/AccountGatewayController.php index e28a1cc949..ee0c11f8b0 100644 --- a/app/Http/Controllers/AccountGatewayController.php +++ b/app/Http/Controllers/AccountGatewayController.php @@ -296,6 +296,7 @@ class AccountGatewayController extends BaseController if ($gatewayId == GATEWAY_STRIPE) { $config->enableAlipay = boolval(Input::get('enable_alipay')); + $config->enableSofort = boolval(Input::get('enable_sofort')); } if ($gatewayId == GATEWAY_STRIPE || $gatewayId == GATEWAY_WEPAY) { diff --git a/app/Models/AccountGateway.php b/app/Models/AccountGateway.php index 2b0acadf94..846c1605d3 100644 --- a/app/Models/AccountGateway.php +++ b/app/Models/AccountGateway.php @@ -152,6 +152,14 @@ class AccountGateway extends EntityModel return ! empty($this->getConfigField('enableAlipay')); } + /** + * @return bool + */ + public function getSofortEnabled() + { + return ! empty($this->getConfigField('enableSofort')); + } + /** * @return bool */ diff --git a/app/Models/Country.php b/app/Models/Country.php index 410e1595b6..34bd9746e5 100644 --- a/app/Models/Country.php +++ b/app/Models/Country.php @@ -24,6 +24,7 @@ class Country extends Eloquent 'swap_currency_symbol', 'thousand_separator', 'decimal_separator', + 'iso_3166_3', ]; /** diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 3ca97f6b21..d11badbbe5 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -142,7 +142,7 @@ class BasePaymentDriver } // For these gateway types we use the API directrly rather than Omnipay - if (in_array($this->gatewayType, [GATEWAY_TYPE_ALIPAY])) { + if ($this->shouldUseSource()) { return $this->createSource(); } @@ -520,6 +520,12 @@ class BasePaymentDriver ]; } + public function shouldUseSource() + { + // Use Omnipay by default + return false; + } + protected function shouldCreateToken() { if ($this->isGatewayType(GATEWAY_TYPE_BANK_TRANSFER)) { diff --git a/app/Ninja/PaymentDrivers/StripePaymentDriver.php b/app/Ninja/PaymentDrivers/StripePaymentDriver.php index 006b3951b6..a2c51921e7 100644 --- a/app/Ninja/PaymentDrivers/StripePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/StripePaymentDriver.php @@ -22,12 +22,13 @@ class StripePaymentDriver extends BasePaymentDriver GATEWAY_TYPE_TOKEN, ]; - if ($this->accountGateway && $this->accountGateway->getAchEnabled()) { - $types[] = GATEWAY_TYPE_BANK_TRANSFER; - } - - if ($this->accountGateway && $this->accountGateway->getAlipayEnabled()) { - $types[] = GATEWAY_TYPE_ALIPAY; + if ($gateway = $this->accountGateway) { + if ($gateway->getAchEnabled() || $gateway->getSofortEnabled()) { + $types[] = GATEWAY_TYPE_BANK_TRANSFER; + } + if ($gateway->getAlipayEnabled()) { + $types[] = GATEWAY_TYPE_ALIPAY; + } } return $types; @@ -64,6 +65,34 @@ class StripePaymentDriver extends BasePaymentDriver } } + public function shouldUseSource() + { + if (in_array($this->gatewayType, [GATEWAY_TYPE_ALIPAY])) { + return true; + } + + if ($this->gatewayType == GATEWAY_TYPE_BANK_TRANSFER) { + $achEnabled = $this->accountGateway->getAchEnabled(); + $sofortEnabled = $this->accountGateway->getSofortEnabled(); + if ($sofortEnabled) { + if (! $achEnabled) { + return true; + } + if ($country = $this->client()->country) { + $country = $country->iso_3166_3; + } elseif ($country = $this->account()->country) { + $country = $country->iso_3166_3; + } + // https://stripe.com/docs/sources/sofort + if ($country && in_array($country, ['AUT', 'BEL', 'DEU', 'ITA', 'NLD', 'ESP'])) { + return true; + } + } + } + + return false; + } + protected function checkCustomerExists($customer) { $response = $this->gateway() diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index cf3ca0276c..5f1b89bf70 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -1213,7 +1213,7 @@ $LANG = array( 'payment_type_stripe' => 'Stripe', 'ach' => 'ACH', - 'enable_ach' => 'Enable ACH', + 'enable_ach' => 'Accept US bank transfers', 'stripe_ach_help' => 'ACH support must also be enabled in :link.', 'ach_disabled' => 'Another gateway is already configured for direct debit.', @@ -2439,9 +2439,12 @@ $LANG = array( 'deleted_account_details' => 'Your account (:account) has been successfully deleted.', 'alipay' => 'Alipay', - 'enable_alipay' => 'Enable Alipay', + 'sofort' => 'Sofort', + 'enable_alipay' => 'Accept Alipay', + 'enable_sofort' => 'Accept EU bank transfers', 'stripe_alipay_help' => 'These gateways also need to be activated in :link.', + ); return $LANG; diff --git a/resources/views/accounts/account_gateway.blade.php b/resources/views/accounts/account_gateway.blade.php index 290c08ba0f..83412512c6 100644 --- a/resources/views/accounts/account_gateway.blade.php +++ b/resources/views/accounts/account_gateway.blade.php @@ -19,6 +19,7 @@ {!! Former::populateField('update_address', intval($accountGateway->update_address)) !!} {!! Former::populateField('publishable_key', $accountGateway->getPublishableStripeKey() ? str_repeat('*', strlen($accountGateway->getPublishableStripeKey())) : '') !!} {!! Former::populateField('enable_ach', $accountGateway->getAchEnabled() ? 1 : 0) !!} + {!! Former::populateField('enable_sofort', $accountGateway->getSofortEnabled() ? 1 : 0) !!} {!! Former::populateField('enable_alipay', $accountGateway->getAlipayEnabled() ? 1 : 0) !!} {!! Former::populateField('enable_paypal', $accountGateway->getPayPalEnabled() ? 1 : 0) !!} {!! Former::populateField('plaid_client_id', $accountGateway->getPlaidClientId() ? str_repeat('*', strlen($accountGateway->getPlaidClientId())) : '') !!} @@ -154,6 +155,11 @@ ->text(trans('texts.enable_ach')) ->value(1) !!} + {!! Former::checkbox('enable_sofort') + ->label(trans('texts.sofort')) + ->text(trans('texts.enable_sofort')) + ->value(1) !!} + {!! Former::checkbox('enable_alipay') ->label(trans('texts.alipay')) ->text(trans('texts.enable_alipay')) @@ -252,21 +258,11 @@ } } - function onEnableAchChanged() { - var visible = $('#enable_ach').is(':checked'); - $('.stripe-webhook-options').toggle(visible); - $('.stripe-ach-options').toggle(visible); - } - - function onEnableAlipayChanged() { - var visible = $('#enable_alipay').is(':checked'); - $('.stripe-webhook-options').toggle(visible); - } - function updateWebhookShown() { var enableAch = $('#enable_ach').is(':checked'); var enableAlipay = $('#enable_alipay').is(':checked'); - $('.stripe-webhook-options').toggle(enableAch || enableAlipay); + var enableSofort = $('#enable_sofort').is(':checked'); + $('.stripe-webhook-options').toggle(enableAch || enableAlipay || enableSofort); $('.stripe-ach-options').toggle(enableAch); } @@ -282,6 +278,7 @@ $('#enable_ach').change(updateWebhookShown); $('#enable_alipay').change(updateWebhookShown); + $('#enable_sofort').change(updateWebhookShown); @if (!$accountGateway && count($secondaryGateways)) $('#primary_gateway_id').append($('