Skip to content

Instantly share code, notes, and snippets.

@DWS-paris
Created June 26, 2024 09:28
Show Gist options
  • Save DWS-paris/cee30d18faf0c4aa912413594dad94a2 to your computer and use it in GitHub Desktop.
Save DWS-paris/cee30d18faf0c4aa912413594dad94a2 to your computer and use it in GitHub Desktop.
const stripe = require('stripe')('votre_clé_secrète_stripe'); // Remplacez par votre clé secrète Stripe
class StripeService {
constructor() {
this.stripe = stripe;
}
// Méthode pour créer un client avec une carte de crédit
async createCustomerWithCard(email, cardToken) {
try {
const customer = await this.stripe.customers.create({
email: email,
source: cardToken // Utilisez un token de carte généré par Stripe.js ou Checkout
});
return customer;
} catch (error) {
console.error('Erreur lors de la création du client:', error);
throw error;
}
}
// Méthode pour souscrire à un abonnement
async subscribeCustomerToPlan(customerId, planId) {
try {
const subscription = await this.stripe.subscriptions.create({
customer: customerId,
items: [{ plan: planId }]
});
return subscription;
} catch (error) {
console.error('Erreur lors de la souscription à un abonnement:', error);
throw error;
}
}
// Méthode pour ajouter un achat supplémentaire à la facturation de l'abonnement
async addInvoiceItem(customerId, amount, currency, description) {
try {
const invoiceItem = await this.stripe.invoiceItems.create({
customer: customerId,
amount: amount,
currency: currency,
description: description
});
return invoiceItem;
} catch (error) {
console.error('Erreur lors de l\'ajout d\'un élément à la facture:', error);
throw error;
}
}
// Méthode pour traiter le token reçu depuis le client
async processPaymentToken(token) {
try {
const customer = await this.createCustomerWithCard('client@example.com', token);
// Effectuer d'autres opérations si nécessaire, comme souscrire à un abonnement
return customer;
} catch (error) {
console.error('Erreur lors du traitement du token:', error);
throw error;
}
}
}
// Exemple d'utilisation de la classe
(async () => {
const stripeService = new StripeService();
try {
// Création d'un client avec une carte de crédit
const email = 'client@example.com';
const cardToken = 'tok_visa'; // Vous devez obtenir ce token avec Stripe.js ou Checkout
const customer = await stripeService.createCustomerWithCard(email, cardToken);
console.log('Client créé:', customer);
// Souscription à un abonnement
const planId = 'plan_G0s0yOHEeW5C1B'; // Remplacez par l'ID de votre plan
const subscription = await stripeService.subscribeCustomerToPlan(customer.id, planId);
console.log('Abonnement créé:', subscription);
// Ajout d'un achat supplémentaire
const amount = 500; // Montant en centimes (par exemple, 500 centimes = 5.00 EUR)
const currency = 'eur';
const description = 'Achat supplémentaire';
const invoiceItem = await stripeService.addInvoiceItem(customer.id, amount, currency, description);
console.log('Achat supplémentaire ajouté:', invoiceItem);
} catch (error) {
console.error('Erreur:', error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment