createPayment( )
Creates a new payment.
Description
Before using the createPayment() function, you need to set up your site to accept payments. To learn more, see About Accepting Payments. When setting up your site to accept payments, be sure to select the payment methods you want to offer and set your payment currency.
The createPayment()
function returns a Promise that resolves to a Payment
object when the payment has been created.
Creating a payment is the first step in the process of accepting a payment from
a user. After creating the payment, call startPayment()
from your site's client-side code using the payment ID returned by createPayment
.
The startPayment()
function prompts the user
to select a payment method and continue the payment process.
To understand how createPayment()
is used in a typical payment lifecycle,
see Typical Payment Lifecycle.
Note:
- To work with the Pay API, you need to save and publish your site.
- For security reasons you should always create the
PaymentInfo
object in backend code. Do not pass payment information from client-side code. Doing so opens a vulnerability that a malicious user can exploit to change payment information, such as the price of an item being purchased.- To learn more about how to keep your payment code secure, see Security Considerations When Working with Wix Code.
Syntax
function createPayment(paymentInfo: PaymentInfo): Promise<Payment>
createPayment Parameters
NAME
TYPE
DESCRIPTION
The information for the payment being created.
Returns
Fulfilled - The created payment.
Return Type:
NAME
TYPE
DESCRIPTION
Payment transaction ID.
Payment total amount.
Payment items.
An object representing information about the user.
Was this helpful?
1/**************************2 * Backend code - pay.jsw *3 **************************/45import wixPayBackend from 'wix-pay-backend';67export function createMyPayment() {8 return wixPayBackend.createPayment({9 items: [{10 name: "Product Name",11 price: 9.9912 }],13 amount: 9.9914 });15}1617/********************18 * Client-side code *19 ********************/2021import { createMyPayment } from 'backend/pay';22import wixPay from 'wix-pay';2324export function myButton_click(event, $w) {25 createMyPayment()26 .then((payment) => {27 wixPay.startPayment(payment.id);28 });29}
1/**************************2 * Backend code - pay.jsw *3 **************************/45import wixPayBackend from 'wix-pay-backend';67export function createMyPayment() {8 return wixPayBackend.createPayment({9 items: [{10 name: "Product Name",11 price: 9.99,12 quantity: 213 }],14 amount: 19.9815 });16}1718/********************19 * Client-side code *20 ********************/2122import { createMyPayment } from 'backend/pay';23import wixPay from 'wix-pay';2425export function myButton_click(event, $w) {26 createMyPayment()27 .then((payment) => {28 wixPay.startPayment(payment.id);29 });30}
1/**************************2 * Backend code - pay.jsw *3 **************************/45import wixPayBackend from 'wix-pay-backend';67export function createMyPayment(userInfo) {8 return wixPayBackend.createPayment({9 items: [{10 name: "Product Name",11 price: 9.99,12 quantity: 213 }],14 amount: 19.98,15 userInfo16 });17}1819/********************20 * Client-side code *21 ********************/2223import { createMyPayment } from 'backend/pay';24import wixPay from 'wix-pay';2526export function myButton_click(event, $w) {27 const firstName = // the user's first name28 const lastName = // the user's last name29 const phone = // the user's phone number30 const email = // the user's email address31 const countryCode = // the user's country code3233 createMyPayment({ firstName, lastName, phone, email, countryCode })34 .then((payment) => {35 wixPay.startPayment(payment.id);36 });37}
1/**************************2 * Backend code - pay.jsw *3 **************************/45import wixPayBackend from 'wix-pay-backend';67export function createMyPayment() {8 return wixPayBackend.createPayment({9 items: [{10 name: "Product Name",11 price: 9.9912 }],13 amount: 9.99,14 currency: "USD"15 });16}1718/********************19 * Client-side code *20 ********************/2122import { createMyPayment } from 'backend/pay';23import wixPay from 'wix-pay';2425export function myButton_click(event, $w) {26 createMyPayment()27 .then((payment) => {28 wixPay.startPayment(payment.id);29 });30}
1/**************************2 * Backend code - pay.jsw *3 **************************/45import wixData from 'wix-data';6import wixPayBackend from 'wix-pay-backend';78export async function createMyPayment(productId) {9 return wixData.get("productCollection", productId)10 .then((product) => {11 return wixPayBackend.createPayment({12 items: [{13 name: product.name,14 price: product.price15 }],16 amount: product.price17 });18 });19}2021/********************22 * Client-side code *23 ********************/2425import { createMyPayment } from 'backend/pay';26import wixPay from 'wix-pay';2728export function myButton_click(event, $w) {29 createMyPayment(event.context.itemId)30 .then((payment) => {31 wixPay.startPayment(payment.id);32 });33}
1/**************************2 * Backend code - pay.jsw *3 **************************/45import wixData from 'wix-data';6import wixPayBackend from 'wix-pay-backend';78export async function createMyPayment(productId) {9 return wixData.get("productCollection", productId)10 .then((product) => {11 return wixPayBackend.createPayment({12 items: [{13 name: product.name,14 price: product.price15 }],16 amount: product.price17 });18 });19}2021/********************22 * Client-side code *23 ********************/2425import { createMyPayment } from 'backend/pay';26import wixPay from 'wix-pay';2728export function myButton_click(event, $w) {29 createMyPayment(event.context.itemId)30 .then((payment) => {31 wixPay.startPayment(payment.id);32 });33}