diff --git a/interface/resources/qml/hifi/commerce/Checkout.qml b/interface/resources/qml/hifi/commerce/Checkout.qml index 0b5d5978dd..48d5f5b1d5 100644 --- a/interface/resources/qml/hifi/commerce/Checkout.qml +++ b/interface/resources/qml/hifi/commerce/Checkout.qml @@ -25,6 +25,7 @@ Rectangle { id: checkoutRoot; property string itemId; + property string itemHref; // Style color: hifi.colors.baseGray; Hifi.QmlCommerce { @@ -234,6 +235,7 @@ Rectangle { // "Cancel" button HifiControlsUit.Button { + id: cancelButton; color: hifi.buttons.black; colorScheme: hifi.colorSchemes.dark; anchors.top: parent.top; @@ -251,6 +253,8 @@ Rectangle { // "Buy" button HifiControlsUit.Button { + property bool buyFailed: false; + id: buyButton; color: hifi.buttons.black; colorScheme: hifi.colorSchemes.dark; anchors.top: parent.top; @@ -262,7 +266,11 @@ Rectangle { width: parent.width/2 - anchors.rightMargin*2; text: "Buy" onClicked: { - sendToScript({method: 'checkout_buyClicked', params: {success: commerce.buy(itemId, parseInt(itemPriceText.text))}}); + if (buyFailed) { + sendToScript({method: 'checkout_cancelClicked', params: itemId}); + } else { + sendToScript({method: 'checkout_buyClicked', success: commerce.buy(itemId, parseInt(itemPriceText.text)), itemId: itemId, itemHref: itemHref}); + } } } } @@ -293,6 +301,13 @@ Rectangle { itemNameText.text = message.params.itemName; itemAuthorText.text = message.params.itemAuthor; itemPriceText.text = message.params.itemPrice; + itemHref = message.params.itemHref; + buyButton.text = "Buy"; + buyButton.buyFailed = false; + break; + case 'buyFailed': + buyButton.text = "Buy Failed"; + buyButton.buyFailed = true; break; default: console.log('Unrecognized message from marketplaces.js:', JSON.stringify(message)); diff --git a/scripts/system/html/js/marketplacesInject.js b/scripts/system/html/js/marketplacesInject.js index 644a1aa046..e078112360 100644 --- a/scripts/system/html/js/marketplacesInject.js +++ b/scripts/system/html/js/marketplacesInject.js @@ -93,24 +93,29 @@ }); } - function buyButtonClicked(id, name, author, price) { + function buyButtonClicked(id, name, author, price, href) { EventBridge.emitWebEvent(JSON.stringify({ type: "CHECKOUT", itemId: id, itemName: name, itemAuthor: author, - itemPrice: price + itemPrice: price, + itemHref: href })); } function injectBuyButtonOnMainPage() { - $('.grid-item').find('#price-or-edit').find('a').attr('href', '#'); + $('.grid-item').find('#price-or-edit').find('a').each(function() { + $(this).attr('data-href', $(this).attr('href')); + $(this).attr('href', '#'); + }); $('.grid-item').find('#price-or-edit').find('.price').text("BUY"); $('.grid-item').find('#price-or-edit').find('a').on('click', function () { buyButtonClicked($(this).closest('.grid-item').attr('data-item-id'), $(this).closest('.grid-item').find('.item-title').text(), $(this).closest('.grid-item').find('.creator').find('.value').text(), - 10); + 10, + $(this).attr('data-href')); }); } @@ -137,13 +142,15 @@ function injectHiFiItemPageCode() { if (confirmAllPurchases) { + var href = $('#side-info').find('.btn').attr('href'); $('#side-info').find('.btn').attr('href', '#'); $('#side-info').find('.btn').html('Buy Item '); $('#side-info').find('.btn').on('click', function () { buyButtonClicked(window.location.pathname.split("/")[3], $('#top-center').find('h1').text(), $('#creator').find('.value').text(), - 10); + 10, + href); }); } } @@ -415,16 +422,18 @@ function onLoad() { EventBridge.scriptEventReceived.connect(function (message) { - var parsedJsonMessage = JSON.parse(message); - if (message.slice(0, CAN_WRITE_ASSETS.length) === CAN_WRITE_ASSETS) { canWriteAssets = message.slice(-4) === "true"; } else if (message.slice(0, CLARA_IO_CANCEL_DOWNLOAD.length) === CLARA_IO_CANCEL_DOWNLOAD) { cancelClaraDownload(); - } else if (parsedJsonMessage.type === "marketplaces") { - if (parsedJsonMessage.action === "inspectionModeSetting") { - confirmAllPurchases = !!parsedJsonMessage.data; - injectCode(); + } else { + var parsedJsonMessage = JSON.parse(message); + + if (parsedJsonMessage.type === "marketplaces") { + if (parsedJsonMessage.action === "inspectionModeSetting") { + confirmAllPurchases = !!parsedJsonMessage.data; + injectCode(); + } } } }); diff --git a/scripts/system/marketplaces/marketplaces.js b/scripts/system/marketplaces/marketplaces.js index e29510b4c8..4dd7438ac2 100644 --- a/scripts/system/marketplaces/marketplaces.js +++ b/scripts/system/marketplaces/marketplaces.js @@ -101,31 +101,14 @@ Entities.canWriteAssetsChanged.connect(onCanWriteAssetsChanged); function onMessage(message) { - var parsedJsonMessage = JSON.parse(message); - if (parsedJsonMessage.type === "CHECKOUT") { - tablet.sendToQml({ method: 'updateCheckoutQML', params: parsedJsonMessage }); - tablet.pushOntoStack(MARKETPLACE_CHECKOUT_QML_PATH); - } else if (parsedJsonMessage.type === "REQUEST_SETTING") { - tablet.emitScriptEvent(JSON.stringify({ - type: "marketplaces", - action: "inspectionModeSetting", - data: Settings.getValue("inspectionMode", false) - })); - } if (message === GOTO_DIRECTORY) { tablet.gotoWebScreen(MARKETPLACES_URL, MARKETPLACES_INJECT_SCRIPT_URL); - } - - if (message === QUERY_CAN_WRITE_ASSETS) { + } else if (message === QUERY_CAN_WRITE_ASSETS) { tablet.emitScriptEvent(CAN_WRITE_ASSETS + " " + Entities.canWriteAssets()); - } - - if (message === WARN_USER_NO_PERMISSIONS) { + } else if (message === WARN_USER_NO_PERMISSIONS) { Window.alert(NO_PERMISSIONS_ERROR_MESSAGE); - } - - if (message.slice(0, CLARA_IO_STATUS.length) === CLARA_IO_STATUS) { + } else if (message.slice(0, CLARA_IO_STATUS.length) === CLARA_IO_STATUS) { if (isDownloadBeingCancelled) { return; } @@ -137,18 +120,26 @@ Window.updateMessageBox(messageBox, CLARA_DOWNLOAD_TITLE, text, CANCEL_BUTTON, NO_BUTTON); } return; - } - - if (message.slice(0, CLARA_IO_DOWNLOAD.length) === CLARA_IO_DOWNLOAD) { + } else if (message.slice(0, CLARA_IO_DOWNLOAD.length) === CLARA_IO_DOWNLOAD) { if (messageBox !== null) { Window.closeMessageBox(messageBox); messageBox = null; } return; - } - - if (message === CLARA_IO_CANCELLED_DOWNLOAD) { + } else if (message === CLARA_IO_CANCELLED_DOWNLOAD) { isDownloadBeingCancelled = false; + } else { + var parsedJsonMessage = JSON.parse(message); + if (parsedJsonMessage.type === "CHECKOUT") { + tablet.sendToQml({ method: 'updateCheckoutQML', params: parsedJsonMessage }); + tablet.pushOntoStack(MARKETPLACE_CHECKOUT_QML_PATH); + } else if (parsedJsonMessage.type === "REQUEST_SETTING") { + tablet.emitScriptEvent(JSON.stringify({ + type: "marketplaces", + action: "inspectionModeSetting", + data: Settings.getValue("inspectionMode", false) + })); + } } } @@ -207,7 +198,14 @@ //tablet.popFromStack(); break; case 'checkout_buyClicked': - print("fromQml: " + JSON.stringify(message)); + if (message.success === true) { + tablet.gotoWebScreen(message.itemHref); + Script.setTimeout(function () { + tablet.gotoWebScreen(MARKETPLACE_URL + '/items/' + message.itemId, MARKETPLACES_INJECT_SCRIPT_URL); + }, 100); + } else { + tablet.sendToQml({ method: 'buyFailed' }); + } //tablet.popFromStack(); break; default: