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.
1. Create a payment link
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 petrapayimport { 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>openCheckout straight from a click handler. Browsers block popups opened later; if that happens, the checkout opens in the same tab instead.Options
paymentIdstringrequired
type: "checkout".type"link" | "checkout"
amountstring | number
mode"popup" | "redirect" | "tab"
popup.overlayboolean
true.autoClosenumber | false
2500.onCreated, onProcessing, onSuccess(payment) => void
payment is { checkoutId, linkId, status, amount, currency }.onClose({ paid, last }) => void
label, theme, size, blockbutton
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.