Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.createStripePaymentIntent = functions.https.onRequest(async (req, res) => {
- const amount = req.body.amount
- const currency = req.body.currency
- const paymentTypes = req.body.payment_method_types
- console.log("profile: "+ amount + currency + paymentTypes)
- const paymentIntent = await stripe.paymentIntents.create({
- amount: amount,
- currency: currency,
- payment_method_types: ['card'],
- });
- // return res.send('yoyoyobeanallmetofortheforge');
- return res.json({client_secret: paymentIntent.client_secret});
- });
- exports.confirm_payment_intent = functions.https.onRequest(async (req, res) => {
- const paymentInt = req.body.payment_intent_id
- stripe.paymentIntents.retrieve(
- paymentInt,
- function(err, paymentIntent) {
- console.log(paymentIntent)
- if (err) {
- console.log(err)
- }
- }
- );
- exports.confirm_payment = functions.https.onRequest(async (req, res) => {
- try {
- let intent;
- if (req.body.payment_method_id) {
- // Create the PaymentIntent
- intent = await stripe.paymentIntents.create({
- payment_method: req.body.payment_method_id,
- amount: 1099,
- currency: 'gbp',
- confirmation_method: 'manual',
- confirm: true
- });
- } else if (req.body.payment_intent_id) {
- intent = await stripe.paymentIntents.confirm(
- req.body.payment_intent_id
- );
- }
- // Send the response to the client
- res.send(generate_payment_response(intent));
- } catch (e) {
- // Display error on client
- console.log("borchikkawowow")
- return res.send({ error: e.message });
- }
- });
- const generate_payment_response = (intent) => {
- // Note that if your API version is before 2019-02-11, 'requires_action'
- // appears as 'requires_source_action'.
- if (
- intent.status === 'requires_action' &&
- intent.next_action.type === 'use_stripe_sdk'
- ) {
- // Tell the client to handle the action
- return {
- requires_action: true,
- payment_intent_client_secret: intent.client_secret
- };
- } else if (intent.status === 'succeeded') {
- // The payment didn’t need any additional actions and completed!
- // Handle post-payment fulfillment
- return {
- success: true
- };
- } else {
- // Invalid status
- return {
- error: 'Invalid PaymentIntent status'
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement