clink-typescript-sdk-test
v0.0.5
Published
npm package for clinkbill
Downloads
16
Readme
ClinkPay Client SDK Guide
Lightweight client for Clink billing APIs, It supports production and sandbox environments and handles authentication and errors for you.
Requirements
- Runtime with
fetchsupport (e.g., Node.js 18+). - Only
envvaluesproductionandsandboxare supported; any other value throws. the default value isproduction.
Import
Choose your package management tool (such as npm, yarn, or pnpm) and import it according to your project configuration.
npm install clink-typescript-sdkQuick Start
import { ClinkPayClient } from 'clink-typescript-sdk';
const client = new ClinkPayClient({
apiKey: 'YOUR_API_KEY',
env: 'sandbox',
});
async function main() {
const session = await client.createCheckoutSession({
originalAmount: 1999,
originalCurrency: 'USD',
successUrl: 'https://merchant.example.com/success',
cancelUrl: 'https://merchant.example.com/cancel',
allowPromotionCodes: true,
});
console.log(session.sessionId);
console.log(session.url);
}
main();API Overview
createCheckoutSession(options): Create a checkout session and get the redirecturl.getCheckoutSession(sessionId): Retrieve checkout session details.getOrder(orderId): Retrieve order details.getRefund(refundId): Retrieve refund details.getSubscription(subscriptionId): Retrieve subscription details.getInvoice(invoiceId): Retrieve subscription invoice details.customerPortalSession(options): Create a customer portal session and get the access link.
All methods are asynchronous and throw on errors.
Error Handling
On non-successful responses or network errors, methods throw exceptions:
- Business errors throw
ClinkApiError. - Non-business errors throw standard
Error.
Example:
import { ClinkPayClient } from 'clink-typescript-sdk';
const client = new ClinkPayClient({ apiKey: 'YOUR_API_KEY', env: 'sandbox' });
async function demo() {
try {
const order = await client.getOrder('ord_123');
console.log(order.status);
} catch (e) {
if (e instanceof ClinkApiError) {
const { code, message } = e;
// your code here
}
// handle other errors
}
}
demo();Detailed Examples
Create a Checkout Session
const client = new ClinkPayClient({ apiKey: 'YOUR_API_KEY', env: 'sandbox' });
const session = await client.createCheckoutSession({
originalAmount: 4999,
originalCurrency: 'USD',
successUrl: 'https://merchant.example.com/success',
cancelUrl: 'https://merchant.example.com/cancel',
});
console.log(session.sessionId);
console.log(session.url);Get Checkout Session
const info = await client.getCheckoutSession('sess_123');
console.log(info.status);Get Order
const order = await client.getOrder('ord_123');
console.log(order.amountTotal);Get Refund
const refund = await client.getRefund('rfd_123');
console.log(refund.status);Get Subscription
const sub = await client.getSubscription('sub_123');
console.log(sub.status);Get Invoice
const invoice = await client.getInvoice('inv_123');
console.log(invoice.status);Customer Portal Session
const portal = await client.customerPortalSession({
customerId: 'cus_123',
returnUrl: 'https://merchant.example.com/return',
});
console.log(portal.url);Get Product Info
const product = await client.getProduct('prod_123');
console.log(product);Get Product List
const products = await client.getProductList({
page: 1,
pageSize: 10,
});
console.log(products);Get Price Info
const price = await client.getPrice('price_123');
console.log(price);Get Price List
const prices = await client.getPriceList({
page: 1,
pageSize: 10,
});
console.log(prices);Get Payment Instrument List
const paymentInstruments = await client.getPaymentInstrumentList({
customerId: 'cus_123',
type: 'card',
});
console.log(paymentInstruments);webhook
Verify and Get Webhook Event
import { ClinkWebhook } from 'clink-typescript-sdk';
const clinkWebhook = new ClinkWebhook({ signatureKey: 'YOUR_SIGNATURE_KEY' });
// if verify error, the verifyAndGet method will throw error
const event = clinkWebhook.verifyAndGet({
timestamp: 'timestamp from request header',
body: 'body(jsonString) from request body',
headerSignature: 'signature from request header',
});
console.log(event);