Bug 1795723 - Unified extensions UI should support High Contrast Mode. r=ayeddi,deskt...
[gecko.git] / dom / payments / test / BasicCardErrorsChromeScript.js
blobf92e5eef5cd554f738b1023c268ddce8ea565aa8
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/ */
5 /* eslint-env mozilla/chrome-script */
7 const { XPCOMUtils } = ChromeUtils.importESModule(
8   "resource://gre/modules/XPCOMUtils.sys.mjs"
9 );
11 const paymentSrv = Cc[
12   "@mozilla.org/dom/payments/payment-request-service;1"
13 ].getService(Ci.nsIPaymentRequestService);
15 const defaultCard = {
16   cardholderName: "",
17   cardNumber: "4111111111111111",
18   expiryMonth: "",
19   expiryYear: "",
20   cardSecurityCode: "",
21   billingAddress: null,
24 function makeBillingAddress() {
25   const billingAddress = Cc[
26     "@mozilla.org/dom/payments/payment-address;1"
27   ].createInstance(Ci.nsIPaymentAddress);
28   const addressLine = Cc["@mozilla.org/array;1"].createInstance(
29     Ci.nsIMutableArray
30   );
31   const address = Cc["@mozilla.org/supports-string;1"].createInstance(
32     Ci.nsISupportsString
33   );
34   address.data = "Easton Ave";
35   addressLine.appendElement(address);
36   const addressArgs = [
37     "USA", // country
38     addressLine, // address line
39     "CA", // region
40     "CA", // regionCode
41     "San Bruno", // city
42     "", // dependent locality
43     "94066", // postal code
44     "123456", // sorting code
45     "", // organization
46     "Bill A. Pacheco", // recipient
47     "+14344413879", // phone
48   ];
49   billingAddress.init(...addressArgs);
50   return billingAddress;
53 function makeBasicCardResponse(details) {
54   const basicCardResponseData = Cc[
55     "@mozilla.org/dom/payments/basiccard-response-data;1"
56   ].createInstance(Ci.nsIBasicCardResponseData);
57   const {
58     cardholderName,
59     cardNumber,
60     expiryMonth,
61     expiryYear,
62     cardSecurityCode,
63     billingAddress,
64   } = details;
66   const address =
67     billingAddress !== undefined ? billingAddress : makeBillingAddress();
69   basicCardResponseData.initData(
70     cardholderName,
71     cardNumber,
72     expiryMonth,
73     expiryYear,
74     cardSecurityCode,
75     address
76   );
78   return basicCardResponseData;
81 const TestingUIService = {
82   showPayment(requestId, details = { ...defaultCard }) {
83     const showResponse = Cc[
84       "@mozilla.org/dom/payments/payment-show-action-response;1"
85     ].createInstance(Ci.nsIPaymentShowActionResponse);
87     showResponse.init(
88       requestId,
89       Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
90       "basic-card", // payment method
91       makeBasicCardResponse(details),
92       "Person name",
93       "Person email",
94       "Person phone"
95     );
97     paymentSrv.respondPayment(
98       showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
99     );
100   },
101   // Handles response.retry({ paymentMethod }):
102   updatePayment(requestId) {
103     // Let's echo what was sent in by the error...
104     const request = paymentSrv.getPaymentRequestById(requestId);
105     this.showPayment(requestId, request.paymentDetails.paymentMethodErrors);
106   },
107   // Handles response.complete()
108   completePayment(requestId) {
109     const request = paymentSrv.getPaymentRequestById(requestId);
110     const completeResponse = Cc[
111       "@mozilla.org/dom/payments/payment-complete-action-response;1"
112     ].createInstance(Ci.nsIPaymentCompleteActionResponse);
113     completeResponse.init(
114       requestId,
115       Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED
116     );
117     paymentSrv.respondPayment(
118       completeResponse.QueryInterface(Ci.nsIPaymentActionResponse)
119     );
120   },
121   get QueryInterface() {
122     return ChromeUtils.generateQI(["nsIPaymentUIService"]);
123   },
126 paymentSrv.setTestingUIService(
127   TestingUIService.QueryInterface(Ci.nsIPaymentUIService)
130 addMessageListener("teardown", () => {
131   paymentSrv.setTestingUIService(null);
132   sendAsyncMessage("teardown-complete");