Complete payment processing with subscriptions, webhooks, and marketplace functionality.
// Stripe Payment Integration
const stripe = require('stripe')('sk_test_...');
class StripePayments {
constructor(secretKey) {
this.stripe = require('stripe')(secretKey);
}
async createPaymentIntent(amount, currency = 'usd', customerId = null) {
try {
const paymentIntent = await this.stripe.paymentIntents.create({
amount: amount * 100, // Convert to cents
currency,
customer: customerId,
automatic_payment_methods: {
enabled: true,
},
});
return {
success: true,
clientSecret: paymentIntent.client_secret,
paymentIntentId: paymentIntent.id
};
} catch (error) {
console.error('Stripe Payment Error:', error);
return {
success: false,
error: error.message
};
}
}
async createCustomer(email, name) {
try {
const customer = await this.stripe.customers.create({
email,
name,
});
return {
success: true,
customerId: customer.id
};
} catch (error) {
return {
success: false,
error: error.message
};
}
}
async handleWebhook(body, signature, webhookSecret) {
try {
const event = this.stripe.webhooks.constructEvent(
body,
signature,
webhookSecret
);
switch (event.type) {
case 'payment_intent.succeeded':
console.log('Payment succeeded:', event.data.object);
break;
case 'payment_intent.payment_failed':
console.log('Payment failed:', event.data.object);
break;
default:
console.log(`Unhandled event type ${event.type}`);
}
return { success: true };
} catch (error) {
return {
success: false,
error: error.message
};
}
}
}
// Usage
const payments = new StripePayments('sk_test_your_key');
payments.createPaymentIntent(29.99).then(console.log);