BasicCardErrorsChromeScript.js (3673B)
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 const { XPCOMUtils } = ChromeUtils.importESModule( 8 "resource://gre/modules/XPCOMUtils.sys.mjs" 9 ); 10 11 const paymentSrv = Cc[ 12 "@mozilla.org/dom/payments/payment-request-service;1" 13 ].getService(Ci.nsIPaymentRequestService); 14 15 const defaultCard = { 16 cardholderName: "", 17 cardNumber: "4111111111111111", 18 expiryMonth: "", 19 expiryYear: "", 20 cardSecurityCode: "", 21 billingAddress: null, 22 }; 23 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; 51 } 52 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; 65 66 const address = 67 billingAddress !== undefined ? billingAddress : makeBillingAddress(); 68 69 basicCardResponseData.initData( 70 cardholderName, 71 cardNumber, 72 expiryMonth, 73 expiryYear, 74 cardSecurityCode, 75 address 76 ); 77 78 return basicCardResponseData; 79 } 80 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); 86 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 ); 96 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 }, 124 }; 125 126 paymentSrv.setTestingUIService( 127 TestingUIService.QueryInterface(Ci.nsIPaymentUIService) 128 ); 129 130 addMessageListener("teardown", () => { 131 paymentSrv.setTestingUIService(null); 132 sendAsyncMessage("teardown-complete"); 133 });