Skip to content

Commit face92d

Browse files
committed
feat: update checkout handling to support currency and improve address input types
1 parent 4c1c79d commit face92d

4 files changed

Lines changed: 81 additions & 19 deletions

File tree

src/hooks.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const handleCommercify: Handle = async ({ event, resolve }) => {
6868
// For shop routes, ensure we have a checkout session and set the cookie
6969
if (event.url.pathname.startsWith('/shop')) {
7070
try {
71-
const checkoutResponse = await event.locals.commercify.getOrCreateCheckout();
71+
const checkoutResponse = await event.locals.commercify.getOrCreateCheckout('DKK');
7272

7373
if (checkoutResponse.success && checkoutResponse.data?.id) {
7474
// Set the checkout session cookie

src/lib/server/api/commercify.ts

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ import {
5353
type CheckoutCompleteResponse,
5454
type OrderSummaryDTO
5555
} from './types';
56-
import type { Address, Checkout, CheckoutItem, CompleteCheckoutInput } from '$lib/types/checkout';
56+
import type {
57+
Address,
58+
Checkout,
59+
CheckoutItem,
60+
CompleteCheckoutInput,
61+
SetAddressInput
62+
} from '$lib/types/checkout';
5763
import type { CreateDiscount, Discount } from '$lib/types/discount';
5864
import type { CommercifyUser } from '$lib/types/user';
5965

@@ -141,15 +147,7 @@ export class CommercifyClient {
141147
headers: Object.keys(headers)
142148
});
143149

144-
// Try to get the response body for more details
145-
try {
146-
const errorBody = await response.text();
147-
console.error('Error response body:', errorBody);
148-
} catch (e) {
149-
console.error('Could not read error response body');
150-
}
151-
152-
throw new Error(`API request failed: ${response.status} ${response.statusText}`);
150+
throw new Error(response.statusText || 'API request failed');
153151
}
154152

155153
return await response.json();
@@ -1305,12 +1303,21 @@ export class CommercifyClient {
13051303
* Set shipping address for the checkout
13061304
*/
13071305
async setShippingAddress(
1308-
data: SetShippingAddressRequest
1306+
data: SetAddressInput
13091307
): Promise<{ success: boolean; data?: Checkout; error?: string }> {
1308+
const requestBody: SetShippingAddressRequest = {
1309+
address_line1: data.addressLine1,
1310+
address_line2: data.addressLine2 || '',
1311+
city: data.city,
1312+
state: data.state || '',
1313+
postal_code: data.postalCode,
1314+
country: data.country
1315+
};
1316+
13101317
try {
13111318
const response = await this.request<ResponseDTO<CheckoutDTO>>('/checkout/shipping-address', {
13121319
method: 'PUT',
1313-
body: JSON.stringify(data)
1320+
body: JSON.stringify(requestBody)
13141321
});
13151322

13161323
if (!response.success || !response.data) {
@@ -1340,12 +1347,21 @@ export class CommercifyClient {
13401347
* Set billing address for the checkout
13411348
*/
13421349
async setBillingAddress(
1343-
data: SetBillingAddressRequest
1350+
data: SetAddressInput
13441351
): Promise<{ success: boolean; data?: Checkout; error?: string }> {
1352+
const requestBody: SetShippingAddressRequest = {
1353+
address_line1: data.addressLine1,
1354+
address_line2: data.addressLine2 || '',
1355+
city: data.city,
1356+
state: data.state || '',
1357+
postal_code: data.postalCode,
1358+
country: data.country
1359+
};
1360+
13451361
try {
13461362
const response = await this.request<ResponseDTO<CheckoutDTO>>('/checkout/billing-address', {
13471363
method: 'PUT',
1348-
body: JSON.stringify(data)
1364+
body: JSON.stringify(requestBody)
13491365
});
13501366

13511367
if (!response.success || !response.data) {
@@ -1650,6 +1666,43 @@ export class CommercifyClient {
16501666
};
16511667
}
16521668

1669+
async getOrderById(orderId: string): Promise<{ success: boolean; data?: Order; error?: string }> {
1670+
if (!orderId) {
1671+
console.warn('No order ID provided, returning null');
1672+
return {
1673+
success: false,
1674+
error: 'No order ID provided'
1675+
};
1676+
}
1677+
1678+
try {
1679+
const response = await this.request<ResponseDTO<OrderDTO>>(`/orders/${orderId}`);
1680+
1681+
if (!response.success || !response.data) {
1682+
return {
1683+
success: false,
1684+
error: response.error || 'Failed to retrieve order'
1685+
};
1686+
}
1687+
1688+
return {
1689+
success: true,
1690+
data: this.mapOrder(response.data)
1691+
};
1692+
} catch (error) {
1693+
console.error(
1694+
`Error fetching order ${orderId}:`,
1695+
error instanceof Error ? error.message : String(error)
1696+
);
1697+
return {
1698+
success: false,
1699+
error: `Error fetching order ${orderId}: ${
1700+
error instanceof Error ? error.message : String(error)
1701+
}`
1702+
};
1703+
}
1704+
}
1705+
16531706
async createDiscount(input: CreateDiscount): Promise<{
16541707
success: boolean;
16551708
data?: Discount;

src/lib/server/api/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ export interface UpdateCheckoutItemRequest {
103103
*/
104104
export interface SetShippingAddressRequest {
105105
address_line1: string;
106-
address_line2: string;
106+
address_line2?: string;
107107
city: string;
108-
state: string;
108+
state?: string;
109109
postal_code: string;
110110
country: string;
111111
}
@@ -114,9 +114,9 @@ export interface SetShippingAddressRequest {
114114
*/
115115
export interface SetBillingAddressRequest {
116116
address_line1: string;
117-
address_line2: string;
117+
address_line2?: string;
118118
city: string;
119-
state: string;
119+
state?: string;
120120
postal_code: string;
121121
country: string;
122122
}

src/lib/types/checkout.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,12 @@ export interface CompleteCheckoutInput {
6161
cvc: string; // e.g., '123'
6262
};
6363
}
64+
65+
export interface SetAddressInput {
66+
addressLine1: string;
67+
addressLine2?: string;
68+
city: string;
69+
state?: string;
70+
postalCode: string;
71+
country: string;
72+
}

0 commit comments

Comments
 (0)