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.
In our site we added the following:
Then we added code to do the following:
Before you start working with Wix payments in code, make sure you do the following:
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.
On the Collection & Repeater page we added:
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.
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.
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.
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
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.
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.
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.
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.
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.