@naturalpay/react
v1.0.0
Published
React component for Natural's embedded payment form
Keywords
Readme
@naturalpay/react
Natural's embedded payment form as a React component. PaymentForm is a client component, so it works in the Next.js App Router.
The guide, Embed the payment form, has the server code, webhook setup, and every error code. Agents can read it as Markdown at https://docs.natural.com/guides/accept/embedded-checkout.md.
How a payment works
- Your server creates a payment intent with
POST /payment-intents, through@naturalpay/sdkor any HTTP client. The response has the secret atdata.attributes.clientSecret. Send it to the browser, and treat it like a password. - Your page renders
PaymentFormwith theclientSecret. The payer pays inside the form. - The form calls
onComplete. Show your confirmation there. - Your server gets the
paymentIntent.completedwebhook. Fulfill the order from the webhook, not fromonComplete.
Add the form
npm install @naturalpay/reactimport { PaymentForm } from "@naturalpay/react";
<PaymentForm clientSecret={clientSecret} onComplete={showConfirmation} onError={showError} />;Props
clientSecretis the payment intent'sclientSecret, or a Promise of it. It's required. A new value mounts a new form, so create a Promise once, not on every render.themeis"light"(the default) or"dark", for a page with a dark background. A new value mounts a new form.onCompletegets{ paymentIntentId, status }, once, when the payer is done. Acompletedstatus means the payer paid. Aprocessingstatus means the result arrives by webhook, and the payment can still fail.onErrorgets a failed mount and every later error. Without it, errors go to the console.classNameis set on the<div>that holds the form.
Set up your site
- Serve your page over HTTPS. Plain
httpworks only onlocalhostand127.0.0.1, and only in Sandbox. - Start in Sandbox. There the form loads on any site.
- Before you take live payments, add each site that shows the form, like
https://shop.example.com, in the Natural dashboard under Developers → API keys → Checkout domains. In live mode, a site that isn't listed getsdomain_not_allowed. - If your page sets a Content Security Policy, allow
https://checkout.natural.cominscript-srcandframe-src. - If your page sends
Cross-Origin-Opener-Policy: same-origin, sendsame-origin-allow-popupsinstead. Otherwise Google Pay fails.
Handle errors
An error is a plain { code, message } object, not an Error.
- Match on
code. A code is never renamed or removed, but new ones are added, so handle a code you don't know. - Log
message. Its wording can change, so don't match on it or show it to payers. - After
onErrorfires, the form can't take a payment. RenderPaymentFormwith a newkeyto let the payer try again. - A declined card shows inside the form. It doesn't call
onError.
The guide lists every code. Changes are in CHANGELOG.md.
What your page must show
The form shows the merchant name, card fields, the authorization sentence, and your policies. Your page shows the rest:
- What the payer is buying.
- Each item, any tax or fee, and the total with its currency.
- Your country and a support email or phone, on the page or in your footer.
- Your delivery policy, if you ship goods.
Natural emails the payer a receipt.
