Android Payment Using MTN Mobile Money, Airtel Money, Africell Mobile Money and UTL m-sente, Free Code Project

Step 1:
Go to google play store and download "Easy Pay Mobile Wallet" application

Step 2:
Use the App to create your account at easy Pay, and create an Mobile Money API that will allow you to transfer money across almost all networks in Africa.

Step 3:
Create a new android Empty Activity Using your android studio, named "EasyPayApp"

Step 4:
Copy the code Below to your "MainActivity.java" File

       

package com.tour.easypayapp;

/*
* Developed by Wagaana Alex
* */

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText amount=(EditText) findViewById(R.id.amount);
        final EditText phone=(EditText) findViewById(R.id.phoneNumber);
        Button mpay=(Button) findViewById(R.id.pay);
        final String transaction_amount=amount.getText().toString();
        final String transaction_phone=phone.getText().toString();

        mpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean pay=true;
                if (transaction_amount!=null && !transaction_amount.isEmpty()){
                    pay=false;
                }else{
                    amount.setError("Amount Is Required");
                }

                if (transaction_phone!=null && !transaction_phone.isEmpty()){
                    pay=false;
                }else{
                    phone.setError("Phone Is Required");
                }

                if (pay) {
                    CallAPI callAPI = new CallAPI(transaction_amount, transaction_phone, MainActivity.this);
                    callAPI.execute();
                }

            }
        });
    }

    public static class CallAPI extends AsyncTask {

        private String post_url="https://www.easypay.co.ug/api/";
        private String client_id="your client id";//change this
        private String secret="your secret";//change this
        private String transaction_action="mmdeposit";
        private String transaction_amount="";
        private String transaction_phone="";
        private String currency="UGX";
        private String reference="1";
        private String reason="Lodge payment";
        Context context;

        public CallAPI(String transaction_amount, String transaction_phone, Context context){
            this.transaction_amount=transaction_amount;
            this.transaction_phone=transaction_phone;
            this.context=context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                final String POST_PARAMS = "{" +
                        "\"username\": \""+client_id+"\"," +
                        "\"password\": \""+secret+"\"," +
                        "\"action\": \""+transaction_action+"\"," +
                        "\"amount\": \"" + transaction_amount + "\"," +
                        "\"currency\": \""+currency+"\"," +
                        "\"phone\": \"" + transaction_phone + "\"," +
                        "\"reference\": \"" + reference + "\"," +
                        "\"reason\": \"" + reason + "\"}";
                System.out.println(POST_PARAMS);
                URL obj = new URL(post_url);
                HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
                postConnection.setRequestMethod("POST");
                postConnection.setRequestProperty("Content-Type", "application/json");
                postConnection.setDoOutput(true);
                OutputStream os = postConnection.getOutputStream();
                os.write(POST_PARAMS.getBytes());
                os.flush();
                os.close();
                int responseCode = postConnection.getResponseCode();
                System.out.println("POST Response Code : " + responseCode);
                System.out.println("POST Response Message : " + postConnection.getResponseMessage());
                BufferedReader in = new BufferedReader(new InputStreamReader(postConnection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                JSONObject js = new JSONObject(String.valueOf(response));
                if(js.get("success").toString().equals("0")) {
                    Log.e("FAILED: ", js.get("errormsg").toString());
                }else if(js.get("success").toString().equals("1")) {
                    Log.e("WORKED: ", response.toString());
                }
            }catch (Exception e){
                Log.e("WORKED: ",  "Error: "+e);
            }
            return "";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    }
}


       
 
Step 5:
Copy the code below to your "activity_main.xml" file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="10dp"
    tools:context=".MainActivity">

    <EditText
        android:layout_marginTop="10dp"
        android:id="@+id/amount"
        android:hint="Amount to pay in UGX"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_marginTop="10dp"
        android:id="@+id/phoneNumber"
        android:hint="Phone number with country code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_marginTop="10dp"
        android:id="@+id/pay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Make Payment" />

</LinearLayout>

Step 6:
Add the following two lines to tour "AndroidManifest.xml" File and run your program




<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Note: you can download a complete source code from this Link

Comments

Popular posts from this blog

In three steps Add Mobile Payments to your application using MTN Mobile Money, Airtel Money, Africell Mobile Money and UTL m-sente, Payments to your Android Application, or any other JAVA application