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"
11 const paymentSrv = Cc[
12 "@mozilla.org/dom/payments/payment-request-service;1"
13 ].getService(Ci.nsIPaymentRequestService);
17 cardNumber: "4111111111111111",
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(
31 const address = Cc["@mozilla.org/supports-string;1"].createInstance(
34 address.data = "Easton Ave";
35 addressLine.appendElement(address);
38 addressLine, // address line
42 "", // dependent locality
43 "94066", // postal code
44 "123456", // sorting code
46 "Bill A. Pacheco", // recipient
47 "+14344413879", // phone
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);
67 billingAddress !== undefined ? billingAddress : makeBillingAddress();
69 basicCardResponseData.initData(
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);
89 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
90 "basic-card", // payment method
91 makeBasicCardResponse(details),
97 paymentSrv.respondPayment(
98 showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
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);
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(
115 Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED
117 paymentSrv.respondPayment(
118 completeResponse.QueryInterface(Ci.nsIPaymentActionResponse)
121 get QueryInterface() {
122 return ChromeUtils.generateQI(["nsIPaymentUIService"]);
126 paymentSrv.setTestingUIService(
127 TestingUIService.QueryInterface(Ci.nsIPaymentUIService)
130 addMessageListener("teardown", () => {
131 paymentSrv.setTestingUIService(null);
132 sendAsyncMessage("teardown-complete");