@dodopayments/tanstack 0.1.1 → 0.1.3
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 +54 -40
- package/dist/checkout/checkout.d.ts.map +1 -1
- package/dist/index.cjs +385 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +385 -60
- package/dist/index.js.map +1 -1
- package/dist/webhooks/webhooks.d.ts.map +1 -1
- package/package.json +54 -55
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var zod = require('zod');
|
|
4
4
|
|
|
5
|
-
const VERSION = '1.
|
|
5
|
+
const VERSION = '1.51.2'; // x-release-please-version
|
|
6
6
|
|
|
7
7
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
8
8
|
class DodoPaymentsError extends Error {
|
|
@@ -1146,6 +1146,46 @@ class DefaultPageNumberPagination extends AbstractPage {
|
|
|
1146
1146
|
return { params: { page_number: currentPage + 1 } };
|
|
1147
1147
|
}
|
|
1148
1148
|
}
|
|
1149
|
+
class CursorPagePagination extends AbstractPage {
|
|
1150
|
+
constructor(client, response, body, options) {
|
|
1151
|
+
super(client, response, body, options);
|
|
1152
|
+
this.data = body.data || [];
|
|
1153
|
+
this.iterator = body.iterator || '';
|
|
1154
|
+
this.done = body.done || false;
|
|
1155
|
+
}
|
|
1156
|
+
getPaginatedItems() {
|
|
1157
|
+
return this.data ?? [];
|
|
1158
|
+
}
|
|
1159
|
+
hasNextPage() {
|
|
1160
|
+
if (this.done === false) {
|
|
1161
|
+
return false;
|
|
1162
|
+
}
|
|
1163
|
+
return super.hasNextPage();
|
|
1164
|
+
}
|
|
1165
|
+
// @deprecated Please use `nextPageInfo()` instead
|
|
1166
|
+
nextPageParams() {
|
|
1167
|
+
const info = this.nextPageInfo();
|
|
1168
|
+
if (!info)
|
|
1169
|
+
return null;
|
|
1170
|
+
if ('params' in info)
|
|
1171
|
+
return info.params;
|
|
1172
|
+
const params = Object.fromEntries(info.url.searchParams);
|
|
1173
|
+
if (!Object.keys(params).length)
|
|
1174
|
+
return null;
|
|
1175
|
+
return params;
|
|
1176
|
+
}
|
|
1177
|
+
nextPageInfo() {
|
|
1178
|
+
const cursor = this.iterator;
|
|
1179
|
+
if (!cursor) {
|
|
1180
|
+
return null;
|
|
1181
|
+
}
|
|
1182
|
+
return {
|
|
1183
|
+
params: {
|
|
1184
|
+
iterator: cursor,
|
|
1185
|
+
},
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1149
1189
|
|
|
1150
1190
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
1151
1191
|
class APIResource {
|
|
@@ -1204,6 +1244,13 @@ class Brands extends APIResource {
|
|
|
1204
1244
|
}
|
|
1205
1245
|
}
|
|
1206
1246
|
|
|
1247
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
1248
|
+
class CheckoutSessions extends APIResource {
|
|
1249
|
+
create(body, options) {
|
|
1250
|
+
return this._client.post('/checkouts', { body, ...options });
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1207
1254
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
1208
1255
|
let CustomerPortal$1 = class CustomerPortal extends APIResource {
|
|
1209
1256
|
create(customerId, params = {}, options) {
|
|
@@ -1609,26 +1656,79 @@ Subscriptions.SubscriptionListResponsesDefaultPageNumberPagination =
|
|
|
1609
1656
|
SubscriptionListResponsesDefaultPageNumberPagination;
|
|
1610
1657
|
|
|
1611
1658
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
1612
|
-
class
|
|
1659
|
+
let Headers$1 = class Headers extends APIResource {
|
|
1613
1660
|
/**
|
|
1614
|
-
*
|
|
1661
|
+
* Get a webhook by id
|
|
1615
1662
|
*/
|
|
1616
|
-
retrieve(
|
|
1617
|
-
return this._client.get(`/
|
|
1663
|
+
retrieve(webhookId, options) {
|
|
1664
|
+
return this._client.get(`/webhooks/${webhookId}/headers`, options);
|
|
1665
|
+
}
|
|
1666
|
+
/**
|
|
1667
|
+
* Patch a webhook by id
|
|
1668
|
+
*/
|
|
1669
|
+
update(webhookId, body, options) {
|
|
1670
|
+
return this._client.patch(`/webhooks/${webhookId}/headers`, {
|
|
1671
|
+
body,
|
|
1672
|
+
...options,
|
|
1673
|
+
headers: { Accept: '*/*', ...options?.headers },
|
|
1674
|
+
});
|
|
1675
|
+
}
|
|
1676
|
+
};
|
|
1677
|
+
|
|
1678
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
1679
|
+
let Webhooks$1 = class Webhooks extends APIResource {
|
|
1680
|
+
constructor() {
|
|
1681
|
+
super(...arguments);
|
|
1682
|
+
this.headers = new Headers$1(this._client);
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* Create a new webhook
|
|
1686
|
+
*/
|
|
1687
|
+
create(body, options) {
|
|
1688
|
+
return this._client.post('/webhooks', { body, ...options });
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* Get a webhook by id
|
|
1692
|
+
*/
|
|
1693
|
+
retrieve(webhookId, options) {
|
|
1694
|
+
return this._client.get(`/webhooks/${webhookId}`, options);
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Patch a webhook by id
|
|
1698
|
+
*/
|
|
1699
|
+
update(webhookId, body, options) {
|
|
1700
|
+
return this._client.patch(`/webhooks/${webhookId}`, { body, ...options });
|
|
1618
1701
|
}
|
|
1619
1702
|
list(query = {}, options) {
|
|
1620
1703
|
if (isRequestOptions(query)) {
|
|
1621
1704
|
return this.list({}, query);
|
|
1622
1705
|
}
|
|
1623
|
-
return this._client.getAPIList('/
|
|
1624
|
-
|
|
1706
|
+
return this._client.getAPIList('/webhooks', WebhookDetailsCursorPagePagination, { query, ...options });
|
|
1707
|
+
}
|
|
1708
|
+
/**
|
|
1709
|
+
* Delete a webhook by id
|
|
1710
|
+
*/
|
|
1711
|
+
delete(webhookId, options) {
|
|
1712
|
+
return this._client.delete(`/webhooks/${webhookId}`, {
|
|
1625
1713
|
...options,
|
|
1714
|
+
headers: { Accept: '*/*', ...options?.headers },
|
|
1626
1715
|
});
|
|
1627
1716
|
}
|
|
1717
|
+
/**
|
|
1718
|
+
* Get webhook secret by id
|
|
1719
|
+
*/
|
|
1720
|
+
retrieveSecret(webhookId, options) {
|
|
1721
|
+
return this._client.get(`/webhooks/${webhookId}/secret`, options);
|
|
1722
|
+
}
|
|
1723
|
+
};
|
|
1724
|
+
class WebhookDetailsCursorPagePagination extends CursorPagePagination {
|
|
1628
1725
|
}
|
|
1629
|
-
|
|
1726
|
+
Webhooks$1.WebhookDetailsCursorPagePagination = WebhookDetailsCursorPagePagination;
|
|
1727
|
+
Webhooks$1.Headers = Headers$1;
|
|
1728
|
+
|
|
1729
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
1730
|
+
class WebhookEvents extends APIResource {
|
|
1630
1731
|
}
|
|
1631
|
-
WebhookEvents.WebhookEventsDefaultPageNumberPagination = WebhookEventsDefaultPageNumberPagination;
|
|
1632
1732
|
|
|
1633
1733
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
1634
1734
|
var _DodoPayments_instances, _a;
|
|
@@ -1675,6 +1775,7 @@ class DodoPayments extends APIClient {
|
|
|
1675
1775
|
fetch: options.fetch,
|
|
1676
1776
|
});
|
|
1677
1777
|
_DodoPayments_instances.add(this);
|
|
1778
|
+
this.checkoutSessions = new CheckoutSessions(this);
|
|
1678
1779
|
this.payments = new Payments(this);
|
|
1679
1780
|
this.subscriptions = new Subscriptions(this);
|
|
1680
1781
|
this.invoices = new Invoices(this);
|
|
@@ -1691,6 +1792,7 @@ class DodoPayments extends APIClient {
|
|
|
1691
1792
|
this.discounts = new Discounts(this);
|
|
1692
1793
|
this.addons = new Addons(this);
|
|
1693
1794
|
this.brands = new Brands(this);
|
|
1795
|
+
this.webhooks = new Webhooks$1(this);
|
|
1694
1796
|
this._options = options;
|
|
1695
1797
|
this.bearerToken = bearerToken;
|
|
1696
1798
|
}
|
|
@@ -1725,6 +1827,7 @@ DodoPayments.PermissionDeniedError = PermissionDeniedError;
|
|
|
1725
1827
|
DodoPayments.UnprocessableEntityError = UnprocessableEntityError;
|
|
1726
1828
|
DodoPayments.toFile = toFile;
|
|
1727
1829
|
DodoPayments.fileFromPath = fileFromPath;
|
|
1830
|
+
DodoPayments.CheckoutSessions = CheckoutSessions;
|
|
1728
1831
|
DodoPayments.Payments = Payments;
|
|
1729
1832
|
DodoPayments.PaymentListResponsesDefaultPageNumberPagination =
|
|
1730
1833
|
PaymentListResponsesDefaultPageNumberPagination;
|
|
@@ -1747,7 +1850,6 @@ DodoPayments.DisputeListResponsesDefaultPageNumberPagination =
|
|
|
1747
1850
|
DodoPayments.Payouts = Payouts;
|
|
1748
1851
|
DodoPayments.PayoutListResponsesDefaultPageNumberPagination = PayoutListResponsesDefaultPageNumberPagination;
|
|
1749
1852
|
DodoPayments.WebhookEvents = WebhookEvents;
|
|
1750
|
-
DodoPayments.WebhookEventsDefaultPageNumberPagination = WebhookEventsDefaultPageNumberPagination;
|
|
1751
1853
|
DodoPayments.Products = Products;
|
|
1752
1854
|
DodoPayments.ProductListResponsesDefaultPageNumberPagination =
|
|
1753
1855
|
ProductListResponsesDefaultPageNumberPagination;
|
|
@@ -1757,7 +1859,20 @@ DodoPayments.DiscountsDefaultPageNumberPagination = DiscountsDefaultPageNumberPa
|
|
|
1757
1859
|
DodoPayments.Addons = Addons;
|
|
1758
1860
|
DodoPayments.AddonResponsesDefaultPageNumberPagination = AddonResponsesDefaultPageNumberPagination;
|
|
1759
1861
|
DodoPayments.Brands = Brands;
|
|
1862
|
+
DodoPayments.Webhooks = Webhooks$1;
|
|
1863
|
+
DodoPayments.WebhookDetailsCursorPagePagination = WebhookDetailsCursorPagePagination;
|
|
1760
1864
|
|
|
1865
|
+
var __assign = (undefined && undefined.__assign) || function () {
|
|
1866
|
+
__assign = Object.assign || function(t) {
|
|
1867
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
1868
|
+
s = arguments[i];
|
|
1869
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
1870
|
+
t[p] = s[p];
|
|
1871
|
+
}
|
|
1872
|
+
return t;
|
|
1873
|
+
};
|
|
1874
|
+
return __assign.apply(this, arguments);
|
|
1875
|
+
};
|
|
1761
1876
|
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
1762
1877
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
1763
1878
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -1794,7 +1909,8 @@ var __generator$1 = (undefined && undefined.__generator) || function (thisArg, b
|
|
|
1794
1909
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
1795
1910
|
}
|
|
1796
1911
|
};
|
|
1797
|
-
var checkoutQuerySchema = zod.z
|
|
1912
|
+
var checkoutQuerySchema = zod.z
|
|
1913
|
+
.object({
|
|
1798
1914
|
productId: zod.z.string(),
|
|
1799
1915
|
quantity: zod.z.string().optional(),
|
|
1800
1916
|
// Customer fields
|
|
@@ -1824,7 +1940,8 @@ var checkoutQuerySchema = zod.z.object({
|
|
|
1824
1940
|
showDiscounts: zod.z.string().optional(),
|
|
1825
1941
|
// Metadata (allow any key starting with metadata_)
|
|
1826
1942
|
// We'll handle metadata separately in the handler
|
|
1827
|
-
})
|
|
1943
|
+
})
|
|
1944
|
+
.catchall(zod.z.unknown());
|
|
1828
1945
|
// Add Zod schema for dynamic checkout body
|
|
1829
1946
|
var dynamicCheckoutBodySchema = zod.z
|
|
1830
1947
|
.object({
|
|
@@ -1863,12 +1980,192 @@ var dynamicCheckoutBodySchema = zod.z
|
|
|
1863
1980
|
// Allow any additional fields (for future compatibility)
|
|
1864
1981
|
})
|
|
1865
1982
|
.catchall(zod.z.unknown());
|
|
1983
|
+
// ========================================
|
|
1984
|
+
// CHECKOUT SESSIONS SCHEMAS & TYPES
|
|
1985
|
+
// ========================================
|
|
1986
|
+
// Product cart item schema for checkout sessions
|
|
1987
|
+
var checkoutSessionProductCartItemSchema = zod.z.object({
|
|
1988
|
+
product_id: zod.z.string().min(1, "Product ID is required"),
|
|
1989
|
+
quantity: zod.z.number().int().positive("Quantity must be a positive integer"),
|
|
1990
|
+
});
|
|
1991
|
+
// Customer information schema for checkout sessions
|
|
1992
|
+
var checkoutSessionCustomerSchema = zod.z
|
|
1993
|
+
.object({
|
|
1994
|
+
email: zod.z.string().email().optional(),
|
|
1995
|
+
name: zod.z.string().min(1).optional(),
|
|
1996
|
+
phone_number: zod.z.string().optional(),
|
|
1997
|
+
})
|
|
1998
|
+
.optional();
|
|
1999
|
+
// Billing address schema for checkout sessions
|
|
2000
|
+
var checkoutSessionBillingAddressSchema = zod.z
|
|
2001
|
+
.object({
|
|
2002
|
+
street: zod.z.string().optional(),
|
|
2003
|
+
city: zod.z.string().optional(),
|
|
2004
|
+
state: zod.z.string().optional(),
|
|
2005
|
+
country: zod.z.string().length(2, "Country must be a 2-letter ISO code"),
|
|
2006
|
+
zipcode: zod.z.string().optional(),
|
|
2007
|
+
})
|
|
2008
|
+
.optional();
|
|
2009
|
+
// Payment method types enum based on Dodo Payments documentation
|
|
2010
|
+
var paymentMethodTypeSchema = zod.z.enum([
|
|
2011
|
+
"credit",
|
|
2012
|
+
"debit",
|
|
2013
|
+
"upi_collect",
|
|
2014
|
+
"upi_intent",
|
|
2015
|
+
"apple_pay",
|
|
2016
|
+
"google_pay",
|
|
2017
|
+
"amazon_pay",
|
|
2018
|
+
"klarna",
|
|
2019
|
+
"affirm",
|
|
2020
|
+
"afterpay_clearpay",
|
|
2021
|
+
"sepa",
|
|
2022
|
+
"ach",
|
|
2023
|
+
]);
|
|
2024
|
+
// Customization options schema
|
|
2025
|
+
var checkoutSessionCustomizationSchema = zod.z
|
|
2026
|
+
.object({
|
|
2027
|
+
theme: zod.z.enum(["light", "dark", "system"]).optional(),
|
|
2028
|
+
show_order_details: zod.z.boolean().optional(),
|
|
2029
|
+
show_on_demand_tag: zod.z.boolean().optional(),
|
|
2030
|
+
})
|
|
2031
|
+
.optional();
|
|
2032
|
+
// Feature flags schema
|
|
2033
|
+
var checkoutSessionFeatureFlagsSchema = zod.z
|
|
2034
|
+
.object({
|
|
2035
|
+
allow_currency_selection: zod.z.boolean().optional(),
|
|
2036
|
+
allow_discount_code: zod.z.boolean().optional(),
|
|
2037
|
+
allow_phone_number_collection: zod.z.boolean().optional(),
|
|
2038
|
+
allow_tax_id: zod.z.boolean().optional(),
|
|
2039
|
+
always_create_new_customer: zod.z.boolean().optional(),
|
|
2040
|
+
})
|
|
2041
|
+
.optional();
|
|
2042
|
+
// Subscription data schema
|
|
2043
|
+
var checkoutSessionSubscriptionDataSchema = zod.z
|
|
2044
|
+
.object({
|
|
2045
|
+
trial_period_days: zod.z.number().int().nonnegative().optional(),
|
|
2046
|
+
})
|
|
2047
|
+
.optional();
|
|
2048
|
+
// Main checkout session payload schema
|
|
2049
|
+
var checkoutSessionPayloadSchema = zod.z.object({
|
|
2050
|
+
// Required fields
|
|
2051
|
+
product_cart: zod.z
|
|
2052
|
+
.array(checkoutSessionProductCartItemSchema)
|
|
2053
|
+
.min(1, "At least one product is required"),
|
|
2054
|
+
// Optional fields
|
|
2055
|
+
customer: checkoutSessionCustomerSchema,
|
|
2056
|
+
billing_address: checkoutSessionBillingAddressSchema,
|
|
2057
|
+
return_url: zod.z.string().url().optional(),
|
|
2058
|
+
allowed_payment_method_types: zod.z.array(paymentMethodTypeSchema).optional(),
|
|
2059
|
+
billing_currency: zod.z
|
|
2060
|
+
.string()
|
|
2061
|
+
.length(3, "Currency must be a 3-letter ISO code")
|
|
2062
|
+
.optional(),
|
|
2063
|
+
show_saved_payment_methods: zod.z.boolean().optional(),
|
|
2064
|
+
confirm: zod.z.boolean().optional(),
|
|
2065
|
+
discount_code: zod.z.string().optional(),
|
|
2066
|
+
metadata: zod.z.record(zod.z.string(), zod.z.string()).optional(),
|
|
2067
|
+
customization: checkoutSessionCustomizationSchema,
|
|
2068
|
+
feature_flags: checkoutSessionFeatureFlagsSchema,
|
|
2069
|
+
subscription_data: checkoutSessionSubscriptionDataSchema,
|
|
2070
|
+
});
|
|
2071
|
+
// Checkout session response schema
|
|
2072
|
+
var checkoutSessionResponseSchema = zod.z.object({
|
|
2073
|
+
session_id: zod.z.string().min(1, "Session ID is required"),
|
|
2074
|
+
checkout_url: zod.z.string().url("Invalid checkout URL"),
|
|
2075
|
+
});
|
|
2076
|
+
/**
|
|
2077
|
+
* Creates a new Dodo Payments Checkout Session using the modern /checkouts endpoint.
|
|
2078
|
+
* This function provides a clean, type-safe interface to the Checkout Sessions API.
|
|
2079
|
+
*
|
|
2080
|
+
* @param payload - The checkout session data, validated against CheckoutSessionPayloadSchema
|
|
2081
|
+
* @param config - Dodo Payments client configuration (bearerToken, environment)
|
|
2082
|
+
* @returns Promise<CheckoutSessionResponse> - The checkout session with session_id and checkout_url
|
|
2083
|
+
*
|
|
2084
|
+
* @throws {Error} When payload validation fails or API request fails
|
|
2085
|
+
*
|
|
2086
|
+
* @example
|
|
2087
|
+
* ```typescript
|
|
2088
|
+
* const session = await createCheckoutSession({
|
|
2089
|
+
* product_cart: [{ product_id: 'prod_123', quantity: 1 }],
|
|
2090
|
+
* customer: { email: 'customer@example.com' },
|
|
2091
|
+
* return_url: 'https://yoursite.com/success'
|
|
2092
|
+
* }, {
|
|
2093
|
+
* bearerToken: process.env.DODO_PAYMENTS_API_KEY,
|
|
2094
|
+
* environment: 'test_mode'
|
|
2095
|
+
* });
|
|
2096
|
+
*
|
|
2097
|
+
* ```
|
|
2098
|
+
*/
|
|
2099
|
+
var createCheckoutSession = function (payload, config) { return __awaiter$1(void 0, void 0, void 0, function () {
|
|
2100
|
+
var validation, dodopayments, sdkPayload, session, responseValidation, error_1;
|
|
2101
|
+
return __generator$1(this, function (_a) {
|
|
2102
|
+
switch (_a.label) {
|
|
2103
|
+
case 0:
|
|
2104
|
+
validation = checkoutSessionPayloadSchema.safeParse(payload);
|
|
2105
|
+
if (!validation.success) {
|
|
2106
|
+
throw new Error("Invalid checkout session payload: ".concat(validation.error.issues
|
|
2107
|
+
.map(function (issue) { return "".concat(issue.path.join("."), ": ").concat(issue.message); })
|
|
2108
|
+
.join(", ")));
|
|
2109
|
+
}
|
|
2110
|
+
dodopayments = new DodoPayments({
|
|
2111
|
+
bearerToken: config.bearerToken,
|
|
2112
|
+
environment: config.environment,
|
|
2113
|
+
});
|
|
2114
|
+
_a.label = 1;
|
|
2115
|
+
case 1:
|
|
2116
|
+
_a.trys.push([1, 3, , 4]);
|
|
2117
|
+
sdkPayload = __assign(__assign({}, validation.data), (validation.data.billing_address && {
|
|
2118
|
+
billing_address: __assign(__assign({}, validation.data.billing_address), { country: validation.data.billing_address.country }),
|
|
2119
|
+
}));
|
|
2120
|
+
return [4 /*yield*/, dodopayments.checkoutSessions.create(sdkPayload)];
|
|
2121
|
+
case 2:
|
|
2122
|
+
session = _a.sent();
|
|
2123
|
+
responseValidation = checkoutSessionResponseSchema.safeParse(session);
|
|
2124
|
+
if (!responseValidation.success) {
|
|
2125
|
+
throw new Error("Invalid checkout session response from API: ".concat(responseValidation.error.issues
|
|
2126
|
+
.map(function (issue) { return "".concat(issue.path.join("."), ": ").concat(issue.message); })
|
|
2127
|
+
.join(", ")));
|
|
2128
|
+
}
|
|
2129
|
+
return [2 /*return*/, responseValidation.data];
|
|
2130
|
+
case 3:
|
|
2131
|
+
error_1 = _a.sent();
|
|
2132
|
+
if (error_1 instanceof Error) {
|
|
2133
|
+
console.error("Dodo Payments Checkout Session API Error:", {
|
|
2134
|
+
message: error_1.message,
|
|
2135
|
+
payload: validation.data,
|
|
2136
|
+
config: {
|
|
2137
|
+
environment: config.environment,
|
|
2138
|
+
hasBearerToken: !!config.bearerToken,
|
|
2139
|
+
},
|
|
2140
|
+
});
|
|
2141
|
+
// Re-throw with a more user-friendly message
|
|
2142
|
+
throw new Error("Failed to create checkout session: ".concat(error_1.message));
|
|
2143
|
+
}
|
|
2144
|
+
// Handle non-Error objects
|
|
2145
|
+
console.error("Unknown error creating checkout session:", error_1);
|
|
2146
|
+
throw new Error("Failed to create checkout session due to an unknown error");
|
|
2147
|
+
case 4: return [2 /*return*/];
|
|
2148
|
+
}
|
|
2149
|
+
});
|
|
2150
|
+
}); };
|
|
1866
2151
|
var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0, function (_b) {
|
|
1867
|
-
var inputData, parseResult, success, data, error, _c, productId, quantity_1, fullName, firstName, lastName, email, country, addressLine, city, state, zipCode, disableFullName, disableFirstName, disableLastName, disableEmail, disableCountry, disableAddressLine, disableCity, disableState, disableZipCode, paymentCurrency, showCurrencySelector, paymentAmount, showDiscounts, dodopayments_1, err_1, url, _i, _d, _e, key, value, dyn, product_id, product_cart, quantity, billing, customer, addons, metadata, allowed_payment_method_types, billing_currency, discount_code, on_demand, bodyReturnUrl, show_saved_payment_methods, tax_id, trial_period_days, dodopayments, isSubscription, productIdToFetch, product, err_2, subscriptionPayload, subscription, err_3, cart, paymentPayload, payment, err_4;
|
|
1868
|
-
var queryParams = _b.queryParams, body = _b.body, returnUrl = _b.returnUrl, bearerToken = _b.bearerToken, environment = _b.environment, _f = _b.type, type = _f === void 0 ? "static" : _f;
|
|
2152
|
+
var session, inputData, parseResult, success, data, error, _c, productId, quantity_1, fullName, firstName, lastName, email, country, addressLine, city, state, zipCode, disableFullName, disableFirstName, disableLastName, disableEmail, disableCountry, disableAddressLine, disableCity, disableState, disableZipCode, paymentCurrency, showCurrencySelector, paymentAmount, showDiscounts, dodopayments_1, err_1, url, _i, _d, _e, key, value, dyn, product_id, product_cart, quantity, billing, customer, addons, metadata, allowed_payment_method_types, billing_currency, discount_code, on_demand, bodyReturnUrl, show_saved_payment_methods, tax_id, trial_period_days, dodopayments, isSubscription, productIdToFetch, product, err_2, subscriptionPayload, subscription, err_3, cart, paymentPayload, payment, err_4;
|
|
2153
|
+
var queryParams = _b.queryParams, body = _b.body, sessionPayload = _b.sessionPayload, returnUrl = _b.returnUrl, bearerToken = _b.bearerToken, environment = _b.environment, _f = _b.type, type = _f === void 0 ? "static" : _f;
|
|
1869
2154
|
return __generator$1(this, function (_g) {
|
|
1870
2155
|
switch (_g.label) {
|
|
1871
2156
|
case 0:
|
|
2157
|
+
if (!(type === "session")) return [3 /*break*/, 2];
|
|
2158
|
+
if (!sessionPayload) {
|
|
2159
|
+
throw new Error("sessionPayload is required when type is 'session'");
|
|
2160
|
+
}
|
|
2161
|
+
return [4 /*yield*/, createCheckoutSession(sessionPayload, {
|
|
2162
|
+
bearerToken: bearerToken,
|
|
2163
|
+
environment: environment,
|
|
2164
|
+
})];
|
|
2165
|
+
case 1:
|
|
2166
|
+
session = _g.sent();
|
|
2167
|
+
return [2 /*return*/, session.checkout_url];
|
|
2168
|
+
case 2:
|
|
1872
2169
|
inputData = type === "dynamic" ? body : queryParams;
|
|
1873
2170
|
if (type === "dynamic") {
|
|
1874
2171
|
parseResult = dynamicCheckoutBodySchema.safeParse(inputData);
|
|
@@ -1880,7 +2177,7 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
1880
2177
|
if (!success) {
|
|
1881
2178
|
throw new Error("Invalid ".concat(type === "dynamic" ? "body" : "query parameters", ".\n ").concat(error.message));
|
|
1882
2179
|
}
|
|
1883
|
-
if (!(type !== "dynamic")) return [3 /*break*/,
|
|
2180
|
+
if (!(type !== "dynamic")) return [3 /*break*/, 7];
|
|
1884
2181
|
_c = data, productId = _c.productId, quantity_1 = _c.quantity, fullName = _c.fullName, firstName = _c.firstName, lastName = _c.lastName, email = _c.email, country = _c.country, addressLine = _c.addressLine, city = _c.city, state = _c.state, zipCode = _c.zipCode, disableFullName = _c.disableFullName, disableFirstName = _c.disableFirstName, disableLastName = _c.disableLastName, disableEmail = _c.disableEmail, disableCountry = _c.disableCountry, disableAddressLine = _c.disableAddressLine, disableCity = _c.disableCity, disableState = _c.disableState, disableZipCode = _c.disableZipCode, paymentCurrency = _c.paymentCurrency, showCurrencySelector = _c.showCurrencySelector, paymentAmount = _c.paymentAmount, showDiscounts = _c.showDiscounts;
|
|
1885
2182
|
dodopayments_1 = new DodoPayments({
|
|
1886
2183
|
bearerToken: bearerToken,
|
|
@@ -1889,18 +2186,18 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
1889
2186
|
// Check that the product exists for this merchant
|
|
1890
2187
|
if (!productId)
|
|
1891
2188
|
throw new Error("Missing required field: productId");
|
|
1892
|
-
_g.label =
|
|
1893
|
-
case
|
|
1894
|
-
_g.trys.push([
|
|
2189
|
+
_g.label = 3;
|
|
2190
|
+
case 3:
|
|
2191
|
+
_g.trys.push([3, 5, , 6]);
|
|
1895
2192
|
return [4 /*yield*/, dodopayments_1.products.retrieve(productId)];
|
|
1896
|
-
case
|
|
2193
|
+
case 4:
|
|
1897
2194
|
_g.sent();
|
|
1898
|
-
return [3 /*break*/,
|
|
1899
|
-
case
|
|
2195
|
+
return [3 /*break*/, 6];
|
|
2196
|
+
case 5:
|
|
1900
2197
|
err_1 = _g.sent();
|
|
1901
2198
|
console.error(err_1);
|
|
1902
2199
|
throw new Error("Product not found");
|
|
1903
|
-
case
|
|
2200
|
+
case 6:
|
|
1904
2201
|
url = new URL("".concat(environment === "test_mode" ? "https://test.checkout.dodopayments.com" : "https://checkout.dodopayments.com", "/buy/").concat(productId));
|
|
1905
2202
|
url.searchParams.set("quantity", quantity_1 ? String(quantity_1) : "1");
|
|
1906
2203
|
if (returnUrl)
|
|
@@ -1960,7 +2257,7 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
1960
2257
|
}
|
|
1961
2258
|
}
|
|
1962
2259
|
return [2 /*return*/, url.toString()];
|
|
1963
|
-
case
|
|
2260
|
+
case 7:
|
|
1964
2261
|
dyn = data;
|
|
1965
2262
|
product_id = dyn.product_id, product_cart = dyn.product_cart, quantity = dyn.quantity, billing = dyn.billing, customer = dyn.customer, addons = dyn.addons, metadata = dyn.metadata, allowed_payment_method_types = dyn.allowed_payment_method_types, billing_currency = dyn.billing_currency, discount_code = dyn.discount_code, on_demand = dyn.on_demand, bodyReturnUrl = dyn.return_url, show_saved_payment_methods = dyn.show_saved_payment_methods, tax_id = dyn.tax_id, trial_period_days = dyn.trial_period_days;
|
|
1966
2263
|
dodopayments = new DodoPayments({
|
|
@@ -1974,18 +2271,18 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
1974
2271
|
}
|
|
1975
2272
|
if (!productIdToFetch)
|
|
1976
2273
|
throw new Error("Missing required field: product_id or product_cart[0].product_id");
|
|
1977
|
-
_g.label =
|
|
1978
|
-
case
|
|
1979
|
-
_g.trys.push([
|
|
2274
|
+
_g.label = 8;
|
|
2275
|
+
case 8:
|
|
2276
|
+
_g.trys.push([8, 10, , 11]);
|
|
1980
2277
|
return [4 /*yield*/, dodopayments.products.retrieve(productIdToFetch)];
|
|
1981
|
-
case
|
|
2278
|
+
case 9:
|
|
1982
2279
|
product = _g.sent();
|
|
1983
|
-
return [3 /*break*/,
|
|
1984
|
-
case
|
|
2280
|
+
return [3 /*break*/, 11];
|
|
2281
|
+
case 10:
|
|
1985
2282
|
err_2 = _g.sent();
|
|
1986
2283
|
console.error(err_2);
|
|
1987
2284
|
throw new Error("Product not found");
|
|
1988
|
-
case
|
|
2285
|
+
case 11:
|
|
1989
2286
|
isSubscription = Boolean(product.is_recurring);
|
|
1990
2287
|
// Required field validation
|
|
1991
2288
|
if (isSubscription && !product_id)
|
|
@@ -1994,7 +2291,7 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
1994
2291
|
throw new Error("Missing required field: billing");
|
|
1995
2292
|
if (!customer)
|
|
1996
2293
|
throw new Error("Missing required field: customer");
|
|
1997
|
-
if (!isSubscription) return [3 /*break*/,
|
|
2294
|
+
if (!isSubscription) return [3 /*break*/, 16];
|
|
1998
2295
|
subscriptionPayload = {
|
|
1999
2296
|
billing: billing,
|
|
2000
2297
|
customer: customer,
|
|
@@ -2030,24 +2327,24 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
2030
2327
|
if (trial_period_days)
|
|
2031
2328
|
subscriptionPayload.trial_period_days = trial_period_days;
|
|
2032
2329
|
subscription = void 0;
|
|
2033
|
-
_g.label =
|
|
2034
|
-
case
|
|
2035
|
-
_g.trys.push([
|
|
2330
|
+
_g.label = 12;
|
|
2331
|
+
case 12:
|
|
2332
|
+
_g.trys.push([12, 14, , 15]);
|
|
2036
2333
|
return [4 /*yield*/, dodopayments.subscriptions.create(subscriptionPayload)];
|
|
2037
|
-
case
|
|
2334
|
+
case 13:
|
|
2038
2335
|
subscription =
|
|
2039
2336
|
_g.sent();
|
|
2040
|
-
return [3 /*break*/,
|
|
2041
|
-
case
|
|
2337
|
+
return [3 /*break*/, 15];
|
|
2338
|
+
case 14:
|
|
2042
2339
|
err_3 = _g.sent();
|
|
2043
2340
|
console.error("Error when creating subscription", err_3);
|
|
2044
2341
|
throw new Error(err_3 instanceof Error ? err_3.message : String(err_3));
|
|
2045
|
-
case
|
|
2342
|
+
case 15:
|
|
2046
2343
|
if (!subscription || !subscription.payment_link) {
|
|
2047
2344
|
throw new Error("No payment link returned from Dodo Payments API (subscription). Make sure to set payment_link as true in payload");
|
|
2048
2345
|
}
|
|
2049
2346
|
return [2 /*return*/, subscription.payment_link];
|
|
2050
|
-
case
|
|
2347
|
+
case 16:
|
|
2051
2348
|
cart = product_cart;
|
|
2052
2349
|
if (!cart && product_id) {
|
|
2053
2350
|
cart = [
|
|
@@ -2083,18 +2380,18 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
2083
2380
|
if (tax_id)
|
|
2084
2381
|
paymentPayload.tax_id = tax_id;
|
|
2085
2382
|
payment = void 0;
|
|
2086
|
-
_g.label =
|
|
2087
|
-
case
|
|
2088
|
-
_g.trys.push([
|
|
2383
|
+
_g.label = 17;
|
|
2384
|
+
case 17:
|
|
2385
|
+
_g.trys.push([17, 19, , 20]);
|
|
2089
2386
|
return [4 /*yield*/, dodopayments.payments.create(paymentPayload)];
|
|
2090
|
-
case
|
|
2387
|
+
case 18:
|
|
2091
2388
|
payment = _g.sent();
|
|
2092
|
-
return [3 /*break*/,
|
|
2093
|
-
case
|
|
2389
|
+
return [3 /*break*/, 20];
|
|
2390
|
+
case 19:
|
|
2094
2391
|
err_4 = _g.sent();
|
|
2095
2392
|
console.error("Error when creating payment link", err_4);
|
|
2096
2393
|
throw new Error(err_4 instanceof Error ? err_4.message : String(err_4));
|
|
2097
|
-
case
|
|
2394
|
+
case 20:
|
|
2098
2395
|
if (!payment || !payment.payment_link) {
|
|
2099
2396
|
throw new Error("No payment link returned from Dodo Payments API. Make sure to set payment_link as true in payload.");
|
|
2100
2397
|
}
|
|
@@ -2110,7 +2407,7 @@ var buildCheckoutUrl = function (_a) { return __awaiter$1(void 0, [_a], void 0,
|
|
|
2110
2407
|
function Checkout(config) {
|
|
2111
2408
|
return async function (request) {
|
|
2112
2409
|
if (request.method === "POST") {
|
|
2113
|
-
// Handle dynamic checkout (POST)
|
|
2410
|
+
// Handle dynamic checkout and checkout sessions (POST)
|
|
2114
2411
|
let body;
|
|
2115
2412
|
try {
|
|
2116
2413
|
body = await request.json();
|
|
@@ -2118,20 +2415,48 @@ function Checkout(config) {
|
|
|
2118
2415
|
catch (e) {
|
|
2119
2416
|
return new Response("Invalid JSON body", { status: 400 });
|
|
2120
2417
|
}
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
url =
|
|
2418
|
+
if (config.type === "dynamic") {
|
|
2419
|
+
// Handle dynamic checkout
|
|
2420
|
+
const { success, data, error } = dynamicCheckoutBodySchema.safeParse(body);
|
|
2421
|
+
if (!success) {
|
|
2422
|
+
return new Response(`Invalid request body.\n ${error.message}`, {
|
|
2423
|
+
status: 400,
|
|
2424
|
+
});
|
|
2425
|
+
}
|
|
2426
|
+
let url = "";
|
|
2427
|
+
try {
|
|
2428
|
+
url = await buildCheckoutUrl({
|
|
2429
|
+
body: data,
|
|
2430
|
+
...config,
|
|
2431
|
+
type: "dynamic",
|
|
2432
|
+
});
|
|
2433
|
+
}
|
|
2434
|
+
catch (error) {
|
|
2435
|
+
return new Response(error.message, { status: 400 });
|
|
2436
|
+
}
|
|
2437
|
+
return Response.json({ checkout_url: url });
|
|
2130
2438
|
}
|
|
2131
|
-
|
|
2132
|
-
|
|
2439
|
+
else {
|
|
2440
|
+
// Handle checkout session
|
|
2441
|
+
const { success, data, error } = checkoutSessionPayloadSchema.safeParse(body);
|
|
2442
|
+
if (!success) {
|
|
2443
|
+
return new Response(`Invalid checkout session payload.\n ${error.message}`, {
|
|
2444
|
+
status: 400,
|
|
2445
|
+
});
|
|
2446
|
+
}
|
|
2447
|
+
let url = "";
|
|
2448
|
+
try {
|
|
2449
|
+
url = await buildCheckoutUrl({
|
|
2450
|
+
sessionPayload: data,
|
|
2451
|
+
...config,
|
|
2452
|
+
type: "session",
|
|
2453
|
+
});
|
|
2454
|
+
}
|
|
2455
|
+
catch (error) {
|
|
2456
|
+
return new Response(error.message, { status: 400 });
|
|
2457
|
+
}
|
|
2458
|
+
return Response.json({ checkout_url: url });
|
|
2133
2459
|
}
|
|
2134
|
-
return Response.redirect(url, 302);
|
|
2135
2460
|
}
|
|
2136
2461
|
else {
|
|
2137
2462
|
// Handle static checkout (GET)
|
|
@@ -2160,7 +2485,7 @@ function Checkout(config) {
|
|
|
2160
2485
|
catch (error) {
|
|
2161
2486
|
return new Response(error.message, { status: 400 });
|
|
2162
2487
|
}
|
|
2163
|
-
return Response.
|
|
2488
|
+
return Response.json({ checkout_url: url });
|
|
2164
2489
|
}
|
|
2165
2490
|
};
|
|
2166
2491
|
}
|