@aixyz/stripe 0.20.0 → 0.21.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/index.ts +36 -42
- package/package.json +3 -3
package/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import express, { type Request, type Response, type NextFunction } from "express";
|
|
2
1
|
import Stripe from "stripe";
|
|
3
|
-
import
|
|
2
|
+
import { BasePlugin } from "aixyz/app/plugin";
|
|
3
|
+
import type { AixyzApp } from "aixyz/app";
|
|
4
4
|
|
|
5
5
|
let stripe: Stripe | null = null;
|
|
6
6
|
|
|
@@ -84,57 +84,51 @@ async function validateAndConsumePaymentIntent(
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
|
-
*
|
|
87
|
+
* Stripe PaymentIntent plugin for aixyz.
|
|
88
88
|
* Automatically configures Stripe payment validation from environment variables.
|
|
89
89
|
*
|
|
90
90
|
* Environment variables:
|
|
91
91
|
* - STRIPE_SECRET_KEY: Stripe secret key (required to enable Stripe)
|
|
92
92
|
* - STRIPE_PRICE_CENTS: Price per request in cents (default: 100)
|
|
93
93
|
*/
|
|
94
|
-
export
|
|
95
|
-
|
|
96
|
-
const priceInCents = Number(process.env.STRIPE_PRICE_CENTS) || 100;
|
|
94
|
+
export class experimental_StripePaymentIntentPlugin extends BasePlugin {
|
|
95
|
+
readonly name = "stripe-payment-intent";
|
|
97
96
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
97
|
+
register(app: AixyzApp): void {
|
|
98
|
+
const secretKey = process.env.STRIPE_SECRET_KEY;
|
|
99
|
+
const priceInCents = Number(process.env.STRIPE_PRICE_CENTS) || 100;
|
|
102
100
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
app.express.post("/stripe/create-payment-intent", express.json(), async (req: Request, res: Response) => {
|
|
107
|
-
console.log("[Stripe] create-payment-intent endpoint hit");
|
|
108
|
-
try {
|
|
109
|
-
const result = await createPaymentIntent({ priceInCents });
|
|
110
|
-
res.json(result);
|
|
111
|
-
} catch (error) {
|
|
112
|
-
const message = error instanceof Error ? error.message : "Failed to create payment intent";
|
|
113
|
-
console.error("[Stripe] Failed to create payment intent:", message);
|
|
114
|
-
const status = message === "Stripe not configured" ? 503 : 500;
|
|
115
|
-
res.status(status).json({ error: message });
|
|
101
|
+
if (!secretKey) {
|
|
102
|
+
console.log("[Stripe] STRIPE_SECRET_KEY not set, Stripe payments disabled");
|
|
103
|
+
return;
|
|
116
104
|
}
|
|
117
|
-
});
|
|
118
105
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
106
|
+
initializeStripe(secretKey);
|
|
107
|
+
|
|
108
|
+
// Endpoint to create payment intents
|
|
109
|
+
app.route("POST", "/stripe/create-payment-intent", async () => {
|
|
110
|
+
try {
|
|
111
|
+
const result = await createPaymentIntent({ priceInCents });
|
|
112
|
+
return Response.json(result);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
const message = error instanceof Error ? error.message : "Failed to create payment intent";
|
|
115
|
+
const status = message === "Stripe not configured" ? 503 : 500;
|
|
116
|
+
return Response.json({ error: message }, { status });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
124
119
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
120
|
+
// Stripe payment validation middleware
|
|
121
|
+
app.use(async (request: Request, next: () => Promise<Response>) => {
|
|
122
|
+
if (!stripe) return next();
|
|
123
|
+
|
|
124
|
+
const stripePaymentIntentId = request.headers.get("x-stripe-payment-intent-id");
|
|
125
|
+
if (stripePaymentIntentId) {
|
|
126
|
+
const result = await validateAndConsumePaymentIntent(stripePaymentIntentId, priceInCents);
|
|
127
|
+
if (result.valid) return next();
|
|
128
|
+
return Response.json({ error: "Payment Required", message: result.error }, { status: 402 });
|
|
130
129
|
}
|
|
131
|
-
return res.status(402).json({
|
|
132
|
-
error: "Payment Required",
|
|
133
|
-
message: result.error,
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
130
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
131
|
+
return next();
|
|
132
|
+
});
|
|
133
|
+
}
|
|
140
134
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aixyz/stripe",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Payment-native SDK for AI Agent",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/express": "^5",
|
|
28
|
-
"aixyz": "0.
|
|
28
|
+
"aixyz": "0.21.0",
|
|
29
29
|
"typescript": "^5.9.3"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"aixyz": "0.
|
|
32
|
+
"aixyz": "0.21.0",
|
|
33
33
|
"stripe": "^20"
|
|
34
34
|
}
|
|
35
35
|
}
|