RequestShippingChromeScript.js (3346B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ */ 4 5 /* eslint-env mozilla/chrome-script */ 6 7 "use strict"; 8 9 const { XPCOMUtils } = ChromeUtils.importESModule( 10 "resource://gre/modules/XPCOMUtils.sys.mjs" 11 ); 12 13 const paymentSrv = Cc[ 14 "@mozilla.org/dom/payments/payment-request-service;1" 15 ].getService(Ci.nsIPaymentRequestService); 16 17 function emitTestFail(message) { 18 sendAsyncMessage("test-fail", message); 19 } 20 21 const shippingAddress = Cc[ 22 "@mozilla.org/dom/payments/payment-address;1" 23 ].createInstance(Ci.nsIPaymentAddress); 24 const addressLine = Cc["@mozilla.org/array;1"].createInstance( 25 Ci.nsIMutableArray 26 ); 27 const address = Cc["@mozilla.org/supports-string;1"].createInstance( 28 Ci.nsISupportsString 29 ); 30 address.data = "Easton Ave"; 31 addressLine.appendElement(address); 32 shippingAddress.init( 33 "", // country 34 addressLine, // address line 35 "", // region 36 "", // region code 37 "", // city 38 "", // dependent locality 39 "", // postal code 40 "", // sorting code 41 "", // organization 42 "", // recipient 43 "" 44 ); // phone 45 46 const NormalUIService = { 47 shippingOptionChanged: false, 48 showPayment(requestId) { 49 paymentSrv.changeShippingAddress(requestId, shippingAddress); 50 }, 51 abortPayment(requestId) {}, 52 completePayment(requestId) { 53 let completeResponse = Cc[ 54 "@mozilla.org/dom/payments/payment-complete-action-response;1" 55 ].createInstance(Ci.nsIPaymentCompleteActionResponse); 56 completeResponse.init( 57 requestId, 58 Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED 59 ); 60 paymentSrv.respondPayment( 61 completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) 62 ); 63 }, 64 updatePayment(requestId) { 65 let showResponse = null; 66 let payRequest = paymentSrv.getPaymentRequestById(requestId); 67 68 const shippingOptions = payRequest.paymentDetails.shippingOptions; 69 if (shippingOptions.length) { 70 emitTestFail("Wrong length for shippingOptions."); 71 } 72 73 const showResponseData = Cc[ 74 "@mozilla.org/dom/payments/general-response-data;1" 75 ].createInstance(Ci.nsIGeneralResponseData); 76 77 try { 78 showResponseData.initData({ 79 paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1", 80 }); 81 } catch (e) { 82 emitTestFail( 83 'Fail to initialize response data with { paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1",}' 84 ); 85 } 86 87 showResponse = Cc[ 88 "@mozilla.org/dom/payments/payment-show-action-response;1" 89 ].createInstance(Ci.nsIPaymentShowActionResponse); 90 showResponse.init( 91 requestId, 92 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, 93 "testing-payment-method", // payment method 94 showResponseData, // payment method data 95 "", // payer name 96 "", // payer email 97 "" 98 ); // payer phone 99 paymentSrv.respondPayment( 100 showResponse.QueryInterface(Ci.nsIPaymentActionResponse) 101 ); 102 }, 103 closePayment(requestId) {}, 104 QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), 105 }; 106 107 addMessageListener("set-normal-ui-service", function () { 108 paymentSrv.setTestingUIService( 109 NormalUIService.QueryInterface(Ci.nsIPaymentUIService) 110 ); 111 }); 112 113 addMessageListener("teardown", function () { 114 paymentSrv.setTestingUIService(null); 115 sendAsyncMessage("teardown-complete"); 116 });