Velo Tutorial: Using the Velo Pay API to Collect Payments for Products in a Database Collection

Note: This tutorial and its steps are based on a Wix Editor site. You can adapt the steps for a Wix Studio site by using the equivalent Wix Studio features.

This article describes how you can use the Velo Pay API to collect payments from your site's visitors for a product stored in a database collection, outside the context of a Wix App (like Wix Stores). Throughout this article we're going to use this site to illustrate the process. You can open the site in the Editor to work with the template. We're going to explain how we set up the sample site and the code we added to make it work.

Note To learn how to use the Velo Pay API for a single predefined product, see Velo: Using the Velo Pay API to Collect Payments for a Single Product.

Overview

In our site we added the following:

  • A Products collection with a list of products for sale. Each product needs a title, image, and price.
  • A Collection & Repeater page. A repeater displays products from the Products collection. Each product in the repeater has a Buy Now button, which visitors can click to purchase the product.

Then we added code to do the following:

  1. When a visitor clicks the Buy Now button, the button's event handler calls a backend function.
  2. The backend function retrieves product payment information from the Products collection, and creates and returns a payment object to the client side.
  3. On the client side, a payment procedure is initiated using the ID from the payment object, causing a payment window to appear.
  4. The visitor enters payment information and completes the transaction.

Before You Start

Before you start working with Wix payments in code, make sure you do the following:

Step 1: Set up the Site

For this example, you'll need some products for sale that are stored in a collection. We added Wix Stores to our site, which automatically adds a Stores/Products collection, but you can also create your own collection of products without Wix Stores.

Note You may need to save or publish the site and refresh your browser to view Stores collections in Content Collections in the Databases section of the Code sidebar.

This is what some of the data in our Products collection looks like:

We also added a Collection & Repeater page to the site to display our products, and we created the BE_Collection.web.js backend module to securely run code related to payment information.

Step 2: Set up the Collection & Repeater Page

On the Collection & Repeater page we added:

  • A repeater to display the products.
  • Text and image elements in the repeater to display the name, price, and picture of each product for sale.
  • A Buy Now button in the repeater to trigger the payment process for the selected product.
  • A dataset connected to our Stores/Products collection for connecting data in the collection to the repeater.

Note When you display data from a collection in a repeater, you must first connect the repeater to the dataset, and then connect each element in the repeater to the dataset.

Step 3: Create the createPaymentForProduct Function in the Backend

We created a backend module called BE_Collection.web.js. In the backend, we start by importing the modules we need to work with backend payments and data in code.

Then we created the createPaymentForProduct function, which retrieves product data from the collection, and creates and returns a payment object based on the data. We also export the function so it can be used on the client side.

Note that amount is the sum of the price property for all items. In this example, there is only one item so amount and price are identical.

Note: Throughout this example we use the async/await JavaScript functionality.

Copy
1

Understanding the Code

Line 1: Import the Permissions enum and webMethod function from wix-web-module.
Line 2: Import the module we need to work with the Wix Pay Backend library.
Line 3: Import the module we need to work with the Wix Data library.
Line 5: Declare the createPaymentForProduct function and export it so it can be used on the client side.
Line 6: Use the Wix Data get function to retrieve product information from the collection and assign it to the product variable. The get function uses the ID of the product selected by the visitor to "know" which product data to retrieve.
Line 7: Return the result of the wix-pay-backend createPayment function, which takes a PaymentInfo object and creates a new payment object.
Lines 8-10: Define the PaymentInfo object using payment information stored in the product variable.

Identifiers you may need to change based on your site's elements

If you want to use this exact scenario and code in your site, you may need to modify this item to match the one on your site:

  • Stores/Products

Step 4: Prepare the Collection & Repeater Page

On the Collection & Repeater page we start by importing the module we need to work with payments in code. We also import the createPaymentForProduct function from the backend.

Copy
1

Understanding the Code

Line 1: Import the module we need to work with the Wix Pay Frontend library.
Line 2: Import the createPaymentForProduct function from the backend module where it was created (see Step 3). This function creates a payment object based on payment information for a product stored in the Products collection.

There are no identifiers you would need to change here to make this code work on your site.

Step 5: Create the repeater1_itemReady function on the Collection & Repeater page

The repeater1_itemReady event handler runs when the repeater is ready to be loaded.

The function runs for each item (product) in the collection. The current item is passed to the event handler as the itemData parameter.

First the function loads the item into the repeater, displaying product information. Then it checks if the user clicked the Buy Now button for this particular item.

If the button for this item was clicked, the onClick event handler runs the backend createPaymentForProduct function that creates a payment object for the selected product.

The event handler then runs the wixPayFrontend startPayment function with the payment object returned from the backend. This opens a payment window and prompts the user to select a payment method and continue the payment process.

In our example, we also included a termsAndConditionsLink, one of the available payment options.

Copy
1

Understanding the Code

Line 1: For each item (product) in the Stores/Products dataset, set up the item in the repeater and then do the following:
Line 2: Store the ID of the item in the itemID variable.
Line 3: If the "Buy Now" button is clicked for this item, run an event handler that does the following:
Line 4: Run the createPaymentForProduct function, which was imported from the backend, with the ID of the selected product. Store the payment object that is returned in the payment variable.
Line 5: Run the Wix Pay Frontend startPayment function with the ID of the payment object. A payment window opens prompting the user for payment information.
The startPayment function runs with an optional termsAndConditionsLink PaymentOption. A checkbox with a link to a terms and conditions page is included in the payment window.

Identifiers you may need to change based on your site's elements

If you want to use this exact scenario and code in your site, you may need to modify this item to match the one on your site:

  • #button1

In the payment window, the site visitor selects a payment method, fills in payment information, clicks Pay Now, and the transaction is completed. The visitor receives an email confirming the payment.

Next Steps

Was this helpful?
Yes
No