diff --git a/addons/pos_stripe/static/src/js/payment_stripe.js b/addons/pos_stripe/static/src/js/payment_stripe.js index 267c7c84998..67b74fe52cb 100644 --- a/addons/pos_stripe/static/src/js/payment_stripe.js +++ b/addons/pos_stripe/static/src/js/payment_stripe.js @@ -12,11 +12,7 @@ const _t = core._t; export const PaymentStripe = PaymentInterface.extend({ init: function (pos, payment_method) { this._super(...arguments); - this.terminal = new StripeTerminal({ - onFetchConnectionToken: this.stripeFetchConnectionToken.bind(this), - onUnexpectedReaderDisconnect: this.stripeUnexpectedDisconnect.bind(this), - }); - this.discoverReaders(); + this.createStripeTerminal(); }, stripeUnexpectedDisconnect: function () { @@ -41,9 +37,9 @@ export const PaymentStripe = PaymentInterface.extend({ } return data.secret; } catch (error) { - this._showError(error.message); - return false; - } + this._showError(error.message.message, 'Fetch Token'); + this.terminal = false; + }; }, discoverReaders: async function () { @@ -60,7 +56,18 @@ export const PaymentStripe = PaymentInterface.extend({ }, checkReader: async function () { - const line = this.pos.get_order().selected_paymentline; + try { + if ( !this.terminal ) { + let createStripeTerminal = this.createStripeTerminal(); + if ( !createStripeTerminal ) { + throw _t('Failed to load resource: net::ERR_INTERNET_DISCONNECTED.'); + } + } + } catch (error) { + this._showError(error); + return false; + } + let line = this.pos.get_order().selected_paymentline; // Because the reader can only connect to one instance of the SDK at a time. // We need the disconnect this reader if we want to use another one if ( @@ -87,22 +94,27 @@ export const PaymentStripe = PaymentInterface.extend({ const discoveredReaders = JSON.parse(this.pos.discoveredReaders); for (const selectedReader of discoveredReaders) { if (selectedReader.serial_number == this.payment_method.stripe_serial_number) { - const connectResult = await this.terminal.connectReader(selectedReader, { - fail_if_in_use: true, - }); - if (connectResult.error) { - this._showError(connectResult.error.message, connectResult.error.code); - line.set_payment_status("retry"); - return false; - } else { + try { + let connectResult = await this.terminal.connectReader(selectedReader, {fail_if_in_use: true}); + if (connectResult.error) { + throw connectResult; + } this.pos.connectedReader = this.payment_method.stripe_serial_number; return true; + } catch (error) { + if (error.error) { + this._showError(error.error.message, error.code); + } else { + this._showError(error); + } + line.set_payment_status('retry'); + return false; } } } this._showError( - sprintf( - this.env._t("Stripe readers %s not listed in your account"), + _.str.sprintf( + _t('Stripe readers %s not listed in your account'), this.payment_method.stripe_serial_number ) ); @@ -140,6 +152,21 @@ export const PaymentStripe = PaymentInterface.extend({ } }, + createStripeTerminal: function () { + try { + this.terminal = StripeTerminal.create({ + onFetchConnectionToken: this.stripeFetchConnectionToken.bind(this), + onUnexpectedReaderDisconnect: this.stripeUnexpectedDisconnect.bind(this), + }); + this.discoverReaders(); + return true; + } catch (error) { + this._showError(_t('Failed to load resource: net::ERR_INTERNET_DISCONNECTED.'), error); + this.terminal = false; + return false; + } + }, + captureAfterPayment: async function (processPayment, line) { const capturePayment = await this.capturePayment(processPayment.paymentIntent.id); if (capturePayment.charges) { @@ -166,7 +193,7 @@ export const PaymentStripe = PaymentInterface.extend({ } return data; } catch (error) { - this._showError(error.message); + this._showError(error.message.message, 'Capture Payment'); return false; } }, @@ -188,7 +215,7 @@ export const PaymentStripe = PaymentInterface.extend({ } return data.client_secret; } catch (error) { - this._showError(error.message); + this._showError(error.message.message, 'Fetch Secret'); return false; } }, @@ -198,11 +225,14 @@ export const PaymentStripe = PaymentInterface.extend({ * Override */ await this._super.apply(this, arguments); - const line = this.pos.get_order().selected_paymentline; - line.set_payment_status("waiting"); - if (await this.checkReader()) { - return await this.collectPayment(line.amount); - } else { + let line = this.pos.get_order().selected_paymentline; + line.set_payment_status('waiting'); + try { + if (await this.checkReader()) { + return await this.collectPayment(line.amount); + } + } catch (error) { + this._showError(error); return false; } }, @@ -221,8 +251,10 @@ export const PaymentStripe = PaymentInterface.extend({ }, stripeCancel: async function () { - if (this.terminal.getConnectionStatus() != "connected") { - this._showError(_t("Payment canceled because not reader connected")); + if (!this.terminal) { + return true; + } else if (this.terminal.getConnectionStatus() != 'connected') { + this._showError(_t('Payment canceled because not reader connected')); return true; } else { const cancelCollectPaymentMethod = await this.terminal.cancelCollectPaymentMethod();