From 9120e678f1b26c5fda9038d55c54dd5046aba19e Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Fri, 11 Aug 2017 16:17:13 -0700 Subject: [PATCH 1/4] Use asynchronous commerce API --- .../resources/qml/hifi/commerce/Checkout.qml | 65 +++++++++---------- .../resources/qml/hifi/commerce/Inventory.qml | 18 ++++- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/interface/resources/qml/hifi/commerce/Checkout.qml b/interface/resources/qml/hifi/commerce/Checkout.qml index a0a5af88ab..458e417654 100644 --- a/interface/resources/qml/hifi/commerce/Checkout.qml +++ b/interface/resources/qml/hifi/commerce/Checkout.qml @@ -24,40 +24,43 @@ Rectangle { HifiConstants { id: hifi; } id: checkoutRoot; - property string itemId; - property string itemHref; - property int balanceAfterPurchase: commerce.balance() - parseInt(itemPriceText.text, 10); - property bool alreadyOwned: checkAlreadyOwned(itemId); + property string itemId: ""; + property string itemHref: ""; + property int hfcBalance: 0; + property int balanceAfterPurchase: 0; + property bool alreadyOwned: false; // Style color: hifi.colors.baseGray; Hifi.QmlCommerce { id: commerce; onBuyResult: { - /* - if (buyFailed) { + if (failureMessage.length) { sendToScript({method: 'checkout_cancelClicked', params: itemId}); } else { - var success = commerce.buy(itemId, parseInt(itemPriceText.text)); - sendToScript({method: 'checkout_buyClicked', success: success, itemId: itemId, itemHref: itemHref}); - if (success) { - if (urlHandler.canHandleUrl(itemHref)) { - urlHandler.handleUrl(itemHref); - } + if (urlHandler.canHandleUrl(itemHref)) { + urlHandler.handleUrl(itemHref); } } - */ - - if (failureMessage.length) { - console.log('buy failed', failureMessage); - //fixme sendToScript({method: 'checkout_cancelClicked', params: itemId}); - } else { - console.log('buy ok'); - //fixme sendToScript({method: 'checkout_buyClicked', success: , itemId: itemId, itemHref: itemHref}); - } } - // FIXME: remove these two after testing - onBalanceResult: console.log('balance', balance, failureMessage); - onInventoryResult: console.log('inventory', inventory, failureMessage); + onBalanceResult: { + if (failureMessage.length) { + console.log("Failed to get balance", failureMessage); + } else { + hfcBalance = balance; + balanceAfterPurchase = hfcBalance - parseInt(itemPriceText.text, 10); + } + } + onInventoryResult: { + if (failureMessage.length) { + console.log("Failed to get inventory", failureMessage); + } else { + if (inventory.indexOf(itemId) !== -1) { + alreadyOwned = true; + } else { + alreadyOwned = false; + } + } + } } // @@ -229,7 +232,7 @@ Rectangle { } RalewayRegular { id: hfcBalanceText; - text: commerce.balance(); + text: hfcBalance; // Text size size: hfcBalanceTextLabel.size; // Anchors @@ -397,16 +400,6 @@ Rectangle { // // FUNCTION DEFINITIONS START // - - function checkAlreadyOwned(idToCheck) { - var inventory = commerce.inventory(); - if (inventory.indexOf(idToCheck) !== -1) { - return true; - } else { - return false; - } - } - // // Function Name: fromScript() // @@ -429,6 +422,8 @@ Rectangle { itemPriceText.text = message.params.itemPrice; itemHref = message.params.itemHref; buyButton.buyFailed = false; + commerce.balance(); + commerce.inventory(); break; case 'buyFailed': buyButton.text = "Buy Failed"; diff --git a/interface/resources/qml/hifi/commerce/Inventory.qml b/interface/resources/qml/hifi/commerce/Inventory.qml index 91fa3b9126..7ed43e6f9e 100644 --- a/interface/resources/qml/hifi/commerce/Inventory.qml +++ b/interface/resources/qml/hifi/commerce/Inventory.qml @@ -29,6 +29,20 @@ Rectangle { color: hifi.colors.baseGray; Hifi.QmlCommerce { id: commerce; + onBalanceResult: { + if (failureMessage.length) { + console.log("Failed to get balance", failureMessage); + } else { + hfcBalanceText.text = balance; + } + } + onInventoryResult: { + if (failureMessage.length) { + console.log("Failed to get inventory", failureMessage); + } else { + inventoryContentsList.model = inventory; + } + } } // @@ -101,7 +115,6 @@ Rectangle { } RalewayRegular { id: hfcBalanceText; - text: commerce.balance(); // Text size size: hfcBalanceTextLabel.size; // Anchors @@ -158,7 +171,6 @@ Rectangle { anchors.left: parent.left; anchors.bottom: parent.bottom; width: parent.width; - model: commerce.inventory(); delegate: Item { width: parent.width; height: 30; @@ -247,6 +259,8 @@ Rectangle { switch (message.method) { case 'updateInventory': referrerURL = message.referrerURL; + commerce.balance(); + commerce.inventory(); break; default: console.log('Unrecognized message from marketplaces.js:', JSON.stringify(message)); From c38837c20021bfc5e758f040be8a563a3483545f Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Fri, 11 Aug 2017 16:21:39 -0700 Subject: [PATCH 2/4] Remove fixmes --- interface/resources/qml/hifi/commerce/Checkout.qml | 12 ++++-------- scripts/system/marketplaces/marketplaces.js | 8 ++------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/interface/resources/qml/hifi/commerce/Checkout.qml b/interface/resources/qml/hifi/commerce/Checkout.qml index 458e417654..cfa8f82fd9 100644 --- a/interface/resources/qml/hifi/commerce/Checkout.qml +++ b/interface/resources/qml/hifi/commerce/Checkout.qml @@ -35,11 +35,13 @@ Rectangle { id: commerce; onBuyResult: { if (failureMessage.length) { - sendToScript({method: 'checkout_cancelClicked', params: itemId}); + buyButton.text = "Buy Failed"; + buyButton.enabled = false; } else { if (urlHandler.canHandleUrl(itemHref)) { urlHandler.handleUrl(itemHref); } + sendToScript({method: 'checkout_buySuccess', params: itemId}); } } onBalanceResult: { @@ -369,13 +371,12 @@ Rectangle { width: parent.width/2 - anchors.leftMargin*2; text: "Cancel" onClicked: { - sendToScript({method: 'checkout_cancelClicked', params: itemId}); //fixme + sendToScript({method: 'checkout_cancelClicked', params: itemId}); } } // "Buy" button HifiControlsUit.Button { - property bool buyFailed: false; // fixme id: buyButton; enabled: balanceAfterPurchase >= 0 && !alreadyOwned; color: hifi.buttons.black; @@ -421,14 +422,9 @@ Rectangle { itemAuthorText.text = message.params.itemAuthor; itemPriceText.text = message.params.itemPrice; itemHref = message.params.itemHref; - buyButton.buyFailed = false; commerce.balance(); commerce.inventory(); 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/marketplaces/marketplaces.js b/scripts/system/marketplaces/marketplaces.js index e44e9fda94..9378a1d95b 100644 --- a/scripts/system/marketplaces/marketplaces.js +++ b/scripts/system/marketplaces/marketplaces.js @@ -204,12 +204,8 @@ // I don't think this is trivial to do since we also want to inject some JS into the DOM. //tablet.popFromStack(); break; - case 'checkout_buyClicked': - if (message.success === true) { - tablet.gotoWebScreen(MARKETPLACE_URL + '/items/' + message.itemId, MARKETPLACES_INJECT_SCRIPT_URL); - } else { - tablet.sendToQml({ method: 'buyFailed' }); - } + case 'checkout_buySuccess': + tablet.gotoWebScreen(MARKETPLACE_URL + '/items/' + message.itemId, MARKETPLACES_INJECT_SCRIPT_URL); //tablet.popFromStack(); break; case 'inventory_itemClicked': From d8e5f7a067155e5d2c9c8f468b3426e0605bee9a Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Fri, 11 Aug 2017 16:28:08 -0700 Subject: [PATCH 3/4] Small improvements --- .../resources/qml/hifi/commerce/Checkout.qml | 15 +++++++++------ .../resources/qml/hifi/commerce/Inventory.qml | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/interface/resources/qml/hifi/commerce/Checkout.qml b/interface/resources/qml/hifi/commerce/Checkout.qml index cfa8f82fd9..e40e2ce67f 100644 --- a/interface/resources/qml/hifi/commerce/Checkout.qml +++ b/interface/resources/qml/hifi/commerce/Checkout.qml @@ -24,9 +24,10 @@ Rectangle { HifiConstants { id: hifi; } id: checkoutRoot; + property bool inventoryReceived: false; + property bool balanceReceived: false; property string itemId: ""; property string itemHref: ""; - property int hfcBalance: 0; property int balanceAfterPurchase: 0; property bool alreadyOwned: false; // Style @@ -48,14 +49,16 @@ Rectangle { if (failureMessage.length) { console.log("Failed to get balance", failureMessage); } else { - hfcBalance = balance; - balanceAfterPurchase = hfcBalance - parseInt(itemPriceText.text, 10); + balanceReceived = true; + hfcBalanceText.text = balance; + balanceAfterPurchase = balance - parseInt(itemPriceText.text, 10); } } onInventoryResult: { if (failureMessage.length) { console.log("Failed to get inventory", failureMessage); } else { + inventoryReceived = true; if (inventory.indexOf(itemId) !== -1) { alreadyOwned = true; } else { @@ -234,7 +237,7 @@ Rectangle { } RalewayRegular { id: hfcBalanceText; - text: hfcBalance; + text: "--"; // Text size size: hfcBalanceTextLabel.size; // Anchors @@ -378,7 +381,7 @@ Rectangle { // "Buy" button HifiControlsUit.Button { id: buyButton; - enabled: balanceAfterPurchase >= 0 && !alreadyOwned; + enabled: balanceAfterPurchase >= 0 && !alreadyOwned && inventoryReceived && balanceReceived; color: hifi.buttons.black; colorScheme: hifi.colorSchemes.dark; anchors.top: parent.top; @@ -388,7 +391,7 @@ Rectangle { anchors.right: parent.right; anchors.rightMargin: 20; width: parent.width/2 - anchors.rightMargin*2; - text: alreadyOwned ? "Already Owned" : "Buy"; + text: (inventoryReceived && balanceReceived) ? (alreadyOwned ? "Already Owned" : "Buy") : "--"; onClicked: { commerce.buy(itemId, parseInt(itemPriceText.text)); } diff --git a/interface/resources/qml/hifi/commerce/Inventory.qml b/interface/resources/qml/hifi/commerce/Inventory.qml index 7ed43e6f9e..a562d00218 100644 --- a/interface/resources/qml/hifi/commerce/Inventory.qml +++ b/interface/resources/qml/hifi/commerce/Inventory.qml @@ -115,6 +115,7 @@ Rectangle { } RalewayRegular { id: hfcBalanceText; + text: "--"; // Text size size: hfcBalanceTextLabel.size; // Anchors From 0835b7b8e6be09ae59af39cbd0aa825e0d46d138 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Fri, 11 Aug 2017 16:36:26 -0700 Subject: [PATCH 4/4] Bugfix --- interface/resources/qml/hifi/commerce/Checkout.qml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/resources/qml/hifi/commerce/Checkout.qml b/interface/resources/qml/hifi/commerce/Checkout.qml index e40e2ce67f..7c2d6b56a5 100644 --- a/interface/resources/qml/hifi/commerce/Checkout.qml +++ b/interface/resources/qml/hifi/commerce/Checkout.qml @@ -42,7 +42,7 @@ Rectangle { if (urlHandler.canHandleUrl(itemHref)) { urlHandler.handleUrl(itemHref); } - sendToScript({method: 'checkout_buySuccess', params: itemId}); + sendToScript({method: 'checkout_buySuccess', itemId: itemId}); } } onBalanceResult: {