[FIX] pos_stripe: Fix loading stripe ressource

Currently the POS does not load if a Stripe payment method is configured and there is no internet connection.
This commit fixes the loading of Stripe resources
to allow the opening of the POS and the display of related errors

task 3177045

closes odoo/odoo#118946

X-original-commit: 00b113721cf6a1de862794f06bd3ba293039ecd5
Signed-off-by: Joseph Caburnay (jcb) <jcb@odoo.com>
Signed-off-by: Quentin Lejeune (qle) <qle@odoo.com>
This commit is contained in:
lejeune quentin
2023-04-24 16:57:38 +02:00
parent afe5455116
commit 0fa36d664b
@@ -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();