TypeScript library to interact with VIC PaymentGateway
This library provide functions for both frontend and backend to interact with PaymentGateway
Provide functions for:
- User connect with web3 wallet
- User switch chain
- User execute pay on smart contract
Provide functions for:
- BE Merchant send request to create payment session
- BE Merchant verify payment event received on webhook
- BE Merchant query payments information
We can directly install from npm:
npm install vic-payment-js
yarn add vic-payment-js
All modules packaged in vic-payment-js, however the usage for frontend and backend is completely different.
The key different is on frontend we don't require any apiKey to initialized library, but on backend we require apiKey
to interact with PaymentGateway.
On Frontend, we use the Client
module from vic-payment-js
Import Client into project:
import { Client } from "vic-payment-js";
Client module provide static method to directly handle PaymentSession returned from BE Merchant, ex:
// Ask User to pay for Order
const session = /*...from BE Merchant ...*/;
Client.userPay(session);
On Backend, we use the Session
, Event
module from vic-payment-js
Session module
Import Session into project:
import { Session } from "vic-payment-js/lib/session";
// Initialize session with apiKey
const session = Session.init({ apiKey });
Session module provide methods to send request to PaymentGateway to create PaymentSession, ex:
const paymentSession = await session.ofUserPay({
paymentId: nanoid(12),
amount: 50000,
chainName: "BINANCE",
metadata: {
orderId: req.params.id,
},
});
Event module
Import Event into project:
import { Event } from "vic-payment-js/lib/session";
// Initialize event with apiKey
const event = Event.init({ apiKey });
Event module provide methods to verify PaymentEvent
sent from PaymentGateway, ex:
router.post("/api/test/webhook", async (req, res) => {
const webhookEvent = req.body;
try {
const paymentEvent = event.verifySignature(webhookEvent);
console.log("paymentEvent", paymentEvent);
return res.json({ message: "Received" });
} catch (err) {
console.log("Fail to verify event", err.message);
return res.json({ message: err.message });
}
});
PaymentEvent
has base schema:
type PaymentEvent = {
paymentId: string;
chainName: string;
metadata?: Record<string, unknown>;
merchantAddress: string;
vicTokenAddress: string;
paymentGatewayAddress: string;
};