-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathactions.ts
More file actions
126 lines (119 loc) · 3.59 KB
/
actions.ts
File metadata and controls
126 lines (119 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use server';
import { createCheckoutSession } from '@godaddy/react/server';
import { redirect } from 'next/navigation';
/**
* Fetches selling plans for given SKU IDs from the external (or local) selling-plans API.
* Only runs when SELLING_PLANS_API_URL is set (e.g. in .env.local). If unset, returns
* empty so no request is made and no 404 is logged.
*/
export async function getSellingPlans(storeId: string, options: any) {
const base = process.env.SELLING_PLANS_API_URL;
if (!base) {
return null;
}
// API shape: GET /api/v1/selling-plans/{storeId}/groups?skuIds=...
const path = `/api/v1/selling-plans/${encodeURIComponent(storeId)}/groups`
const url = new URL(path, base);
for (const id of options.skuIds ?? []) {
url.searchParams.append('skuIds', id);
}
for (const id of options.skuGroupIds ?? []) {
url.searchParams.append('skuGroupIds', id);
}
url.searchParams.append('includes', 'catalogPrices');
url.searchParams.append('includes', 'allocations');
// url.searchParams.append('planStatus', 'ACTIVE'); // uncomment it when push to repo
try {
const res = await fetch(url.toString(), { cache: 'no-store' });
if (!res.ok) {
return null;
}
const data = await res.json();
return data.groups;
} catch {
// Network/socket errors (e.g. selling-plans API not running): fail silently so product page still works
return null;
}
}
export async function checkoutWithOrder(orderId: string) {
const session = await createCheckoutSession(
{
returnUrl: `https://godaddy.com`,
successUrl: `https://godaddy.com/success`,
draftOrderId: orderId,
storeId: process.env.NEXT_PUBLIC_GODADDY_STORE_ID || '',
channelId: process.env.NEXT_PUBLIC_GODADDY_CHANNEL_ID || '',
enableShippingAddressCollection: true,
enableBillingAddressCollection: true,
enableTaxCollection: true,
shipping: {
fulfillmentLocationId: 'default-location',
originAddress: {
addressLine1: '1600 Pennsylvania Ave NW',
adminArea1: 'DC',
adminArea3: 'Washington',
countryCode: 'US',
postalCode: '20500',
},
},
taxes: {
originAddress: {
addressLine1: '1600 Pennsylvania Ave NW',
adminArea1: 'DC',
adminArea3: 'Washington',
countryCode: 'US',
postalCode: '20500',
},
},
locations: [
{
id: 'default-location',
isDefault: true,
address: {
addressLine1: '1600 Pennsylvania Ave NW',
adminArea1: 'DC',
adminArea3: 'Washington',
countryCode: 'US',
postalCode: '20500',
},
},
],
paymentMethods: {
card: {
processor: 'godaddy',
checkoutTypes: ['standard'],
},
ccavenue: {
processor: 'ccavenue',
checkoutTypes: ['standard'],
},
express: {
processor: 'godaddy',
checkoutTypes: ['express'],
},
paypal: {
processor: 'paypal',
checkoutTypes: ['standard'],
},
offline: {
processor: 'offline',
checkoutTypes: ['standard'],
},
},
},
{
auth: {
clientId: process.env.NEXT_PUBLIC_GODADDY_CLIENT_ID || '',
clientSecret: process.env.GODADDY_CLIENT_SECRET || '',
},
}
);
if (!session) {
throw new Error('Failed to create checkout session');
}
console.log({ session });
if (!session.url) {
throw new Error('No checkout URL returned');
}
redirect(session.url);
}