5 Step Guide on How to Set Up Velo Pay API for a Single-Product Online Payments

Written by velo | Published 2021/04/01
Tech Story Tags: wix | beginners | javascript | tutorial | software-development | website-development | website-design | business

TLDR This article describes how you can use the Velo Pay API to collect payments from your site's visitors for a single predefined product. We're going to explain how we set up the sample site and the code we added to make it work. Make sure to always define payment information in the backend of the payment process. There are no identifiers you would need to change here to make this code work on your site. In our example we included a "Buy Now" button on the Pay API page, which visitors can click to purchase the product.via the TL;DR App

This article describes how you can use the Velo Pay API to collect payments
from your site's visitors for a single predefined product, 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 products stored in a collection, see Velo: Using the Wix Pay API to Collect Payments for Products in a Database Collection.

Overview

In our site we added the following:
  • Text elements and an image on the Pay API page, displaying a single product for sale.
  • A "Buy Now" button on the Pay API page, 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 creates and returns a payment object to the client side. The payment object contains payment information for the product as defined in the backend code.
  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:
  • Understand the necessary security precautions for working with payments in code. Specifically, make sure to always define payment information in the backend. This prevents malicious users from accessing and possibly manipulating payment information or the payment process.
  • 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 Pay API Page

On the Pay API page we added:
An image of the robot for sale.
Text elements for the name, price, and description of the robot.
A Buy Now button to trigger the payment process. In the Properties & Events panel of the button, we added an onClick event handler.

Step 2: Create the createPaymentForProduct Function in the Backend

We created a backend module called
BE_PayAPI.jsw
. Then we imported the module we need to work with payments in backend code.
Then we declared the
createPaymentForProduct
function, which creates and returns a payment object. 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.
import wixPay from 'wix-pay-backend';

export function createPaymentForProduct() {
  return wixPay.createPayment( {
    amount: 0.50,
    items: [{name: 'DIY Robot', price: 0.50}],
  } );
}
Understanding the Code
Line 1: Import the module we need to work with the Wix Pay Backend library.
Line 3: Declare the
createPaymentForProduct
function and export it so it can be used on the client side. In the function, do the following:
Line 4: Return the result of the wix-backend-pay createPayment function, which takes a PaymentInfo object and creates a new payment object.
Lines 5-6: Define the PaymentInfo (product payment information) directly in the code.
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 these items to match the ones on your site:
  • 0.50 (amount)
  • DIY Robot (name)
  • 0.50 (price)

Step 3: Prepare the Pay API Page

On the Pay API page, we start by importing the module we need to work with payments in code. We also import the createPaymentForProduct function from the backend.
import wixPay from 'wix-pay';
import {createPaymentForProduct} from 'backend/BE_PayAPI';
Understanding the Code
Line 1: Import the module we need to work with the Wix Pay library.
Line 2: Import the createPaymentForProduct function from the backend module where it was created (see Step 2). This function creates a payment object based on payment information for a single product defined in the backend.
There are no identifiers you would need to change here to make this code work on your site.

Step 4: Create the button1_click function on the Pay API Page

The
button1_click
event handler runs when a visitor clicks the Buy Now button. The event handler calls backend code that returns a payment object based on product payment information.
The event handler then runs the wixPay 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 included a
termsAndConditionsLink
, one of the payment options available for payments.
export function button1_click(event) {
 createPaymentForProduct().then(payment => {
   wixPay.startPayment(payment.id, {"termsAndConditionsLink": "https://www.wix.com/"});
 });
}
Understanding the Code
Line 1: When the "Buy Now" button is clicked, run an event handler that does the following:
Line 2: Run the
createPaymentForProduct
function, which was imported from the backend. With the payment object that is returned, do the following:
Line 3: Run the wixPay 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.
There are no identifiers you would need to change here to make this code work on your site.
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

1. Open this example in the Editor to work with the template.
2. Publish the site.
3. Learn more:

Written by velo | Velo is a full-stack development platform that empowers you to rapidly build, manage and deploy professional web apps.
Published by HackerNoon on 2021/04/01