Complete Integration Guide

KLYCoin Merchant Integration Guide

Complete guide for merchants to integrate KLYCoin x402 payments into their platforms

Overview

KLYCoin x402 allows merchants to accept crypto payments while automatically crediting KolayMiles rewards to customers

Key Features
✅ Instant Payments: ERC-20 transfers with real-time settlement
✅ Automatic Miles: Customers earn rewards on every transaction
✅ Low Fees: 0.30% protocol fee (configurable)
✅ Multiple Assets: Support for USDC, EURC, and other ERC-20 tokens
✅ Oracle-Signed Rates: Secure, auditable miles calculation
✅ Merchant Dashboard: Track transactions and conversions
✅ Webhooks: Real-time payment notifications
Supported Networks & Assets

Networks

  • BSC Testnet: Development & Testing
  • BSC Mainnet: Production (coming soon)

Payment Assets

  • USDC (BEP-20)
  • EURC (BEP-20)
  • Any standard ERC-20 stablecoin on BSC
Quick Start

Quick Start

Get up and running in minutes

1. Prerequisites
  • Your merchant wallet address
  • Basic understanding of blockchain/crypto payments
  • Node.js 18+ (for backend integration)
  • Frontend access to Web3 wallets (MetaMask, WalletConnect, etc.)
2. Get Your Merchant Address

Your merchant address is the wallet address where you'll receive payments.

# Example merchant address
MERCHANT_ADDRESS=0x8ba1f109551bD432803012645Hac136c461c0000

⚠️ Important

Keep your merchant private key secure. Use a hardware wallet for production.

3. Test Network Configuration
// Contract addresses (BSC Testnet)
const CONFIG = {
  facilitator: "0x565a7Af88C3C00757a00098f3fcc6b8fB355c899",
  paymentsApi: "http://localhost:8081",
  oracleApi: "http://localhost:8090",
  rpcUrl: "https://data-seed-prebsc-1-s1.binance.org:8545",
  assets: {
    usdc: "0x64544969ed7EBf5f083679233325356EbE738930"
  }
};
4. Test Your First Payment
curl -X POST http://localhost:8081/api/x402/charge \
  -H "Content-Type: application/json" \
  -d '{
    "payer": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
    "merchant": "0x8ba1f109551bD432803012645Hac136c461c0000",
    "asset": "USDC",
    "amount": 10.50,
    "metadata": {
      "receiptId": "ORDER-12345"
    }
  }'

Payment Flow

Understand how payments are processed from start to finish

Architecture Flow
Customer Checkout
    ↓
Frontend (Web3 Wallet)
    ↓ (1) Create Charge
KLYCoin Payments API
    ↓ (2) Get Oracle Rate
Oracle Service
    ↓ (3) Sign Rate
KLYCoin Payments API
    ↓ (4) Return Signed Payload
Frontend
    ↓ (5) Execute payWithKolay()
KolayFacilitator Contract (BSC)
    ↓ (6) Emit Paid Event
Listener Service
    ↓ (7) Forward to KolayMiles
KolayMiles API
    ↓ (8) Credit Miles + Send Webhook
Merchant Backend
Step-by-Step Flow

Step 1: Customer Initiates Payment

Customer selects "Pay with Kolay" at checkout and connects wallet.

Step 2: Create Payment Charge

POST /api/x402/charge
{
  "payer": "0x...",        // Customer wallet
  "merchant": "0x...",     // Your merchant address
  "asset": "USDC",         // Payment token
  "amount": 10.50,         // Amount in USD
  "metadata": {
    "receiptId": "ORDER-123",
    "description": "Product Purchase"
  }
}

Step 3: Get Oracle-Signed Rate

POST /api/v1/prepare-payment
{
  "payer": "0x...",
  "merchant": "0x...",
  "asset": "0x...",        // USDC address
  "amount": "10500000"     // In wei (10.5 USDC)
}

Step 4: Execute On-Chain Payment

// 1. Approve facilitator to spend tokens
await erc20.approve(facilitatorAddress, amountWei);

// 2. Execute payment
await facilitator.payWithKolay(
  assetAddress,
  amountWei,
  merchantAddress,
  receiptId,
  oracleRate,
  oracleSignature
);

Step 5: Receive Webhook Notification

POST /your-webhook-endpoint
{
  "x402Id": "402-ORDER-123-abc",
  "txHash": "0x...",
  "status": "succeeded",
  "amount": 10.50,
  "payer": "0x...",
  "merchant": "0x...",
  "milesCredited": 2500,
  "timestamp": 1730500000
}
API Reference

API Reference

Complete API documentation for KLYCoin services

POST /api/x402/charge
Creates a new payment charge

Request

{
  "payer": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
  "merchant": "0x8ba1f109551bD432803012645Hac136c461c0000",
  "asset": "USDC",
  "amount": 10.50,
  "metadata": {
    "receiptId": "ORDER-123",
    "description": "Product purchase"
  }
}

Response

{
  "ok": true,
  "x402Id": "402-ORDER-123-abc",
  "expires_in": 300,
  "signature": "0x...",
  "status": "pending"
}
POST /api/x402/webhook
Receives payment confirmations
{
  "x402Id": "402-ORDER-123-abc",
  "txHash": "0x...",
  "status": "succeeded",
  "amount": 10.50,
  "payer": "0x...",
  "merchant": "0x...",
  "timestamp": 1730500000
}
Code Examples

Code Examples

Real-world integration examples

React / Next.js Integration
Complete payment flow implementation
// lib/payment.ts
import { ethers } from 'ethers';
import KOLAY_FACILITATOR_ABI from '@/lib/abi/KolayFacilitator.json';

const CONFIG = {
  facilitator: '0x565a7Af88C3C00757a00098f3fcc6b8fB355c899',
  paymentsApi: process.env.NEXT_PUBLIC_PAYMENTS_URL || 'http://localhost:8081',
  facilitatorApi: process.env.NEXT_PUBLIC_FACILITATOR_URL || 'http://localhost:8082',
};

export async function executePayment({
  provider,
  amount,
  assetAddress,
  merchantAddress,
  receiptId
}: {
  provider: ethers.BrowserProvider;
  amount: string;
  assetAddress: string;
  merchantAddress: string;
  receiptId: string;
}) {
  const signer = await provider.getSigner();
  const payerAddress = await signer.getAddress();

  // Step 1: Get oracle-signed rate
  const prepareRes = await fetch(`${CONFIG.facilitatorApi}/api/v1/prepare-payment`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      payer: payerAddress,
      merchant: merchantAddress,
      asset: assetAddress,
      amount: ethers.parseUnits(amount, 18).toString()
    })
  });

  const { oracle, payment } = await prepareRes.json();

  // Step 2: Approve facilitator
  const erc20 = new ethers.Contract(
    assetAddress,
    ['function approve(address,uint256) external returns(bool)'],
    signer
  );

  const amountWei = ethers.parseUnits(amount, 18);
  const approveTx = await erc20.approve(CONFIG.facilitator, amountWei);
  await approveTx.wait();

  // Step 3: Execute payment
  const facilitator = new ethers.Contract(
    CONFIG.facilitator,
    KOLAY_FACILITATOR_ABI.abi,
    signer
  );

  const payTx = await facilitator.payWithKolay(
    assetAddress,
    amountWei,
    merchantAddress,
    receiptId,
    oracle.payload,
    oracle.signature
  );

  const receipt = await payTx.wait();
  
  return {
    txHash: receipt.hash,
    blockNumber: receipt.blockNumber,
    milesCredited: payment.milesCredited,
    fee: payment.fee
  };
}
Payment Button Component
Reusable React component for payments
// components/PayWithKolayButton.tsx
'use client';

import { useState } from 'react';
import { executePayment } from '@/lib/payment';
import toast from 'react-hot-toast';

export function PayWithKolayButton({
  amount,
  assetAddress,
  merchantAddress,
  orderId
}: {
  amount: string;
  assetAddress: string;
  merchantAddress: string;
  orderId: string;
}) {
  const [loading, setLoading] = useState(false);

  const handlePay = async () => {
    if (!window.ethereum) {
      toast.error('Please install MetaMask');
      return;
    }

    try {
      setLoading(true);

      const provider = new ethers.BrowserProvider(window.ethereum);
      
      const result = await executePayment({
        provider,
        amount,
        assetAddress,
        merchantAddress,
        receiptId: orderId
      });

      toast.success(`Payment confirmed! Miles credited: ${result.milesCredited}`);
      
      // Handle success (redirect, update UI, etc.)
      console.log('Payment result:', result);
      
    } catch (error: any) {
      console.error('Payment error:', error);
      toast.error(error.message || 'Payment failed');
    } finally {
      setLoading(false);
    }
  };

  return (
    <button
      onClick={handlePay}
      disabled={loading}
      className="px-6 py-3 bg-blue-600 text-white rounded-lg disabled:opacity-50"
    >
      {loading ? 'Processing...' : `Pay ${amount} with Kolay`}
    </button>
  );
}

Dashboard & Analytics

Track transactions, conversions, and customer metrics

Access Your Merchant Dashboard

Login at: https://kolaymiles.com/merchant

Transaction History: View all payments
Conversion Tracking: Miles → KLY conversions
Analytics: Volume, frequency, customer metrics
Miles Earned: Total rewards distributed
Webhook Logs: Payment notification history
Security & Best Practices

Security & Best Practices

Protect your integration with security best practices

Security Checklist
✅ Use HTTPS for all API calls in production
✅ Validate webhooks with HMAC signatures
✅ Store secrets securely (use environment variables)
✅ Verify transaction confirmations on-chain
✅ Rate limit your payment endpoints
✅ Log all transactions for audit trail
✅ Monitor for unusual activity
Webhook Security
Always verify webhook signatures
import crypto from 'crypto';

function verifyWebhook(body: string, signature: string, secret: string): boolean {
  const hmac = crypto.createHmac('sha256', secret);
  const expected = `sha256=${hmac.update(body).digest('hex')}`;
  return crypto.timingSafeEqual(
    Buffer.from(signature),
    Buffer.from(expected)
  );
}

// In your webhook handler
app.post('/webhooks/payment', (req, res) => {
  const signature = req.headers['x-kolay-signature'];
  const isValid = verifyWebhook(
    JSON.stringify(req.body),
    signature,
    process.env.WEBHOOK_SECRET
  );

  if (!isValid) {
    return res.status(401).json({ error: 'Invalid signature' });
  }

  // Process webhook
  processPayment(req.body);
  
  res.json({ ok: true });
});

Testing

Test your integration on BSC Testnet

Test on BSC Testnet

1. Get Testnet BNB

Get testnet BNB from faucets:

  • https://testnet.bnbchain.org/faucet-smart
  • https://www.bnbchain.org/en/testnet-faucet

2. Get Testnet USDC

Swap on PancakeSwap testnet or contact support for faucet

3. Test Payment Flow

curl -X POST http://localhost:8082/api/v1/prepare-payment \
  -H "Content-Type: application/json" \
  -d '{
    "payer": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
    "merchant": "0x8ba1f109551bD432803012645Hac136c461c0000",
    "asset": "0x64544969ed7EBf5f083679233325356EbE738930",
    "amount": "1000000000000000000"
  }'

Support

Get help when you need it

Resources
Documentation: https://docs.klycoin.io
API Reference: https://api-docs.klycoin.io
Dashboard: https://kolaymiles.com/merchant
Status Page: https://status.klycoin.io
Contact
Email: merchants@klycoin.io
Discord: https://discord.gg/klycoin
Telegram: @klycoin_support
Common Issues

Q: Payment stuck "pending"

A: Check customer approved transaction in wallet, sufficient gas (BNB) in wallet, network connectivity, and BSC network is selected in wallet.

Q: Webhook not received

A: Check webhook URL is publicly accessible (no localhost), SSL certificate is valid, firewall allows inbound requests, and HMAC signature verification is correct.

Q: Miles not credited

A: Check oracle service is running, rate signature is valid, customer address is correct, and check on-chain event logs.

Next Steps
✅ Set up your merchant wallet
✅ Configure webhook endpoint
✅ Test payment flow on BSC Testnet
✅ Integrate into your checkout
✅ Monitor first transactions
✅ Request mainnet access

Ready to go live?

Contact us at merchants@klycoin.io for mainnet setup and merchant verification.

Ready to Integrate?

Start accepting crypto payments with automatic KolayMiles rewards today.

WhatsApp: +905447693163

Cookie Preferences

We use cookies to improve your experience. Strictly necessary cookies are always on. Others require your consent.

See our Cookie Policy for details.