Accepting PayPal
PayPal
PayPal adds flexibility to your payment method offerings.
Add PayPal payment buttons to your checkout with Stax.js using a minimal $1.00 configuration.
- Initializing Stax.js.
- Optional: Manually setting the price on a Transaction with Stax.js
- Setting up your HTML form.
- Displaying the payment buttons.
A note about TokenizationIf you are tokenizing payment methods, PayPal tokenization is currently not supported. If you are storing payment methods, the PayPal payment option will not appear on customer payment page as an option.
1. Initialize Stax.js to use PayPal:
-
Add Stax.js to the
<head>of your HTML page using this script:<script src="https://staxjs.staxpayments.com/staxjs-captcha.js"></script> -
Copy your Web Payments Token (public key) from Stax Pay > Apps > API Keys. Use your sandbox account for testing; never put a private API key in browser code.
-
Run the following JavaScript in your browser page after Stax.js loads. Enter your public token when prompted, then run the examples in steps 2 and 3 on the same page. These snippets share the
staxjsinstance; they are not standalone scripts.
Start with only price: card fields and OCR settings are not needed for this PayPal example. All configuration fields are optional, but if you omit price, call setTotal() before displaying the buttons.
// Copy your public token from Stax Pay > Apps > API Keys.
const webpaymentsToken = window.prompt("Enter your Web Payments Token (public key):");
if (!webpaymentsToken || !webpaymentsToken.trim()) {
throw new Error("A Web Payments Token is required. Reload the page to try again.");
}
if (typeof StaxJs !== "function") {
throw new Error("Stax.js has not loaded. Check the script in your page's head.");
}
const staxJsConfig = {
price: "1.00", // The transaction amount is in dollars, not cents.
};
const staxjs = new StaxJs(webpaymentsToken.trim(), staxJsConfig);Expected result: the page creates a Stax.js instance configured for $1.00. It does not display buttons or charge a customer yet. If you cancel the prompt or leave it blank, the example stops with an error instead of using an invalid credential.
i. Optional: Manually setting the price on a Transaction with Stax.js
After Stax.js has been initialized, you can set the transaction amount. If you passed in a value for price in the initial Stax.js config, it will be overwritten by this call. This can be useful for dynamically updating the price as customers add or remove items on a checkout page. The total is represented in dollars, which can be a string or number type. “1.00”, “1” or 1 will charge the customer $1.00.
/* Set the total to $1.00 */
staxjs.setTotal(1.00);2. Create the HTML Container
In your HTML, create the container where the PayPal buttons will be rendered. Ensure that the main div has an ID so that the showPayPalButtons() can render the buttons appropriately.
<div class="group">
<div id="paypal-button-container"></div>
</div>3. Display PayPal Options
After you add the container from step 2, call showPayPalButtons() using its ID. Start with only selector; omit invoiceId unless you want to associate an existing invoice, and omit enableFunding to display PayPal, Venmo, and Buy Now, Pay Later.
const paypalConfig = {
selector: "#paypal-button-container",
};
if (!document.querySelector(paypalConfig.selector)) {
throw new Error("Add the paypal-button-container element before displaying the buttons.");
}
staxjs.showPayPalButtons(paypalConfig);Expected result: the payment buttons appear in paypal-button-container. Displaying the buttons is not confirmation of a successful payment. If no buttons appear, check that Stax.js loaded, the container ID matches, and PayPal is enabled using the merchant check at the end of this page.
Optional: Associate an existing invoice
To associate an existing customer with the payment, follow the customer-matching steps below to create the customer and invoice first. Then run this snippet before calling showPayPalButtons() in step 3.
Copy the invoice's id from the POST /invoice API response. Invoice IDs use strings such as "101c4a15-5cdb-41e5-837c-f23470c7d670"; this illustrates the format, not an invoice to reuse. The source material does not specify a Dashboard location for this ID, so use the API response rather than guessing a Dashboard path.
// Use the id returned by POST /invoice for an invoice in your merchant account.
const invoiceId = window.prompt("Enter the invoice id from your POST /invoice response:");
if (!invoiceId || !invoiceId.trim()) {
throw new Error("An invoice ID is required. Reload the page to try again.");
}
staxjs.setPayPalTransactionInvoiceId(invoiceId.trim());Expected result: Stax.js uses the supplied invoice for the transaction. This call does not create an invoice or confirm payment.
Optional: Pass additional transaction data
Before the user clicks a payment button, call setPayPalTransactionPayload() to supply additional transaction data. Skip this section for the minimal example.
| Parameter | Type | Default | Behavior and constraints |
|---|---|---|---|
meta | object | Not specified | Optional transaction metadata, such as a memo. |
customer_id | string | Not specified | Optional Stax customer ID from your merchant account. See the customer-matching rules below for invoice association. |
email_receipt | boolean | Not specified | Optional; set to true to request an email receipt or false not to request one. |
Copy the customer's id from the POST /customer API response. Customer IDs use strings such as "e8978baa-0278-47c2-9036-2849c6f522e1"; use your own customer's ID, not this example. The source material does not specify a Dashboard location for this ID.
// Use the id returned by POST /customer for a customer in your merchant account.
const customerId = window.prompt("Enter the customer id from your POST /customer response:");
if (!customerId || !customerId.trim()) {
throw new Error("A customer ID is required for this example. Reload the page to try again.");
}
const memo = "Note about the transaction";
const additionalTransactionData = {
meta: { memo },
customer_id: customerId.trim(),
email_receipt: true,
};
staxjs.setPayPalTransactionPayload(additionalTransactionData);Expected result: Stax.js receives the additional payload for the payment. Setting the payload does not charge the customer or send a receipt immediately. If you do not want to supply a customer ID, omit customer_id and its prompt rather than passing an empty string.
Optional: Enable Venmo and Buy Now, Pay Later
The minimal example omits enableFunding, so it displays all three options. To control which additional options appear, add enableFunding to paypalConfig before calling showPayPalButtons(): use ["venmo"] for Venmo, ["paylater"] for Buy Now, Pay Later, or ["venmo", "paylater"] for both. Use an empty array ([]) to display only PayPal.
| enableFunding Value | Buttons Shown |
|---|---|
| null or undefined | PayPal, Venmo, and Buy Now, Pay Later |
| [“venmo”, “paylater”] | PayPal, Venmo, and Buy Now, Pay Later |
| [“venmo”] | PayPal and Venmo |
| [“paylater”] | PayPal and Buy Now, Pay Later |
| [] | PayPal |
Notes on Customer Matching on the Hosted Payments Page
When a merchant uses the Hosted Payments page in Stax Pay, a customer_id cannot be passed in. A new customer will be created or matched. The customer’s details are gathered from the PayPal account. The customer is matched based on the Core API’s matching in this order:
- company, address_1, and address_city
- firstname and phone
- firstname, lastname, address_1, and address_zip
- lastname and phone
Customer Matching on ISV/Integration
When a merchant/partner is not using the Hosted Payments page but using StaxJS manually, customers are only matched on the Invoice’s Customer ID. If the payment does not have an associated invoice, a new customer (and invoice) will be created. To avoid this, take the following steps:
- Create a customer using the
POST /customerAPI. - Create an invoice using the
POST /invoiceAPI. Be sure to include the above customer.id. - Set the invoice for the transaction using the
StaxJS#setPayPalTransactionInvoiceId().
The final transaction will now be linked to the customer created in the initial request.
PayPal enabled checkYou can use the GET a merchant by ID to validate if PayPal is enabled for them by checking if allow_paypal value is set to true or false.
GET Merchant by ID
Updated 4 days ago
