Skip to content

Create Web development - #299

Open
Samkulkarni11 wants to merge 1 commit into
CodeWithHarry:mainfrom
Samkulkarni11:patch-1
Open

Samkulkarni11 wants to merge 1 commit into
CodeWithHarry:mainfrom
Samkulkarni11:patch-1

Conversation

@Samkulkarni11

Copy link
Copy Markdown

No description provided.

@Anuragkumar6389

Copy link
Copy Markdown

This course is very helpful in my journey.
Thankyou so much sir

@chefpattBSc

Copy link
Copy Markdown

const express = require('express');
const app = express();
app.use(express.json());

// Daraja M-Pesa STK Push Confirmation Webhook Listener
app.post('/api/v1/payments/daraja-callback', (req, res) => {
try {
const { Body } = req.body;
const resultCode = Body.stkCallback.ResultCode;
const metadata = Body.stkCallback.CallbackMetadata?.Item;

    if (resultCode === 0) {
        const amount = metadata.find(i => i.Name === 'Amount').Value;
        const receipt = metadata.find(i => i.Name === 'MpesaReceiptNumber').Value;
        const phone = metadata.find(i => i.Name === 'PhoneNumber').Value;

        console.log(`[VERIFIED PAYMENT] Receipt: ${receipt} | Amount: KES ${amount} | Phone: ${phone}`);
        // Trigger automated order dispatch & SMS confirmation logic here
    } else {
        console.log(`[PAYMENT FAILED] Error Code: ${resultCode}`);
    }

    res.status(200).json({ ResultCode: 0, ResultDesc: "Accepted" });
} catch (error) {
    console.error("Webhook Error:", error);
    res.status(500).json({ ResultCode: 1, ResultDesc: "Server Error" });
}

});

app.listen(3000, () => console.log('Daraja Webhook Engine Active on Port 3000'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants