Skip to content

GoPayLocal Client

Full API reference for the GoPayLocal JavaScript SDK client class.

The GoPayLocal class is the core of the JavaScript SDK. It handles initialization, data fetching, price calculation, and banner rendering.

import { GoPayLocal } from '@gopaylocal/js';
const gpl = new GoPayLocal({
apiKey: 'gpl_pk_live_abc123',
});

See Installation for all configuration options.

Initialize the SDK by fetching PPP data for the current visitor. Call this once on page load.

const data = await gpl.init();

Returns: Promise<PPPData | null>

  • Returns PPPData if the visitor is eligible for a discount
  • Returns null if no discount applies (e.g., US visitor, VPN detected)
  • Uses sessionStorage cache if available (5-minute TTL)
  • Has an 8-second request timeout
const data = await gpl.init();
if (data) {
console.log(data.country); // "IN"
console.log(data.countryName); // "India"
console.log(data.discount); // 30
console.log(data.code); // "PPP-INDIA-30"
console.log(data.currencyCode); // "INR"
console.log(data.currencySymbol); // "₹"
console.log(data.fxRate); // 83.12
console.log(data.flag); // "🇮🇳"
console.log(data.show); // true
}

Get the current PPP data without triggering a fetch. Returns null if init() has not been called or no discount applies.

const data = gpl.getData();

Returns: PPPData | null

Check if data is currently being fetched.

if (gpl.isLoading()) {
showSpinner();
}

Returns: boolean

Get the last error, if any.

const error = gpl.getError();
if (error) {
console.error('GoPayLocal failed:', error.message);
}

Returns: Error | null

Get the coupon code.

const code = gpl.getCode();
// "PPP-INDIA-30" or null

Returns: string | null

Get country information for the current visitor.

const country = gpl.getCountry();
if (country) {
console.log(country.code); // "IN"
console.log(country.name); // "India"
console.log(country.flag); // "🇮🇳"
}

Returns: { code: string; name: string; flag: string } | null


Calculate a localized, discounted price from a base price.

const pricing = gpl.getPrice(99.99);

Parameters:

ParameterTypeDefaultDescription
basePricenumber(required)Price in your base currency
options.localizebooleantrueConvert to visitor’s local currency
options.showDecimalbooleantrueInclude decimal digits

Returns: PricingResult

interface PricingResult {
originalPrice: number; // 99.99
discountedPrice: number; // 69.99 (after 30% discount)
localPrice: number; // 5819.27 (in visitor's currency)
formatted: string; // "₹5,819.27"
formattedOriginal: string; // "$99.99"
integer: string; // "5,819"
decimal: string; // "27"
currencySymbol: string; // "₹"
currencyCode: string; // "INR"
hasDiscount: boolean; // true
discount: number; // 30
}

Examples:

// With localization (default)
const p1 = gpl.getPrice(99.99);
console.log(p1.formatted); // "₹5,819"
// Without localization (discount only, stay in USD)
const p2 = gpl.getPrice(99.99, { localize: false });
console.log(p2.formatted); // "$69.99"
// Without decimals
const p3 = gpl.getPrice(99.99, { showDecimal: false });
console.log(p3.formatted); // "₹5,819"

Format a number as currency using Intl.NumberFormat.

const formatted = gpl.formatPrice(5819.27);
// "₹5,819.27"
const formattedUsd = gpl.formatPrice(99.99, 'USD');
// "$99.99"

Parameters:

ParameterTypeDefaultDescription
amountnumber(required)Number to format
currencyCodestringVisitor’s currencyCurrency code (ISO 4217)

Returns: string


Subscribe to data changes. The listener is called whenever PPP data is loaded or cleared.

const unsubscribe = gpl.subscribe((data) => {
if (data) {
updatePricingUI(data);
}
});
// Later, to stop listening:
unsubscribe();

Parameters:

ParameterTypeDescription
listener(data: PPPData | null) => voidCallback for data changes

Returns: () => void (unsubscribe function)

This is useful for reactive frameworks like Vue or Svelte:

// Vue 3 example
import { ref, onMounted, onUnmounted } from 'vue';
const pppData = ref(null);
let unsubscribe;
onMounted(async () => {
unsubscribe = gpl.subscribe((data) => {
pppData.value = data;
});
await gpl.init();
});
onUnmounted(() => {
unsubscribe?.();
});

Show a PPP discount banner. Returns a handle with a remove() method.

const banner = gpl.showBanner({
position: 'bottom-right',
backgroundColor: '#1a1a2e',
onClose: () => console.log('Banner closed'),
});
// Later, to remove the banner:
banner.remove();

See Banner for full options.


import { GoPayLocal } from '@gopaylocal/js';
const gpl = new GoPayLocal({
apiKey: 'gpl_pk_live_abc123',
onLoad: (data) => {
console.log(`Loaded PPP data for ${data.countryName}`);
},
onError: (error) => {
console.error('Failed to load PPP data:', error);
},
});
async function initPricing() {
const data = await gpl.init();
if (!data) {
// No discount — show original prices
return;
}
// Update pricing display
const plans = [
{ name: 'Pro', basePrice: 9 },
{ name: 'Business', basePrice: 29 },
];
for (const plan of plans) {
const pricing = gpl.getPrice(plan.basePrice);
const el = document.querySelector(`[data-plan="${plan.name}"]`);
if (el) {
el.querySelector('.price').textContent = pricing.formatted;
el.querySelector('.period').textContent = '/mo';
if (pricing.hasDiscount) {
el.querySelector('.original-price').textContent = pricing.formattedOriginal;
el.querySelector('.discount-badge').textContent = `${pricing.discount}% off`;
}
}
}
// Show the discount banner
gpl.showBanner({ position: 'bottom-right' });
}
initPricing();