Skip to content

Commit bb4c273

Browse files
authored
Payment gateway within App (#4933)
1 parent 783e50b commit bb4c273

File tree

4 files changed

+180
-148
lines changed

4 files changed

+180
-148
lines changed

services/static-webserver/client/source/class/osparc/desktop/credits/BillingCenter.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ qx.Class.define("osparc.desktop.credits.BillingCenter", {
9191
__activityPage: null,
9292
__transactionsPage: null,
9393
__usagePage: null,
94-
__buyCredits: null,
9594
__transactionsTable: null,
9695

9796
__getOverviewPage: function() {
@@ -103,17 +102,7 @@ qx.Class.define("osparc.desktop.credits.BillingCenter", {
103102
overview.set({
104103
margin: 10
105104
});
106-
overview.addListener("buyCredits", e => {
107-
this.__openBuyCredits();
108-
const {
109-
walletId
110-
} = e.getData();
111-
const store = osparc.store.Store.getInstance();
112-
const found = store.getWallets().find(wallet => wallet.getWalletId() === parseInt(walletId));
113-
if (found) {
114-
this.__buyCredits.setWallet(found);
115-
}
116-
});
105+
overview.addListener("buyCredits", () => this.__openBuyCredits());
117106
overview.addListener("toWallets", () => this.openWallets());
118107
overview.addListener("toActivity", () => this.__openActivity());
119108
overview.addListener("toTransactions", () => this.__openTransactions());
@@ -136,17 +125,7 @@ qx.Class.define("osparc.desktop.credits.BillingCenter", {
136125
walletsView.set({
137126
margin: 10
138127
});
139-
walletsView.addListener("buyCredits", e => {
140-
this.__openBuyCredits();
141-
const {
142-
walletId
143-
} = e.getData();
144-
const store = osparc.store.Store.getInstance();
145-
const found = store.getWallets().find(wallet => wallet.getWalletId() === parseInt(walletId));
146-
if (found) {
147-
this.__buyCredits.setWallet(found);
148-
}
149-
});
128+
walletsView.addListener("buyCredits", () => this.__openBuyCredits());
150129
page.add(walletsView);
151130
return page;
152131
},

services/static-webserver/client/source/class/osparc/desktop/credits/OneTimePayment.js

Lines changed: 131 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -234,26 +234,6 @@ qx.Class.define("osparc.desktop.credits.OneTimePayment", {
234234
});
235235
row++;
236236

237-
const walletTitle = new qx.ui.basic.Label().set({
238-
value: "Credit Account",
239-
font: "text-14"
240-
});
241-
layout.add(walletTitle, {
242-
row,
243-
column: 0
244-
});
245-
const walletLabel = new qx.ui.basic.Label().set({
246-
font: "text-14"
247-
});
248-
this.bind("wallet", walletLabel, "value", {
249-
converter: wallet => wallet ? wallet.getName() : this.tr("Select Credit Account")
250-
});
251-
layout.add(walletLabel, {
252-
row,
253-
column: 1
254-
});
255-
row++;
256-
257237
return layout;
258238
},
259239

@@ -265,113 +245,141 @@ qx.Class.define("osparc.desktop.credits.OneTimePayment", {
265245
maxWidth: 150,
266246
center: true
267247
});
248+
buyBtn.addListener("execute", () => this.__startPayment());
249+
return buyBtn;
250+
},
268251

269-
const buyingBtn = () => {
270-
buyBtn.set({
271-
fetching: true,
272-
label: this.tr("Buying...")
273-
});
252+
__buyingCredits: function(isBuying) {
253+
const buyBtn = this.getChildControl("buy-button");
254+
buyBtn.set({
255+
fetching: isBuying,
256+
label: isBuying ? this.tr("Buying...") : this.tr("Buy Credits")
257+
});
258+
},
259+
260+
__paymentCompleted: function(paymentData) {
261+
this.__buyingCredits(false);
262+
263+
if (paymentData["completedStatus"]) {
264+
const msg = this.tr("Payment ") + osparc.utils.Utils.onlyFirstsUp(paymentData["completedStatus"]);
265+
switch (paymentData["completedStatus"]) {
266+
case "SUCCESS":
267+
osparc.FlashMessenger.getInstance().logAs(msg, "INFO");
268+
break;
269+
case "PENDING":
270+
osparc.FlashMessenger.getInstance().logAs(msg, "WARNING");
271+
break;
272+
case "CANCELED":
273+
case "FAILED":
274+
osparc.FlashMessenger.getInstance().logAs(msg, "ERROR");
275+
break;
276+
default:
277+
console.error("completedStatus unknown");
278+
break;
279+
}
280+
}
281+
this.fireEvent("transactionCompleted");
282+
},
283+
284+
__cancelPayment: function(paymentId) {
285+
this.__buyingCredits(false);
286+
287+
const wallet = this.getWallet();
288+
// inform backend
289+
const params = {
290+
url: {
291+
walletId: wallet.getWalletId(),
292+
paymentId
293+
}
274294
};
275-
const buyCreditsBtn = () => {
276-
buyBtn.set({
277-
fetching: false,
278-
label: this.tr("Buy Credits")
279-
});
295+
osparc.data.Resources.fetch("payments", "cancelPayment", params);
296+
},
297+
298+
__windowClosed: function(paymentId) {
299+
const msg = this.tr("The window was closed. Try again and follow the instructions inside the opened window.");
300+
osparc.FlashMessenger.getInstance().logAs(msg, "WARNING");
301+
this.__cancelPayment(paymentId);
302+
},
303+
304+
__startPayment: function() {
305+
this.__buyingCredits(true);
306+
307+
const wallet = this.getWallet();
308+
const nCredits = this.getNCredits();
309+
const totalPrice = this.getTotalPrice();
310+
const params = {
311+
url: {
312+
walletId: wallet.getWalletId()
313+
},
314+
data: {
315+
priceDollars: totalPrice,
316+
osparcCredits: nCredits
317+
}
280318
};
281-
buyBtn.addListener("execute", () => {
282-
const nCredits = this.getNCredits();
283-
const totalPrice = this.getTotalPrice();
284-
const wallet = this.getWallet();
285-
buyingBtn();
286-
287-
const params = {
288-
url: {
289-
walletId: wallet.getWalletId()
290-
},
291-
data: {
292-
priceDollars: totalPrice,
293-
osparcCredits: nCredits
294-
}
295-
};
296-
osparc.data.Resources.fetch("payments", "startPayment", params)
297-
.then(data => {
298-
const paymentId = data["paymentId"];
299-
const url = data["paymentFormUrl"];
300-
const options = {
301-
width: 400,
302-
height: 400,
303-
top: 200,
304-
left: 100,
305-
scrollbars: false
306-
};
307-
const modal = true;
308-
const useNativeModalDialog = false; // this allow using the Blocker
309-
310-
const pgWindow = osparc.desktop.credits.PaymentGatewayWindow.popUp(
311-
url,
312-
"pgWindow",
313-
options,
314-
modal,
315-
useNativeModalDialog
316-
);
317-
318-
// Listen to socket event
319-
const socket = osparc.wrapper.WebSocket.getInstance();
320-
const slotName = "paymentCompleted";
321-
socket.on(slotName, jsonString => {
322-
const paymentData = JSON.parse(jsonString);
323-
if (paymentData["completedStatus"]) {
324-
const msg = this.tr("Payment ") + osparc.utils.Utils.onlyFirstsUp(paymentData["completedStatus"]);
325-
switch (paymentData["completedStatus"]) {
326-
case "SUCCESS":
327-
osparc.FlashMessenger.getInstance().logAs(msg, "INFO");
328-
// demo purposes
329-
wallet.setCreditsAvailable(wallet.getCreditsAvailable() + nCredits);
330-
break;
331-
case "PENDING":
332-
osparc.FlashMessenger.getInstance().logAs(msg, "WARNING");
333-
break;
334-
case "CANCELED":
335-
case "FAILED":
336-
osparc.FlashMessenger.getInstance().logAs(msg, "ERROR");
337-
break;
338-
default:
339-
console.error("completedStatus unknown");
340-
break;
341-
}
342-
}
343-
socket.removeSlot(slotName);
344-
buyCreditsBtn();
345-
pgWindow.close();
346-
this.fireEvent("transactionCompleted");
347-
});
348-
349-
const cancelPayment = () => {
350-
socket.removeSlot(slotName);
351-
buyCreditsBtn();
352-
// inform backend
353-
const params2 = {
354-
url: {
355-
walletId: wallet.getWalletId(),
356-
paymentId
357-
}
358-
};
359-
osparc.data.Resources.fetch("payments", "cancelPayment", params2);
360-
};
361-
// Listen to close window event (Bug: it doesn't work)
362-
pgWindow.onbeforeunload = () => {
363-
const msg = this.tr("The window was close. Try again and follow the instructions inside the opened window.");
364-
osparc.FlashMessenger.getInstance().logAs(msg, "WARNING");
365-
cancelPayment();
366-
};
367-
})
368-
.catch(err => {
369-
console.error(err);
370-
osparc.FlashMessenger.logAs(err.message, "ERROR");
371-
buyCreditsBtn();
319+
osparc.data.Resources.fetch("payments", "startPayment", params)
320+
.then(data => {
321+
const paymentId = data["paymentId"];
322+
const url = data["paymentFormUrl"];
323+
const stayWithinApp = true;
324+
const pgWindow = stayWithinApp ? this.__popUpPaymentGateway(paymentId, url) : this.__popUpPaymentGatewayOld(paymentId, url);
325+
326+
// Listen to socket event
327+
const socket = osparc.wrapper.WebSocket.getInstance();
328+
const slotName = "paymentCompleted";
329+
socket.on(slotName, jsonString => {
330+
const paymentData = JSON.parse(jsonString);
331+
this.__paymentCompleted(paymentData);
332+
socket.removeSlot(slotName);
333+
pgWindow.close();
372334
});
373-
});
374-
return buyBtn;
335+
})
336+
.catch(err => {
337+
console.error(err);
338+
osparc.FlashMessenger.logAs(err.message, "ERROR");
339+
this.__buyingCredits(false);
340+
});
341+
},
342+
343+
__popUpPaymentGateway: function(paymentId, url) {
344+
const options = {
345+
width: 400,
346+
height: 600
347+
};
348+
349+
const pgWindow = osparc.desktop.credits.PaymentGatewayWindow.popUp(
350+
url,
351+
"Buy Credits",
352+
options
353+
);
354+
// listen to "tap" instead of "execute": the "execute" is not propagated
355+
pgWindow.getChildControl("close-button").addListener("tap", () => this.__windowClosed(paymentId));
356+
357+
return pgWindow;
358+
},
359+
360+
__popUpPaymentGatewayOld: function(paymentId, url) {
361+
const options = {
362+
width: 400,
363+
height: 600,
364+
top: 100,
365+
left: 200,
366+
scrollbars: false
367+
};
368+
const modal = true;
369+
const useNativeModalDialog = false; // this allow using the Blocker
370+
371+
const pgWindow = osparc.desktop.credits.PaymentGatewayWindow.popUpOld(
372+
url,
373+
"pgWindow",
374+
options,
375+
modal,
376+
useNativeModalDialog
377+
);
378+
379+
// Listen to close window event (Bug: it doesn't work)
380+
pgWindow.onbeforeunload = () => this.__windowClosed(paymentId);
381+
382+
return pgWindow;
375383
}
376384
}
377385
});

services/static-webserver/client/source/class/osparc/desktop/credits/PaymentGatewayWindow.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ qx.Class.define("osparc.desktop.credits.PaymentGatewayWindow", {
1919
type: "static",
2020

2121
statics: {
22-
popUp: function(url, id, options, modal, useNativeModalDialog) {
22+
popUp: function(url, title, options) {
23+
const iframe = new qx.ui.embed.Iframe(url);
24+
return osparc.ui.window.Window.popUpInWindow(iframe, title, options.width, options.height).set({
25+
clickAwayClose: false
26+
});
27+
},
28+
29+
popUpOld: function(url, id, options, modal, useNativeModalDialog) {
2330
const blocker = qx.bom.Window.getBlocker();
2431
blocker.setBlockerColor("#FFF");
2532
blocker.setBlockerOpacity(0.6);

services/static-webserver/client/source/class/osparc/desktop/paymentMethods/PaymentMethods.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,48 @@ qx.Class.define("osparc.desktop.paymentMethods.PaymentMethods", {
100100
}
101101
};
102102
osparc.data.Resources.fetch("paymentMethods", "init", params)
103-
.then(() => this.__fetchPaymentMethods());
103+
.then(data => {
104+
this.__popUpPaymentGateway(data["paymentMethodId"], data["paymentMethodFormUrl"]);
105+
this.__fetchPaymentMethods();
106+
});
104107
}
105108
},
106109

110+
__cancelPaymentMethod: function(paymentMethodId) {
111+
const wallet = this.getWallet();
112+
// inform backend
113+
const params = {
114+
url: {
115+
walletId: wallet.getWalletId(),
116+
paymentMethodId
117+
}
118+
};
119+
osparc.data.Resources.fetch("paymentMethods", "cancel", params);
120+
},
121+
122+
__windowClosed: function(paymentMethodId) {
123+
const msg = this.tr("The window was closed. Try again and follow the instructions inside the opened window.");
124+
osparc.FlashMessenger.getInstance().logAs(msg, "WARNING");
125+
this.__cancelPaymentMethod(paymentMethodId);
126+
},
127+
128+
__popUpPaymentGateway: function(paymentMethodId, url) {
129+
const options = {
130+
width: 400,
131+
height: 600
132+
};
133+
134+
const pgWindow = osparc.desktop.credits.PaymentGatewayWindow.popUp(
135+
url,
136+
"Add payment method",
137+
options
138+
);
139+
// listen to "tap" instead of "execute": the "execute" is not propagated
140+
pgWindow.getChildControl("close-button").addListener("tap", () => this.__windowClosed(paymentMethodId));
141+
142+
return pgWindow;
143+
},
144+
107145
__fetchPaymentMethods: function() {
108146
const listLayout = this.getChildControl("payment-methods-list-layout");
109147
listLayout.removeAll();

0 commit comments

Comments
 (0)