# Stripe

## Stripe Setup Tutorial

Fast Phoenix integrates seamlessly with Stripe to handle subscriptions and payments. Follow these steps to configure Stripe for your project.

***

**Step 1: Prepare Your Environment Variables**

Fast Phoenix uses environment variables to manage Stripe configuration. Open your `.env` file and ensure the following variables are set:

```
# Stripe API Configuration
STRIPE_API_KEY=sk_test_your_stripe_api_key
STRIPE_SIGNING_SECRET=your_signing_secret
STRIPE_API_BASE_URL=http://localhost:4000

# Product and Pricing IDs
STRIPE_PRO_PRICE_ID=price_pro_id
STRIPE_PRO_PRICE=30
STRIPE_ENTERPRISE_PRICE_ID=price_enterprise_id
STRIPE_ENTERPRISE_PRICE=100

STRIPE_PORTAL_ID=portal_test_id
```

Replace the placeholder values with your actual Stripe credentials and product/pricing details.

***

**Step 2: Create Products and Prices in Stripe**

1. **Log in to Stripe Dashboard**\
   Access your [Stripe Dashboard](https://dashboard.stripe.com/).
2. **Create Two Products**
   * **Pro Plan**
     * Name: Pro
     * Price: $30
     * Billing: Monthly
   * **Enterprise Plan**
     * Name: Enterprise
     * Price: $100
     * Billing: Monthly
3. **Copy Product Price IDs**\
   After creating the products, navigate to the pricing details and copy the *Price IDs* for each product. Replace the `STRIPE_PRO_PRICE_ID` and `STRIPE_ENTERPRISE_PRICE_ID` values in your `.env` file with these IDs.

***

**Step 3: Set Up Stripe Webhooks**

Stripe uses webhooks to notify your application about important events like successful payments.

1. **Run Your Application**\
   Start your local development server:

   ```bash
   mix phx.server
   ```
2. **Install Stripe CLI**\
   Download and install the [Stripe CLI](https://stripe.com/docs/stripe-cli) to set up webhook forwarding.
3. **Forward Events to Your Application**\
   Use the Stripe CLI to forward webhook events to your local server:

   ```bash
   stripe listen --forward-to localhost:4000/webhook
   ```

   Copy the generated signing secret and update your `.env` file’s `STRIPE_SIGNING_SECRET`.
4. **Set Up Webhook Endpoint in Stripe For Prod**
   * Navigate to the Webhooks section of the Stripe Dashboard.
   * Add a new endpoint pointing to `https://your-domain.com/webhooks/stripe` for production.

***

**Step 4: Test Your Setup**

1. **Test Subscription Creation**\
   Use Stripe's test card numbers (e.g., `4242 4242 4242 4242`) to simulate subscriptions.

***

**Step 5: Optional - Enable Customer Portal**

1. In the Stripe Dashboard, enable the [Customer Portal](https://dashboard.stripe.com/settings/billing/portal).
2. Copy the Customer Portal URL and save it in your `.env` as `STRIPE_PORTAL_ID`.

***

### Extra:

We have written a stripe\_helpers.ex module that creates checkout sessions for new subscribers and upgrading/downgrading subscribers, and a request helper function for hitting up Stripe's API. For example:&#x20;

```elixir
price_id = StripeHelpers.get_price_id(session_id)

# email being the logged in user.
case StripeHelpers.create_checkout_session(price_id, email) do
      {:ok, url} ->
        redirect(conn, external: url)
      {:error, reason} ->
        IO.inspect(reason)
        redirect(conn, to: "/")
      _ ->
        redirect(conn, to: "/")
    end
```

Your Stripe setup is now complete! You can start accepting payments, managing subscriptions, and leveraging Fast Phoenix’s built-in features to streamline your billing processes. 🚀
