From 695d64de5fb47433f17205af4181d4740eb4aec3 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 10 Oct 2021 06:47:03 +0200 Subject: [PATCH 1/7] Added models --- app/Models/Gateway.php | 1 + app/Models/GatewayType.php | 3 +++ app/Models/PaymentType.php | 1 + 3 files changed, 5 insertions(+) diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index abb0d2c50ac0..b632dfc7dff7 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -106,6 +106,7 @@ class Gateway extends StaticModel GatewayType::SOFORT => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], //Stripe GatewayType::SEPA => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], GatewayType::GIROPAY => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], + GatewayType::EPS => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], GatewayType::IDEAL => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']]]; case 39: diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index 6397cef7fc6e..eb2b3c891116 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -30,6 +30,7 @@ class GatewayType extends StaticModel const IDEAL = 13; const HOSTED_PAGE = 14; // For gateways that contain multiple methods. const GIROPAY = 15; + const EPS = 17; public function gateway() { @@ -72,6 +73,8 @@ class GatewayType extends StaticModel return ctrans('texts.aio_checkout'); case self::GIROPAY: return ctrans('texts.giropay'); + case self::EPS: + return ctrans('texts.EPS'); default: return 'Undefined.'; break; diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index a77b5f81addc..7999f22a8d7d 100644 --- a/app/Models/PaymentType.php +++ b/app/Models/PaymentType.php @@ -48,6 +48,7 @@ class PaymentType extends StaticModel const IDEAL = 37; const HOSTED_PAGE = 38; const GIROPAY = 39; + const EPS = 41; public static function parseCardType($cardName) { From b3d701278cc78141b38805378d5bf30e021562ee Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 10 Oct 2021 06:51:51 +0200 Subject: [PATCH 2/7] Add config for eps --- app/PaymentDrivers/Stripe/EPS.php | 141 +++++++++++++++++++++ app/PaymentDrivers/StripePaymentDriver.php | 11 ++ 2 files changed, 152 insertions(+) create mode 100644 app/PaymentDrivers/Stripe/EPS.php diff --git a/app/PaymentDrivers/Stripe/EPS.php b/app/PaymentDrivers/Stripe/EPS.php new file mode 100644 index 000000000000..10e7b560e22f --- /dev/null +++ b/app/PaymentDrivers/Stripe/EPS.php @@ -0,0 +1,141 @@ +stripe = $stripe; + } + + public function authorizeView($data) + { + return render('gateways.stripe.eps.authorize', $data); + } + + public function paymentView(array $data) + { + $data['gateway'] = $this->stripe; + $data['return_url'] = $this->buildReturnUrl(); + $data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()); + $data['client'] = $this->stripe->client; + $data['customer'] = $this->stripe->findOrCreateCustomer()->id; + $data['country'] = $this->stripe->client->country->iso_3166_2; + + $intent = \Stripe\PaymentIntent::create([ + 'amount' => $data['stripe_amount'], + 'currency' => 'eur', + 'payment_method_types' => ['eps'], + 'customer' => $this->stripe->findOrCreateCustomer(), + 'description' => $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')), + + ]); + + $data['pi_client_secret'] = $intent->client_secret; + + $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]); + $this->stripe->payment_hash->save(); + + return render('gateways.stripe.eps.pay', $data); + } + + private function buildReturnUrl(): string + { + return route('client.payments.response', [ + 'company_gateway_id' => $this->stripe->company_gateway->id, + 'payment_hash' => $this->stripe->payment_hash->hash, + 'payment_method_id' => GatewayType::EPS, + ]); + } + + public function paymentResponse($request) + { + $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all()); + $this->stripe->payment_hash->save(); + + if ($request->redirect_status == 'succeeded') { + return $this->processSuccessfulPayment($request->payment_intent); + } + + return $this->processUnsuccessfulPayment(); + } + + public function processSuccessfulPayment(string $payment_intent) + { + /* @todo: https://github.com/invoiceninja/invoiceninja/pull/3789/files#r436175798 */ + + $this->stripe->init(); + + $data = [ + 'payment_method' => $payment_intent, + 'payment_type' => PaymentType::EPS, + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), + 'transaction_reference' => $payment_intent, + 'gateway_type_id' => GatewayType::EPS, + ]; + + $this->stripe->createPayment($data, Payment::STATUS_PENDING); + + SystemLogger::dispatch( + ['response' => $this->stripe->payment_hash->data, 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + return redirect()->route('client.payments.index'); + } + + public function processUnsuccessfulPayment() + { + $server_response = $this->stripe->payment_hash->data; + + PaymentFailureMailer::dispatch( + $this->stripe->client, + $server_response, + $this->stripe->client->company, + $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()) + ); + + $message = [ + 'server_response' => $server_response, + 'data' => $this->stripe->payment_hash->data, + ]; + + SystemLogger::dispatch( + $message, + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + throw new PaymentFailed('Failed to process the payment.', 500); + } +} diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index cac69474a1ac..ca3cc778878e 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -35,6 +35,7 @@ use App\PaymentDrivers\Stripe\SOFORT; use App\PaymentDrivers\Stripe\SEPA; use App\PaymentDrivers\Stripe\GIROPAY; use App\PaymentDrivers\Stripe\iDeal; +use App\PaymentDrivers\Stripe\EPS; use App\PaymentDrivers\Stripe\UpdatePaymentMethods; use App\PaymentDrivers\Stripe\Utilities; use App\Utils\Traits\MakesHash; @@ -81,6 +82,7 @@ class StripePaymentDriver extends BaseDriver GatewayType::SEPA => SEPA::class, GatewayType::GIROPAY => GIROPAY::class, GatewayType::IDEAL => iDeal::class, + GatewayType::EPS => EPS::class, ]; @@ -174,6 +176,13 @@ class StripePaymentDriver extends BaseDriver && in_array($this->client->country->iso_3166_3, ["NLD"])) $types[] = GatewayType::IDEAL; + if ($this->client + && $this->client->currency() + && ($this->client->currency()->code == 'EUR') + && isset($this->client->country) + && in_array($this->client->country->iso_3166_3, ["AUT", "DEU"])) // TODO: remove test country + $types[] = GatewayType::EPS; + return $types; } @@ -202,6 +211,8 @@ class StripePaymentDriver extends BaseDriver break; case GatewayType::IDEAL: return 'gateways.stripe.ideal'; + case GatewayType::EPS: + return 'gateways.stripe.eps'; default: break; } From 22f97e8d0a0e4941baee6ac1a988da50c215d324 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 10 Oct 2021 06:54:04 +0200 Subject: [PATCH 3/7] Added views --- .../gateways/stripe/eps/authorize.blade.php | 7 +++++ .../gateways/stripe/eps/eps.blade.php | 11 ++++++++ .../gateways/stripe/eps/pay.blade.php | 28 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 resources/views/portal/ninja2020/gateways/stripe/eps/authorize.blade.php create mode 100644 resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php create mode 100644 resources/views/portal/ninja2020/gateways/stripe/eps/pay.blade.php diff --git a/resources/views/portal/ninja2020/gateways/stripe/eps/authorize.blade.php b/resources/views/portal/ninja2020/gateways/stripe/eps/authorize.blade.php new file mode 100644 index 000000000000..ceb2d28000d5 --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/stripe/eps/authorize.blade.php @@ -0,0 +1,7 @@ +@extends('portal.ninja2020.layout.payments', ['gateway_title' => ctrans('texts.bank_account'), 'card_title' => ctrans('texts.bank_account')]) + +@section('gateway_content') + @component('portal.ninja2020.components.general.card-element-single', ['title' => ctrans('texts.bank_account'), 'show_title' => false]) + {{ __('texts.sofort_authorize_label') }} + @endcomponent +@endsection diff --git a/resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php b/resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php new file mode 100644 index 000000000000..305e10f9a500 --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php @@ -0,0 +1,11 @@ +
+ @component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.name')]) + + +
+
+
+ @endcomponent +
diff --git a/resources/views/portal/ninja2020/gateways/stripe/eps/pay.blade.php b/resources/views/portal/ninja2020/gateways/stripe/eps/pay.blade.php new file mode 100644 index 000000000000..2146d5e6908a --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/stripe/eps/pay.blade.php @@ -0,0 +1,28 @@ +@extends('portal.ninja2020.layout.payments', ['gateway_title' => 'EPS', 'card_title' => 'EPS']) + +@section('gateway_head') + + + + + + + +@endsection + +@section('gateway_content') + + + @include('portal.ninja2020.gateways.includes.payment_details') + + @component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.payment_type')]) + {{ ctrans('texts.eps') }} ({{ ctrans('texts.bank_transfer') }}) + @endcomponent + @include('portal.ninja2020.gateways.stripe.eps.eps') + @include('portal.ninja2020.gateways.includes.pay_now') +@endsection + +@push('footer') + + +@endpush From 965c1a9d260c363405b47d2d0f340b9ae0a1a029 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 10 Oct 2021 06:58:49 +0200 Subject: [PATCH 4/7] Added js --- public/js/clients/payments/stripe-eps.js | 1 + .../payments/stripe-eps.js.LICENSE.txt | 9 +++ resources/js/clients/payments/stripe-eps.js | 80 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 public/js/clients/payments/stripe-eps.js create mode 100644 public/js/clients/payments/stripe-eps.js.LICENSE.txt create mode 100644 resources/js/clients/payments/stripe-eps.js diff --git a/public/js/clients/payments/stripe-eps.js b/public/js/clients/payments/stripe-eps.js new file mode 100644 index 000000000000..d587e26d4aea --- /dev/null +++ b/public/js/clients/payments/stripe-eps.js @@ -0,0 +1 @@ +!function(n){var r={};function o(e){if(r[e])return r[e].exports;var t=r[e]={i:e,l:!1,exports:{}};return n[e].call(t.exports,t,t.exports,o),t.l=!0,t.exports}o.m=n,o.c=r,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)o.d(n,r,function(e){return t[e]}.bind(null,r));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="/",o(o.s=6)}({6:function(e,t,n){e.exports=n("RFiP")},RFiP:function(e,t){var n;function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var r=null!==(n=null===(r=document.querySelector('meta[name="stripe-publishable-key"]'))||void 0===r?void 0:r.content)&&void 0!==n?n:"",i=null!==(n=null===(n=document.querySelector('meta[name="stripe-account-id"]'))||void 0===n?void 0:n.content)&&void 0!==n?n:"";new function t(e,n){var r=this;!function(e){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this),o(this,"setupStripe",function(){r.stripe=Stripe(r.key),r.stripeConnect&&(r.stripe.stripeAccount=i);let e=r.stripe.elements();return r.eps=e.create("epsBank",{style:{base:{padding:"10px 12px",color:"#32325d",fontSize:"16px","::placeholder":{color:"#aab7c4"}}}}),r.eps.mount("#eps-bank-element"),r}),o(this,"handle",function(){document.getElementById("pay-now").addEventListener("click",e=>{let t=document.getElementById("errors");if(!document.getElementById("eps-name").value)return t.textContent="Enter name",t.hidden=!1,void console.log("name");document.getElementById("pay-now").disabled=!0,document.querySelector("#pay-now > svg").classList.remove("hidden"),document.querySelector("#pay-now > span").classList.add("hidden"),this.stripe.confirmEpsPayment(document.querySelector("meta[name=pi-client-secret").content,{payment_method:{eps:r.eps,billing_details:{name:document.getElementById("eps-name").value}},return_url:document.querySelector('meta[name="return-url"]').content})})}),this.key=e,this.errors=document.getElementById("errors"),this.stripeConnect=n}(r,i).setupStripe().handle()}}); diff --git a/public/js/clients/payments/stripe-eps.js.LICENSE.txt b/public/js/clients/payments/stripe-eps.js.LICENSE.txt new file mode 100644 index 000000000000..585c6ab0e4fc --- /dev/null +++ b/public/js/clients/payments/stripe-eps.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://opensource.org/licenses/AAL + */ diff --git a/resources/js/clients/payments/stripe-eps.js b/resources/js/clients/payments/stripe-eps.js new file mode 100644 index 000000000000..1013d40cfb72 --- /dev/null +++ b/resources/js/clients/payments/stripe-eps.js @@ -0,0 +1,80 @@ +/** + * 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://opensource.org/licenses/AAL + */ + +class ProcessEPSPay { + constructor(key, stripeConnect) { + this.key = key; + this.errors = document.getElementById('errors'); + this.stripeConnect = stripeConnect; + } + + setupStripe = () => { + this.stripe = Stripe(this.key); + + if(this.stripeConnect) + this.stripe.stripeAccount = stripeConnect; + let elements = this.stripe.elements(); + var options = { + style: { + base: { + padding: '10px 12px', + color: '#32325d', + fontSize: '16px', + '::placeholder': { + color: '#aab7c4' + }, + }, + }, + }; + this.eps = elements.create('epsBank', options); + this.eps.mount("#eps-bank-element"); + return this; + }; + + handle = () => { + document.getElementById('pay-now').addEventListener('click', (e) => { + let errors = document.getElementById('errors'); + + if (!document.getElementById('eps-name').value) { + errors.textContent = "Enter name"; + errors.hidden = false; + console.log("name"); + return ; + } + document.getElementById('pay-now').disabled = true; + document.querySelector('#pay-now > svg').classList.remove('hidden'); + document.querySelector('#pay-now > span').classList.add('hidden'); + + this.stripe.confirmEpsPayment( + document.querySelector('meta[name=pi-client-secret').content, + { + payment_method: { + eps: this.eps, + billing_details: { + name: document.getElementById("ideal-name").value, + }, + }, + return_url: document.querySelector( + 'meta[name="return-url"]' + ).content, + } + ); + }); + }; +} + +const publishableKey = document.querySelector( + 'meta[name="stripe-publishable-key"]' +)?.content ?? ''; + +const stripeConnect = + document.querySelector('meta[name="stripe-account-id"]')?.content ?? ''; + +new ProcessEPSPay(publishableKey, stripeConnect).setupStripe().handle(); From 45e517dcff4762d48041edd65ac3ac98643f6998 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 10 Oct 2021 06:58:57 +0200 Subject: [PATCH 5/7] Small fix --- .../views/portal/ninja2020/gateways/stripe/eps/eps.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php b/resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php index 305e10f9a500..6cac5fd24539 100644 --- a/resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php +++ b/resources/views/portal/ninja2020/gateways/stripe/eps/eps.blade.php @@ -1,7 +1,7 @@
@component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.name')])
From b598d6bfed38336af02215d00c4256e69912579a Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 10 Oct 2021 07:06:24 +0200 Subject: [PATCH 6/7] Added language strings --- resources/lang/en/texts.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index cf5fc9e17714..28c0984d6a24 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4321,7 +4321,8 @@ $LANG = array( 'bank_account_holder' => 'Bank Account Holder', 'aio_checkout' => 'All-in-one checkout', 'giropay' => 'GiroPay', - 'giropay_law' => 'By entering your Customer information (such as name, sort code and account number) you (the Customer) agree that this information is given voluntarily.' + 'giropay_law' => 'By entering your Customer information (such as name, sort code and account number) you (the Customer) agree that this information is given voluntarily.', + 'eps' => 'EPS', ); return $LANG; From c756a000395837340396103891a32933d6a01ea5 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 10 Oct 2021 07:08:34 +0200 Subject: [PATCH 7/7] remove test country --- app/PaymentDrivers/StripePaymentDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index ca3cc778878e..397adf6f8aa6 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -180,7 +180,7 @@ class StripePaymentDriver extends BaseDriver && $this->client->currency() && ($this->client->currency()->code == 'EUR') && isset($this->client->country) - && in_array($this->client->country->iso_3166_3, ["AUT", "DEU"])) // TODO: remove test country + && in_array($this->client->country->iso_3166_3, ["AUT"])) $types[] = GatewayType::EPS; return $types;