--- tags: brainstorming --- # Payment service Endpoints ### POST /sessions Accetta in input un oggetto `people`: Se `object.stripe_customer_id`, ok Se non esiste , va creato `stripe_customer_id` e salvato. **Create** ```javascript= const customer = await stripe.customers.create(object); ``` In questa fase, ho per forza l'oggetto customer su stripe. Poi, fare la setupIntent ```javascript= const setupIntent = await stripe.setupIntents.create({ payment_method_types: ['sepa_debit', 'credit_card'], customer: customer.id, }); const clientSecret = setupIntent.client_secret; return clientSecret; ``` ### /create-subscription Dato il `customer.id` di Stripe e il `paymentMethod.id` ottenuto da frontend dopo la conferma del pagamento. E il `price.id ` salva il metodo di pagamento come default per il customer, e crea la subscription su Stripe. Se non ha già un metodo di pagamento fare cosi: ```javascript= const customer = await stripe.customers.update( 'cus_Gk0uVzT2M4xOKD', { invoice_settings: { default_payment_method: 'pm_1F0c9v2eZvKYlo2CJDeTrB4n', }, } ); ``` Se ce lo ha già, aggiungerne un altro: ```javascript= // Attach the payment method to the customer try { await stripe.paymentMethods.attach(req.body.paymentMethodId, { customer: req.body.customerId, }); } catch (error) { return ``` ```javascript const subscription = await stripe.subscriptions.create({ customer: customer.id, items: [ { price: 'price_FSDjyHWis0QVwl', }, ], expand: ['latest_invoice.payment_intent'] }); ``` ### /hook Aggiungere webhook su stripe stare in ascolto dell'evento di conferma del pagamento (da definire qual'è) e nel caso di success inviare la fattura. ```javascript= switch(type) {...} ```