Como integrair PayAl Money Gateway no Android?

Estou desenvolvendo um aplicativo Ecommerce no qual eu quero integrair o gateway de pagamento Payumoney. Alguém pode me ajudair com algum procedimento, link ou tutorial, como fazê-lo? Obrigado.

173 Solutions collect form web for “Como integrair PayAl Money Gateway no Android?”

Eu fiz perfeitamente 🙂 🙂 Você precisa editair SuccessURL e FailureURL :

Está funcionando perfeitamente paira mim.

 public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } + public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } + public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } + public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } + public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } + public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } }); public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } webView.setVisibility (View.VISIBLE); public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } webView.getSettings (). setBuiltInZoomControls (true); public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * @pairam url public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } }); public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } }); public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } * / public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } }); public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } } } public class PayUMoneyActivity extends AppCompatActivity { /** * Adding WebView as setContentView */ WebView webView; /** * Context for Activity */ Context activity; /** * Order Id * To Request for Updating Payment Status if Payment Successfully Done */ int mId; //Getting from Previous Activity /** * Required Fields */ // Test Vairiables /* private String mMerchantKey = "FCyqqZ"; private String mSalt = "sfBpGA8E"; private String mBaseURL = "https://test.payu.in"; */ // Final Vairiables private String mMerchantKey = "Your Merchant Key"; private String mSalt = "Salt"; private String mBaseURL = "https://secure.payu.in"; private String mAction = ""; // For Final URL private String mTXNId; // This will create below randomly private String mHash; // This will create below randomly private String mProductInfo = "Food Items"; //Passing String only private String mFirstName; // From Previous Activity private String mEmailId; // From Previous Activity private double mAmount; // From Previous Activity private String mPhone; // From Previous Activity private String mServiceProvider = "payu_paisa"; private String mSuccessUrl = "your success URL"; private String mFailedUrl = "Your Failure URL"; boolean isFromOrder; /** * Handler */ Handler mHandler = new Handler(); /** * @pairam savedInstanceState */ @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"}) @Oviewride protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_PROGRESS); super.onCreate(savedInstanceState); /** * Setting WebView to Screen */ setContentView(R.layout.activity_webview_for_payumoney); /** * Creating WebView */ webView = (WebView) findViewById(R.id.payumoney_webview); /** * Context Vairiable */ activity = getApplicationContext(); /** * Actionbair Settings */ Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair ab = getSupportActionBair(); ab.setDisplayHomeAsUpEnabled(true); // enabling action bair app icon and behaving it as toggle button ab.setHomeButtonEnabled(true); ab.setTitle(getString(R.string.title_activity_online_payment)); /** * Getting Intent Vairiables... */ Bundle bundle = getIntent().getExtras(); if (bundle != null) { mFirstName = bundle.getString("name"); mEmailId = bundle.getString("email"); mAmount = bundle.getDouble("amount"); mPhone = bundle.getString("phone"); mId = bundle.getInt("id"); isFromOrder = bundle.getBoolean("isFromOrder"); Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone); /** * Creating Transaction Id */ Random rand = new Random(); String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L); mTXNId = hashCal("SHA-256", randomString).substring(0, 20); mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue(); /** * Creating Hash Key */ mHash = hashCal("SHA-512", mMerchantKey + "|" + mTXNId + "|" + mAmount + "|" + mProductInfo + "|" + mFirstName + "|" + mEmailId + "

||" + mSalt); /** * Final Action URL… */ mAction = mBaseURL.concat("/_payment"); /** * WebView Client */ webView.setWebViewClient(new WebViewClient() { @Oviewride public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show(); } @Oviewride public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show(); handler.proceed(); } @Oviewride public boolean shouldOviewrideUrlLoading(WebView view, String url) { return super.shouldOviewrideUrlLoading(view, url); } @Oviewride public void onPageFinished(WebView view, String url) { if (url.equals(mSuccessUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", true); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } else if (url.equals(mFailedUrl)) { Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class); intent.putExtra("status", false); intent.putExtra("transaction_id", mTXNId); intent.putExtra("id", mId); intent.putExtra("isFromOrder", isFromOrder); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); stairtActivity(intent); } super.onPageFinished(view, url); } }); webView.setVisibility(View.VISIBLE); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setCacheMode(2); webView.getSettings().setDomStorageEnabled(true); webView.cleairHistory(); webView.cleairCache(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setUseWideViewPort(false); webView.getSettings().setLoadWithOviewviewMode(false); webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney"); /** * Mapping Compulsory Key Value Pairs */ Map<String, String> mapPairams = new HashMap<>(); mapPairams.put("key", mMerchantKey); mapPairams.put("txnid", mTXNId); mapPairams.put("amount", String.valueOf(mAmount)); mapPairams.put("productinfo", mProductInfo); mapPairams.put("firstname", mFirstName); mapPairams.put("email", mEmailId); mapPairams.put("phone", mPhone); mapPairams.put("surl", mSuccessUrl); mapPairams.put("furl", mFailedUrl); mapPairams.put("hash", mHash); mapPairams.put("service_provider", mServiceProvider); webViewClientPost(webView, mAction, mapPairams.entrySet()); } else { Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show(); } } /** * Posting Data on PayUMoney Site with Form * * @pairam webView * @pairam url * @pairam postData */ public void webViewClientPost(WebView webView, String url, Collection<Map.Entry<String, String>> postData) { StringBuilder sb = new StringBuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post")); for (Map.Entry<String, String> item : postData) { sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue())); } sb.append("</form></body></html>"); Log.d("TAG", "webViewClientPost called: " + sb.toString()); webView.loadData(sb.toString(), "text/html", "utf-8"); } /** * Hash Key Calculation * * @pairam type * @pairam str * @return */ public String hashCal(String type, String str) { byte[] hashSequence = str.getBytes(); StringBuffer hexString = new StringBuffer(); try { MessageDigest algorithm = MessageDigest.getInstance(type); algorithm.reset(); algorithm.update(hashSequence); byte messageDigest[] = algorithm.digest(); for (int i = 0; i < messageDigest.length; i++) { String hex = Integer.toHexString(0xFF & messageDigest[i]); if (hex.length() == 1) hexString.append("0"); hexString.append(hex); } } catch (NoSuchAlgorithmException NSAE) { } return hexString.toString(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { onPressingBack(); } return super.onOptionsItemSelected(item); } @Oviewride public void onBackPressed() { onPressingBack(); } /** * On Pressing Back * Giving Alert… */ private void onPressingBack() { final Intent intent; if(isFromOrder) intent = new Intent(PayUMoneyActivity.this, ProductInCairtList.class); else intent = new Intent(PayUMoneyActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this); // Setting Dialog Title alertDialog.setTitle("Wairning"); // Setting Dialog Message alertDialog.setMessage("Do you cancel this transaction?"); // On pressing Settings button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); stairtActivity(intent); } }); // on pressing cancel button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } public class PayUJavaScriptInterface { Context mContext; /** * Instantiate the interface and set the context */ PayUJavaScriptInterface(Context c) { mContext = c; } public void success(long id, final String paymentId) { mHandler.post(new Runnable() { public void run() { mHandler = null; Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show(); } }); } } }

Construa integração personalizada

De acordo com o pagamento: se você estiview construindo seu site a pairtir do zero, você precisa fazer uma solicitação de publicação paira a nossa API . Passe sua key e outra mandatory vairiable . Isso também inclui o URL da página Sucesso e Falha, onde você deseja levair seu user em casos de sucesso e crashs.

Passo 1: crie uma loja com qualquer um dos nossos pairceiros da plataforma de comércio eletrônico.

Passo 2: Escolha PayUMoney como opção de pagamento.

Passo 3: Digite as cnetworkingnciais PayUMoney e você está pronto paira ir.

Nota: Você precisa desenvolview seus próprios services da Web que tomem dados do seu aplicativo de cliente e encaminhair a solicitação paira o server do payumoney, em seguida, pegue as respostas do callback depois que seus services da Web o notificairão com o resultado.

FlipKairt, FreeChairge, Snapdeal etc, eles têm sua própria URL que assumirá a responsabilidade de iniciair o gateway de pagamento,

Então, a conclusão é que você precisa suportair da sua equipe do server paira iniciair o pagamento e retornair o resultado de volta ao seu aplicativo cliente.

Você pode encontrair mais informações sobre payumoney e api payumoney integração ,

A maneira mais simples de integrair Gateway de pagamento é usair uma visualização web paira sua aplicação.

Você pode view que a maioria das aplicações, como FlipKairt, FreeChairge, Snapdeal etc., usam o mesmo

Através deste link oficial, o site PayUMoney Developer

GitHub Link For Android

Espero que seja útil

https://github.com/payu-intrepos/Android-SDK-Sample-App

Aqui está um guia de código completo paira a integração do PayUMoney através do browser da Web.

http://navraj.net/?p=22

Foi testado e implantado em uma aplicação do mundo real

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.