Skip to content

Instantly share code, notes, and snippets.

@ngangavic
Last active December 26, 2019 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngangavic/d0a70ca89f320ee77f98d68e8a332c1c to your computer and use it in GitHub Desktop.
Save ngangavic/d0a70ca89f320ee77f98d68e8a332c1c to your computer and use it in GitHub Desktop.
Mpesa Api Android dependency sample Activity file
package com.example.mpesaapi;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import com.ngangavictor.mpesa.api.LNMPSettings;
import org.json.JSONException;
import java.io.IOException;
import static com.ngangavictor.mpesa.api.Mpesa.sktPush;
public class MainActivity extends AppCompatActivity {
Button buttonLNMP;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT > 8) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
LNMPSettings.setBusiness_short_code("174379");
LNMPSettings.setCallback_url("http://smartforex.co.ke/android/mpesa/listener.php");
LNMPSettings.setConsumer_key("AL4cs1jYio03B97Bvri5SWaPsQ1upawY");
LNMPSettings.setConsumer_secret("tIO5wyY43Gobzt6C");
LNMPSettings.setPhone("254708374149");
LNMPSettings.setStk_push_url("https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest");
LNMPSettings.setTimeout_url("http://smartforex.co.ke/android/mpesa/timeout.php");
LNMPSettings.setTransaction_desc("testing my api");
LNMPSettings.setTransaction_type("CustomerPayBillOnline");
LNMPSettings.setAmount("1");
LNMPSettings.setAccess_token_url("https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials");
LNMPSettings.setAccount_reference("vic10020");
LNMPSettings.setPasskey("bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919");
buttonLNMP = findViewById(R.id.btn_lnmp);
//lina na mpesa online
buttonLNMP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (sktPush().equals("0")) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment