Skip to main content

Overview

File: src/tools/PaymentTool.ts Demonstrates payment integration using Stripe API with secure environment variable management.

Complete Code

Key Concepts

1. Environment Variables

Never hardcode API keys!

2. Validation

Always check environment variables exist:

3. Error Handling

Handle external API failures gracefully:

4. Amount Conversion

Stripe uses cents, not dollars:

Setup Required

1. Get Stripe API Key

  1. Create account at https://stripe.com
  2. Go to Dashboard → Developers → API Keys
  3. Copy “Secret key” (starts with sk_test_)

2. Add to .env File

Create .env in project root:

3. Test

Select create_payment_link:
  • Amount: 29.99
  • Currency: USD
  • Description: Product Purchase

Customization Ideas

Add Customer Info

Add Success/Cancel URLs

Add Metadata

Other Payment Providers

Same pattern works for other providers:

Security Best Practices

What You’ll Learn

Environment Variables

Secure secret management

External Integration

Third-party API integration

Payment Processing

Real payment workflows

Error Handling

Graceful failure handling

Next Steps

Environment Variables

Complete guide to configuration

Environment API

API reference for env()