diff --git a/app/Models/Country.php b/app/Models/Country.php index 3b490892b765..45a840f4c9ba 100644 --- a/app/Models/Country.php +++ b/app/Models/Country.php @@ -35,4 +35,8 @@ class Country extends StaticModel { return trans('texts.country_'.$this->name); } + public function getID() :string + { + return $this->id; + } } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index b44e63ceb755..c6a4f9fdbcc9 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -20,6 +20,7 @@ use App\Http\Requests\Request; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; +use App\Models\Country; use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; @@ -118,7 +119,7 @@ class StripePaymentDriver extends BaseDriver throw new StripeConnectFailure('Stripe Connect has not been configured'); } } else { - + $this->stripe = new StripeClient( $this->company_gateway->getConfigField('apiKey') ); @@ -240,12 +241,19 @@ class StripePaymentDriver extends BaseDriver } if ($this->client && $this->client->currency() - && in_array($this->client->currency()->code, ['EUR', 'DKK', 'GBP', 'NOK', 'SEK', 'USD', 'AUD', 'NZD', 'CAD', 'PLN', 'CHF']) + && in_array($this->client->currency()->code, ['EUR', 'DKK', 'GBP', 'NOK', 'SEK', 'AUD', 'NZD', 'CAD', 'PLN', 'CHF']) && isset($this->client->country) + && in_array($this->client->country->iso_3166_3, ['AUT','BEL','DNK','FIN','FRA','DEU','IRL','ITA','NLD','NOR','ESP','SWE','GBR'])) { + $types[] = GatewayType::KLARNA; + } + if ($this->client + && $this->client->currency() + && in_array($this->client->currency()->code, ['EUR', 'DKK', 'GBP', 'NOK', 'SEK', 'AUD', 'NZD', 'CAD', 'PLN', 'CHF', 'USD']) + && isset($this->client->country) + && in_array($this->client->company->country()->getID(), ['840']) && in_array($this->client->country->iso_3166_3, ['AUT','BEL','DNK','FIN','FRA','DEU','IRL','ITA','NLD','NOR','ESP','SWE','GBR','USA'])) { $types[] = GatewayType::KLARNA; } - if ( $this->client && isset($this->client->country) diff --git a/lang/en/texts.php b/lang/en/texts.php index ed0b75459202..3f2371032171 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -2503,6 +2503,7 @@ $LANG = array( 'alipay' => 'Alipay', 'sofort' => 'Sofort', 'sepa' => 'SEPA Direct Debit', + 'name_without_special_characters' => 'Please enter a name with only the letters a-z and whitespaces', 'enable_alipay' => 'Accept Alipay', 'enable_sofort' => 'Accept EU bank transfers', 'stripe_alipay_help' => 'These gateways also need to be activated in :link.', diff --git a/public/js/clients/payments/stripe-klarna.js b/public/js/clients/payments/stripe-klarna.js new file mode 100644 index 000000000000..fb5dba3f9a0a --- /dev/null +++ b/public/js/clients/payments/stripe-klarna.js @@ -0,0 +1,2 @@ +/*! For license information please see stripe-klarna.js.LICENSE.txt */ +(()=>{var e,t,n,r;function o(e,t){for(var n=0;n svg").classList.add("hidden"),document.querySelector("#pay-now > span").classList.remove("hidden"),r.errors.textContent="",r.errors.textContent=e,r.errors.hidden=!1})),c(this,"handle",(function(){document.getElementById("pay-now").addEventListener("click",(function(e){var t=document.getElementById("errors"),n=document.getElementById("klarna-name").value;/^[A-Za-z\s]*$/.test(n)?(document.getElementById("pay-now").disabled=!0,document.querySelector("#pay-now > svg").classList.remove("hidden"),document.querySelector("#pay-now > span").classList.add("hidden"),r.stripe.confirmKlarnaPayment(document.querySelector("meta[name=pi-client-secret").content,{payment_method:{billing_details:{name:n,email:document.querySelector("meta[name=email]").content,address:{line1:document.querySelector("meta[name=address-1]").content,line2:document.querySelector("meta[name=address-2]").content,city:document.querySelector("meta[name=city]").content,postal_code:document.querySelector("meta[name=postal_code]").content,state:document.querySelector("meta[name=state]").content,country:document.querySelector("meta[name=country]").content}}},return_url:document.querySelector('meta[name="return-url"]').content}).then((function(e){if(e.hasOwnProperty("error"))return r.handleError(e.error.message)}))):(document.getElementById("klarna-name-correction").hidden=!1,document.getElementById("klarna-name").textContent=n.replace(/^[A-Za-z\s]*$/,""),document.getElementById("klarna-name").focus(),t.textContent=document.querySelector("meta[name=translation-name-without-special-characters]").content,t.hidden=!1)}))})),this.key=t,this.errors=document.getElementById("errors"),this.stripeConnect=n}));new i(null!==(e=null===(t=document.querySelector('meta[name="stripe-publishable-key"]'))||void 0===t?void 0:t.content)&&void 0!==e?e:"",null!==(n=null===(r=document.querySelector('meta[name="stripe-account-id"]'))||void 0===r?void 0:r.content)&&void 0!==n?n:"").setupStripe().handle()})(); \ No newline at end of file diff --git a/public/js/clients/payments/stripe-klarna.js.LICENSE.txt b/public/js/clients/payments/stripe-klarna.js.LICENSE.txt new file mode 100644 index 000000000000..97e5374d6e40 --- /dev/null +++ b/public/js/clients/payments/stripe-klarna.js.LICENSE.txt @@ -0,0 +1,9 @@ +/** + * 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://www.elastic.co/licensing/elastic-license + */ diff --git a/resources/js/clients/payments/stripe-klarna.js b/resources/js/clients/payments/stripe-klarna.js index 67c379abd99d..cebbb6e42fcb 100644 --- a/resources/js/clients/payments/stripe-klarna.js +++ b/resources/js/clients/payments/stripe-klarna.js @@ -18,10 +18,10 @@ class ProcessKlarna { setupStripe = () => { if (this.stripeConnect){ - // this.stripe.stripeAccount = this.stripeConnect; + // this.stripe.stripeAccount = this.stripeConnect; - this.stripe = Stripe(this.key, { - stripeAccount: this.stripeConnect, + this.stripe = Stripe(this.key, { + stripeAccount: this.stripeConnect, }); } @@ -46,38 +46,47 @@ class ProcessKlarna { handle = () => { document.getElementById('pay-now').addEventListener('click', (e) => { let errors = document.getElementById('errors'); - - document.getElementById('pay-now').disabled = true; - document.querySelector('#pay-now > svg').classList.remove('hidden'); - document.querySelector('#pay-now > span').classList.add('hidden'); - - this.stripe.confirmKlarnaPayment( - document.querySelector('meta[name=pi-client-secret').content, - { - payment_method: { - billing_details: { - name: document.getElementById("klarna-name").value, - email: document.querySelector('meta[name=email').content, - address: { - line1: document.querySelector('input[name=address1]').value, - line2: document.querySelector('input[name=address2]').value, - city: document.querySelector('input[name=city]').value, - postal_code: document.querySelector('input[name=postal_code]').value, - state: document.querySelector('input[name=state]').value, - country: document.querySelector('meta[name=country').content, - } + let name = document.getElementById("klarna-name").value; + if (! /^[A-Za-z\s]*$/.test(name)){ + document.getElementById('klarna-name-correction').hidden = false; + document.getElementById('klarna-name').textContent = name.replace(/^[A-Za-z\s]*$/, "") + document.getElementById('klarna-name').focus(); + errors.textContent = document.querySelector( + 'meta[name=translation-name-without-special-characters]' + ).content; + errors.hidden = false; + } + else { + document.getElementById('pay-now').disabled = true; + document.querySelector('#pay-now > svg').classList.remove('hidden'); + document.querySelector('#pay-now > span').classList.add('hidden'); + this.stripe.confirmKlarnaPayment( + document.querySelector('meta[name=pi-client-secret').content, + { + payment_method: { + billing_details: { + name: name, + email: document.querySelector('meta[name=email]').content, + address: { + line1: document.querySelector('meta[name=address-1]').content, + line2: document.querySelector('meta[name=address-2]').content, + city: document.querySelector('meta[name=city]').content, + postal_code: document.querySelector('meta[name=postal_code]').content, + state: document.querySelector('meta[name=state]').content, + country: document.querySelector('meta[name=country]').content, + } + }, }, - }, - return_url: document.querySelector( - 'meta[name="return-url"]' - ).content, - } - ).then((result) => { - if (result.hasOwnProperty('error')) { - return this.handleError(result.error.message); - } + return_url: document.querySelector( + 'meta[name="return-url"]' + ).content, + } + ).then((result) => { + if (result.hasOwnProperty('error')) { + return this.handleError(result.error.message); + } - });; + });} }); }; } diff --git a/resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php b/resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php index 69bda5a6c29c..4306e1bd5fad 100644 --- a/resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php +++ b/resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php @@ -1,68 +1,7 @@ -
+