@rebilly/instruments 8.9.0 → 8.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -648,7 +648,7 @@ function isRef(r2) {
648
648
  return !!(r2 && r2.__v_isRef === true);
649
649
  }
650
650
  function isDOMElement(element) {
651
- return element instanceof Element || element instanceof HTMLDocument;
651
+ return element instanceof Element || element instanceof Document;
652
652
  }
653
653
  const summaryLoaderHTML = `
654
654
  <div class="rebilly-instruments-summary-loader-total rebilly-instruments-loader-display-flex rebilly-instruments-loader-align-center rebilly-instruments-loader-justify-space-between">
@@ -1626,6 +1626,11 @@ function validateOptions$1(options) {
1626
1626
  );
1627
1627
  }
1628
1628
  }
1629
+ function validateData(data) {
1630
+ if (data == null) {
1631
+ throw new Error("Could not access data instance");
1632
+ }
1633
+ }
1629
1634
  const state = (() => {
1630
1635
  let state2 = null;
1631
1636
  const defaultState = {
@@ -2707,6 +2712,7 @@ class BaseEvent {
2707
2712
  const events = {
2708
2713
  dataReady: new BaseEvent("data-ready"),
2709
2714
  instrumentReady: new BaseEvent("instrument-ready"),
2715
+ payoutCompleted: new BaseEvent("payout-completed"),
2710
2716
  purchaseCompleted: new BaseEvent("purchase-completed"),
2711
2717
  setupCompleted: new BaseEvent("setup-completed")
2712
2718
  };
@@ -5977,7 +5983,7 @@ function cloneArrayDeep(val, instanceClone) {
5977
5983
  return res;
5978
5984
  }
5979
5985
  var cloneDeep_1 = cloneDeep;
5980
- const version = "55.8.1";
5986
+ const version = "55.9.0";
5981
5987
  let urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
5982
5988
  let nanoid = (size2 = 21) => {
5983
5989
  let id2 = "";
@@ -10515,7 +10521,11 @@ function getPaymentMethods() {
10515
10521
  expressMethods: [],
10516
10522
  methods: []
10517
10523
  };
10518
- (_a = state.data.readyToPay) == null ? void 0 : _a.forEach((method) => {
10524
+ let methods = state.data.readyToPay;
10525
+ if ((_a = state.options) == null ? void 0 : _a.payout) {
10526
+ methods = state.data.readyToPayout;
10527
+ }
10528
+ methods == null ? void 0 : methods.forEach((method) => {
10519
10529
  if (method.metadata.isExpressMethod) {
10520
10530
  result.expressMethods.push(method);
10521
10531
  } else {
@@ -14061,6 +14071,66 @@ async function fetchReadyToPay() {
14061
14071
  return filterReadyToPay(readyToPay);
14062
14072
  });
14063
14073
  }
14074
+ function filterReadyToPayout(readyToPayout) {
14075
+ const paymentMethodsMetadata = [...paymentMethods];
14076
+ const EXPRESS_METHODS = ["Google Pay", "Apple Pay", "paypal"];
14077
+ const BLOCKED_METHODS = [
14078
+ // account number, routing number, and account type required
14079
+ "echeck",
14080
+ // payment instrument is required
14081
+ // card number is required
14082
+ // card cvv is required
14083
+ // expiration month is required
14084
+ // expiration year is required
14085
+ "Khelocard",
14086
+ // Klarna authorization token is required
14087
+ // Klarna session ID is required
14088
+ "Klarna"
14089
+ ];
14090
+ const isExpressMethod = ({ method, feature }) => EXPRESS_METHODS.includes(method) || EXPRESS_METHODS.includes(feature == null ? void 0 : feature.name);
14091
+ return readyToPayout.filter((readyData) => {
14092
+ const isOldPayPal = readyData.method === "paypal" && !readyData.feature;
14093
+ const isPlaid = readyData.method === "ach" && readyData.feature;
14094
+ const isBlocked = BLOCKED_METHODS.includes(readyData.method);
14095
+ return !isOldPayPal && !isPlaid && !isBlocked;
14096
+ }).map((fields, index2) => {
14097
+ const metadata2 = paymentMethodsMetadata.find(
14098
+ (methodMetadata) => methodMetadata.apiName === fields.method
14099
+ ) || {};
14100
+ metadata2.isExpressMethod = isExpressMethod(fields);
14101
+ return new ReadyToPayModel({
14102
+ index: index2,
14103
+ metadata: metadata2,
14104
+ ...fields
14105
+ });
14106
+ }).filter(({ metadata: metadata2 }) => metadata2.storefrontEnabled);
14107
+ }
14108
+ async function fetchReadyToPayout() {
14109
+ return Endpoint(async () => {
14110
+ var _a, _b, _c;
14111
+ validateOptions$1(state.options);
14112
+ validateData(state.data);
14113
+ validateStorefront(state.storefront);
14114
+ if (!((_a = state == null ? void 0 : state.data) == null ? void 0 : _a.riskMetadata)) {
14115
+ const { riskMetadata: data2 } = await j();
14116
+ state.data.riskMetadata = data2;
14117
+ }
14118
+ const data = {
14119
+ riskMetadata: state.data.riskMetadata,
14120
+ websiteId: state.options.websiteId || null
14121
+ };
14122
+ if ((_c = (_b = state == null ? void 0 : state.data) == null ? void 0 : _b.account) == null ? void 0 : _c.address) {
14123
+ data.billingAddress = state.data.account.address;
14124
+ }
14125
+ if (state.data.payout) {
14126
+ data.amount = state.data.payout.amount;
14127
+ data.currency = state.data.payout.currency;
14128
+ }
14129
+ const { fields: readyToPayFields } = await state.storefront.readyToPayout.readyToPayout({ data });
14130
+ const readyToPayout = Object.values(readyToPayFields);
14131
+ return filterReadyToPayout(readyToPayout);
14132
+ });
14133
+ }
14064
14134
  const _InvoiceModel = class _InvoiceModel extends BaseModel {
14065
14135
  get isPaid() {
14066
14136
  return this.status === _InvoiceModel.Status.paid;
@@ -14217,6 +14287,15 @@ async function fetchPaymentInstrument() {
14217
14287
  return instruments.map(({ fields }) => fields);
14218
14288
  });
14219
14289
  }
14290
+ async function createPaymentInstrument({
14291
+ data
14292
+ }) {
14293
+ return Endpoint(async () => {
14294
+ validateStorefront(state.storefront);
14295
+ const { fields: instrument } = await state.storefront.paymentInstruments.create({ data });
14296
+ return instrument;
14297
+ });
14298
+ }
14220
14299
  class DepositRequestModel extends BaseModel {
14221
14300
  constructor(fields) {
14222
14301
  super(fields);
@@ -14253,6 +14332,26 @@ async function fetchDepositRequest({
14253
14332
  return new DepositRequestModel(fields);
14254
14333
  });
14255
14334
  }
14335
+ class PayoutRequestModel extends BaseModel {
14336
+ constructor(fields = {}) {
14337
+ super(fields);
14338
+ }
14339
+ }
14340
+ async function fetchPayoutRequest(data) {
14341
+ return Endpoint(async () => {
14342
+ validateStorefront(state.storefront);
14343
+ validateOptions$1(state.options);
14344
+ const { fields } = await state.storefront.payoutRequests.get(data);
14345
+ return new PayoutRequestModel(fields);
14346
+ });
14347
+ }
14348
+ async function updatePayoutRequest(data) {
14349
+ return Endpoint(async () => {
14350
+ validateStorefront(state.storefront);
14351
+ const { fields } = await state.storefront.payoutRequests.update(data);
14352
+ return new PayoutRequestModel(fields);
14353
+ });
14354
+ }
14256
14355
  function optional(schema2) {
14257
14356
  return schema2;
14258
14357
  }
@@ -14443,18 +14542,22 @@ async function fetchData({
14443
14542
  fetchTransaction: fetchTransaction$1 = fetchTransaction,
14444
14543
  fetchAccountAndWebsite: fetchAccountAndWebsite$1 = fetchAccountAndWebsite,
14445
14544
  fetchInstruments = fetchPaymentInstrument,
14446
- fetchDepositRequest: fetchDepositRequest$1 = fetchDepositRequest
14545
+ fetchDepositRequest: fetchDepositRequest$1 = fetchDepositRequest,
14546
+ fetchPayoutRequest: fetchPayoutRequest$1 = fetchPayoutRequest
14447
14547
  } = {}) {
14448
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
14548
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
14449
14549
  try {
14450
14550
  state.data = new DataInstance();
14551
+ if (state.storefront && ((_a = state.options) == null ? void 0 : _a.jwt)) {
14552
+ state.storefront.setSessionToken(state.options.jwt);
14553
+ }
14451
14554
  if (!riskMetadata) {
14452
14555
  const { riskMetadata: data } = await j();
14453
14556
  state.data.riskMetadata = data;
14454
14557
  } else {
14455
14558
  state.data.riskMetadata = riskMetadata;
14456
14559
  }
14457
- if ((_a = state.options) == null ? void 0 : _a.deposit) {
14560
+ if ((_b = state.options) == null ? void 0 : _b.deposit) {
14458
14561
  if (isDepositRequest(state.options.deposit)) {
14459
14562
  state.data.deposit = await fetchDepositRequest$1({
14460
14563
  data: {
@@ -14468,9 +14571,15 @@ async function fetchData({
14468
14571
  );
14469
14572
  }
14470
14573
  }
14574
+ if ((_c = state.options) == null ? void 0 : _c.payout) {
14575
+ state.data.payout = await fetchPayoutRequest$1({
14576
+ id: state.options.payout.payoutRequestId
14577
+ });
14578
+ }
14471
14579
  let readyToPayPromise = Promise.resolve(null);
14580
+ let readyToPayoutPromise = Promise.resolve(null);
14472
14581
  let availableInstrumentsPromise = null;
14473
- if ((_b = state.options) == null ? void 0 : _b.jwt) {
14582
+ if ((_d = state.options) == null ? void 0 : _d.jwt) {
14474
14583
  fetchAccountAndWebsite$1().then(({ account, website }) => {
14475
14584
  if (state.data == null) {
14476
14585
  throw new Error("Invalid data instance");
@@ -14478,12 +14587,20 @@ async function fetchData({
14478
14587
  state.data.account = account;
14479
14588
  state.data.website = website;
14480
14589
  });
14481
- readyToPayPromise = fetchReadyToPay();
14590
+ if ((_e = state.options) == null ? void 0 : _e.payout) {
14591
+ readyToPayoutPromise = fetchReadyToPayout();
14592
+ } else {
14593
+ readyToPayPromise = fetchReadyToPay();
14594
+ }
14482
14595
  availableInstrumentsPromise = fetchInstruments();
14483
14596
  } else {
14484
- readyToPayPromise = fetchReadyToPay();
14597
+ if ((_f = state.options) == null ? void 0 : _f.payout) {
14598
+ readyToPayoutPromise = fetchReadyToPayout();
14599
+ } else {
14600
+ readyToPayPromise = fetchReadyToPay();
14601
+ }
14485
14602
  }
14486
- if ((_c = state.options) == null ? void 0 : _c.transactionId) {
14603
+ if ((_g = state.options) == null ? void 0 : _g.transactionId) {
14487
14604
  state.data.transaction = await fetchTransaction$1({
14488
14605
  data: {
14489
14606
  id: state.options.transactionId
@@ -14491,7 +14608,7 @@ async function fetchData({
14491
14608
  });
14492
14609
  }
14493
14610
  let productsPromise;
14494
- const invoiceId = ((_d = state.options) == null ? void 0 : _d.invoiceId) || ((_f = (_e = state.data) == null ? void 0 : _e.transaction) == null ? void 0 : _f.invoiceId);
14611
+ const invoiceId = ((_h = state.options) == null ? void 0 : _h.invoiceId) || ((_j = (_i = state.data) == null ? void 0 : _i.transaction) == null ? void 0 : _j.invoiceId);
14495
14612
  if (invoiceId) {
14496
14613
  const { invoice, products: products2 } = await fetchInvoiceAndProducts$1({
14497
14614
  data: {
@@ -14501,23 +14618,25 @@ async function fetchData({
14501
14618
  productsPromise = Promise.resolve(products2);
14502
14619
  state.data.invoice = invoice;
14503
14620
  }
14504
- const previewPurchasePromise = ((_g = state.options) == null ? void 0 : _g.items) ? fetchSummary() : null;
14505
- if (!((_h = state.options) == null ? void 0 : _h.jwt)) {
14621
+ const previewPurchasePromise = ((_k = state.options) == null ? void 0 : _k.items) ? fetchSummary() : null;
14622
+ if (!((_l = state.options) == null ? void 0 : _l.jwt)) {
14506
14623
  productsPromise = fetchProductsFromPlans();
14507
14624
  }
14508
14625
  let plansPromise = new Promise((resolve2) => {
14509
14626
  resolve2([]);
14510
14627
  });
14511
- if (((_i = state.options) == null ? void 0 : _i.addons) || ((_j = state.options) == null ? void 0 : _j.bumpOffer)) {
14628
+ if (((_m = state.options) == null ? void 0 : _m.addons) || ((_n = state.options) == null ? void 0 : _n.bumpOffer)) {
14512
14629
  plansPromise = fetchPlansFromAddonsBumpOffer();
14513
14630
  }
14514
14631
  const [
14632
+ readyToPayout,
14515
14633
  readyToPay,
14516
14634
  previewPurchase,
14517
14635
  products,
14518
14636
  plans,
14519
14637
  availableInstruments
14520
14638
  ] = await Promise.all([
14639
+ readyToPayoutPromise,
14521
14640
  readyToPayPromise,
14522
14641
  previewPurchasePromise,
14523
14642
  productsPromise,
@@ -14525,6 +14644,7 @@ async function fetchData({
14525
14644
  availableInstrumentsPromise
14526
14645
  ]);
14527
14646
  return new DataInstance({
14647
+ readyToPayout,
14528
14648
  readyToPay,
14529
14649
  previewPurchase,
14530
14650
  products,
@@ -14586,7 +14706,7 @@ function generateFramepayConfig({ methodIds } = {}) {
14586
14706
  locale: (options == null ? void 0 : options.locale) || "auto",
14587
14707
  organizationId: options.organizationId,
14588
14708
  websiteId: options.websiteId,
14589
- methods: data.readyToPay
14709
+ methods: options.payout ? data.readyToPayout : data.readyToPay
14590
14710
  };
14591
14711
  if (methodIds.includes("pay-pal-billing-agreement")) {
14592
14712
  config.transactionData = merge$1(
@@ -15139,10 +15259,10 @@ function processPropertyAsDOMElement({
15139
15259
  if (typeof prop === "undefined") {
15140
15260
  throw new Error('processPropertyDOMElement: Missing argument "prop"');
15141
15261
  }
15142
- if (hasValidCSSSelector(prop)) {
15143
- DOMElement = document.querySelector(prop);
15144
- } else if (isDOMElement(prop)) {
15262
+ if (isDOMElement(prop)) {
15145
15263
  DOMElement = prop;
15264
+ } else if (typeof prop === "string" && hasValidCSSSelector(prop)) {
15265
+ DOMElement = document.querySelector(prop);
15146
15266
  } else {
15147
15267
  throw new Error(
15148
15268
  `Please provide a valid CSS class, id or DOM element for "${propName}" property`
@@ -22262,6 +22382,19 @@ jsonSchema201909.default = addMetaSchema2019;
22262
22382
  })(_2019, _2019.exports);
22263
22383
  var _2019Exports = _2019.exports;
22264
22384
  const AJV = /* @__PURE__ */ getDefaultExportFromCjs(_2019Exports);
22385
+ const planItemsSchema = {
22386
+ type: "array",
22387
+ items: {
22388
+ type: "object",
22389
+ properties: {
22390
+ planId: { type: "string" },
22391
+ quantity: { type: "number" },
22392
+ thumbnail: optional({ type: "string" })
22393
+ },
22394
+ required: ["planId", "quantity"]
22395
+ },
22396
+ default: []
22397
+ };
22265
22398
  const itemsSchema = {
22266
22399
  type: "array",
22267
22400
  items: {
@@ -22294,19 +22427,6 @@ const itemsSchema = {
22294
22427
  },
22295
22428
  minItems: 1
22296
22429
  };
22297
- const planItemsSchema = {
22298
- type: "array",
22299
- items: {
22300
- type: "object",
22301
- properties: {
22302
- planId: { type: "string" },
22303
- quantity: { type: "number" },
22304
- thumbnail: optional({ type: "string" })
22305
- },
22306
- required: ["planId", "quantity"]
22307
- },
22308
- default: []
22309
- };
22310
22430
  const moneySchema = {
22311
22431
  type: "object",
22312
22432
  properties: {
@@ -22321,6 +22441,15 @@ const moneySchema = {
22321
22441
  },
22322
22442
  required: ["amount", "currency"]
22323
22443
  };
22444
+ const payoutSchema = {
22445
+ type: "object",
22446
+ properties: {
22447
+ payoutRequestId: {
22448
+ type: "string"
22449
+ }
22450
+ },
22451
+ required: ["payoutRequestId"]
22452
+ };
22324
22453
  const i18nSchema = {
22325
22454
  type: "object",
22326
22455
  patternProperties: {
@@ -22936,6 +23065,15 @@ const devSchema = {
22936
23065
  framePayStyleLink: optional({ type: "string" })
22937
23066
  }
22938
23067
  };
23068
+ const purchaseDataKeys = [
23069
+ "items",
23070
+ "transactionId",
23071
+ "invoiceId",
23072
+ "money",
23073
+ "deposit",
23074
+ "payout"
23075
+ ];
23076
+ const requireJWT = ["invoiceId", "transactionId", "deposit", "payout"];
22939
23077
  const schema = {
22940
23078
  title: "Mount Options Schema",
22941
23079
  type: "object",
@@ -22944,21 +23082,48 @@ const schema = {
22944
23082
  publishableKey: optional({ type: "string" }),
22945
23083
  websiteId: optional({ type: "string" }),
22946
23084
  items: itemsSchema,
22947
- addons: optional(planItemsSchema),
22948
- bumpOffer: optional(planItemsSchema),
22949
23085
  money: moneySchema,
22950
23086
  invoiceId: { type: "string" },
22951
23087
  transactionId: { type: "string" },
22952
23088
  deposit: depositSchema,
23089
+ payout: payoutSchema,
23090
+ jwt: optional({ type: "string" }),
23091
+ addons: optional(planItemsSchema),
23092
+ bumpOffer: optional(planItemsSchema),
22953
23093
  form: optional({
22954
- type: "string",
22955
- default: ".rebilly-instruments"
23094
+ default: ".rebilly-instruments",
23095
+ type: ["string", "object"],
23096
+ oneOf: [
23097
+ {
23098
+ type: "string"
23099
+ },
23100
+ {
23101
+ type: "object",
23102
+ properties: {
23103
+ id: {
23104
+ type: "string"
23105
+ }
23106
+ }
23107
+ }
23108
+ ]
22956
23109
  }),
22957
23110
  summary: optional({
22958
- type: "string",
22959
- default: ".rebilly-instruments-summary"
23111
+ default: ".rebilly-instruments-summary",
23112
+ type: ["string", "object"],
23113
+ oneOf: [
23114
+ {
23115
+ type: "string"
23116
+ },
23117
+ {
23118
+ type: "object",
23119
+ properties: {
23120
+ id: {
23121
+ type: "string"
23122
+ }
23123
+ }
23124
+ }
23125
+ ]
22960
23126
  }),
22961
- jwt: optional({ type: "string" }),
22962
23127
  apiMode: {
22963
23128
  type: "string",
22964
23129
  enum: ["sandbox", "live"]
@@ -22985,71 +23150,36 @@ const schema = {
22985
23150
  },
22986
23151
  required: ["apiMode"],
22987
23152
  dependentRequired: {
22988
- invoiceId: ["jwt"],
22989
- transactionId: ["jwt"],
22990
- deposit: ["jwt"]
23153
+ ...requireJWT.reduce((accumulator, currentValue) => {
23154
+ return {
23155
+ ...accumulator,
23156
+ [`${currentValue}`]: ["jwt"]
23157
+ };
23158
+ }, {})
22991
23159
  },
22992
23160
  anyOf: [
22993
- {
22994
- if: { required: ["items"] },
22995
- then: {
22996
- properties: {
22997
- transactionId: false,
22998
- invoiceId: false,
22999
- deposit: false,
23000
- money: false
23001
- }
23002
- },
23003
- else: false
23004
- },
23005
- {
23006
- if: { required: ["money"] },
23007
- then: {
23008
- properties: {
23009
- transactionId: false,
23010
- invoiceId: false,
23011
- deposit: false,
23012
- items: false
23013
- }
23014
- },
23015
- else: false
23016
- },
23017
- {
23018
- if: { required: ["transactionId"] },
23019
- then: {
23020
- properties: {
23021
- invoiceId: false,
23022
- deposit: false,
23023
- items: false,
23024
- money: false
23025
- }
23026
- },
23027
- else: false
23028
- },
23029
- {
23030
- if: { required: ["invoiceId"] },
23031
- then: {
23032
- properties: {
23033
- transactionId: false,
23034
- deposit: false,
23035
- items: false,
23036
- money: false
23037
- }
23038
- },
23039
- else: false
23040
- },
23041
- {
23042
- if: { required: ["deposit"] },
23043
- then: {
23044
- properties: {
23045
- invoiceId: false,
23046
- transactionId: false,
23047
- items: false,
23048
- money: false
23049
- }
23050
- },
23051
- else: false
23052
- },
23161
+ ...purchaseDataKeys.map((key) => {
23162
+ const otherPurchaseDataKeys = purchaseDataKeys.filter(
23163
+ (value) => value !== key
23164
+ );
23165
+ return {
23166
+ if: { required: [key] },
23167
+ then: {
23168
+ properties: {
23169
+ ...otherPurchaseDataKeys.reduce(
23170
+ (otherProperties, currentKey) => {
23171
+ return {
23172
+ ...otherProperties,
23173
+ [currentKey]: false
23174
+ };
23175
+ },
23176
+ {}
23177
+ )
23178
+ }
23179
+ },
23180
+ else: false
23181
+ };
23182
+ }),
23053
23183
  {
23054
23184
  required: ["jwt"]
23055
23185
  }
@@ -23072,7 +23202,11 @@ const ajv = new AJV({
23072
23202
  });
23073
23203
  const validate = ajv.compile(schema);
23074
23204
  function sanitize2(data) {
23075
- return JSON.parse(JSON.stringify(data));
23205
+ return Object.assign(JSON.parse(JSON.stringify(data)), {
23206
+ // HTMLElement is turned into objects by JSON.stringify
23207
+ form: data.form,
23208
+ summary: data.summary
23209
+ });
23076
23210
  }
23077
23211
  function validateOptions(data) {
23078
23212
  const sanitizedData = sanitize2(data);
@@ -23155,7 +23289,7 @@ function handleComputedProperty(options) {
23155
23289
  var _a;
23156
23290
  return Object.assign({}, options, {
23157
23291
  _computed: {
23158
- version: "8.8.1",
23292
+ version: "8.9.0",
23159
23293
  paymentMethodsUrl: ((_a = options._dev) == null ? void 0 : _a.paymentMethodsUrl) ?? "https://forms.secure-payments.app"
23160
23294
  }
23161
23295
  });
@@ -25729,62 +25863,6 @@ function on({ eventName, callback }) {
25729
25863
  const internalEventName = camelCase$1(eventName);
25730
25864
  events[internalEventName].addEventListener(callback);
25731
25865
  }
25732
- async function postPurchase({ data }) {
25733
- return Endpoint(async () => {
25734
- validateStorefront(state.storefront);
25735
- const purchaseSchemaKeys = [
25736
- "websiteId",
25737
- "paymentInstruction",
25738
- "items",
25739
- "billingAddress",
25740
- "deliveryAddress",
25741
- "shippingRateId",
25742
- "couponIds",
25743
- "password",
25744
- "redirectUrl"
25745
- ];
25746
- Object.keys(data).forEach((key) => {
25747
- if (!purchaseSchemaKeys.includes(key)) {
25748
- delete data[key];
25749
- }
25750
- });
25751
- return state.storefront.purchase.purchase({ data });
25752
- });
25753
- }
25754
- async function postPayment({ data }) {
25755
- return Endpoint(async () => {
25756
- validateStorefront(state.storefront);
25757
- const {
25758
- token,
25759
- transactionId,
25760
- invoiceId,
25761
- websiteId,
25762
- paymentInstrumentId
25763
- } = data;
25764
- const purchaseSchemaKeys = ["riskMetadata", "redirectUrl"];
25765
- if (token) {
25766
- purchaseSchemaKeys.push("token");
25767
- }
25768
- if (paymentInstrumentId) {
25769
- purchaseSchemaKeys.push("paymentInstrumentId");
25770
- }
25771
- if (transactionId) {
25772
- purchaseSchemaKeys.push("transactionId");
25773
- }
25774
- if (invoiceId) {
25775
- purchaseSchemaKeys.push("invoiceId");
25776
- }
25777
- if (websiteId) {
25778
- purchaseSchemaKeys.push("amount", "currency", "websiteId");
25779
- }
25780
- Object.keys(data).forEach((key) => {
25781
- if (!purchaseSchemaKeys.includes(key)) {
25782
- delete data[key];
25783
- }
25784
- });
25785
- return state.storefront.purchase.payment({ data });
25786
- });
25787
- }
25788
25866
  const modalTemplate = (isRedirect, method) => `
25789
25867
  <div class="rebilly-instruments-modal-overlay">
25790
25868
  <div class="rebilly-instruments-modal-container ${method ? `rebilly-instruments-${method}` : ""} ${isRedirect ? "is-redirect" : ""}">
@@ -25868,6 +25946,117 @@ async function mountModal({
25868
25946
  });
25869
25947
  return iframe;
25870
25948
  }
25949
+ function handleApprovalUrl({ fields, payload }) {
25950
+ var _a, _b, _c, _d, _e;
25951
+ if (state.options.features.fullPageRedirect && (payload.redirectUrl || ((_a = fields.transaction) == null ? void 0 : _a.approvalUrl))) {
25952
+ window.location = payload.redirectUrl || ((_b = fields.transaction) == null ? void 0 : _b.approvalUrl);
25953
+ return;
25954
+ }
25955
+ if (payload.redirectUrl || !((_c = fields.transaction) == null ? void 0 : _c.approvalUrl)) {
25956
+ const { paymentMethodsUrl } = state.options._computed;
25957
+ const modelSafeFields = JSON.parse(JSON.stringify(fields));
25958
+ const model = {};
25959
+ if (state.data.isPayment) {
25960
+ model.payment = modelSafeFields;
25961
+ } else {
25962
+ model.purchase = modelSafeFields;
25963
+ }
25964
+ state.data = new DataInstance({ ...fields });
25965
+ const name = "rebilly-instruments-approval-url";
25966
+ mountModal({
25967
+ name,
25968
+ url: `${paymentMethodsUrl}?name=${name}`,
25969
+ model,
25970
+ close: (updatedPurchase) => {
25971
+ events.purchaseCompleted.dispatch(updatedPurchase);
25972
+ }
25973
+ });
25974
+ } else if ((_d = fields.transaction) == null ? void 0 : _d.approvalUrl) {
25975
+ window.location = (_e = fields.transaction) == null ? void 0 : _e.approvalUrl;
25976
+ }
25977
+ }
25978
+ async function postPurchase({ data }) {
25979
+ return Endpoint(async () => {
25980
+ validateStorefront(state.storefront);
25981
+ const purchaseSchemaKeys = [
25982
+ "websiteId",
25983
+ "paymentInstruction",
25984
+ "items",
25985
+ "billingAddress",
25986
+ "deliveryAddress",
25987
+ "shippingRateId",
25988
+ "couponIds",
25989
+ "password",
25990
+ "redirectUrl"
25991
+ ];
25992
+ Object.keys(data).forEach((key) => {
25993
+ if (!purchaseSchemaKeys.includes(key)) {
25994
+ delete data[key];
25995
+ }
25996
+ });
25997
+ return state.storefront.purchase.purchase({ data });
25998
+ });
25999
+ }
26000
+ async function postPayment({ data }) {
26001
+ return Endpoint(async () => {
26002
+ validateStorefront(state.storefront);
26003
+ const {
26004
+ token,
26005
+ transactionId,
26006
+ invoiceId,
26007
+ websiteId,
26008
+ paymentInstrumentId
26009
+ } = data;
26010
+ const purchaseSchemaKeys = ["riskMetadata", "redirectUrl"];
26011
+ if (token) {
26012
+ purchaseSchemaKeys.push("token");
26013
+ }
26014
+ if (paymentInstrumentId) {
26015
+ purchaseSchemaKeys.push("paymentInstrumentId");
26016
+ }
26017
+ if (transactionId) {
26018
+ purchaseSchemaKeys.push("transactionId");
26019
+ }
26020
+ if (invoiceId) {
26021
+ purchaseSchemaKeys.push("invoiceId");
26022
+ }
26023
+ if (websiteId) {
26024
+ purchaseSchemaKeys.push("amount", "currency", "websiteId");
26025
+ }
26026
+ Object.keys(data).forEach((key) => {
26027
+ if (!purchaseSchemaKeys.includes(key)) {
26028
+ delete data[key];
26029
+ }
26030
+ });
26031
+ return state.storefront.purchase.payment({ data });
26032
+ });
26033
+ }
26034
+ async function makePurchase({ payload }) {
26035
+ const data = {
26036
+ websiteId: state.options.websiteId,
26037
+ paymentInstruction: {
26038
+ token: payload._raw.id
26039
+ },
26040
+ ...payload
26041
+ };
26042
+ if (state.data.acceptBumpOffer) {
26043
+ data.items = state.options.bumpOffer;
26044
+ } else {
26045
+ data.items = mapItemsQuantities(state.options.items);
26046
+ }
26047
+ if (state.data.couponIds && Array.isArray(state.data.couponIds)) {
26048
+ data.couponIds = state.data.couponIds;
26049
+ }
26050
+ if (state.options.addons && state.data.addons && Array.isArray(state.data.addons)) {
26051
+ state.options.addons.forEach((addon) => {
26052
+ if (state.data.addons.includes(addon.planId)) {
26053
+ data.items.push(addon);
26054
+ }
26055
+ });
26056
+ }
26057
+ const { fields } = await postPurchase({ data });
26058
+ return fields;
26059
+ }
25871
26060
  async function makePayment({ payload }) {
25872
26061
  var _a, _b, _c, _d, _e, _f;
25873
26062
  const {
@@ -25906,10 +26095,7 @@ async function makePayment({ payload }) {
25906
26095
  if ((_f = state.data) == null ? void 0 : _f.riskMetadata) {
25907
26096
  data.riskMetadata = state.data.riskMetadata;
25908
26097
  }
25909
- let { fields } = await postPayment({
25910
- state,
25911
- data
25912
- });
26098
+ let { fields } = await postPayment({ data });
25913
26099
  fields = {
25914
26100
  transaction: fields,
25915
26101
  token: fields.token || state.options.jwt
@@ -25919,66 +26105,28 @@ async function makePayment({ payload }) {
25919
26105
  }
25920
26106
  return fields;
25921
26107
  }
25922
- async function makePurchase({ payload }) {
25923
- const data = {
25924
- websiteId: state.options.websiteId,
25925
- paymentInstruction: {
25926
- token: payload._raw.id
25927
- },
25928
- ...payload
25929
- };
25930
- if (state.data.acceptBumpOffer) {
25931
- data.items = state.options.bumpOffer;
25932
- } else {
25933
- data.items = mapItemsQuantities(state.options.items);
25934
- }
25935
- if (state.data.couponIds && Array.isArray(state.data.couponIds)) {
25936
- data.couponIds = state.data.couponIds;
26108
+ async function makePayout({
26109
+ payload: {
26110
+ isExistingInstrument,
26111
+ _raw: { id: paymentInstrumentId }
25937
26112
  }
25938
- if (state.options.addons && state.data.addons && Array.isArray(state.data.addons)) {
25939
- state.options.addons.forEach((addon) => {
25940
- if (state.data.addons.includes(addon.planId)) {
25941
- data.items.push(addon);
25942
- }
26113
+ }) {
26114
+ if (!isExistingInstrument) {
26115
+ const { id: newPaymentInstrumentId } = await createPaymentInstrument({
26116
+ data: { token: paymentInstrumentId }
25943
26117
  });
26118
+ paymentInstrumentId = newPaymentInstrumentId;
25944
26119
  }
25945
- const { fields } = await postPurchase({
25946
- state,
25947
- data
25948
- });
25949
- return fields;
25950
- }
25951
- function handleApprovalUrl({ fields, payload }) {
25952
- var _a, _b, _c, _d, _e;
25953
- if (state.options.features.fullPageRedirect && (payload.redirectUrl || ((_a = fields.transaction) == null ? void 0 : _a.approvalUrl))) {
25954
- window.location = payload.redirectUrl || ((_b = fields.transaction) == null ? void 0 : _b.approvalUrl);
25955
- return;
25956
- }
25957
- if (payload.redirectUrl || !((_c = fields.transaction) == null ? void 0 : _c.approvalUrl)) {
25958
- const { paymentMethodsUrl } = state.options._computed;
25959
- const modelSafeFields = JSON.parse(JSON.stringify(fields));
25960
- const model = {};
25961
- if (state.data.isPayment) {
25962
- model.payment = modelSafeFields;
25963
- } else {
25964
- model.purchase = modelSafeFields;
26120
+ const payoutRequest = await updatePayoutRequest({
26121
+ id: state.data.payout.id,
26122
+ data: {
26123
+ paymentInstrumentId
25965
26124
  }
25966
- state.data = new DataInstance({ ...fields });
25967
- const name = "rebilly-instruments-approval-url";
25968
- mountModal({
25969
- name,
25970
- url: `${paymentMethodsUrl}?name=${name}`,
25971
- model,
25972
- close: (updatedPurchase) => {
25973
- events.purchaseCompleted.dispatch(updatedPurchase);
25974
- }
25975
- });
25976
- } else if ((_d = fields.transaction) == null ? void 0 : _d.approvalUrl) {
25977
- window.location = (_e = fields.transaction) == null ? void 0 : _e.approvalUrl;
25978
- }
26125
+ });
26126
+ return payoutRequest;
25979
26127
  }
25980
26128
  async function purchase({ payload }) {
25981
- var _a, _b, _c;
26129
+ var _a, _b;
25982
26130
  Object.keys(payload).forEach((key) => {
25983
26131
  if (payload[key] === null) {
25984
26132
  delete payload[key];
@@ -25986,19 +26134,27 @@ async function purchase({ payload }) {
25986
26134
  });
25987
26135
  try {
25988
26136
  let fields;
25989
- if (state.data.isPayment) {
26137
+ let completedEvent;
26138
+ if (state.options.payout) {
26139
+ fields = await makePayout({ payload });
26140
+ completedEvent = events.payoutCompleted;
26141
+ } else if (state.data.isPayment) {
25990
26142
  fields = await makePayment({ payload });
26143
+ completedEvent = events.purchaseCompleted;
25991
26144
  } else {
25992
26145
  fields = await makePurchase({ payload });
26146
+ completedEvent = events.purchaseCompleted;
25993
26147
  }
25994
26148
  if (((_a = fields.transaction) == null ? void 0 : _a.approvalUrl) && ((_b = fields.transaction) == null ? void 0 : _b.result) === "unknown") {
25995
26149
  if (state.options.features.fullPageRedirect) {
25996
- window.location = (_c = fields.transaction) == null ? void 0 : _c.approvalUrl;
26150
+ window.location = fields.transaction.approvalUrl;
25997
26151
  } else {
25998
26152
  handleApprovalUrl({ fields, payload });
25999
26153
  }
26154
+ } else if (fields.selectedPaymentInstrumentRedirectUrl) {
26155
+ window.location = fields.selectedPaymentInstrumentRedirectUrl;
26000
26156
  } else {
26001
- events.purchaseCompleted.dispatch(fields);
26157
+ completedEvent.dispatch(fields);
26002
26158
  }
26003
26159
  } catch (error2) {
26004
26160
  showError(error2);
@@ -26100,10 +26256,15 @@ async function mountResult({ payload }) {
26100
26256
  }
26101
26257
  const modelSafeState = state.toModel();
26102
26258
  const model = {
26259
+ data: modelSafeState.data,
26103
26260
  options: modelSafeState.options,
26104
- mainStyleVars: modelSafeState.mainStyleVars,
26105
- [state.options.transactionType]: payload
26261
+ mainStyleVars: modelSafeState.mainStyleVars
26106
26262
  };
26263
+ if (state.data.payout) {
26264
+ model.payoutRequest = payload;
26265
+ } else {
26266
+ model[state.options.transactionType] = payload;
26267
+ }
26107
26268
  (_a = iframe == null ? void 0 : iframe.component) == null ? void 0 : _a.call("route", {
26108
26269
  name: "result"
26109
26270
  });
@@ -26162,6 +26323,13 @@ const setupUserFlow = ({ state: state2 = {} }) => {
26162
26323
  });
26163
26324
  }
26164
26325
  if (!((_d = (_c = state2 == null ? void 0 : state2.options) == null ? void 0 : _c.features) == null ? void 0 : _d.hideResult)) {
26326
+ on({
26327
+ eventName: "payout-completed",
26328
+ callback: (payload) => {
26329
+ payload = JSON.parse(JSON.stringify(payload));
26330
+ showResult({ state: state2, payload });
26331
+ }
26332
+ });
26165
26333
  on({
26166
26334
  eventName: "purchase-completed",
26167
26335
  callback: (payload) => {
@@ -26182,7 +26350,7 @@ async function mount({
26182
26350
  setupFramepay: setupFramepay$1 = setupFramepay,
26183
26351
  ...options
26184
26352
  } = {}) {
26185
- var _a, _b, _c, _d, _e, _f, _g, _h;
26353
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
26186
26354
  try {
26187
26355
  state.data = {};
26188
26356
  state.options = {};
@@ -26236,7 +26404,13 @@ async function mount({
26236
26404
  false
26237
26405
  );
26238
26406
  }
26239
- if (!((_h = data.readyToPay) == null ? void 0 : _h.length)) {
26407
+ let hasNoPaymentMethods = false;
26408
+ if (data.payout) {
26409
+ hasNoPaymentMethods = !((_h = data.readyToPayout) == null ? void 0 : _h.length);
26410
+ } else {
26411
+ hasNoPaymentMethods = !((_i = data.readyToPay) == null ? void 0 : _i.length);
26412
+ }
26413
+ if (hasNoPaymentMethods) {
26240
26414
  state.loader.stopLoading({ id: "rebilly-instruments-form" });
26241
26415
  showError(
26242
26416
  state.translate.getTranslation("form.error.noPaymentMethods")
@@ -26304,7 +26478,7 @@ class RebillyInstrumentsInstance {
26304
26478
  await show({ componentName, payload });
26305
26479
  }
26306
26480
  get version() {
26307
- return `RebillyInstruments Ver.${"8.8.1"}`;
26481
+ return `RebillyInstruments Ver.${"8.9.0"}`;
26308
26482
  }
26309
26483
  on(eventName, callback) {
26310
26484
  on({ eventName, callback });