UpdateErrorsChromeScript.js (6338B)
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 function emitTestPass(message) { 21 sendAsyncMessage("test-pass", message); 22 } 23 24 const shippingAddress = Cc[ 25 "@mozilla.org/dom/payments/payment-address;1" 26 ].createInstance(Ci.nsIPaymentAddress); 27 const addressLine = Cc["@mozilla.org/array;1"].createInstance( 28 Ci.nsIMutableArray 29 ); 30 const address = Cc["@mozilla.org/supports-string;1"].createInstance( 31 Ci.nsISupportsString 32 ); 33 address.data = "Easton Ave"; 34 addressLine.appendElement(address); 35 shippingAddress.init( 36 "USA", // country 37 addressLine, // address line 38 "CA", // region 39 "CA", // region code 40 "San Bruno", // city 41 "Test locality", // dependent locality 42 "94066", // postal code 43 "123456", // sorting code 44 "Testing Org", // organization 45 "Bill A. Pacheco", // recipient 46 "+1-434-441-3879" 47 ); // phone 48 49 function acceptShow(requestId) { 50 const responseData = Cc[ 51 "@mozilla.org/dom/payments/general-response-data;1" 52 ].createInstance(Ci.nsIGeneralResponseData); 53 responseData.initData({ 54 paymentToken: "6880281f-0df3-4b8e-916f-66575e2457c1", 55 }); 56 let showResponse = Cc[ 57 "@mozilla.org/dom/payments/payment-show-action-response;1" 58 ].createInstance(Ci.nsIPaymentShowActionResponse); 59 showResponse.init( 60 requestId, 61 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, 62 "testing-payment-method", // payment method 63 responseData, // payment method data 64 "Bill A. Pacheco", // payer name 65 "", // payer email 66 "" 67 ); // payer phone 68 paymentSrv.respondPayment( 69 showResponse.QueryInterface(Ci.nsIPaymentActionResponse) 70 ); 71 } 72 73 function rejectShow(requestId) { 74 const responseData = Cc[ 75 "@mozilla.org/dom/payments/general-response-data;1" 76 ].createInstance(Ci.nsIGeneralResponseData); 77 responseData.initData({}); 78 const showResponse = Cc[ 79 "@mozilla.org/dom/payments/payment-show-action-response;1" 80 ].createInstance(Ci.nsIPaymentShowActionResponse); 81 showResponse.init( 82 requestId, 83 Ci.nsIPaymentActionResponse.PAYMENT_REJECTED, 84 "", // payment method 85 responseData, // payment method data 86 "", // payer name 87 "", // payer email 88 "" 89 ); // payer phone 90 paymentSrv.respondPayment( 91 showResponse.QueryInterface(Ci.nsIPaymentActionResponse) 92 ); 93 } 94 95 function updateShow(requestId) { 96 paymentSrv.changeShippingAddress(requestId, shippingAddress); 97 } 98 99 function showRequest(requestId) { 100 updateShow(requestId); 101 } 102 103 function abortRequest(requestId) { 104 let abortResponse = Cc[ 105 "@mozilla.org/dom/payments/payment-abort-action-response;1" 106 ].createInstance(Ci.nsIPaymentAbortActionResponse); 107 abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED); 108 paymentSrv.respondPayment(abortResponse); 109 } 110 111 function completeRequest(requestId) { 112 let payRequest = paymentSrv.getPaymentRequestById(requestId); 113 let completeResponse = Cc[ 114 "@mozilla.org/dom/payments/payment-complete-action-response;1" 115 ].createInstance(Ci.nsIPaymentCompleteActionResponse); 116 completeResponse.init( 117 requestId, 118 Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED 119 ); 120 paymentSrv.respondPayment( 121 completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) 122 ); 123 } 124 125 function checkAddressErrors(errors) { 126 if (!errors) { 127 emitTestFail("Expect non-null shippingAddressErrors, but got null."); 128 } 129 if (errors.addressLine != "addressLine error") { 130 emitTestFail( 131 "Expect shippingAddressErrors.addressLine as 'addressLine error', but got" + 132 errors.addressLine 133 ); 134 } 135 if (errors.city != "city error") { 136 emitTestFail( 137 "Expect shippingAddressErrors.city as 'city error', but got" + errors.city 138 ); 139 } 140 if (errors.dependentLocality != "dependentLocality error") { 141 emitTestFail( 142 "Expect shippingAddressErrors.dependentLocality as 'dependentLocality error', but got" + 143 errors.dependentLocality 144 ); 145 } 146 if (errors.organization != "organization error") { 147 emitTestFail( 148 "Expect shippingAddressErrors.organization as 'organization error', but got" + 149 errors.organization 150 ); 151 } 152 if (errors.phone != "phone error") { 153 emitTestFail( 154 "Expect shippingAddressErrors.phone as 'phone error', but got" + 155 errors.phone 156 ); 157 } 158 if (errors.postalCode != "postalCode error") { 159 emitTestFail( 160 "Expect shippingAddressErrors.postalCode as 'postalCode error', but got" + 161 errors.postalCode 162 ); 163 } 164 if (errors.recipient != "recipient error") { 165 emitTestFail( 166 "Expect shippingAddressErrors.recipient as 'recipient error', but got" + 167 errors.recipient 168 ); 169 } 170 if (errors.region != "region error") { 171 emitTestFail( 172 "Expect shippingAddressErrors.region as 'region error', but got" + 173 errors.region 174 ); 175 } 176 if (errors.regionCode != "regionCode error") { 177 emitTestFail( 178 "Expect shippingAddressErrors.regionCode as 'regionCode error', but got" + 179 errors.region 180 ); 181 } 182 if (errors.sortingCode != "sortingCode error") { 183 emitTestFail( 184 "Expect shippingAddressErrors.sortingCode as 'sortingCode error', but got" + 185 errors.sortingCode 186 ); 187 } 188 } 189 190 function updateRequest(requestId) { 191 let request = paymentSrv.getPaymentRequestById(requestId); 192 const addressErrors = request.paymentDetails.shippingAddressErrors; 193 const payerErrors = request.paymentDetails.payerErrors; 194 checkAddressErrors(addressErrors); 195 rejectShow(requestId); 196 } 197 198 const DummyUIService = { 199 showPayment: showRequest, 200 abortPayment: abortRequest, 201 completePayment: completeRequest, 202 updatePayment: updateRequest, 203 closePayment(requestId) {}, 204 QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]), 205 }; 206 207 paymentSrv.setTestingUIService( 208 DummyUIService.QueryInterface(Ci.nsIPaymentUIService) 209 ); 210 211 addMessageListener("teardown", function () { 212 paymentSrv.setTestingUIService(null); 213 sendAsyncMessage("teardown-complete"); 214 });