@efaj/pulumi-dynamic-stripe 0.0.2 → 0.1.0-9ccdc21
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/CHANGELOG.md +8 -0
- package/README.md +35 -4
- package/dist/StripePaymentLink.js +3 -3
- package/dist/StripePrice.js +3 -3
- package/dist/StripeProduct.js +4 -4
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +26 -0
- package/docs/troubleshooting-stuck-state.md +63 -0
- package/package.json +4 -6
- package/tests/index.test.ts +257 -28
- package/vitest.config.ts +7 -0
- package/jest.config.js +0 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0](https://github.com/efaj/pulumi-dynamic-stripe/compare/v0.0.2...v0.1.0) (2026-08-28)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* Handle delete case w/o apiKey gracefully ([0639e18](https://github.com/efaj/pulumi-dynamic-stripe/commit/0639e18beff72d6d4fe251be171e2e2e4bc47bda))
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# pulumi-dynamic-stripe
|
|
2
2
|
|
|
3
|
-

|
|
4
|
-

|
|
3
|
+

|
|
4
|
+

|
|
5
5
|
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fefaj%2Fpulumi-dynamic-stripe?ref=badge_shield)
|
|
6
6
|
|
|
7
7
|
A native Pulumi Dynamic Provider for Stripe.
|
|
@@ -28,7 +28,7 @@ Contributions to add more dynamic providers for other Stripe resources are highl
|
|
|
28
28
|
## Installation
|
|
29
29
|
|
|
30
30
|
```bash
|
|
31
|
-
npm install pulumi-dynamic-stripe
|
|
31
|
+
npm install @efaj/pulumi-dynamic-stripe
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
## Usage
|
|
@@ -37,7 +37,7 @@ Here is a full example of provisioning a Product, attaching a Price to it, and g
|
|
|
37
37
|
|
|
38
38
|
```typescript
|
|
39
39
|
import * as pulumi from "@pulumi/pulumi";
|
|
40
|
-
import { Product, Price, PaymentLink } from "pulumi-dynamic-stripe";
|
|
40
|
+
import { Product, Price, PaymentLink } from "@efaj/pulumi-dynamic-stripe";
|
|
41
41
|
|
|
42
42
|
const config = new pulumi.Config();
|
|
43
43
|
const stripeApiKey = config.requireSecret("stripeApiKey");
|
|
@@ -71,3 +71,34 @@ export const paymentUrl = premiumPaymentLink.url;
|
|
|
71
71
|
## How it works
|
|
72
72
|
|
|
73
73
|
Behind the scenes, this library defines custom Pulumi Dynamic Resource Providers that implement the CRUD operations for Stripe entities via the `@pulumi/pulumi` and `stripe` Node packages.
|
|
74
|
+
|
|
75
|
+
## Resource Deletion & State Fallbacks
|
|
76
|
+
|
|
77
|
+
Stripe entities generally cannot be hard-deleted via the API; instead, this provider safely **deactivates** them (e.g., setting `active: false` on Payment Links, Products, and Prices) when Pulumi destroys the resource.
|
|
78
|
+
|
|
79
|
+
Due to the way Pulumi manages dynamic provider state, if an input property like `apiKey` is not explicitly declared as a public output on the resource class, it may be scrubbed from the state file. When Pulumi attempts to delete a replaced or destroyed resource, it reads from the old state. If the `apiKey` is missing from the state during deletion, the provider will gracefully attempt to fall back to the `STRIPE_SECRET_KEY` or `STRIPE_API_KEY` environment variables.
|
|
80
|
+
|
|
81
|
+
If no API key is found in the state or environment variables, the provider will log a warning and **skip the Stripe deactivation (NoOp)** to allow Pulumi to successfully finish deleting the resource from its internal state without crashing.
|
|
82
|
+
|
|
83
|
+
## Development
|
|
84
|
+
|
|
85
|
+
If you wish to contribute or build the project locally, please use the provided `Makefile`.
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Install dependencies
|
|
89
|
+
make install
|
|
90
|
+
|
|
91
|
+
# Build the TypeScript source
|
|
92
|
+
make build
|
|
93
|
+
|
|
94
|
+
# Run the Vitest test suite
|
|
95
|
+
make test
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Contributing & Releases
|
|
99
|
+
|
|
100
|
+
This project uses [Release Please](https://github.com/googleapis/release-please) to automate versioning, changelog generation, and NPM publishing.
|
|
101
|
+
|
|
102
|
+
When contributing, you **must** format your commit messages using [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) (e.g., `feat: add new resource`, `fix: handle edge case`).
|
|
103
|
+
|
|
104
|
+
When your PR is merged into `main`, Release Please will automatically open a "Release PR" bumping the semantic version and updating the `CHANGELOG.md`. Merging that Release PR will trigger the GitHub Action to cut a GitHub Release and publish the new version to NPM.
|
|
@@ -39,9 +39,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.PaymentLink = exports.StripePaymentLinkProvider = void 0;
|
|
40
40
|
const pulumi = __importStar(require("@pulumi/pulumi"));
|
|
41
41
|
const stripe_1 = __importDefault(require("stripe"));
|
|
42
|
+
const utils_1 = require("./utils");
|
|
42
43
|
class StripePaymentLinkProvider {
|
|
43
44
|
async create(inputs) {
|
|
44
|
-
const stripe = new stripe_1.default(inputs.apiKey
|
|
45
|
+
const stripe = new stripe_1.default(inputs.apiKey);
|
|
45
46
|
const paymentLink = await stripe.paymentLinks.create({
|
|
46
47
|
line_items: [{
|
|
47
48
|
price: inputs.priceId,
|
|
@@ -93,8 +94,7 @@ class StripePaymentLinkProvider {
|
|
|
93
94
|
};
|
|
94
95
|
}
|
|
95
96
|
async delete(id, props) {
|
|
96
|
-
|
|
97
|
-
await stripe.paymentLinks.update(id, { active: false });
|
|
97
|
+
await (0, utils_1.safeDeactivateStripeResource)("StripePaymentLinkProvider", id, props.apiKey, async (stripe) => { await stripe.paymentLinks.update(id, { active: false }); });
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
exports.StripePaymentLinkProvider = StripePaymentLinkProvider;
|
package/dist/StripePrice.js
CHANGED
|
@@ -39,9 +39,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.Price = exports.StripePriceProvider = void 0;
|
|
40
40
|
const pulumi = __importStar(require("@pulumi/pulumi"));
|
|
41
41
|
const stripe_1 = __importDefault(require("stripe"));
|
|
42
|
+
const utils_1 = require("./utils");
|
|
42
43
|
class StripePriceProvider {
|
|
43
44
|
async create(inputs) {
|
|
44
|
-
const stripe = new stripe_1.default(inputs.apiKey
|
|
45
|
+
const stripe = new stripe_1.default(inputs.apiKey);
|
|
45
46
|
const price = await stripe.prices.create({
|
|
46
47
|
product: inputs.productId,
|
|
47
48
|
unit_amount: inputs.unitAmount,
|
|
@@ -71,8 +72,7 @@ class StripePriceProvider {
|
|
|
71
72
|
};
|
|
72
73
|
}
|
|
73
74
|
async delete(id, props) {
|
|
74
|
-
|
|
75
|
-
await stripe.prices.update(id, { active: false });
|
|
75
|
+
await (0, utils_1.safeDeactivateStripeResource)("StripePriceProvider", id, props.apiKey, async (stripe) => { await stripe.prices.update(id, { active: false }); });
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
exports.StripePriceProvider = StripePriceProvider;
|
package/dist/StripeProduct.js
CHANGED
|
@@ -39,9 +39,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.Product = exports.StripeProductProvider = void 0;
|
|
40
40
|
const pulumi = __importStar(require("@pulumi/pulumi"));
|
|
41
41
|
const stripe_1 = __importDefault(require("stripe"));
|
|
42
|
+
const utils_1 = require("./utils");
|
|
42
43
|
class StripeProductProvider {
|
|
43
44
|
async create(inputs) {
|
|
44
|
-
const stripe = new stripe_1.default(inputs.apiKey
|
|
45
|
+
const stripe = new stripe_1.default(inputs.apiKey);
|
|
45
46
|
const product = await stripe.products.create({
|
|
46
47
|
name: inputs.name,
|
|
47
48
|
description: inputs.description,
|
|
@@ -71,7 +72,7 @@ class StripeProductProvider {
|
|
|
71
72
|
};
|
|
72
73
|
}
|
|
73
74
|
async update(id, olds, news) {
|
|
74
|
-
const stripe = new stripe_1.default(news.apiKey
|
|
75
|
+
const stripe = new stripe_1.default(news.apiKey);
|
|
75
76
|
await stripe.products.update(id, {
|
|
76
77
|
name: news.name,
|
|
77
78
|
description: news.description,
|
|
@@ -85,8 +86,7 @@ class StripeProductProvider {
|
|
|
85
86
|
};
|
|
86
87
|
}
|
|
87
88
|
async delete(id, props) {
|
|
88
|
-
|
|
89
|
-
await stripe.products.update(id, { active: false });
|
|
89
|
+
await (0, utils_1.safeDeactivateStripeResource)("StripeProductProvider", id, props.apiKey, async (stripe) => { await stripe.products.update(id, { active: false }); });
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
exports.StripeProductProvider = StripeProductProvider;
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
/**
|
|
3
|
+
* Safely deactivates a Stripe resource during Pulumi deletion.
|
|
4
|
+
* Falls back to environment variables if the apiKey is missing from state.
|
|
5
|
+
* Prevents Pulumi state engine crashes when a resource cannot be deactivated.
|
|
6
|
+
*/
|
|
7
|
+
export declare function safeDeactivateStripeResource(providerName: string, resourceId: string, apiKeyFromState: string | undefined, deactivateFn: (stripe: Stripe) => Promise<void>): Promise<void>;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.safeDeactivateStripeResource = safeDeactivateStripeResource;
|
|
7
|
+
const stripe_1 = __importDefault(require("stripe"));
|
|
8
|
+
/**
|
|
9
|
+
* Safely deactivates a Stripe resource during Pulumi deletion.
|
|
10
|
+
* Falls back to environment variables if the apiKey is missing from state.
|
|
11
|
+
* Prevents Pulumi state engine crashes when a resource cannot be deactivated.
|
|
12
|
+
*/
|
|
13
|
+
async function safeDeactivateStripeResource(providerName, resourceId, apiKeyFromState, deactivateFn) {
|
|
14
|
+
const apiKey = apiKeyFromState || process.env.STRIPE_SECRET_KEY || process.env.STRIPE_API_KEY;
|
|
15
|
+
if (!apiKey) {
|
|
16
|
+
console.warn(`[${providerName}] Skipping deletion of ${resourceId} due to missing apiKey in state and environment variables.`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
const stripe = new stripe_1.default(apiKey);
|
|
21
|
+
await deactivateFn(stripe);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
console.warn(`[${providerName}] Failed to deactivate ${resourceId}:`, error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Troubleshooting Stuck State in Pulumi Dynamic Providers
|
|
2
|
+
|
|
3
|
+
## The Problem
|
|
4
|
+
|
|
5
|
+
When using `pulumi-dynamic-stripe` versions **0.0.1** and **0.0.2**, you might encounter an issue where Pulumi fails to delete a Stripe resource (e.g., a Payment Link) during a `pulumi up`, `pulumi destroy`, or replacement operation.
|
|
6
|
+
|
|
7
|
+
The stack will show the resource as `deleting failed` with an error similar to this:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
Error: Neither apiKey nor config.authenticator provided
|
|
11
|
+
at Stripe._setAuthenticator (node_modules/stripe/src/stripe.core.ts:1319:13)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Why This Happens
|
|
15
|
+
|
|
16
|
+
This is fundamentally caused by how Pulumi Dynamic Providers function under the hood.
|
|
17
|
+
|
|
18
|
+
When you deploy a resource using a Dynamic Provider, Pulumi takes the **JavaScript source code of your provider methods** (like `create`, `update`, `diff`, and importantly `delete`) and **serializes the compiled code directly into the Pulumi state file in the cloud**.
|
|
19
|
+
|
|
20
|
+
In versions `0.0.1` and `0.0.2` of this library, the `delete` method had a bug where it crashed if it couldn't find an API key in the Pulumi state (due to how state diffs and replacement work). Although we patched this bug in the library's source code by adding environment variable fallbacks and `try/catch` handlers, **Pulumi does not use your newly updated local code to delete older resources.**
|
|
21
|
+
|
|
22
|
+
Instead, Pulumi downloads the *old, buggy, serialized `delete` function* that was saved in the cloud during the resource's initial creation, and attempts to run it. When that old code inevitably crashes, the resource gets stuck in a perpetual `deleting failed` state.
|
|
23
|
+
|
|
24
|
+
## The Solution
|
|
25
|
+
|
|
26
|
+
Since you cannot fix an old resource's deletion behavior by updating your local provider package, you must manually excise the corrupted resource from the Pulumi state.
|
|
27
|
+
|
|
28
|
+
### Step 1: Export the Pulumi State
|
|
29
|
+
First, export your stack's state to a local JSON file:
|
|
30
|
+
```bash
|
|
31
|
+
pulumi stack export > state.json
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Step 2: Identify and Remove the Corrupted Resource
|
|
35
|
+
Open `state.json` in a text editor and search for the resource that is stuck. You are looking for a JSON object in the `deployment.resources` array that matches your resource's URN and has `"delete": true` set.
|
|
36
|
+
|
|
37
|
+
For example, look for something like this and **delete the entire JSON object from the array**:
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"urn": "urn:pulumi:prod.quiz.crenet-games::quizai-infrastructure::pulumi-nodejs:dynamic:Resource::sparks-link",
|
|
41
|
+
"custom": true,
|
|
42
|
+
"delete": true,
|
|
43
|
+
"id": "plink_1U8pkf1i5elBtkZIXxmnduNm",
|
|
44
|
+
"type": "pulumi-nodejs:dynamic:Resource",
|
|
45
|
+
"inputs": { ... }
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
*Alternatively, you can script this step using Node or `jq`.*
|
|
50
|
+
|
|
51
|
+
### Step 3: Import the Fixed State
|
|
52
|
+
Once the offending resource object is removed from the JSON array, save the file and import it back to Pulumi:
|
|
53
|
+
```bash
|
|
54
|
+
pulumi stack import --file state.json
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Step 4: Re-run Pulumi Up
|
|
58
|
+
Now that the stuck state is gone, Pulumi will no longer attempt to run the broken deletion hook. Run your update command:
|
|
59
|
+
```bash
|
|
60
|
+
pulumi up -y
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Moving forward, any new resources created by the updated provider will have the new, robust, crash-free `delete` function serialized into the state, permanently solving this issue.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@efaj/pulumi-dynamic-stripe",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0-9ccdc21",
|
|
4
4
|
"description": "A Pulumi Dynamic Provider for Stripe, supporting modern features like Payment Links.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pulumi",
|
|
@@ -22,17 +22,15 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "tsc",
|
|
24
24
|
"prepare": "tsc",
|
|
25
|
-
"test": "
|
|
25
|
+
"test": "vitest run"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@pulumi/pulumi": "^3.259.0",
|
|
29
29
|
"stripe": "^22.6.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@types/jest": "^30.0.0",
|
|
33
32
|
"@types/node": "^26.4.0",
|
|
34
|
-
"
|
|
35
|
-
"ts-jest": "^29.4.12",
|
|
33
|
+
"vitest": "^2.0.5",
|
|
36
34
|
"typescript": "^6.0.3"
|
|
37
35
|
}
|
|
38
|
-
}
|
|
36
|
+
}
|
package/tests/index.test.ts
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
import { StripePaymentLinkProvider, StripeProductProvider, StripePriceProvider } from "../src/index";
|
|
2
2
|
import Stripe from "stripe";
|
|
3
|
-
import { describe, it, expect,
|
|
3
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
4
|
+
import type { Mock } from 'vitest';
|
|
4
5
|
|
|
5
6
|
// Mock the Stripe SDK
|
|
6
|
-
|
|
7
|
+
vi.mock("stripe");
|
|
7
8
|
|
|
8
|
-
const MockedStripe = Stripe as
|
|
9
|
+
const MockedStripe = Stripe as any;
|
|
9
10
|
|
|
10
11
|
describe("Stripe Providers", () => {
|
|
11
|
-
let mockPaymentLinksCreate:
|
|
12
|
-
let mockPaymentLinksUpdate:
|
|
13
|
-
let mockProductsCreate:
|
|
14
|
-
let mockProductsUpdate:
|
|
15
|
-
let mockPricesCreate:
|
|
16
|
-
let mockPricesUpdate:
|
|
12
|
+
let mockPaymentLinksCreate: Mock<any>;
|
|
13
|
+
let mockPaymentLinksUpdate: Mock<any>;
|
|
14
|
+
let mockProductsCreate: Mock<any>;
|
|
15
|
+
let mockProductsUpdate: Mock<any>;
|
|
16
|
+
let mockPricesCreate: Mock<any>;
|
|
17
|
+
let mockPricesUpdate: Mock<any>;
|
|
17
18
|
|
|
18
19
|
beforeEach(() => {
|
|
19
20
|
// Reset mocks before each test
|
|
20
|
-
|
|
21
|
+
vi.clearAllMocks();
|
|
21
22
|
|
|
22
|
-
mockPaymentLinksCreate =
|
|
23
|
-
mockPaymentLinksUpdate =
|
|
23
|
+
mockPaymentLinksCreate = vi.fn<any>().mockResolvedValue({ id: "plink_123", url: "https://stripe.com/plink_123" } as any);
|
|
24
|
+
mockPaymentLinksUpdate = vi.fn<any>().mockResolvedValue({ id: "plink_123" } as any);
|
|
24
25
|
|
|
25
|
-
mockProductsCreate =
|
|
26
|
-
mockProductsUpdate =
|
|
26
|
+
mockProductsCreate = vi.fn<any>().mockResolvedValue({ id: "prod_123" } as any);
|
|
27
|
+
mockProductsUpdate = vi.fn<any>().mockResolvedValue({ id: "prod_123" } as any);
|
|
27
28
|
|
|
28
|
-
mockPricesCreate =
|
|
29
|
-
mockPricesUpdate =
|
|
29
|
+
mockPricesCreate = vi.fn<any>().mockResolvedValue({ id: "price_123" } as any);
|
|
30
|
+
mockPricesUpdate = vi.fn<any>().mockResolvedValue({ id: "price_123" } as any);
|
|
30
31
|
|
|
31
32
|
// Setup the mocked Stripe instance's internal resource objects
|
|
32
33
|
MockedStripe.mockImplementation(() => {
|
|
@@ -102,7 +103,7 @@ describe("Stripe Providers", () => {
|
|
|
102
103
|
expect(result.outs?.paymentLinkId).toBe("plink_123");
|
|
103
104
|
expect(result.outs?.url).toBe("https://stripe.com/plink_123");
|
|
104
105
|
|
|
105
|
-
expect(MockedStripe).toHaveBeenCalledWith(inputs.apiKey
|
|
106
|
+
expect(MockedStripe).toHaveBeenCalledWith(inputs.apiKey);
|
|
106
107
|
expect(mockPaymentLinksCreate).toHaveBeenCalledWith(expectedPayload);
|
|
107
108
|
});
|
|
108
109
|
});
|
|
@@ -141,9 +142,85 @@ describe("Stripe Providers", () => {
|
|
|
141
142
|
});
|
|
142
143
|
|
|
143
144
|
describe("delete", () => {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
const deleteCases = [
|
|
146
|
+
{
|
|
147
|
+
name: "apiKey is in state",
|
|
148
|
+
state: { apiKey: "sk_test_state" },
|
|
149
|
+
envSecret: undefined,
|
|
150
|
+
envApi: undefined,
|
|
151
|
+
mockError: false,
|
|
152
|
+
expectStripeInit: "sk_test_state",
|
|
153
|
+
expectUpdate: true,
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
name: "apiKey is missing, falls back to STRIPE_SECRET_KEY",
|
|
157
|
+
state: {},
|
|
158
|
+
envSecret: "sk_test_secret",
|
|
159
|
+
envApi: undefined,
|
|
160
|
+
mockError: false,
|
|
161
|
+
expectStripeInit: "sk_test_secret",
|
|
162
|
+
expectUpdate: true,
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
name: "apiKey is missing, falls back to STRIPE_API_KEY",
|
|
166
|
+
state: {},
|
|
167
|
+
envSecret: undefined,
|
|
168
|
+
envApi: "sk_test_api",
|
|
169
|
+
mockError: false,
|
|
170
|
+
expectStripeInit: "sk_test_api",
|
|
171
|
+
expectUpdate: true,
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: "apiKey is missing everywhere (NoOp)",
|
|
175
|
+
state: {},
|
|
176
|
+
envSecret: undefined,
|
|
177
|
+
envApi: undefined,
|
|
178
|
+
mockError: false,
|
|
179
|
+
expectStripeInit: null,
|
|
180
|
+
expectUpdate: false,
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
name: "Stripe SDK throws an error (caught safely)",
|
|
184
|
+
state: { apiKey: "sk_test_error" },
|
|
185
|
+
envSecret: undefined,
|
|
186
|
+
envApi: undefined,
|
|
187
|
+
mockError: true,
|
|
188
|
+
expectStripeInit: "sk_test_error",
|
|
189
|
+
expectUpdate: true,
|
|
190
|
+
}
|
|
191
|
+
];
|
|
192
|
+
|
|
193
|
+
it.each(deleteCases)("should handle deletion when $name", async ({ state, envSecret, envApi, mockError, expectStripeInit, expectUpdate }) => {
|
|
194
|
+
const originalSecret = process.env.STRIPE_SECRET_KEY;
|
|
195
|
+
const originalApi = process.env.STRIPE_API_KEY;
|
|
196
|
+
|
|
197
|
+
if (envSecret !== undefined) process.env.STRIPE_SECRET_KEY = envSecret;
|
|
198
|
+
else delete process.env.STRIPE_SECRET_KEY;
|
|
199
|
+
|
|
200
|
+
if (envApi !== undefined) process.env.STRIPE_API_KEY = envApi;
|
|
201
|
+
else delete process.env.STRIPE_API_KEY;
|
|
202
|
+
|
|
203
|
+
if (mockError) {
|
|
204
|
+
mockPaymentLinksUpdate.mockRejectedValueOnce(new Error("Stripe error"));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
await provider.delete("plink_123", state as any);
|
|
208
|
+
|
|
209
|
+
if (expectStripeInit) {
|
|
210
|
+
expect(MockedStripe).toHaveBeenCalledWith(expectStripeInit);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (expectUpdate) {
|
|
214
|
+
expect(mockPaymentLinksUpdate).toHaveBeenCalledWith("plink_123", { active: false });
|
|
215
|
+
} else {
|
|
216
|
+
expect(mockPaymentLinksUpdate).not.toHaveBeenCalled();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (originalSecret !== undefined) process.env.STRIPE_SECRET_KEY = originalSecret;
|
|
220
|
+
else delete process.env.STRIPE_SECRET_KEY;
|
|
221
|
+
|
|
222
|
+
if (originalApi !== undefined) process.env.STRIPE_API_KEY = originalApi;
|
|
223
|
+
else delete process.env.STRIPE_API_KEY;
|
|
147
224
|
});
|
|
148
225
|
});
|
|
149
226
|
});
|
|
@@ -187,7 +264,7 @@ describe("Stripe Providers", () => {
|
|
|
187
264
|
expect(result.id).toBe("prod_123");
|
|
188
265
|
expect(result.outs?.productId).toBe("prod_123");
|
|
189
266
|
|
|
190
|
-
expect(MockedStripe).toHaveBeenCalledWith(inputs.apiKey
|
|
267
|
+
expect(MockedStripe).toHaveBeenCalledWith(inputs.apiKey);
|
|
191
268
|
expect(mockProductsCreate).toHaveBeenCalledWith(expectedPayload);
|
|
192
269
|
});
|
|
193
270
|
});
|
|
@@ -240,9 +317,85 @@ describe("Stripe Providers", () => {
|
|
|
240
317
|
});
|
|
241
318
|
|
|
242
319
|
describe("delete", () => {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
320
|
+
const deleteCases = [
|
|
321
|
+
{
|
|
322
|
+
name: "apiKey is in state",
|
|
323
|
+
state: { apiKey: "sk_test_state" },
|
|
324
|
+
envSecret: undefined,
|
|
325
|
+
envApi: undefined,
|
|
326
|
+
mockError: false,
|
|
327
|
+
expectStripeInit: "sk_test_state",
|
|
328
|
+
expectUpdate: true,
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
name: "apiKey is missing, falls back to STRIPE_SECRET_KEY",
|
|
332
|
+
state: {},
|
|
333
|
+
envSecret: "sk_test_secret",
|
|
334
|
+
envApi: undefined,
|
|
335
|
+
mockError: false,
|
|
336
|
+
expectStripeInit: "sk_test_secret",
|
|
337
|
+
expectUpdate: true,
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
name: "apiKey is missing, falls back to STRIPE_API_KEY",
|
|
341
|
+
state: {},
|
|
342
|
+
envSecret: undefined,
|
|
343
|
+
envApi: "sk_test_api",
|
|
344
|
+
mockError: false,
|
|
345
|
+
expectStripeInit: "sk_test_api",
|
|
346
|
+
expectUpdate: true,
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
name: "apiKey is missing everywhere (NoOp)",
|
|
350
|
+
state: {},
|
|
351
|
+
envSecret: undefined,
|
|
352
|
+
envApi: undefined,
|
|
353
|
+
mockError: false,
|
|
354
|
+
expectStripeInit: null,
|
|
355
|
+
expectUpdate: false,
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
name: "Stripe SDK throws an error (caught safely)",
|
|
359
|
+
state: { apiKey: "sk_test_error" },
|
|
360
|
+
envSecret: undefined,
|
|
361
|
+
envApi: undefined,
|
|
362
|
+
mockError: true,
|
|
363
|
+
expectStripeInit: "sk_test_error",
|
|
364
|
+
expectUpdate: true,
|
|
365
|
+
}
|
|
366
|
+
];
|
|
367
|
+
|
|
368
|
+
it.each(deleteCases)("should handle deletion when $name", async ({ state, envSecret, envApi, mockError, expectStripeInit, expectUpdate }) => {
|
|
369
|
+
const originalSecret = process.env.STRIPE_SECRET_KEY;
|
|
370
|
+
const originalApi = process.env.STRIPE_API_KEY;
|
|
371
|
+
|
|
372
|
+
if (envSecret !== undefined) process.env.STRIPE_SECRET_KEY = envSecret;
|
|
373
|
+
else delete process.env.STRIPE_SECRET_KEY;
|
|
374
|
+
|
|
375
|
+
if (envApi !== undefined) process.env.STRIPE_API_KEY = envApi;
|
|
376
|
+
else delete process.env.STRIPE_API_KEY;
|
|
377
|
+
|
|
378
|
+
if (mockError) {
|
|
379
|
+
mockProductsUpdate.mockRejectedValueOnce(new Error("Stripe error"));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
await provider.delete("prod_123", state as any);
|
|
383
|
+
|
|
384
|
+
if (expectStripeInit) {
|
|
385
|
+
expect(MockedStripe).toHaveBeenCalledWith(expectStripeInit);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (expectUpdate) {
|
|
389
|
+
expect(mockProductsUpdate).toHaveBeenCalledWith("prod_123", { active: false });
|
|
390
|
+
} else {
|
|
391
|
+
expect(mockProductsUpdate).not.toHaveBeenCalled();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (originalSecret !== undefined) process.env.STRIPE_SECRET_KEY = originalSecret;
|
|
395
|
+
else delete process.env.STRIPE_SECRET_KEY;
|
|
396
|
+
|
|
397
|
+
if (originalApi !== undefined) process.env.STRIPE_API_KEY = originalApi;
|
|
398
|
+
else delete process.env.STRIPE_API_KEY;
|
|
246
399
|
});
|
|
247
400
|
});
|
|
248
401
|
});
|
|
@@ -274,7 +427,7 @@ describe("Stripe Providers", () => {
|
|
|
274
427
|
expect(result.id).toBe("price_123");
|
|
275
428
|
expect(result.outs?.priceId).toBe("price_123");
|
|
276
429
|
|
|
277
|
-
expect(MockedStripe).toHaveBeenCalledWith(inputs.apiKey
|
|
430
|
+
expect(MockedStripe).toHaveBeenCalledWith(inputs.apiKey);
|
|
278
431
|
expect(mockPricesCreate).toHaveBeenCalledWith(expectedPayload);
|
|
279
432
|
});
|
|
280
433
|
});
|
|
@@ -313,9 +466,85 @@ describe("Stripe Providers", () => {
|
|
|
313
466
|
});
|
|
314
467
|
|
|
315
468
|
describe("delete", () => {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
469
|
+
const deleteCases = [
|
|
470
|
+
{
|
|
471
|
+
name: "apiKey is in state",
|
|
472
|
+
state: { apiKey: "sk_test_state" },
|
|
473
|
+
envSecret: undefined,
|
|
474
|
+
envApi: undefined,
|
|
475
|
+
mockError: false,
|
|
476
|
+
expectStripeInit: "sk_test_state",
|
|
477
|
+
expectUpdate: true,
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
name: "apiKey is missing, falls back to STRIPE_SECRET_KEY",
|
|
481
|
+
state: {},
|
|
482
|
+
envSecret: "sk_test_secret",
|
|
483
|
+
envApi: undefined,
|
|
484
|
+
mockError: false,
|
|
485
|
+
expectStripeInit: "sk_test_secret",
|
|
486
|
+
expectUpdate: true,
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
name: "apiKey is missing, falls back to STRIPE_API_KEY",
|
|
490
|
+
state: {},
|
|
491
|
+
envSecret: undefined,
|
|
492
|
+
envApi: "sk_test_api",
|
|
493
|
+
mockError: false,
|
|
494
|
+
expectStripeInit: "sk_test_api",
|
|
495
|
+
expectUpdate: true,
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
name: "apiKey is missing everywhere (NoOp)",
|
|
499
|
+
state: {},
|
|
500
|
+
envSecret: undefined,
|
|
501
|
+
envApi: undefined,
|
|
502
|
+
mockError: false,
|
|
503
|
+
expectStripeInit: null,
|
|
504
|
+
expectUpdate: false,
|
|
505
|
+
},
|
|
506
|
+
{
|
|
507
|
+
name: "Stripe SDK throws an error (caught safely)",
|
|
508
|
+
state: { apiKey: "sk_test_error" },
|
|
509
|
+
envSecret: undefined,
|
|
510
|
+
envApi: undefined,
|
|
511
|
+
mockError: true,
|
|
512
|
+
expectStripeInit: "sk_test_error",
|
|
513
|
+
expectUpdate: true,
|
|
514
|
+
}
|
|
515
|
+
];
|
|
516
|
+
|
|
517
|
+
it.each(deleteCases)("should handle deletion when $name", async ({ state, envSecret, envApi, mockError, expectStripeInit, expectUpdate }) => {
|
|
518
|
+
const originalSecret = process.env.STRIPE_SECRET_KEY;
|
|
519
|
+
const originalApi = process.env.STRIPE_API_KEY;
|
|
520
|
+
|
|
521
|
+
if (envSecret !== undefined) process.env.STRIPE_SECRET_KEY = envSecret;
|
|
522
|
+
else delete process.env.STRIPE_SECRET_KEY;
|
|
523
|
+
|
|
524
|
+
if (envApi !== undefined) process.env.STRIPE_API_KEY = envApi;
|
|
525
|
+
else delete process.env.STRIPE_API_KEY;
|
|
526
|
+
|
|
527
|
+
if (mockError) {
|
|
528
|
+
mockPricesUpdate.mockRejectedValueOnce(new Error("Stripe error"));
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
await provider.delete("price_123", state as any);
|
|
532
|
+
|
|
533
|
+
if (expectStripeInit) {
|
|
534
|
+
expect(MockedStripe).toHaveBeenCalledWith(expectStripeInit);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (expectUpdate) {
|
|
538
|
+
expect(mockPricesUpdate).toHaveBeenCalledWith("price_123", { active: false });
|
|
539
|
+
} else {
|
|
540
|
+
expect(mockPricesUpdate).not.toHaveBeenCalled();
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (originalSecret !== undefined) process.env.STRIPE_SECRET_KEY = originalSecret;
|
|
544
|
+
else delete process.env.STRIPE_SECRET_KEY;
|
|
545
|
+
|
|
546
|
+
if (originalApi !== undefined) process.env.STRIPE_API_KEY = originalApi;
|
|
547
|
+
else delete process.env.STRIPE_API_KEY;
|
|
319
548
|
});
|
|
320
549
|
});
|
|
321
550
|
});
|
package/vitest.config.ts
ADDED