0

我正在尝试保存 MPesa 回调数据,当用户使用 Lipa Na Mpesa 付款时,需要使用来自令牌的解码用户名保存 CheckoutRequestID,然后如果付款成功,则 cllback URL 需要使用 mysql 更新关联的 CheckoutRequestID金额,从那里它需要用新值更新用户的钱包。目前代码运行良好,但我无法保存,当我尝试访问用户名或任何值时,回调不返回任何数据。这是 LipaNaMpesa 代码和 STK 回调代码。

LipaNaMpesa 代码

router.get('/stk', middleware.access, middleware.checkToken, (req, res) => {
    let endpoint = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
    let auth = "Bearer " + req.access_token


    let datenow = new Date()
    //console.log(datenow.getUTCDate())
    const timestamp = datenow.getFullYear() +"" + addZero((datenow.getMonth()+1))+"" +addZero(datenow.getUTCDate())+ ""+ addZero(datenow.getHours())+"" +  addZero(datenow.getMinutes())+"" +  addZero(datenow.getSeconds())
   // console.log(timestamp)
    const password = (new Buffer.from('174379' + 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919' + timestamp).toString('base64'))

    request(
        {
            url: endpoint,
            method: "POST",
            headers: {
                "Authorization": auth
            },
            json: {

                "BusinessShortCode": 174379,
                "Password": password,
                //"MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMjExMjExMDcyODAw"
                "Timestamp": timestamp,
                "TransactionType": "CustomerPayBillOnline",
                "Amount": 1,
                "PartyA": 254792482180,//254792482180,
                "PartyB": 174379,
                "PhoneNumber": 254792482180,//254704148972,
                "CallBackURL": "https://9b68-41-80-113-243.ngrok.io/mrequests/stk_confirm",
                "AccountReference": "GoChama",
                "TransactionDesc": "Wallet Deposit"
            }
        },
        function (error, response, body) {
            if (error) {
                console.log(error)
            }
            else {
             // console.log(body.CheckoutRequestID)
               //console.log(req.decoded.username) 
              res.status(200).json(body)
            }
        }
    )
})

STK 回调代码

router.post('/stk_confirm', (req, res) => {
    console.log('....................... stk_confirm .............')
    console.log("Payload Received", req.body.Body.stkCallback)
   /* const callbackData = req.body.Body.stkCallback
    console.log("Payload Received", callbackData)
    var resultCode = callbackData.ResultCode;
    var checkoutId = callbackData.CheckoutRequestID
    var username = req.decoded.username
    if(resultCode === 0){
        const details = callbackData.CallbackMetadata.Item

        var mReceipt;
        var mPhoneNumber;
        var mAmount;

        await details.forEach(entry =>{
            switch (entry.Name){
                case "MpesaReceiptNumber":
                mReceipt = entry.Value
                break;

                case "PhoneNumber":
                mPhoneNumber = entry.Value
                break;

                case "Amount":
                mAmount = entry.Value
                break;

                default:
                    break;
            }
        })
        

    }*/
    res.status(200).json(req.body)
    
})

添加时,代码的注释部分不返回任何值。有什么我做错了吗?

4

0 回答 0