public async Task <IActionResult> MpesaOnlineTransactionStatus(LipaNaMpesaOnlinePushStkResponse response) { LipaNaMpesaQueryStkResponse queryResult; try { var LipaNaMpesaOnlineQuery = new LipaNaMpesaQuery ( businessShortCode: _mpesaApiConfiguration.LNMOshortCode, passkey: _mpesaApiConfiguration.PassKey, timeStamp: DateTime.Now, checkoutRequestId: response.CheckoutRequestID ); queryResult = await _mpesaClient.QueryLipaNaMpesaTransactionAsync(LipaNaMpesaOnlineQuery, await RetrieveAccessToken(), MpesaRequestEndpoint.QueryLipaNaMpesaOnlineTransaction); } catch (MpesaAPIException ex) { _logger.LogError(ex, ex.Message); return(View().WithDanger("Error", ex.Message)); } var LNMOQueryResponse = queryResult; LNMOQueryResponse.PhoneNumber = response.PhoneNumber; return(RedirectToAction(nameof(MpesaPaymentConfirmation), LNMOQueryResponse)); }
/// <summary> /// Queries Mpesa Online Transaction Status /// </summary> /// <param name="lipaNaMpesaQuery">Transaction Query Data transfer object</param> /// <param name="accesstoken">Acccesstoken retrieved by the <c>GetAuthTokenAsync</c> method.</param> /// <param name="mpesaRequestEndpoint">Set to <c>MpesaRequestEndpoint.QueryLipaNaMpesaOnlieTransaction</c></param> /// <param name="cancellationToken">Cancellation Token</param> /// <returns> /// A JSON string containing data from MPESA API reposnse /// </returns> /// <remarks> /// Use only for transactions initiated with <c>MakeLipaNaMpesaOnlinePayment</c> method. /// For Other transaction based methods (C2B,B2C,B2B) use <c>QueryMpesaTransactionStatusAsync</c> method. /// </remarks> public LipaNaMpesaQueryStkResponse QueryLipaNaMpesaTransaction(LipaNaMpesaQuery lipaNaMpesaQuery, string accesstoken, string mpesaRequestEndpoint, CancellationToken cancellationToken = default) { var validator = new LipaNaMpesaQueryValidator(); var results = validator.Validate(lipaNaMpesaQuery); return(!results.IsValid ? throw new MpesaAPIException(HttpStatusCode.BadRequest, string.Join(Environment.NewLine, results.Errors.Select(x => x.ErrorMessage.ToString()))) : MpesaPostRequestAsync <LipaNaMpesaQueryStkResponse>(lipaNaMpesaQuery, accesstoken, mpesaRequestEndpoint, cancellationToken).GetAwaiter().GetResult()); }
private async Task ExecuteMpesaStkQueryCommand(LipaNaMpesaOnlinePushStkResponse response) { try { var LipaNaMpesaOnlineQuery = new LipaNaMpesaQuery ( businessShortCode: mpesaAPIConfiguration.LNMOshortCode, timeStamp: DateTime.Now, passkey: mpesaAPIConfiguration.PassKey, checkoutRequestId: response.CheckoutRequestID ); var stkQueryResults = await _mpesaClient.QueryLipaNaMpesaTransactionAsync(LipaNaMpesaOnlineQuery, await RetrieveAccessToken(), MpesaRequestEndpoint.QueryLipaNaMpesaOnlineTransaction); if (stkQueryResults.ResultCode.Equals("0")) { Device.BeginInvokeOnMainThread(() => { _dialogs.Alert(new AlertConfig() .SetMessage("Thank you for your payment!") .SetTitle(stkQueryResults.ResponseDescription) .SetAction(async() => await _navigationService.NavigateAsync("/NavigationPage/MpesaPushStkPage"))); }); } else { Device.BeginInvokeOnMainThread(() => { _dialogs.Alert(new AlertConfig() .SetMessage("Something went wrong with the transaction. Please try again") .SetTitle(stkQueryResults.ResponseDescription) .SetAction(async() => await _navigationService.NavigateAsync("/NavigationPage/MpesaPushStkPage"))); }); } } catch (MpesaAPIException ex) { Device.BeginInvokeOnMainThread(() => { _dialogs.Alert(new AlertConfig() .SetMessage(ex.Message.ToString()) .SetTitle(ex.StatusCode.ToString())); }); } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => { _dialogs.Alert(new AlertConfig() .SetMessage(ex.Message.ToString()) .SetTitle("Error")); }); } }