XPay Web SDK
SDK URL
javascript
//stage env
https://js.xstak.com/v4/xpay-stage.js
//live env
https://js.xstak.com/v4/xpay.jsReact Package
javascript
//stage
npm i @xstak/xpay-element-stage-v4
//live
npm i @xstak/xpay-element-live-v4Steps to follow for integration on React-based web apps.
- Add the SDK URL to the index.html file of yours as a script.
javascript
<script defer src="https://js.xstak.com/v4/xpay.js"></script>
<script defer src="https://js.xstak.com/v4/xpay-stage.js"></script>- In the main checkout component, import the XPay SDK constructor.
- Wrap it around the payment component in which the payment element (payment input fields) will be embedded.
javascript
import { XPay } from '@xstak/xpay-element-stage-v4';
<XPay xpay={{ publishableKey: import.meta.env.VITE_PUBLISHABLE_KEY, accountId: import.meta.env.VITE_ACCOUNT_ID, hmacSecret: import.meta.env.VITE_HMAC_SECRET}}>
<Payment />
</XPay>- In the payment component, import the XPay payment element along with the XPay hook.
javascript
import { PaymentElement, useXpay } from '@xstak/xpay-element-stage-v4';
export const Payment = () => {
const [loading, setLoading] = useState(false);
// moify input field styling and labels
const options = {
override: true,
fields: {
creditCard: {
placeholder: "1234 1234 1234 1234",
label: "Enter your credit card",
},
exp: {
placeholder: "Exp. Date",
},
},
style: {
".input": {},
".invalid": {},
".label": {},
":focus": {},
":hover": {},
"::placeholder": {},
"::selection": {},
},
};
const xpay = useXpay();
const payNow = async() => {
let customer = { name: 'guest user'};
try {
setLoading(true);
const { clientSecret, encryptionKey } = await fetch("http://localhost:4242/create-payment-intent", {
method: "post",
})
.then((res) => res.json())
.then((res) => res);
const { error, message } = await xpay.confirmPayment("card", clientSecret, customer, encryptionKey);
setLoading(false);
alert(message);
} catch (e) {
setLoading(false);
alert(e.message);
}
}
return(
<div id="payment-form">
<PaymentElement options={options} onReady={(event) => {
console.log("ready event", event);
}} />
<Button type="primary" onClick={payNow} loading={loading}>
Pay Now
</Button>
</div>
)
}Here, the e2e flow remains the same, and the frontend will call the backend, which will call the create PI API and PI CS, and an encryption key will be passed to the frontend, and those will be passed while calling the XPay confirmPayment SDK method.