@affiliateo/web 0.1.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 +44 -0
- package/package.json +14 -0
- package/src/index.ts +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @affiliateo/web
|
|
2
|
+
|
|
3
|
+
Affiliateo web SDK — pass affiliate attribution metadata to your Stripe checkout.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @affiliateo/web
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
1. Make sure you have the Affiliateo tracking script on your site (for visitor tracking)
|
|
14
|
+
2. Connect your Stripe account on the Affiliateo dashboard
|
|
15
|
+
3. Add one line to your Stripe checkout creation:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { getMetadata } from '@affiliateo/web';
|
|
19
|
+
|
|
20
|
+
// Your existing checkout route
|
|
21
|
+
const session = await stripe.checkout.sessions.create({
|
|
22
|
+
line_items: [{ price: 'price_xxx', quantity: 1 }],
|
|
23
|
+
mode: 'payment',
|
|
24
|
+
success_url: 'https://yoursite.com/success',
|
|
25
|
+
cancel_url: 'https://yoursite.com/cancel',
|
|
26
|
+
metadata: getMetadata(req),
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
That's it. Affiliateo will automatically attribute the purchase to the right affiliate.
|
|
31
|
+
|
|
32
|
+
## How it works
|
|
33
|
+
|
|
34
|
+
1. Your tracking script (`t.js`) sets cookies when a visitor arrives via an affiliate link
|
|
35
|
+
2. `getMetadata(req)` reads those cookies and returns them as Stripe metadata
|
|
36
|
+
3. When the payment completes, Affiliateo's webhook reads the metadata and credits the affiliate
|
|
37
|
+
|
|
38
|
+
## Framework support
|
|
39
|
+
|
|
40
|
+
Works with any Node.js framework:
|
|
41
|
+
- **Next.js** (App Router & Pages Router)
|
|
42
|
+
- **Express**
|
|
43
|
+
- **Fastify**
|
|
44
|
+
- Any framework that exposes request cookies
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@affiliateo/web",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Affiliateo web SDK — pass affiliate attribution metadata to your Stripe checkout",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"types": "src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"typecheck": "tsc --noEmit"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["affiliateo", "affiliate", "attribution", "stripe", "web"],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"files": ["src"]
|
|
14
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @affiliateo/web — Pass affiliate attribution metadata to your Stripe checkout.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { getMetadata } from '@affiliateo/web';
|
|
7
|
+
*
|
|
8
|
+
* const session = await stripe.checkout.sessions.create({
|
|
9
|
+
* line_items: [...],
|
|
10
|
+
* mode: 'payment',
|
|
11
|
+
* metadata: getMetadata(req),
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Extract affiliateo cookies from an incoming request.
|
|
18
|
+
* Works with Next.js (App Router & Pages Router), Express, and any framework
|
|
19
|
+
* that exposes cookies on the request object.
|
|
20
|
+
*
|
|
21
|
+
* Returns metadata object to pass to Stripe checkout session.
|
|
22
|
+
*/
|
|
23
|
+
export function getMetadata(
|
|
24
|
+
req: {
|
|
25
|
+
cookies?: Record<string, string> | { get?: (name: string) => { value: string } | undefined }
|
|
26
|
+
headers?: { cookie?: string } | Headers
|
|
27
|
+
}
|
|
28
|
+
): { affiliateo_visitor_id?: string; affiliateo_ref?: string } {
|
|
29
|
+
const metadata: { affiliateo_visitor_id?: string; affiliateo_ref?: string } = {}
|
|
30
|
+
|
|
31
|
+
// Try Next.js App Router cookies (ReadonlyRequestCookies with .get())
|
|
32
|
+
if (req.cookies && typeof req.cookies === 'object' && 'get' in req.cookies && typeof req.cookies.get === 'function') {
|
|
33
|
+
const visitorId = req.cookies.get('affiliateo_visitor_id')
|
|
34
|
+
const ref = req.cookies.get('affiliateo_ref')
|
|
35
|
+
if (visitorId?.value) metadata.affiliateo_visitor_id = visitorId.value
|
|
36
|
+
if (ref?.value) metadata.affiliateo_ref = ref.value
|
|
37
|
+
return metadata
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Try plain object cookies (Express, Pages Router, etc.)
|
|
41
|
+
if (req.cookies && typeof req.cookies === 'object') {
|
|
42
|
+
const cookies = req.cookies as Record<string, string>
|
|
43
|
+
if (cookies.affiliateo_visitor_id) metadata.affiliateo_visitor_id = cookies.affiliateo_visitor_id
|
|
44
|
+
if (cookies.affiliateo_ref) metadata.affiliateo_ref = cookies.affiliateo_ref
|
|
45
|
+
return metadata
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Fallback: parse cookie header manually
|
|
49
|
+
const cookieHeader = req.headers instanceof Headers
|
|
50
|
+
? req.headers.get('cookie')
|
|
51
|
+
: (req.headers as { cookie?: string })?.cookie
|
|
52
|
+
|
|
53
|
+
if (cookieHeader) {
|
|
54
|
+
const cookies = parseCookies(cookieHeader)
|
|
55
|
+
if (cookies.affiliateo_visitor_id) metadata.affiliateo_visitor_id = cookies.affiliateo_visitor_id
|
|
56
|
+
if (cookies.affiliateo_ref) metadata.affiliateo_ref = cookies.affiliateo_ref
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return metadata
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Parse a cookie header string into key-value pairs.
|
|
64
|
+
*/
|
|
65
|
+
function parseCookies(cookieHeader: string): Record<string, string> {
|
|
66
|
+
const cookies: Record<string, string> = {}
|
|
67
|
+
for (const pair of cookieHeader.split(';')) {
|
|
68
|
+
const [key, ...rest] = pair.trim().split('=')
|
|
69
|
+
if (key) cookies[key.trim()] = rest.join('=').trim()
|
|
70
|
+
}
|
|
71
|
+
return cookies
|
|
72
|
+
}
|