Bug 1387221 - integrate DOM API "changeshippingaddress" when switching address selected. r=mattn draft
authorJonathan Guillotte-Blouin <jguillotteblouin@mozilla.com>
Mon, 07 Aug 2017 17:47:55 -0700
changeset 645214 61d0a6a3601933dc9b7fc826cc507f80faa0ad5d
parent 645213 cdf1c2db5ca5386e83627ce5e254cd985329b16c
child 645215 1b78fe7299033d159d8fc1d45272b2bebd419626
push id73702
push userbmo:jonathan.guillotte.blouin@gmail.com
push dateFri, 11 Aug 2017 23:50:47 +0000
reviewersmattn
bugs1387221
milestone57.0a1
Bug 1387221 - integrate DOM API "changeshippingaddress" when switching address selected. r=mattn MozReview-Commit-ID: 6rY26khZois
toolkit/components/payments/content/paymentDialog.js
toolkit/components/payments/paymentUIService.js
toolkit/components/payments/res/paymentRequest.js
--- a/toolkit/components/payments/content/paymentDialog.js
+++ b/toolkit/components/payments/content/paymentDialog.js
@@ -41,49 +41,96 @@ let PaymentDialog = {
       case Ci.nsIPaymentShowActionResponse: {
         componentName = "@mozilla.org/dom/payments/payment-show-action-response;1";
         break;
       }
       case Ci.nsIGeneralResponseData: {
         componentName = "@mozilla.org/dom/payments/general-response-data;1";
         break;
       }
+      case Ci.nsIPaymentAddress: {
+        componentName = "@mozilla.org/dom/payments/payment-address;1";
+        break;
+      }
+      case Ci.nsIMutableArray: {
+        componentName = "@mozilla.org/array;1";
+        break;
+      }
+      case Ci.nsISupportsString: {
+        componentName = "@mozilla.org/supports-string;1";
+        break;
+      }
     }
     let component = this.componentsLoaded.get(componentName);
 
     if (!component) {
       component = Cc[componentName];
       this.componentsLoaded.set(componentName, component);
     }
 
     return component.createInstance(componentInterface);
   },
 
+  createShippingAddress({addressLines, city = "", country = "", name = "",
+                         organization = "", phone = "", postalCode = "", region = ""}) {
+    const billingAddress = this.createComponentInstance(Ci.nsIPaymentAddress);  
+    const addressLinesInstance = this.createComponentInstance(Ci.nsIMutableArray);
+    for (let addressLine of addressLines) {
+      const addressInstance = this.createComponentInstance(Ci.nsISupportsString);
+      addressInstance.data = addressLine;
+      addressLinesInstance.appendElement(addressInstance);
+    }
+
+    billingAddress.init(
+      country,
+      addressLinesInstance,
+      region,
+      city,
+      "", // XXX: dependent locality
+      postalCode,
+      "", // XXX: sorting code
+      "", // XXX: language code
+      organization,
+      name,
+      phone,
+    );
+    return billingAddress;
+  },
+
   onPaymentCancel(requestId) {
     const showResponse = this.createShowResponse({
       requestId,
       acceptStatus: Ci.nsIPaymentActionResponse.PAYMENT_REJECTED,
     });
     paymentSrv.respondPayment(showResponse);
     window.close();
   },
 
+  onChangeShippingAddress({requestId, data}) {
+    const billingAddress = this.createShippingAddress(data);
+    paymentSrv.changeShippingAddress(requestId, billingAddress);
+  },
+
   receiveMessage({data}) {
     let {messageType, requestId} = data;
 
     switch (messageType) {
       case "DOMContentLoaded": {
         this.mm.sendAsyncMessage("paymentChromeToContent", {
           messageType: "showPaymentRequest",
           data: window.arguments[0],
         });
         break;
       }
       case "paymentCancel": {
         this.onPaymentCancel(requestId);
         break;
       }
+      case "changeShippingAddress": {
+        this.onChangeShippingAddress(data.detail);
+        break;
+      }
     }
   },
 };
 
 let frame = document.getElementById("paymentRequestFrame");
 PaymentDialog.init(frame);
--- a/toolkit/components/payments/paymentUIService.js
+++ b/toolkit/components/payments/paymentUIService.js
@@ -129,16 +129,20 @@ PaymentUIService.prototype = {
                              .createInstance(Ci.nsIPaymentCompleteActionResponse);
     completeResponse.init(requestId, Ci.nsIPaymentActionResponse.COMPLTETE_SUCCEEDED);
     paymentSrv.respondPayment(completeResponse.QueryInterface(Ci.nsIPaymentActionResponse));
   },
 
   updatePayment(requestId) {
   },
 
+  updatePayment() {
+    this.log.debug(`updateArgs: ${JSON.stringify(arguments)}`)
+  },
+
   // other helper methods
 
   requestIdForWindow(window) {
     let windowName = window.name;
 
     return windowName.startsWith(this.REQUEST_ID_PREFIX) ?
       windowName.replace(this.REQUEST_ID_PREFIX, "") : // returns suffix, which is the requestId
       null;
--- a/toolkit/components/payments/res/paymentRequest.js
+++ b/toolkit/components/payments/res/paymentRequest.js
@@ -120,16 +120,21 @@ let PaymentRequest = {
             profileElem.textContent = renderedProfile;
             profileElem.classList.add("shipment-info");
             profileElem.dataset.i = i;
             // make 1st profile visible, hide others (by default)
             if (i == 0) {
               profileElem.classList.add("selected");
               this.shippings.selected = 0;
               this.shippings.status = "selected";
+
+              // the profile visible, if valid, must trigger the merchant as a new address
+              if (shipping.error.length == 0) {
+                this.sendMessageToContent("changeShippingAddress", profile);
+              }
             }
             if (shipping.error.length > 0) {
               profileElem.classList.add("error");
             }
             this.addBalancedListener(profileElem, "click", this.changeShippingAddress);
             shipmentInfoElem.appendChild(profileElem);
             this.shippings.list.push(shipping);
           });
@@ -171,16 +176,22 @@ let PaymentRequest = {
 
       // there has been no change in the address selected, stop here
       let i = parseInt(e.target.dataset.i, 10);
       if (i == this.shippings.selected) {
         return;
       }
 
       this.shippings.selected = i;
+
+      // if this address has no errors, then we can alert the DOM that we have a new address
+      // otherwise, this address is problematic and will need some ulterior changes
+      if (this.shippings.list[i].error.length == 0) {
+        this.sendMessageToContent("changeShippingAddress", shippings.list[i].data);
+      }
     }
   },
 
   onPaymentRequestLoad(requestId) {
     let cancelBtn = document.getElementById("cancel");
     cancelBtn.addEventListener("click", this, {once: true});
 
     window.addEventListener("unload", this, {once: true});