Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe Plugin does not always use the passed in "stripeSecretKey" #10668

Closed
cgilly2fast opened this issue Jan 19, 2025 · 0 comments · Fixed by #10671
Closed

Stripe Plugin does not always use the passed in "stripeSecretKey" #10668

cgilly2fast opened this issue Jan 19, 2025 · 0 comments · Fixed by #10671
Assignees

Comments

@cgilly2fast
Copy link

cgilly2fast commented Jan 19, 2025

Describe the Bug

Title: Stripe Plugin Environment Variable Handling for Non-dotenv Configurations

Description
The current implementation of the Stripe plugin has a rigid dependency on process.env.STRIPE_SECRET_KEY, assuming it's set via dotenv. This causes issues when using alternative deployment configurations like SST, where environment variables are injected differently.

Current Behavior

  1. The plugin currently looks for process.env.STRIPE_SECRET_KEY in three instances, ignoring the Stripe key that's passed into the plugin configuration.
  2. This forces users to implement hacky workarounds, such as manually setting process.env.STRIPE_SECRET_KEY at the top of the payload config, which can lead to errors and is not a robust solution.

Current places Payload does not use the passed in Stripe secret key but only looks for the env var:

https://github.com/payloadcms/payload/blob/main/packages/plugin-stripe/src/hooks/createNewInStripe.ts

https://github.com/payloadcms/payload/blob/main/packages/plugin-stripe/src/hooks/deleteFromStripe.ts

https://github.com/payloadcms/payload/blob/main/packages/plugin-stripe/src/hooks/syncExistingWithStripe.ts

Ideal Behavior
The plugin should:

  1. Prioritize the Stripe key passed through the plugin configuration
  2. Fall back to or not use process.env.STRIPE_SECRET_KEY
  3. Support different environment variable injection methods beyond dotenv

Proposed Solution
Refactor the plugin to implement a more flexible environment variable handling system:

// Example implementation
const getStripeKey = (config) => {
  if (config.stripeKey) {
    return config.stripeKey;
  }
  
  if (process.env.STRIPE_SECRET_KEY) {
    return process.env.STRIPE_SECRET_KEY;
  }
  
  throw new Error('No Stripe key provided. Please provide it either through plugin configuration or STRIPE_KEY environment variable.');
}

Use Case
When using SST (Serverless Stack) for deployment, environment variables are injected at runtime, and we cannot guarantee they'll be available through process.env.STRIPE_KEY. The current implementation makes it difficult to use the plugin in this and similar scenarios.

Impact
This issue affects anyone using the Stripe plugin with:

  • SST
  • Other serverless platforms with custom environment variable injection
  • Custom environment variable management solutions
  • Container-based deployments with different env variable patterns

Let me know if you need any additional information or clarification.

Link to the code that reproduces this issue

https://github.com/payloadcms/payload

Reproduction Steps

Example stripe plugin set up produces error:

 stripePlugin({
      stripeSecretKey: Resource.StripeSecretKey.value,
      sync: [
        {
          collection: 'billings',
          stripeResourceType: 'customers',
          stripeResourceTypeSingular: 'customer',
          fields: [
            {
              fieldPath: 'firm',
              stripeProperty: 'name',
            },
          ],
        },
      ],
    })

Which area(s) are affected? (Select all that apply)

plugin: stripe

Environment Info

**Additional Context**
- Using Payload CMS version: 3.17.1
- Using @payloadcms/plugin-stripe version: 3.17.1
- Deployment platform: SST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants