Hey there, fellow JavaScript wizards! Ready to dive into the world of Razorpay API? Let's get our hands dirty with some data syncing magic for user-facing integrations. Buckle up!
Razorpay's API is a powerhouse for handling payments and managing customer data. When it comes to user-facing integrations, keeping everything in sync is crucial. Trust me, your users will thank you for it!
First things first, let's get that API set up:
npm install razorpay
Now, let's authenticate:
const Razorpay = require('razorpay'); const instance = new Razorpay({ key_id: 'YOUR_KEY_ID', key_secret: 'YOUR_KEY_SECRET' });
Pro tip: Keep those keys secret! We'll talk more about security later.
Time to fetch some data! Here's how you can grab customer info:
instance.customers.fetch('cust_1234567890') .then(customer => console.log(customer)) .catch(error => console.error(error));
Want transaction history? No sweat:
instance.payments.all({ from: '2023-01-01', to: '2023-12-31' }) .then(transactions => console.log(transactions)) .catch(error => console.error(error));
Creating a new customer? Easy peasy:
instance.customers.create({ name: 'John Doe', email: '[email protected]', contact: '9999999999' }) .then(customer => console.log(customer)) .catch(error => console.error(error));
Initiating a payment? Check this out:
instance.orders.create({ amount: 50000, currency: 'INR', receipt: 'receipt_123', notes: { key1: 'value3', key2: 'value2' } }) .then(order => console.log(order)) .catch(error => console.error(error));
Webhooks are your friend here. Set up an endpoint in your server:
app.post('/webhook', (req, res) => { const secret = 'YOUR_WEBHOOK_SECRET'; const shasum = crypto.createHmac('sha256', secret); shasum.update(JSON.stringify(req.body)); const digest = shasum.digest('hex'); if (digest === req.headers['x-razorpay-signature']) { console.log('Event received:', req.body); // Process the event res.json({ status: 'ok' }); } else { res.status(400).send('Invalid signature'); } });
Always wrap your API calls in try-catch blocks:
try { const result = await instance.payments.fetch(paymentId); // Handle success } catch (error) { if (error.statusCode === 400) { console.error('Bad request:', error.error.description); } else { console.error('Something went wrong:', error); } }
Respect rate limits and implement caching where possible:
const cache = new Map(); async function getCustomerWithCache(customerId) { if (cache.has(customerId)) { return cache.get(customerId); } const customer = await instance.customers.fetch(customerId); cache.set(customerId, customer); return customer; }
Never expose your API keys in client-side code. Use environment variables:
const instance = new Razorpay({ key_id: process.env.RAZORPAY_KEY_ID, key_secret: process.env.RAZORPAY_KEY_SECRET });
Use Razorpay's test environment to avoid real transactions:
const instance = new Razorpay({ key_id: 'rzp_test_1234567890', key_secret: 'YOUR_TEST_SECRET_KEY' });
There you have it, folks! You're now armed with the knowledge to read and write data like a pro using the Razorpay API. Remember, practice makes perfect, so get out there and start coding!
Got questions? Hit up the Razorpay docs or join a developer community. Keep pushing those pixels, and happy coding!