@delopay/sdk 0.188.0 → 0.190.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/README.md CHANGED
@@ -330,6 +330,67 @@ const all = await delopay.subscriptions.list({ limit: 20 }, opts);
330
330
  Each billing cycle raises an invoice (`sub.invoice`) with its own payment leg
331
331
  (`sub.payment`); track cycle outcomes via the subscription/invoice webhooks.
332
332
 
333
+ **Bill on a catalog product.** Instead of the processor's `item_price_id`, name a
334
+ recurring product of the shop (and optionally one of its prices). The server
335
+ resolves it to the reference the billing account holds and refuses the request
336
+ before anything is charged when the processor's price no longer matches. Check
337
+ first which prices can start a subscription:
338
+
339
+ ```typescript
340
+ const { prices } = await delopay.products.billingReadiness(['prd_pro'], profileId);
341
+ // state: 'ready' | 'unverified' | 'other_environment' | 'needs_reference'
342
+
343
+ await delopay.subscriptions.createAndConfirm(
344
+ { product_id: 'prd_pro', customer_id: 'cus_abc123', payment_details: { payment_method_id: pm } },
345
+ opts,
346
+ );
347
+ await delopay.subscriptions.update(sub.id, { product_id: 'prd_pro', price_id: 'prc_yearly' }, opts);
348
+ ```
349
+
350
+ **Mirror the processor.** `subscriptions.sync({ subscription_ids })` (1 to 50) and
351
+ `subscriptions.syncOne(id)` read subscriptions back from the processor and mirror
352
+ what it reports; nothing is written there.
353
+
354
+ **Take existing processor subscriptions over.** `subscriptions.importPreview`
355
+ lists what a billing account holds and classifies each row; `importApply` adopts
356
+ up to 25 of them. Word `capabilities.reason_code` and each row's `note_code`
357
+ yourself; the English `reason` / `note` beside them are for API callers.
358
+
359
+ ```typescript
360
+ const preview = await delopay.subscriptions.importPreview({ merchant_connector_id: mca }, opts);
361
+
362
+ if (!preview.capabilities.listing || !preview.capabilities.adoption) {
363
+ // PayPal cannot be listed (`reason_code: 'listing_unsupported'`); an
364
+ // account may list without allowing adoption. Say why, and stop.
365
+ showUnavailable(preview.capabilities.reason_code);
366
+ } else {
367
+ // Rows whose buyer is already a DeloPay customer can be taken over as they
368
+ // are; `needs_customer` rows need a customer named first.
369
+ const picks = preview.items.flatMap((row) =>
370
+ row.classification === 'new' && row.customer_id
371
+ ? [{ external_subscription_id: row.external_subscription_id, customer_id: row.customer_id }]
372
+ : [],
373
+ );
374
+ // The route takes 1 to 25 selections: send nothing when there is nothing
375
+ // to take over, and batch anything larger.
376
+ for (let i = 0; i < picks.length; i += 25) {
377
+ const { results } = await delopay.subscriptions.importApply(
378
+ { merchant_connector_id: mca, items: picks.slice(i, i + 25) },
379
+ opts,
380
+ );
381
+ // One result per selection: `adopted`, `already_held`, or `failed` with a `reason_code`.
382
+ }
383
+ }
384
+ ```
385
+
386
+ **A buyer returning from the processor's checkout** can read the subscription's
387
+ status with the return token (`rt`) the return URL carried, from the browser:
388
+
389
+ ```typescript
390
+ const session = new CheckoutSession({ merchantId, paymentId: subscriptionId });
391
+ const { status, awaiting_processor } = await session.subscriptionReturnStatus(rt);
392
+ ```
393
+
333
394
  ### Platform fee rules
334
395
 
335
396
  Price the platform fee by payment method, connector, amount, currency or card
@@ -5036,6 +5036,25 @@ var Products = class {
5036
5036
  query: { ...query }
5037
5037
  });
5038
5038
  }
5039
+ /**
5040
+ * Which active prices of the named recurring products can start a
5041
+ * subscription on the shop's billing processor account, in the environment
5042
+ * the account is in now. `GET /products/billing-readiness`
5043
+ *
5044
+ * Reads only; subscribing still checks the processor's price before
5045
+ * anything is billed. Unknown ids, one-time products and archived prices
5046
+ * are left out of the answer.
5047
+ *
5048
+ * @param productIds - 1 to 100 product ids.
5049
+ * @param profileId - The shop the products belong to.
5050
+ */
5051
+ async billingReadiness(productIds, profileId, options) {
5052
+ return this.request("GET", "/products/billing-readiness", {
5053
+ ...this.scoped(profileId, options),
5054
+ // One comma-separated value, as the route reads it — not repeated keys.
5055
+ query: { product_ids: productIds.join(",") }
5056
+ });
5057
+ }
5039
5058
  /**
5040
5059
  * Change a product's name, description, image, metadata or category
5041
5060
  * (`category_id: null` makes it uncategorized).
@@ -5685,6 +5704,31 @@ var Subscriptions = class {
5685
5704
  ...options
5686
5705
  });
5687
5706
  }
5707
+ /**
5708
+ * {@link getItems}, with whether the list is only the first part of what the
5709
+ * processor holds. `GET /subscriptions/items`
5710
+ *
5711
+ * The backend states that in the `X-Delopay-Items-Truncated` response
5712
+ * header (Stripe Billing lists its first 100 products), which the plain
5713
+ * {@link getItems} cannot return. Without `limit` and `offset` every item
5714
+ * the billing account offers is listed, up to 500.
5715
+ */
5716
+ async getItemsPage(params, options) {
5717
+ let truncated = false;
5718
+ const items = await this.request(
5719
+ "GET",
5720
+ "/subscriptions/items",
5721
+ {
5722
+ query: params,
5723
+ ...options,
5724
+ onDownloadProgress: (event) => {
5725
+ truncated = event.headers.get("x-delopay-items-truncated")?.toLowerCase() === "true";
5726
+ options?.onDownloadProgress?.(event);
5727
+ }
5728
+ }
5729
+ );
5730
+ return { items, truncated };
5731
+ }
5688
5732
  /**
5689
5733
  * Pause a subscription. `POST /subscriptions/{subscriptionId}/pause`
5690
5734
  *
@@ -5705,13 +5749,64 @@ var Subscriptions = class {
5705
5749
  ...options
5706
5750
  });
5707
5751
  }
5708
- /** Cancel a subscription. `POST /subscriptions/{subscriptionId}/cancel` */
5752
+ /**
5753
+ * Cancel a subscription. `POST /subscriptions/{subscriptionId}/cancel`
5754
+ *
5755
+ * A subscription in a state it cannot be cancelled from is refused with
5756
+ * `DE_11`; `error.data.reason` then names why (`SubscriptionActionRefusal`).
5757
+ */
5709
5758
  async cancel(subscriptionId, params, options) {
5710
5759
  return this.request("POST", `/subscriptions/${encodeURIComponent(subscriptionId)}/cancel`, {
5711
5760
  body: params ?? {},
5712
5761
  ...options
5713
5762
  });
5714
5763
  }
5764
+ /**
5765
+ * Read 1 to 50 subscriptions back from their billing processor and mirror
5766
+ * what it reports. `POST /subscriptions/sync`
5767
+ *
5768
+ * Nothing is written at the processor. A status is mirrored only with the
5769
+ * time the processor stamped it with, and never over a newer event already
5770
+ * recorded; a plan only with its own stamp. One result per id, in request
5771
+ * order; one that fails does not stop the others.
5772
+ */
5773
+ async sync(params, options) {
5774
+ return this.request("POST", "/subscriptions/sync", { body: params, ...options });
5775
+ }
5776
+ /**
5777
+ * Read one subscription back from its billing processor and mirror what it
5778
+ * reports, as {@link sync} does, with what the processor reported beside
5779
+ * what the mirror now holds. `POST /subscriptions/{subscriptionId}/sync`
5780
+ */
5781
+ async syncOne(subscriptionId, options) {
5782
+ return this.request("POST", `/subscriptions/${encodeURIComponent(subscriptionId)}/sync`, {
5783
+ ...options
5784
+ });
5785
+ }
5786
+ /**
5787
+ * List the subscriptions a billing account holds at its processor, and how
5788
+ * each relates to DeloPay's: already held, new (its buyer is a DeloPay
5789
+ * customer), needing a customer, ended, or not adoptable here.
5790
+ * `POST /subscriptions/import/preview`
5791
+ *
5792
+ * Reads only; nothing is written here or at the processor. Creem and
5793
+ * Stripe Billing can be listed; PayPal cannot, which `capabilities` states
5794
+ * (`reason_code: 'listing_unsupported'`).
5795
+ */
5796
+ async importPreview(params, options) {
5797
+ return this.request("POST", "/subscriptions/import/preview", { body: params, ...options });
5798
+ }
5799
+ /**
5800
+ * Take 1 to 25 processor subscriptions over, each through {@link adopt}
5801
+ * with every one of its checks, one after another.
5802
+ * `POST /subscriptions/import/apply`
5803
+ *
5804
+ * One result per selection, in request order; one refused does not stop
5805
+ * the others.
5806
+ */
5807
+ async importApply(params, options) {
5808
+ return this.request("POST", "/subscriptions/import/apply", { body: params, ...options });
5809
+ }
5715
5810
  /**
5716
5811
  * Resolve which of the given payments were raised by a subscription.
5717
5812
  * `POST /subscriptions/payments/lookup`
@@ -9971,6 +10066,38 @@ var CheckoutSession = class {
9971
10066
  }
9972
10067
  );
9973
10068
  }
10069
+ /**
10070
+ * The status a buyer coming back from the processor's checkout may read:
10071
+ * the subscription's status, and whether the processor has reported it yet
10072
+ * (`awaiting_processor`). Construct the session with the `sub_…` id in the
10073
+ * `paymentId` slot.
10074
+ *
10075
+ * Authenticated with the return token (`rt`) the return URL carried, sent as
10076
+ * `Authorization: Bearer <rt>`. That token reads only this, for a day; it is
10077
+ * never the client secret, which this call does not send.
10078
+ *
10079
+ * `GET /subscriptions/return-status/{merchantId}/{subscriptionId}`
10080
+ */
10081
+ async subscriptionReturnStatus(returnToken, options) {
10082
+ if (!returnToken) {
10083
+ throw new DelopayError("This call requires the return token (rt) from the return URL", {
10084
+ status: 0,
10085
+ code: "MISSING_CREDENTIAL",
10086
+ type: "invalid_request"
10087
+ });
10088
+ }
10089
+ return this.client.request(
10090
+ "GET",
10091
+ `/subscriptions/return-status/${encodeURIComponent(this.merchantId)}/${encodeURIComponent(this.paymentId)}`,
10092
+ {
10093
+ ...options,
10094
+ headers: {
10095
+ ...withoutCredentialHeaders(options?.headers),
10096
+ Authorization: `Bearer ${returnToken}`
10097
+ }
10098
+ }
10099
+ );
10100
+ }
9974
10101
  /**
9975
10102
  * Confirm a subscription (PayPal approval rail). The client secret is
9976
10103
  * attached automatically; the shop's profile id travels as the
@@ -10938,4 +11065,4 @@ export {
10938
11065
  decodeNativePanes,
10939
11066
  encodeNativePanes
10940
11067
  };
10941
- //# sourceMappingURL=chunk-7M6HDH2Q.js.map
11068
+ //# sourceMappingURL=chunk-YNJUCISZ.js.map