Before starting with payments, it is important that you fully understand how to handle the security concerns detailed below that arise when collecting payments.
Prerequisites
Note:
When setting up your site to accept payments, be sure to select the payment methods you want to offer and set your payment currency.
Pay API
Note:
To work with the Pay API, you need to save and publish your site.
- Called in backend code to create a payment and generate acreatePayment()
.paymentId
- Called in client-side code to prompt the current site visitor to enter the payment information.startPayment()
- Fired in backend code when a payment's status has changed.onPaymentUpdate()
Payment Lifecycle
- A site visitor clicks a button to start the payment process.
- The button's event handler calls a backend function.
- A
object containing information about the payment, such as the payment amount, is created in the backend function.PaymentInfo
- The backend function calls
using thecreatePayment()
object and returns the generatedPaymentInfo
object to the calling client-side event handler.Payment
- The event handler then calls the
function with thestartPayment()
from theid
object, which opens the payment popup on your site.Payment
- The site visitor enters the payment information.
- The event handler optionally handles the returned
.PaymentResult
- Handle additional status updates to the payment transaction using the
event.onPaymentUpdate()
Payment Code Example
/********************
* client-side code *
********************/
import {createMyPayment} from 'backend/pay';
import wixPay from 'wix-pay';
import wixWindow from 'wix-window';
// Step 1 - User clicks a button.
export function myButton_click(event) {
// Step 2 - Call backend function.
// (Next, see step 3 in the backend code below.)
createMyPayment()
// When the payment has been created and a paymentId has been returned:
.then( (payment) => {
// Step 5 - Call the startPayment() function with the paymentId.
// Include PaymentOptions to customize the payment experience.
wixPay.startPayment(payment.id, {
"showThankYouPage": false,
"termsAndConditionsLink": "https://mysite.com/terms"
})
// Step 6 - Visitor enters the payment information.
// When the payment form is completed:
.then( (result) => {
// Step 7 - Handle the payment result.
// (Next, see step 8 in the backend code below.)
if (result.status === "Successful") {
wixWindow.openLightbox("Success Box");
} else if (result.status === "Pending") {
wixWindow.openLightbox("Pending Box");
}
} );
} );
}
/**************************
* backend code - pay.jsw *
**************************/
import wixPay from 'wix-pay-backend';
export function createMyPayment() {
// Step 3 - Create payment info object.
// Here we use static data. You might want to use data from a
// collection. To see an example of such a usage, see the API Reference.
let paymentInfo = {
"items": [
{
name: "Product 1",
price: 9.99
},
{
name: "Product 2",
price: 19.99
}
],
amount: 29.98
}
// Step 4 - Call createPayment() with the payment information
// and return the paymentId.
// (Next, see step 5 in the client-side code above.)
return wixPay.createPayment(paymentInfo);
}
/*****************************
* backend code - events.js *
*****************************/
export function wixPay_onPaymentUpdate(event) {
// Step 8 - Handle additional status updates using
// the onPaymentUpdate() event.
let paymentId = event.payment.id;
let newTransactionStatus = event.status;
let userInfo = event.userInfo;
// Handle new payment status.
}
Security Considerations
PaymentInfo
object (step 3 above) in backend code. Do not pass payment information from client-side code.