Skip to main content
Creates a new payment session and optionally redirects or embeds an iframe.
async createSession(
  sessionParameters: SessionParameters,
  parameters?: PayvioxParameters
): Promise<void | string>

Parameters

sessionParameters
SessionParameters
required
Object containing payment session details.
parameters
PayvioxParameters
Optional configuration for integration mode and behavior.

Return value

return
Promise<void | string>
Returns a Promise that resolves to:
  • void when using redirect: true (page redirects before returning)
  • string (session ID) in all other cases
// With redirect
await payviox.createSession(params, { redirect: true });
// Page redirects immediately

// Without redirect
const sessionId = await payviox.createSession(params);
console.log('Session ID:', sessionId);

Direct Redirect (Skip Payment Page)

When you specify a paymentMethodId for a redirect-based payment provider (like Zen, Nicepay, PayPal, etc.), the SDK will automatically redirect the customer directly to the payment provider, bypassing the Payviox payment page entirely.
This feature works automatically when using redirect: true or iframeMode: true with a redirect-based provider.

How it works

  1. You call createSession() with a paymentMethodId for a redirect-based provider
  2. The API creates the session and initiates the payment in one call
  3. The SDK receives a redirect_url in the response
  4. The customer is redirected directly to the provider’s payment page

Example with Direct Redirect

const payviox = new Payviox('your_api_token');

// This will redirect DIRECTLY to Zen's payment page (not Payviox)
await payviox.createSession({
  amount: 5000,
  currency: 'USD',
  customer: '[email protected]',
  description: 'Premium Plan',
  paymentMethodId: 'zen_card', // Redirect-based provider
  order_id: 'order_' + Date.now(),
  items: [
    { name: 'Premium Plan', quantity: 1, price: 5000 }
  ]
}, {
  redirect: true  // Will redirect to provider directly
});

Supported Providers

Direct redirect works with these providers:
  • Zen (zen_card, zen_blik, etc.)
  • Nicepay (nicepay_va, nicepay_ewallet, etc.)
  • PayPal (paypal)
  • Payssion (various local payment methods)
  • Pallapay (crypto payments)
  • Crypto.com (cryptocom)
If you don’t specify a paymentMethodId, or use a non-redirect provider, the standard flow applies (redirect to Payviox payment page).

Handling Errors

If the payment initiation fails, the SDK will fall back to the standard Payviox payment page where the customer can try again or choose another payment method.
try {
  const sessionId = await payviox.createSession({
    amount: 5000,
    currency: 'USD',
    customer: '[email protected]',
    description: 'Premium Plan',
    paymentMethodId: 'zen_card',
    order_id: 'order_' + Date.now(),
    items: [{ name: 'Premium Plan', quantity: 1, price: 5000 }]
  }, { redirect: true });
  
  // If redirect didn't happen, you have the session_id
  console.log('Session ID:', sessionId);
} catch (error) {
  console.error('Error:', error.message);
}

Examples

const payviox = new Payviox('your_api_token');

await payviox.createSession({
  amount: 5000,
  currency: 'USD',
  customer: '[email protected]',
  description: 'Premium Plan',
  paymentMethodId: 'pm_card',
  order_id: 'order_' + Date.now(),
  items: [
    { name: 'Premium Plan', quantity: 1, price: 5000 }
  ]
}, {
  redirect: true
});

Error handling

try {
  const sessionId = await payviox.createSession(sessionParams);
  console.log('Success:', sessionId);
} catch (error) {
  if (error.message.includes('Unauthorized')) {
    console.error('Invalid API token');
  } else if (error.message.includes('Bad request')) {
    console.error('Invalid parameters:', error.message);
  } else {
    console.error('Payment error:', error.message);
  }
}