diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 68b56001a84c..e610d0cfca8e 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -114,7 +114,8 @@ class Gateway extends StaticModel GatewayType::BANCONTACT => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], GatewayType::BECS => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], GatewayType::IDEAL => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], - GatewayType::ACSS => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']]]; + GatewayType::ACSS => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], + GatewayType::FPX => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']]]; case 39: return [GatewayType::CREDIT_CARD => ['refund' => true, 'token_billing' => true, 'webhooks' => [' ']]]; //Checkout break; diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index 23262cd9eb0b..da847e587d8e 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -38,6 +38,7 @@ class GatewayType extends StaticModel const ACSS = 19; const BECS = 20; const INSTANT_BANK_PAY = 21; + const FPX = 22; public function gateway() { @@ -92,6 +93,8 @@ class GatewayType extends StaticModel return ctrans('texts.payment_type_direct_debit'); case self::INSTANT_BANK_PAY: return ctrans('texts.payment_type_instant_bank_pay'); + case self::FPX: + return ctrans('texts.fpx'); default: return 'Undefined.'; break; diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index 3a28863def71..d5c4bca31667 100644 --- a/app/Models/PaymentType.php +++ b/app/Models/PaymentType.php @@ -54,6 +54,7 @@ class PaymentType extends StaticModel const BECS = 43; const ACSS = 44; const INSTANT_BANK_PAY = 45; + const FPX = 46; public static function parseCardType($cardName) { diff --git a/app/PaymentDrivers/Stripe/FPX.php b/app/PaymentDrivers/Stripe/FPX.php new file mode 100644 index 000000000000..c1817cadd4b1 --- /dev/null +++ b/app/PaymentDrivers/Stripe/FPX.php @@ -0,0 +1,141 @@ +stripe = $stripe; + } + + public function authorizeView($data) + { + return render('gateways.stripe.fpx.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' => ['fpx'], + '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.fpx.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::FPX, + ]); + } + + 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::FPX, + '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::FPX, + ]; + + $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 8e5b960b268c..a1a055821d48 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -41,6 +41,7 @@ use App\PaymentDrivers\Stripe\Bancontact; use App\PaymentDrivers\Stripe\BECS; use App\PaymentDrivers\Stripe\ACSS; use App\PaymentDrivers\Stripe\BrowserPay; +use App\PaymentDrivers\Stripe\FPX; use App\PaymentDrivers\Stripe\UpdatePaymentMethods; use App\PaymentDrivers\Stripe\Utilities; use App\Utils\Traits\MakesHash; @@ -198,6 +199,13 @@ class StripePaymentDriver extends BaseDriver && in_array($this->client->country->iso_3166_3, ["AUT"])) $types[] = GatewayType::EPS; + if ($this->client + && $this->client->currency() + && ($this->client->currency()->code == 'MYR') + && isset($this->client->country) + && in_array($this->client->country->iso_3166_3, ["MYS"])) + $types[] = GatewayType::FPX; + if ($this->client && $this->client->currency() && ($this->client->currency()->code == 'EUR') @@ -266,6 +274,8 @@ class StripePaymentDriver extends BaseDriver return 'gateways.stripe.becs'; case GatewayType::ACSS: return 'gateways.stripe.acss'; + case GatewayType::FPX: + return 'gateways.stripe.fpx'; default: break; } @@ -540,7 +550,7 @@ class StripePaymentDriver extends BaseDriver $payment = Payment::query() ->where('company_id', $request->getCompany()->id) ->where('transaction_reference', $transaction['id']) - ->first(); + ->first(); } if ($payment) { diff --git a/public/js/clients/payments/stripe-fpx.js b/public/js/clients/payments/stripe-fpx.js new file mode 100644 index 000000000000..5c14c98ed666 --- /dev/null +++ b/public/js/clients/payments/stripe-fpx.js @@ -0,0 +1,2 @@ +/*! For license information please see stripe-fpx.js.LICENSE.txt */ +(()=>{var e,t,n,r;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 i=null!==(e=null===(t=document.querySelector('meta[name="stripe-publishable-key"]'))||void 0===t?void 0:t.content)&&void 0!==e?e:"",c=null!==(n=null===(r=document.querySelector('meta[name="stripe-account-id"]'))||void 0===r?void 0:r.content)&&void 0!==n?n:"";new function e(t,n){var r=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),o(this,"setupStripe",(function(){r.stripe=Stripe(r.key),r.stripeConnect&&(r.stripe.stripeAccount=c);var e=r.stripe.elements();return r.fpx=e.create("fpxBank",{style:{base:{padding:"10px 12px",color:"#32325d",fontSize:"16px"}},accountHolderType:"individual"}),r.fpx.mount("#fpx-bank-element"),r})),o(this,"handle",(function(){document.getElementById("pay-now").addEventListener("click",(function(e){document.getElementById("pay-now").disabled=!0,document.querySelector("#pay-now > svg").classList.remove("hidden"),document.querySelector("#pay-now > span").classList.add("hidden"),r.stripe.confirmFpxPayment(document.querySelector("meta[name=pi-client-secret").content,{payment_method:{fpx:r.fpx},return_url:document.querySelector('meta[name="return-url"]').content})}))})),this.key=t,this.errors=document.getElementById("errors"),this.stripeConnect=n}(i,c).setupStripe().handle()})(); \ No newline at end of file diff --git a/public/js/clients/payments/stripe-fpx.js.LICENSE.txt b/public/js/clients/payments/stripe-fpx.js.LICENSE.txt new file mode 100644 index 000000000000..585c6ab0e4fc --- /dev/null +++ b/public/js/clients/payments/stripe-fpx.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/public/mix-manifest.json b/public/mix-manifest.json index 6c469b4c70b2..b240eb83b68f 100755 --- a/public/mix-manifest.json +++ b/public/mix-manifest.json @@ -37,6 +37,7 @@ "/js/clients/payments/stripe-ideal.js": "/js/clients/payments/stripe-ideal.js?id=73ce56676f9252b0cecf", "/js/clients/payments/stripe-przelewy24.js": "/js/clients/payments/stripe-przelewy24.js?id=f3a14f78bec8209c30ba", "/js/clients/payments/stripe-browserpay.js": "/js/clients/payments/stripe-browserpay.js?id=71e49866d66a6d85b88a", + "/js/clients/payments/stripe-fpx.js": "/js/clients/payments/stripe-fpx.js?id=915712157bc0634b9b21", "/css/app.css": "/css/app.css?id=cab8a6526b0f9f71842d", "/css/card-js.min.css": "/css/card-js.min.css?id=62afeb675235451543ad" } diff --git a/resources/js/clients/payments/stripe-fpx.js b/resources/js/clients/payments/stripe-fpx.js new file mode 100644 index 000000000000..415cb6449469 --- /dev/null +++ b/resources/js/clients/payments/stripe-fpx.js @@ -0,0 +1,65 @@ +/** + * 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 ProcessFPXPay { + 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(); + let style = { + base: { + // Add your base input styles here. For example: + padding: '10px 12px', + color: '#32325d', + fontSize: '16px', + }, + }; + this.fpx = elements.create('fpxBank', {style: style, accountHolderType: 'individual',}); + this.fpx.mount("#fpx-bank-element"); + return this; + }; + + handle = () => { + document.getElementById('pay-now').addEventListener('click', (e) => { + document.getElementById('pay-now').disabled = true; + document.querySelector('#pay-now > svg').classList.remove('hidden'); + document.querySelector('#pay-now > span').classList.add('hidden'); + + this.stripe.confirmFpxPayment( + document.querySelector('meta[name=pi-client-secret').content, + { + payment_method: { + fpx: this.fpx, + }, + 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 ProcessFPXPay(publishableKey, stripeConnect).setupStripe().handle(); diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 693f8d4b28bd..894ce33d4cf3 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4543,6 +4543,7 @@ $LANG = array( 'activity_122' => ':user archived recurring expense :recurring_expense', 'activity_123' => ':user deleted recurring expense :recurring_expense', 'activity_124' => ':user restored recurring expense :recurring_expense', + 'fpx' => "FPX", ); diff --git a/resources/views/portal/ninja2020/gateways/stripe/fpx/authorize.blade.php b/resources/views/portal/ninja2020/gateways/stripe/fpx/authorize.blade.php new file mode 100644 index 000000000000..ceb2d28000d5 --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/stripe/fpx/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/fpx/fpx.blade.php b/resources/views/portal/ninja2020/gateways/stripe/fpx/fpx.blade.php new file mode 100644 index 000000000000..d3935e080aef --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/stripe/fpx/fpx.blade.php @@ -0,0 +1,8 @@ +