Nain Kazi
Nain Kazi

Reputation: 191

How to implement Telenor easypay API in Android

I am implementing telenor easypay API, Which consist of two Webservices to the first one I post my store id and other information which give me successful response with Auth_token and postbackurl.When I post the auth token and postback url to the next webservice URL https://easypaystg.easypaisa.com.pk/easypay/Confirm.jsf it redirects me to easypaisa checkout screen which show me the following error on easypay checkout screen.

please click below link to see error screenshot

My code:

private class PostTask extends AsyncTask < String, String, String > {
 @Override
 protected void onPreExecute() {
  super.onPreExecute();
  mBT.setEnabled(false);
 }

 @Override
 protected String doInBackground(String...data) {

  OkHttpClient client; // = new OkHttpClient();
  client = getUnsafeOkHttpClient();
  client.setHostnameVerifier(new HostnameVerifier() {
   @Override
   public boolean verify(String hostname, SSLSession session) {
    return true;
   }
  });

  MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
  RequestBody body = RequestBody.create(mediaType, "amount=10&orderRefNum=110&storeId=xxxx&postBackURL=https://www.jeevaysehat.com/");
  Request request = new Request.Builder()
   .url("https://easypaystg.easypaisa.com.pk/easypay/Index.jsf")
   .post(body)
   .addHeader("content-type", "application/x-www-form-urlencoded")
   .addHeader("cache-control", "no-cache")
   .build();
  Response response = null;
  String resp = null;
  try {
   response = client.newCall(request).execute();
   resp = response.body().string();
  } catch (IOException e) {
   e.printStackTrace();
  }

  //return resp;
  return response.request().url().toString();
 }

 @Override
 protected void onPostExecute(String s) {
  super.onPostExecute(s);
  Log.e("data", s);
  try {
   mBT.setEnabled(true);
   String[] ist = s.split("=");
   String[] snd = ist[1].split("&");
   Token = snd[0];

   Log.e("token", Token);
   Log.e("posturl", ist[2]);

   pburl = ist[2];
   medPost.setText(pburl);
   medtoken.setText(Token);


   //  Log.e("pburl", pburl);
   /* Intent ii = new Intent(MainActivity.this, Payment_details.class);
    ii.putExtra("data", token);
     startActivity(ii);*/
   //http://jeevaysehat.com/?auth_token=260915100358342650147434472217522869797&postBackURL=http%3A%2F%2Fjeevaysehat.com%2F


  } catch (Exception e) {

  }
 }
}

private class PostTask1 extends AsyncTask < String, String, String > {
 String mtoken;
 String PBURL;

 public PostTask1(String token, String pb) {
  mtoken = token;
  PBURL = pb;
 }

 @Override
 protected String doInBackground(String...data) {


  OkHttpClient client; // = new OkHttpClient();
  client = getUnsafeOkHttpClient();
  client.setHostnameVerifier(new HostnameVerifier() {
   @Override
   public boolean verify(String hostname, SSLSession session) {
    return true;
   }
  });
  MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
  RequestBody body = RequestBody.create(mediaType, "auth_token=" + mtoken + "&postBackURL=https://www.jeevaysehat.com/");
  Request request = new Request.Builder()
   .url("https://easypaystg.easypaisa.com.pk/easypay/Confirm.jsf")
   .post(body)
   .addHeader("content-type", "application/x-www-form-urlencoded")
   .addHeader("cache-control", "no-cache")
   .build();

  Response response = null;
  String resp = null;
  try {
   response = client.newCall(request).execute();
   resp = response.body().string();
  } catch (Exception e) {
   e.printStackTrace();
  }

  return response.request().url().toString();
 }

 @Override
 protected void onPostExecute(String s) {
  super.onPostExecute(s);
  Log.e("data", s);
  //here i redirect to webview activity
  Intent ii = new Intent(MainActivity.this, Payment_details.class);
  ii.putExtra("data", s);
  startActivity(ii);
  // Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
  // startActivity(browserIntent);

 }
}


 

Upvotes: 3

Views: 7752

Answers (1)

Nain Kazi
Nain Kazi

Reputation: 191

I have solved the issue and implemented Telenor easypay API, The magic is we will do all the things in webview... using webview will post data to first URL it will return auth_token and postbackurl attached to the url... then post auth_token and postbackurl to the second URL in webview it will successfully land you on easypay checkout screen.

public class Payment_details extends AppCompatActivity {
    private WebView webView;
    String postData = null;
    private RelativeLayout mConfirm;
    String data;
    boolean isFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.payment_details);
        mConfirm = (RelativeLayout) findViewById(R.id.pd_confirm);


        webView = (WebView) findViewById(R.id.pdwebView);

        data = "https://easypaystg.easypaisa.com.pk/easypay/Index.jsf";
        Log.e("data", data);


        try {


            postData = URLEncoder.encode("amount", "UTF-8")
                    + "=" + URLEncoder.encode("10", "UTF-8");

            postData += "&" + URLEncoder.encode("storeId", "UTF-8") + "="
                    + URLEncoder.encode("xxxx", "UTF-8");

            postData += "&" + URLEncoder.encode("postBackURL", "UTF-8")
                    + "=" + URLEncoder.encode("your post back url any url", "UTF-8");

            postData += "&" + URLEncoder.encode("orderRefNum", "UTF-8")
                    + "=" + URLEncoder.encode("1111", "UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        webView.setWebViewClient(new MyWebViewClient());
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);

        webView.postUrl(data, postData.getBytes());




    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);


            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            Log.e("purl", url);
            if(isFirst) {
                isFirst = false;
                String[] ist = url.split("=");
                String[] snd = ist[1].split("&");
                String Token = snd[0];

                Log.e("token", Token);
                Log.e("posturl", ist[2]);
                secondredirect(Token, view);
            }

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);




        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

    }

    @Override
    public void onBackPressed() {
    }

   private void secondredirect(String token, WebView view){
       String sData = null;
       String sURL = "https://easypaystg.easypaisa.com.pk/easypay/Confirm.jsf";
       try {
           sData = URLEncoder.encode("auth_token", "UTF-8")
                   + "=" + URLEncoder.encode(token, "UTF-8");

           sData += "&" + URLEncoder.encode("postBackURL", "UTF-8") + "="
               + URLEncoder.encode("any url as a postback url", "UTF-8");
           view.postUrl(sURL, sData.getBytes());
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
   }
}

Upvotes: 15

Related Questions