Skip to content

Instantly share code, notes, and snippets.

@ademcan
Created June 21, 2020 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ademcan/a2e86c7eaa6936aad78ff5f7a3f47929 to your computer and use it in GitHub Desktop.
Save ademcan/a2e86c7eaa6936aad78ff5f7a3f47929 to your computer and use it in GitHub Desktop.
router.post('/createStripeCustomer', function (req, res) {
var stripe = require("stripe")(stripe_sk);
var payment_method = req.body.payment_method
var name = req.body.name // optional
var email = req.body.email // optional
// This creates a new Customer and attaches the PaymentMethod in one API call.
stripe.customers.create({
payment_method: payment_method,
name: name,
email: email
}, function (err, customer) {
res.json({
customer: customer.id
})
});
})
@hamzaDv
Copy link

hamzaDv commented Jun 21, 2020

YES,
But I don't know if this payment method should be attached also to subscription or no? Thank you!
// Create Subscription With Payment Method
router.post('/createSubscription', function (req, res){
const {payment_method, customerId} = req.body;
var stripe = require("stripe")(stripe_sk);
try{
stripe.subscriptions
.create({
customer: customerId,
items: [{
plan: 'plan_HTGCI8ljPYFTHQ'
}],
default_payment_method: payment_method,
expand: ["latest_invoice.payment_intent"],
enable_incomplete_payments: true
}).then(subscription => {
res.send({
subscription : subscription
})
}).catch(err => {
res.send({
err
})
})
} catch (error) {
res.send("Error : ", error);
}

@ademcan
Copy link
Author

ademcan commented Jun 21, 2020

YES,
But I don't know if this payment method should be attached also to subscription or no? Thank you!

I am not sure about that, haven't work with subscriptions yes! A setupIntent is just one payment method associated to a customer so I suppose you can also use it with subscriptions...

@hamzaDv
Copy link

hamzaDv commented Jun 21, 2020

Ok thank you so much for help and support,

I would let you know if I make it work (y)

@hamzaDv
Copy link

hamzaDv commented Jun 22, 2020

I'm stuck on this issue:
When I try to submit this subscription, I got an error of "subscription_payment_intent_requires_action", should I handle it on client side or there is another way to do it?

Thank you all!

// Create Subscription With Payment Method + Customer ID
router.post('/createSubscription', function (req, res){
const {payment_method, customerId} = req.body;
var stripe = require("stripe")(stripe_sk);
try{
stripe.subscriptions
.create({
customer: customerId,
items: [{
plan: 'plan_HTGCI8ljPYFTHQ'
}],
default_payment_method: payment_method,
expand: ["latest_invoice.payment_intent"],
// enable_incomplete_payments: true
}).then(subscription => {
res.send({
subscription : subscription
})
}).catch(err => {
res.send({
err
})
})
} catch (error) {
res.send("Error : ", error);
}
});

@hamzaDv
Copy link

hamzaDv commented Jun 22, 2020

Where I am now:

Subscription created with status "incomplete".

So what I have to do is to convert this code to react-native (objective C)

  function manageSubscriptionStatus(subscription) {
    const { latest_invoice } = subscription;
    const { payment_intent } = latest_invoice;

    if (payment_intent) {
      /* Do NOT share or embed your client_secret anywhere */
      const { client_secret, status } = payment_intent;
      if (status === "requires_action" || status === "requires_payment_method") {
        stripe.confirmCardPayment(client_secret) //  **Which stripe-ios function does the same treatment to generate authentification**
        .then((result) => {
          if (result.error) {
            showState('payment-form');
            displayError(result.error); // Display error message to customer
          } else {
            showState('success'); // Show success state
          }
        }).catch((err) => {
          console.error('Error confirming card payment:', err);
          showState('error'); // Show error state
        });
      } else {
        showState('success'); // Show success state
      }
    } else {
      /* If no payment intent exists, show the success state
       * Usually in this case if you set up a trial with the subscription
       */
      showState('success');
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment