Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.createStripePaymentIntent = functions.https.onRequest(async (req, res) => {
  2.   const amount       = req.body.amount
  3.   const currency     = req.body.currency
  4.   const paymentTypes = req.body.payment_method_types
  5.  
  6.   console.log("profile: "+ amount + currency + paymentTypes)
  7.  
  8.     const paymentIntent = await stripe.paymentIntents.create({
  9.       amount: amount,
  10.       currency: currency,
  11.       payment_method_types: ['card'],
  12.     });
  13.   // return res.send('yoyoyobeanallmetofortheforge');
  14.   return res.json({client_secret: paymentIntent.client_secret});
  15. });
  16.  
  17.  
  18. exports.confirm_payment_intent = functions.https.onRequest(async (req, res) => {
  19. const paymentInt = req.body.payment_intent_id
  20.  
  21.  
  22. stripe.paymentIntents.retrieve(
  23.   paymentInt,
  24.   function(err, paymentIntent) {
  25.     console.log(paymentIntent)
  26.     if (err) {
  27.       console.log(err)
  28.     }
  29.   }
  30. );
  31.  
  32.  
  33.  
  34. exports.confirm_payment = functions.https.onRequest(async (req, res) => {
  35.   try {
  36.     let intent;
  37.     if (req.body.payment_method_id) {
  38.       // Create the PaymentIntent
  39.       intent = await stripe.paymentIntents.create({
  40.         payment_method: req.body.payment_method_id,
  41.         amount: 1099,
  42.         currency: 'gbp',
  43.         confirmation_method: 'manual',
  44.         confirm: true
  45.       });
  46.     } else if (req.body.payment_intent_id) {
  47.       intent = await stripe.paymentIntents.confirm(
  48.         req.body.payment_intent_id
  49.       );
  50.     }
  51.     // Send the response to the client
  52.     res.send(generate_payment_response(intent));
  53.   } catch (e) {
  54.     // Display error on client
  55.     console.log("borchikkawowow")
  56.     return res.send({ error: e.message });
  57.   }
  58. });
  59.  
  60. const generate_payment_response = (intent) => {
  61.   // Note that if your API version is before 2019-02-11, 'requires_action'
  62.   // appears as 'requires_source_action'.
  63.   if (
  64.     intent.status === 'requires_action' &&
  65.     intent.next_action.type === 'use_stripe_sdk'
  66.   ) {
  67.     // Tell the client to handle the action
  68.     return {
  69.       requires_action: true,
  70.       payment_intent_client_secret: intent.client_secret
  71.     };
  72.   } else if (intent.status === 'succeeded') {
  73.     // The payment didn’t need any additional actions and completed!
  74.     // Handle post-payment fulfillment
  75.     return {
  76.       success: true
  77.     };
  78.   } else {
  79.     // Invalid status
  80.     return {
  81.       error: 'Invalid PaymentIntent status'
  82.     }
  83.   }
  84. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement