PayerDetailsChromeScript.js (2756B)
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 TestingUIService = { 16 showPayment(requestId, name = "", email = "", phone = "") { 17 const showResponseData = Cc[ 18 "@mozilla.org/dom/payments/general-response-data;1" 19 ].createInstance(Ci.nsIGeneralResponseData); 20 showResponseData.initData({}); 21 const showResponse = Cc[ 22 "@mozilla.org/dom/payments/payment-show-action-response;1" 23 ].createInstance(Ci.nsIPaymentShowActionResponse); 24 showResponse.init( 25 requestId, 26 Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED, 27 "testing-payment-method", // payment method 28 showResponseData, // payment method data 29 name, 30 email, 31 phone 32 ); 33 paymentSrv.respondPayment( 34 showResponse.QueryInterface(Ci.nsIPaymentActionResponse) 35 ); 36 }, 37 // .retry({ payer }) and .updateWith({payerErrors}) both get routed here: 38 updatePayment(requestId) { 39 // Let's echo what was sent in by the error... 40 const request = paymentSrv.getPaymentRequestById(requestId); 41 const { name, email, phone } = request.paymentDetails.payerErrors; 42 const { error } = request.paymentDetails; 43 // Let's use the .error as the switch 44 switch (error) { 45 case "retry-fire-payerdetaichangeevent": { 46 paymentSrv.changePayerDetail(requestId, name, email, phone); 47 break; 48 } 49 case "update-with": { 50 this.showPayment(requestId, name, email, phone); 51 break; 52 } 53 default: { 54 const msg = `Expect details.error value: '${error}'`; 55 sendAsyncMessage("test-fail", msg); 56 } 57 } 58 }, 59 completePayment(requestId) { 60 const request = paymentSrv.getPaymentRequestById(requestId); 61 const completeResponse = Cc[ 62 "@mozilla.org/dom/payments/payment-complete-action-response;1" 63 ].createInstance(Ci.nsIPaymentCompleteActionResponse); 64 completeResponse.init( 65 requestId, 66 Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED 67 ); 68 paymentSrv.respondPayment( 69 completeResponse.QueryInterface(Ci.nsIPaymentActionResponse) 70 ); 71 }, 72 get QueryInterface() { 73 return ChromeUtils.generateQI(["nsIPaymentUIService"]); 74 }, 75 }; 76 77 paymentSrv.setTestingUIService( 78 TestingUIService.QueryInterface(Ci.nsIPaymentUIService) 79 ); 80 81 addMessageListener("teardown", () => { 82 paymentSrv.setTestingUIService(null); 83 sendAsyncMessage("teardown-complete"); 84 });