tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RetryPaymentChromeScript.js (6627B)


      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 billingAddress = 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 billingAddress.init(
     36  "USA", // country
     37  addressLine, // address line
     38  "CA", // region
     39  "CA", // region code
     40  "San Bruno", // city
     41  "", // dependent locality
     42  "94066", // postal code
     43  "123456", // sorting code
     44  "", // organization
     45  "Bill A. Pacheco", // recipient
     46  "+14344413879"
     47 ); // phone
     48 
     49 function acceptPayment(requestId, mode) {
     50  const basiccardResponseData = Cc[
     51    "@mozilla.org/dom/payments/basiccard-response-data;1"
     52  ].createInstance(Ci.nsIBasicCardResponseData);
     53  const showResponse = Cc[
     54    "@mozilla.org/dom/payments/payment-show-action-response;1"
     55  ].createInstance(Ci.nsIPaymentShowActionResponse);
     56  basiccardResponseData.initData(
     57    "Bill A. Pacheco", // cardholderName
     58    "4916855166538720", // cardNumber
     59    "01", // expiryMonth
     60    "2024", // expiryYear
     61    "180", // cardSecurityCode
     62    billingAddress
     63  ); // billingAddress
     64  if (mode === "show") {
     65    showResponse.init(
     66      requestId,
     67      Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
     68      "basic-card", // payment method
     69      basiccardResponseData, // payment method data
     70      "Bill A. Pacheco", // payer name
     71      "", // payer email
     72      ""
     73    ); // payer phone
     74  }
     75  if (mode == "retry") {
     76    showResponse.init(
     77      requestId,
     78      Ci.nsIPaymentActionResponse.PAYMENT_ACCEPTED,
     79      "basic-card", // payment method
     80      basiccardResponseData, // payment method data
     81      "Bill A. Pacheco", // payer name
     82      "bpacheco@test.org", // payer email
     83      "+123456789"
     84    ); // payer phone
     85  }
     86  paymentSrv.respondPayment(
     87    showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
     88  );
     89 }
     90 
     91 function rejectPayment(requestId) {
     92  const responseData = Cc[
     93    "@mozilla.org/dom/payments/general-response-data;1"
     94  ].createInstance(Ci.nsIGeneralResponseData);
     95  responseData.initData({});
     96  const showResponse = Cc[
     97    "@mozilla.org/dom/payments/payment-show-action-response;1"
     98  ].createInstance(Ci.nsIPaymentShowActionResponse);
     99  showResponse.init(
    100    requestId,
    101    Ci.nsIPaymentActionResponse.PAYMENT_REJECTED,
    102    "", // payment method
    103    responseData, // payment method data
    104    "", // payer name
    105    "", // payer email
    106    ""
    107  ); // payer phone
    108  paymentSrv.respondPayment(
    109    showResponse.QueryInterface(Ci.nsIPaymentActionResponse)
    110  );
    111 }
    112 
    113 function checkAddressErrors(testName, errors) {
    114  if (!errors) {
    115    emitTestFail(
    116      `${testName}: Expect non-null shippingAddressErrors, but got null.`
    117    );
    118    return;
    119  }
    120  for (const [key, msg] of Object.entries(errors)) {
    121    const expected = `${key} error`;
    122    if (msg !== expected) {
    123      emitTestFail(
    124        `${testName}: Expected '${expected}' on shippingAddressErrors.${key}, but got '${msg}'.`
    125      );
    126      return;
    127    }
    128  }
    129 }
    130 
    131 function checkPayerErrors(testName, errors) {
    132  if (!errors) {
    133    emitTestFail(`${testName}: Expect non-null payerErrors, but got null.`);
    134    return;
    135  }
    136  for (const [key, msg] of Object.entries(errors)) {
    137    const expected = `${key} error`;
    138    if (msg !== expected) {
    139      emitTestFail(
    140        `${testName}: Expected '${expected}' on payerErrors.${key}, but got '${msg}'.`
    141      );
    142      return;
    143    }
    144  }
    145 }
    146 
    147 function checkPaymentMethodErrors(testName, errors) {
    148  if (!errors) {
    149    emitTestFail(
    150      `${testName} :Expect non-null paymentMethodErrors, but got null.`
    151    );
    152    return;
    153  }
    154  for (const [key, msg] of Object.entries(errors)) {
    155    const expected = `method ${key} error`;
    156    if (msg !== expected) {
    157      emitTestFail(
    158        `${testName}: Expected '${expected}' on paymentMethodErrors.${key}, but got '${msg}'.`
    159      );
    160      return;
    161    }
    162  }
    163 }
    164 
    165 const DummyUIService = {
    166  testName: "",
    167  rejectRetry: false,
    168  showPayment(requestId) {
    169    acceptPayment(requestId, "show");
    170  },
    171  abortPaymen(requestId) {
    172    respondRequestId = requestId;
    173  },
    174  completePayment(requestId) {
    175    let completeResponse = Cc[
    176      "@mozilla.org/dom/payments/payment-complete-action-response;1"
    177    ].createInstance(Ci.nsIPaymentCompleteActionResponse);
    178    completeResponse.init(
    179      requestId,
    180      Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED
    181    );
    182    paymentSrv.respondPayment(
    183      completeResponse.QueryInterface(Ci.nsIPaymentActionResponse)
    184    );
    185  },
    186  updatePayment(requestId) {
    187    const payment = paymentSrv.getPaymentRequestById(requestId);
    188    if (payment.paymentDetails.error !== "error") {
    189      emitTestFail(
    190        "Expect 'error' on details.error, but got '" +
    191          payment.paymentDetails.error +
    192          "'"
    193      );
    194    }
    195    checkAddressErrors(
    196      this.testName,
    197      payment.paymentDetails.shippingAddressErrors
    198    );
    199    checkPayerErrors(this.testName, payment.paymentDetails.payerErrors);
    200    checkPaymentMethodErrors(
    201      this.testName,
    202      payment.paymentDetails.paymentMethodErrors
    203    );
    204    if (this.rejectRetry) {
    205      rejectPayment(requestId);
    206    } else {
    207      acceptPayment(requestId, "retry");
    208    }
    209  },
    210  closePayment: requestId => {
    211    respondRequestId = requestId;
    212  },
    213  QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]),
    214 };
    215 
    216 paymentSrv.setTestingUIService(
    217  DummyUIService.QueryInterface(Ci.nsIPaymentUIService)
    218 );
    219 
    220 addMessageListener("start-test", function (testName) {
    221  DummyUIService.testName = testName;
    222  sendAsyncMessage("start-test-complete");
    223 });
    224 
    225 addMessageListener("finish-test", function () {
    226  DummyUIService.testName = "";
    227  sendAsyncMessage("finish-test-complete");
    228 });
    229 
    230 addMessageListener("reject-retry", function () {
    231  DummyUIService.rejectRetry = true;
    232  sendAsyncMessage("reject-retry-complete");
    233 });
    234 
    235 addMessageListener("teardown", function () {
    236  paymentSrv.setTestingUIService(null);
    237  sendAsyncMessage("teardown-complete");
    238 });