Stripe Recurring Payments (Subscription API) - Wix Code

#stripeAPI #subscriptions #wixCode

Hey guys,

Here is the working Stripe Recurring Payments (Subscriptions API) Tutorial. Please note that in this tutorial I only showed how to create a subscription for the first time, it will take a lot more to connect the dots, setup the website or web app to update and cancel subscriptions.

TUTORIAL: Wix Code Stripe Recurring Payments (Subscriptions) Tutorial - Dude Lemon Mauritius - YouTube

-Shan

3 Likes

@shan thanks! After an initial look this seems to work just as I would need it! A couple questions. You mentioned there would be follow on videos for the below topics - do you have an ETA?? These would be a huge help!

  1. updating & canceling subscription
  2. saving stripe customer id in wix database
  3. do you know how to save the customer’s CC/billing information so that if there are any one-off purchases the CC/billing info auto loads for that individual customer with tokenization & charges them by the click of a button?

Hi @tarrahopwood

I will do a second part as soon as I finish up with my ongoing projects. Expect it to come out sometime in Feb.

In the meantime, have a go at it yourself (Read the Stripe API Stripe API reference – curl) and if you run into any trouble with code post it on the forum and tag me, I’ll do my best to help.

In reply to your 2nd question (saving stripe customer id in wix database):

You receive the customer id from stripe via the backend module after that you can save it to your database like this

"customerId": idFromStripe //define this on the page like i have done

I will answer your other 2 questions in the next video

@shantanukumar847 Thank you so much! Looking forward to the next videos!

@shan Any luck on the next part of this video series!? Thanks!

Hi @tarrahopwood

Sorry but I have been swamped with work but I will try to help you with half of the questions you asked.

Question 2 & 3.

> saving stripe customer id in wix database

> do you know how to save the customer’s CC/billing information so that if there are any one-off purchases the CC/billing info auto loads for that individual customer with tokenization & charges them by the click of a button?

Tokens cannot be stored or used more than once. To store card or bank account information for later use, you can create Customer objects

To save a customer Id first, you need to retrieve the customer id from the api while creating a customer or charging for a subscription. To get the customer id, see the backend code below.

const response = await fetch("https://api.stripe.com/v1/customers", {
    method: 'post',
    headers: {
 "Content-Type": "application/x-www-form-urlencoded",
 "Authorization": "Bearer " + apiKey
    },
    body: encodeClient(client)
  });

 if (response.status >= 200 && response.status < 300) {
 const ret = await response.json();
 let cus = ret.id; //fetching the customer id from the api
 return {"cusId": cus}; //sending it back to the page
}

Then on the page you need to use .then() to receive your customer id like below.

createCustomer(customer) //create a new customer
    .then((response) => {
 if(response.cusId !== undefined) {
 let item = {
 "customer": response.cusId, //this is your customer ID
 "items[0][plan]": planId //this is your plan id
};
 cusId = response.cusId; //global variable being set to the received customer id

After that you can easily store it in the database once the order is processed. See below.

async function jackpot(response) {
 let toInsert = {
 'image': $w("#image1").src,
 'itemName': $w("#name").text,
 'price': $w("#price").text,
 'category': 'Subscribed',
 'email': $w("#email").value,
 'customerId': cusId, //customer id here
 'subId': response.subId
        };
 await wixData.insert("Orders", toInsert);
        wixLocation.to("/thank-you-page");
}

Your database should look like this then.

If you are able to do this then you will also be able to do the cancellation and update part by reading the Stripe API Stripe API reference – Subscriptions – curl