Your First Payment

Create and process your first USDC payment in just a few steps.

1

Set Up Your Project

Install the SDK if you haven't already:

npm install @zyntrialabs/zynpay-sdk ethers
2

Initialize the Client

Create a ZynPay client with your merchant wallet address:

import { ZynPayClient, Environment } from '@zyntrialabs/zynpay-sdk';

const client = new ZynPayClient({
  merchantWallet: '0xYourMerchantWalletAddress', // Replace with your wallet
  environment: Environment.TESTNET,
  defaultChain: 'base',
  verbose: true // Enable logging for debugging
});
3

Connect Customer's Wallet

Use ethers.js to connect to the customer's MetaMask wallet:

import { BrowserProvider } from 'ethers';

async function connectWallet() {
  // Check if MetaMask is installed
  if (!window.ethereum) {
    throw new Error('Please install MetaMask');
  }

  // Request account access
  const provider = new BrowserProvider(window.ethereum);
  await provider.send('eth_requestAccounts', []);
  
  // Get signer
  const signer = await provider.getSigner();
  const address = await signer.getAddress();
  
  console.log('Connected wallet:', address);
  return signer;
}
4

Create a Payment

Create a payment request for a specific amount:

// Create payment for $49.99 USDC
const payment = client.createPayment(49.99, 'base');

console.log('Payment created:', {
  id: payment.id,
  amount: payment.amount,
  chain: payment.chain
});
5

Process the Payment

Execute the payment using the customer's wallet:

async function processPayment() {
  try {
    // Connect wallet
    const signer = await connectWallet();
    
    // Check balance
    const customerAddress = await signer.getAddress();
    const balance = await client.getBalance(customerAddress);
    console.log('Customer balance:', balance.usdc, 'USDC');
    
    if (balance.usdc < payment.amount) {
      throw new Error('Insufficient USDC balance');
    }
    
    // Process payment
    console.log('Processing payment...');
    const txHash = await client.pay(payment, signer, true);
    
    console.log('โœ… Payment successful!');
    console.log('Transaction hash:', txHash);
    console.log('View on explorer:', client.getExplorerUrl('base', txHash));
    
    return txHash;
  } catch (error) {
    if (error.code === 'ACTION_REJECTED') {
      console.log('Payment cancelled by user');
    } else {
      console.error('Payment failed:', error);
    }
    throw error;
  }
}

// Execute
processPayment();
6

Verify Payment

Check your merchant wallet to confirm you received the payment:

// Check your merchant wallet balance
const merchantBalance = await client.getBalance(
  '0xYourMerchantWalletAddress',
  'base'
);

console.log('Merchant balance:', merchantBalance.usdc, 'USDC');

// You should have received 97% of the payment
// For a $49.99 payment, you get ~$48.49

What Just Happened?

  1. Customer connected their MetaMask wallet
  2. Your app created a payment request for $49.99 USDC
  3. Customer approved the transaction in MetaMask
  4. Smart contract split the payment: $48.49 to you, $1.50 to protocol
  5. USDC instantly appeared in your merchant wallet