Esempio n. 1
0
        public async Task <IActionResult> StkPushCallbackAsync([FromBody] LipaNaMpesaCallback callback)
        {
            //do something with the call databack

            _logger.LogInformation(callback.ToString());
            return(Ok());
        }
Esempio n. 2
0
        public async Task <IActionResult> LipaNaMpesaCallback(string requestId, [FromBody] LipaNaMpesaCallback lipaNaMpesaCallback)
        {
            // Handle callback data for processing. Either store data in a database or do further processing.

            if (lipaNaMpesaCallback is null)
            {
                return(Ok(new
                {
                    ResultCode = 1,
                    ResultDesc = "Transaction Rejected"
                }));
            }

            // To retrieve results from callback metadata

            var result = lipaNaMpesaCallback.Body.StkCallback.LipaNaMPesaCallbackMetadata.ResultParameter.ToDictionary(x => x.Key, x => x.Value);

            var test = result["MpesaReceiptNumber"];

            var filename = $"{requestId}.json";

            // Get root path directory
            var rootPath = Path.Combine(_environment.WebRootPath, "Application_Files\\MpesaResults\\");

            // To check if directory exists. If the directory does not exists we create a new directory
            if (!Directory.Exists(rootPath))
            {
                Directory.CreateDirectory(rootPath);
            }

            // Get the path of filename
            var filePath = Path.Combine(_environment.WebRootPath, "Application_Files\\MpesaResults\\", filename);

            await System.IO.File.WriteAllTextAsync(filePath, JsonConvert.SerializeObject(lipaNaMpesaCallback, Formatting.Indented));

            _logger.LogInformation(JsonConvert.SerializeObject(lipaNaMpesaCallback, Formatting.Indented));
            return(Ok(new
            {
                ResultCode = "00000000",
                ResultDesc = "Success"
            }));
        }