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:
- When a visitor clicks the Buy Now button, the button's event handler calls a backend function.
- The backend function retrieves product payment information from the Products collection, and creates and returns a payment object to the client side.
- On the client side, a payment procedure is initiated using the ID from the payment object, causing a payment window to appear.
- 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:
- Understand the necessary security precautions for working with payments in code. Specifically, make sure to:
- Always define payment information in the backend.
- Assign collection permissions that protect payment information stored in your database. Make sure only site Admins can can create, update, and delete content.
- Set up accepted payment methods on your site. We also recommend that you understand the typical payment process before proceeding.
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 Velo 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.jsw 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.
-
In the Properties & Events panel of the repeater, we added an onItemReady event handler that will run when the repeater is ready to be loaded.
-
In the Properties & Events panel of the Buy Now button, we added an onClick event handler that will run when the button is clicked.
Step 3: Create the createPaymentForProduct Function in the Backend
We created a backend module called BE_Collection.jsw
. 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.
1import wixPay from 'wix-pay-backend';2import wixData from 'wix-data';3
4export async function createPaymentForProduct(productId) {5 let product = await wixData.get('Stores/Products', productId);6 return wixPay.createPayment( {7 amount: product.discountedPrice,8 items: [9 { name: product.name, price: product.discountedPrice }10 ]11 } );12}
Understanding the Code
Line 1: Import the module we need to work with the Wix Pay Backend library.
Line 2: Import the module we need to work with the Wix Data library.
Line 4: Declare the createPaymentForProduct
function and export it so it can be used on the client side. In the function, do the following:
Line 5: 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 6: Return the result of the wix-pay-backend
createPayment
function, which takes a PaymentInfo
object and creates a new payment object.
Lines 7-9: 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.
1import wixPayFrontend from 'wix-pay-frontend';2import {createPaymentForProduct} from 'backend/BE_Collection';
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.
1export function repeater1_itemReady($item, itemData, index) {2 let itemId = itemData._id;3 $item('#button1').onClick(async () => {4 let payment = await createPaymentForProduct(itemId);5 await wixPayFrontend.startPayment((payment.id), { "termsAndConditionsLink": "https://www.wix.com/" });6 });7}
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
- Open this example in the Editor to work with the template.
- Publish the site.
- Learn more: