Developers

Pay button (npm)

Add a Pay with PetraPay button to any website with the petrapay package.

The petrapay package adds a Pay with PetraPay button to any website. It opens the hosted checkout in a popup, so customers never leave your page, and tells your page when they've paid. No API key is needed in the browser.

In Payment links, create a link and copy its id: the part after /l/. Choose Customer chooses pricing if your site passes the amount (a cart total, for example), or Fixed price for a single product.

2. Add the button

React and Next.js

npm install petrapay
import { PetraPayButton } from "petrapay/react";

export function Checkout({ total }: { total: number }) {
  return (
    <PetraPayButton
      paymentId="YOUR_LINK_ID"
      amount={total}
      onSuccess={(payment) => console.log("Paid", payment.checkoutId)}
    />
  );
}

Plain HTML

<script src="https://unpkg.com/[email protected]/dist/petrapay.global.js"></script>

<petrapay-button payment-id="YOUR_LINK_ID" amount="25"></petrapay-button>

<script>
  document.querySelector("petrapay-button").addEventListener("petrapay:success", (e) => {
    console.log("Paid", e.detail.checkoutId);
  });
</script>
Call openCheckout straight from a click handler. Browsers block popups opened later; if that happens, the checkout opens in the same tab instead.

Options

paymentIdstringrequired

A payment link id, or a checkout id with type: "checkout".

type"link" | "checkout"

Default link. Use checkout for checkouts you create with the API.

amountstring | number

Fills in the amount on pay-what-you-want links, so the customer goes straight to paying. Ignored for fixed-price links.

mode"popup" | "redirect" | "tab"

Default popup.

overlayboolean

Dims your page with a "finish paying in the PetraPay window" notice while the popup is open. Default true.

autoClosenumber | false

Closes the popup this many milliseconds after payment. Default 2500.

onCreated, onProcessing, onSuccess(payment) => void

Called as the payment progresses. payment is { checkoutId, linkId, status, amount, currency }.

onClose({ paid, last }) => void

The popup was closed.

label, theme, size, blockbutton

Button text, dark or light, small / medium / large, and full width.

Confirm payments on your server

onSuccess runs in the customer's browser, so use it to update your page. Before shipping an order or granting access, confirm the payment with a webhook or by fetching the checkout.