@rebilly/instruments 8.9.0 → 8.10.1

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
@@ -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(
@@ -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,12 +23082,14 @@ 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
23094
  type: "string",
22955
23095
  default: ".rebilly-instruments"
@@ -22958,7 +23098,6 @@ const schema = {
22958
23098
  type: "string",
22959
23099
  default: ".rebilly-instruments-summary"
22960
23100
  }),
22961
- jwt: optional({ type: "string" }),
22962
23101
  apiMode: {
22963
23102
  type: "string",
22964
23103
  enum: ["sandbox", "live"]
@@ -22985,71 +23124,36 @@ const schema = {
22985
23124
  },
22986
23125
  required: ["apiMode"],
22987
23126
  dependentRequired: {
22988
- invoiceId: ["jwt"],
22989
- transactionId: ["jwt"],
22990
- deposit: ["jwt"]
23127
+ ...requireJWT.reduce((accumulator, currentValue) => {
23128
+ return {
23129
+ ...accumulator,
23130
+ [`${currentValue}`]: ["jwt"]
23131
+ };
23132
+ }, {})
22991
23133
  },
22992
23134
  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
- },
23135
+ ...purchaseDataKeys.map((key) => {
23136
+ const otherPurchaseDataKeys = purchaseDataKeys.filter(
23137
+ (value) => value !== key
23138
+ );
23139
+ return {
23140
+ if: { required: [key] },
23141
+ then: {
23142
+ properties: {
23143
+ ...otherPurchaseDataKeys.reduce(
23144
+ (otherProperties, currentKey) => {
23145
+ return {
23146
+ ...otherProperties,
23147
+ [currentKey]: false
23148
+ };
23149
+ },
23150
+ {}
23151
+ )
23152
+ }
23153
+ },
23154
+ else: false
23155
+ };
23156
+ }),
23053
23157
  {
23054
23158
  required: ["jwt"]
23055
23159
  }
@@ -23155,7 +23259,7 @@ function handleComputedProperty(options) {
23155
23259
  var _a;
23156
23260
  return Object.assign({}, options, {
23157
23261
  _computed: {
23158
- version: "8.8.1",
23262
+ version: "8.10.0",
23159
23263
  paymentMethodsUrl: ((_a = options._dev) == null ? void 0 : _a.paymentMethodsUrl) ?? "https://forms.secure-payments.app"
23160
23264
  }
23161
23265
  });
@@ -25729,62 +25833,6 @@ function on({ eventName, callback }) {
25729
25833
  const internalEventName = camelCase$1(eventName);
25730
25834
  events[internalEventName].addEventListener(callback);
25731
25835
  }
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
25836
  const modalTemplate = (isRedirect, method) => `
25789
25837
  <div class="rebilly-instruments-modal-overlay">
25790
25838
  <div class="rebilly-instruments-modal-container ${method ? `rebilly-instruments-${method}` : ""} ${isRedirect ? "is-redirect" : ""}">
@@ -25868,6 +25916,117 @@ async function mountModal({
25868
25916
  });
25869
25917
  return iframe;
25870
25918
  }
25919
+ function handleApprovalUrl({ fields, payload }) {
25920
+ var _a, _b, _c, _d, _e;
25921
+ if (state.options.features.fullPageRedirect && (payload.redirectUrl || ((_a = fields.transaction) == null ? void 0 : _a.approvalUrl))) {
25922
+ window.location = payload.redirectUrl || ((_b = fields.transaction) == null ? void 0 : _b.approvalUrl);
25923
+ return;
25924
+ }
25925
+ if (payload.redirectUrl || !((_c = fields.transaction) == null ? void 0 : _c.approvalUrl)) {
25926
+ const { paymentMethodsUrl } = state.options._computed;
25927
+ const modelSafeFields = JSON.parse(JSON.stringify(fields));
25928
+ const model = {};
25929
+ if (state.data.isPayment) {
25930
+ model.payment = modelSafeFields;
25931
+ } else {
25932
+ model.purchase = modelSafeFields;
25933
+ }
25934
+ state.data = new DataInstance({ ...fields });
25935
+ const name = "rebilly-instruments-approval-url";
25936
+ mountModal({
25937
+ name,
25938
+ url: `${paymentMethodsUrl}?name=${name}`,
25939
+ model,
25940
+ close: (updatedPurchase) => {
25941
+ events.purchaseCompleted.dispatch(updatedPurchase);
25942
+ }
25943
+ });
25944
+ } else if ((_d = fields.transaction) == null ? void 0 : _d.approvalUrl) {
25945
+ window.location = (_e = fields.transaction) == null ? void 0 : _e.approvalUrl;
25946
+ }
25947
+ }
25948
+ async function postPurchase({ data }) {
25949
+ return Endpoint(async () => {
25950
+ validateStorefront(state.storefront);
25951
+ const purchaseSchemaKeys = [
25952
+ "websiteId",
25953
+ "paymentInstruction",
25954
+ "items",
25955
+ "billingAddress",
25956
+ "deliveryAddress",
25957
+ "shippingRateId",
25958
+ "couponIds",
25959
+ "password",
25960
+ "redirectUrl"
25961
+ ];
25962
+ Object.keys(data).forEach((key) => {
25963
+ if (!purchaseSchemaKeys.includes(key)) {
25964
+ delete data[key];
25965
+ }
25966
+ });
25967
+ return state.storefront.purchase.purchase({ data });
25968
+ });
25969
+ }
25970
+ async function postPayment({ data }) {
25971
+ return Endpoint(async () => {
25972
+ validateStorefront(state.storefront);
25973
+ const {
25974
+ token,
25975
+ transactionId,
25976
+ invoiceId,
25977
+ websiteId,
25978
+ paymentInstrumentId
25979
+ } = data;
25980
+ const purchaseSchemaKeys = ["riskMetadata", "redirectUrl"];
25981
+ if (token) {
25982
+ purchaseSchemaKeys.push("token");
25983
+ }
25984
+ if (paymentInstrumentId) {
25985
+ purchaseSchemaKeys.push("paymentInstrumentId");
25986
+ }
25987
+ if (transactionId) {
25988
+ purchaseSchemaKeys.push("transactionId");
25989
+ }
25990
+ if (invoiceId) {
25991
+ purchaseSchemaKeys.push("invoiceId");
25992
+ }
25993
+ if (websiteId) {
25994
+ purchaseSchemaKeys.push("amount", "currency", "websiteId");
25995
+ }
25996
+ Object.keys(data).forEach((key) => {
25997
+ if (!purchaseSchemaKeys.includes(key)) {
25998
+ delete data[key];
25999
+ }
26000
+ });
26001
+ return state.storefront.purchase.payment({ data });
26002
+ });
26003
+ }
26004
+ async function makePurchase({ payload }) {
26005
+ const data = {
26006
+ websiteId: state.options.websiteId,
26007
+ paymentInstruction: {
26008
+ token: payload._raw.id
26009
+ },
26010
+ ...payload
26011
+ };
26012
+ if (state.data.acceptBumpOffer) {
26013
+ data.items = state.options.bumpOffer;
26014
+ } else {
26015
+ data.items = mapItemsQuantities(state.options.items);
26016
+ }
26017
+ if (state.data.couponIds && Array.isArray(state.data.couponIds)) {
26018
+ data.couponIds = state.data.couponIds;
26019
+ }
26020
+ if (state.options.addons && state.data.addons && Array.isArray(state.data.addons)) {
26021
+ state.options.addons.forEach((addon) => {
26022
+ if (state.data.addons.includes(addon.planId)) {
26023
+ data.items.push(addon);
26024
+ }
26025
+ });
26026
+ }
26027
+ const { fields } = await postPurchase({ data });
26028
+ return fields;
26029
+ }
25871
26030
  async function makePayment({ payload }) {
25872
26031
  var _a, _b, _c, _d, _e, _f;
25873
26032
  const {
@@ -25906,10 +26065,7 @@ async function makePayment({ payload }) {
25906
26065
  if ((_f = state.data) == null ? void 0 : _f.riskMetadata) {
25907
26066
  data.riskMetadata = state.data.riskMetadata;
25908
26067
  }
25909
- let { fields } = await postPayment({
25910
- state,
25911
- data
25912
- });
26068
+ let { fields } = await postPayment({ data });
25913
26069
  fields = {
25914
26070
  transaction: fields,
25915
26071
  token: fields.token || state.options.jwt
@@ -25919,66 +26075,28 @@ async function makePayment({ payload }) {
25919
26075
  }
25920
26076
  return fields;
25921
26077
  }
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;
26078
+ async function makePayout({
26079
+ payload: {
26080
+ isExistingInstrument,
26081
+ _raw: { id: paymentInstrumentId }
25937
26082
  }
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
- }
26083
+ }) {
26084
+ if (!isExistingInstrument) {
26085
+ const { id: newPaymentInstrumentId } = await createPaymentInstrument({
26086
+ data: { token: paymentInstrumentId }
25943
26087
  });
26088
+ paymentInstrumentId = newPaymentInstrumentId;
25944
26089
  }
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;
26090
+ const payoutRequest = await updatePayoutRequest({
26091
+ id: state.data.payout.id,
26092
+ data: {
26093
+ paymentInstrumentId
25965
26094
  }
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
- }
26095
+ });
26096
+ return payoutRequest;
25979
26097
  }
25980
26098
  async function purchase({ payload }) {
25981
- var _a, _b, _c;
26099
+ var _a, _b;
25982
26100
  Object.keys(payload).forEach((key) => {
25983
26101
  if (payload[key] === null) {
25984
26102
  delete payload[key];
@@ -25986,19 +26104,27 @@ async function purchase({ payload }) {
25986
26104
  });
25987
26105
  try {
25988
26106
  let fields;
25989
- if (state.data.isPayment) {
26107
+ let completedEvent;
26108
+ if (state.options.payout) {
26109
+ fields = await makePayout({ payload });
26110
+ completedEvent = events.payoutCompleted;
26111
+ } else if (state.data.isPayment) {
25990
26112
  fields = await makePayment({ payload });
26113
+ completedEvent = events.purchaseCompleted;
25991
26114
  } else {
25992
26115
  fields = await makePurchase({ payload });
26116
+ completedEvent = events.purchaseCompleted;
25993
26117
  }
25994
26118
  if (((_a = fields.transaction) == null ? void 0 : _a.approvalUrl) && ((_b = fields.transaction) == null ? void 0 : _b.result) === "unknown") {
25995
26119
  if (state.options.features.fullPageRedirect) {
25996
- window.location = (_c = fields.transaction) == null ? void 0 : _c.approvalUrl;
26120
+ window.location = fields.transaction.approvalUrl;
25997
26121
  } else {
25998
26122
  handleApprovalUrl({ fields, payload });
25999
26123
  }
26124
+ } else if (fields.selectedPaymentInstrumentRedirectUrl) {
26125
+ window.location = fields.selectedPaymentInstrumentRedirectUrl;
26000
26126
  } else {
26001
- events.purchaseCompleted.dispatch(fields);
26127
+ completedEvent.dispatch(fields);
26002
26128
  }
26003
26129
  } catch (error2) {
26004
26130
  showError(error2);
@@ -26100,10 +26226,15 @@ async function mountResult({ payload }) {
26100
26226
  }
26101
26227
  const modelSafeState = state.toModel();
26102
26228
  const model = {
26229
+ data: modelSafeState.data,
26103
26230
  options: modelSafeState.options,
26104
- mainStyleVars: modelSafeState.mainStyleVars,
26105
- [state.options.transactionType]: payload
26231
+ mainStyleVars: modelSafeState.mainStyleVars
26106
26232
  };
26233
+ if (state.data.payout) {
26234
+ model.payoutRequest = payload;
26235
+ } else {
26236
+ model[state.options.transactionType] = payload;
26237
+ }
26107
26238
  (_a = iframe == null ? void 0 : iframe.component) == null ? void 0 : _a.call("route", {
26108
26239
  name: "result"
26109
26240
  });
@@ -26162,6 +26293,13 @@ const setupUserFlow = ({ state: state2 = {} }) => {
26162
26293
  });
26163
26294
  }
26164
26295
  if (!((_d = (_c = state2 == null ? void 0 : state2.options) == null ? void 0 : _c.features) == null ? void 0 : _d.hideResult)) {
26296
+ on({
26297
+ eventName: "payout-completed",
26298
+ callback: (payload) => {
26299
+ payload = JSON.parse(JSON.stringify(payload));
26300
+ showResult({ state: state2, payload });
26301
+ }
26302
+ });
26165
26303
  on({
26166
26304
  eventName: "purchase-completed",
26167
26305
  callback: (payload) => {
@@ -26182,7 +26320,7 @@ async function mount({
26182
26320
  setupFramepay: setupFramepay$1 = setupFramepay,
26183
26321
  ...options
26184
26322
  } = {}) {
26185
- var _a, _b, _c, _d, _e, _f, _g, _h;
26323
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
26186
26324
  try {
26187
26325
  state.data = {};
26188
26326
  state.options = {};
@@ -26236,7 +26374,13 @@ async function mount({
26236
26374
  false
26237
26375
  );
26238
26376
  }
26239
- if (!((_h = data.readyToPay) == null ? void 0 : _h.length)) {
26377
+ let hasNoPaymentMethods = false;
26378
+ if (data.payout) {
26379
+ hasNoPaymentMethods = !((_h = data.readyToPayout) == null ? void 0 : _h.length);
26380
+ } else {
26381
+ hasNoPaymentMethods = !((_i = data.readyToPay) == null ? void 0 : _i.length);
26382
+ }
26383
+ if (hasNoPaymentMethods) {
26240
26384
  state.loader.stopLoading({ id: "rebilly-instruments-form" });
26241
26385
  showError(
26242
26386
  state.translate.getTranslation("form.error.noPaymentMethods")
@@ -26304,7 +26448,7 @@ class RebillyInstrumentsInstance {
26304
26448
  await show({ componentName, payload });
26305
26449
  }
26306
26450
  get version() {
26307
- return `RebillyInstruments Ver.${"8.8.1"}`;
26451
+ return `RebillyInstruments Ver.${"8.10.0"}`;
26308
26452
  }
26309
26453
  on(eventName, callback) {
26310
26454
  on({ eventName, callback });