1.0.3 • Published 7 months ago
opengig-stripe v1.0.3
Stripe Payment Wrapper
A TypeScript wrapper for Stripe payment services that simplifies working with Checkout Sessions and Payment Intents.
Features
- 🛒 Easy-to-use Stripe Checkout session creation
- 💳 Simplified Payment Intent management
- 🔒 Type-safe interfaces
- 💰 Support for subscriptions and one-time payments
- 🌐 Currency customization
- 📦 Metadata support
Installation
npm install stripe-payment-wrapper
Quick Start
import { createStripeClient } from 'stripe-payment-wrapper';
const stripe = createStripeClient('your_stripe_secret_key');
// Create a checkout session
const checkoutSession = await stripe.checkout.createPaymentSession({
priceId: 'price_H5ggYwtDq4fbrJ',
successUrl: 'https://example.com/success',
cancelUrl: 'https://example.com/cancel'
});
// Create a payment intent
const paymentIntent = await stripe.paymentIntent.create({
amount: 2000, // $20.00
currency: 'usd'
});
Usage Examples
Checkout Sessions
Create a Subscription Checkout
const session = await stripe.checkout.createPaymentSession({
priceId: 'price_H5ggYwtDq4fbrJ',
customerId: 'cus_123456',
successUrl: 'https://example.com/success',
cancelUrl: 'https://example.com/cancel',
mode: 'subscription',
quantity: 1,
metadata: {
orderId: '6735'
}
});
Create a One-Time Payment Checkout
const session = await stripe.checkout.createOneTimePaymentSession({
amount: 2000, // $20.00
currency: 'usd',
successUrl: 'https://example.com/success',
cancelUrl: 'https://example.com/cancel',
metadata: {
orderId: '6735'
}
});
Payment Intents
Create a Payment Intent
const intent = await stripe.paymentIntent.create({
amount: 2000, // $20.00
currency: 'usd',
customerId: 'cus_123456',
metadata: {
orderId: '6735'
}
});
Confirm a Payment Intent
const confirmedIntent = await stripe.paymentIntent.confirm(
'pi_123456',
'pm_123456'
);
Retrieve a Payment Intent
const intent = await stripe.paymentIntent.retrieve('pi_123456');
Update a Payment Intent
const updatedIntent = await stripe.paymentIntent.update('pi_123456', {
metadata: {
status: 'processed'
}
});
Cancel a Payment Intent
const cancelledIntent = await stripe.paymentIntent.cancel('pi_123456');
API Reference
StripeCheckout
createPaymentSession(options)
priceId
: string - Stripe Price IDcustomerId?
: string - Optional Stripe Customer IDsuccessUrl
: string - URL to redirect after successful paymentcancelUrl
: string - URL to redirect after cancelled paymentmode?
: 'subscription' | 'payment' (default: 'subscription')quantity?
: number (default: 1)metadata?
: Record<string, string>
createOneTimePaymentSession(options)
amount
: number - Amount in centscurrency?
: string (default: 'usd')successUrl
: stringcancelUrl
: stringmetadata?
: Record<string, string>
StripePaymentIntent
create(options)
amount
: number - Amount in dollarscurrency?
: string (default: 'usd')customerId?
: stringmetadata?
: Record<string, string>
confirm(paymentIntentId, paymentMethodId?)
paymentIntentId
: stringpaymentMethodId?
: string
retrieve(paymentIntentId)
paymentIntentId
: string
update(paymentIntentId, data)
paymentIntentId
: stringdata
: any
cancel(paymentIntentId)
paymentIntentId
: string
Error Handling
try {
const session = await stripe.checkout.createPaymentSession({
// options
});
} catch (error) {
if (error instanceof Error) {
console.error('Error:', error.message);
}
}
Best Practices
- Environment Variables: Store your Stripe secret key in environment variables
- Error Handling: Always implement proper error handling
- Webhooks: Set up webhooks to handle asynchronous events
- Testing: Use Stripe's test mode keys for development
- Currency: Always specify amounts in the smallest currency unit (cents for USD)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details