monitor web browser programmatically in Android?

2019-06-13 19:11发布

I've got a tricky question here. I need users to make a payment to a bank (namely Barclaycard) in UK. To do so, I have a https URL , I add the parameters (such as amount to pay, order reference, etc) to the URL, start this http connection as an Intent.ActionView, which will redirect the user to the browser where he can enter his credit card details on the bank's webpage and make the payment to our account successfully. So far so good ?

The code I use is below (I changed values for privacy reasons) The problem is, I need to get back to the app when the user has completed/failed/cancelled the payment. Barclaycardautomatically redirects to a particular URL when the payment has succeeded, another one if it failed. Is there no way of knowing when Barclaycard payment has succeeded so that then I would go back to the android app somehow ?

Button cardbutton = (Button) findViewById(R.id.card_button);
cardbutton.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        String preHashString = new String();
        String proHashString = new String();
        String SHAPassPhrase = new String();

        SHAPassPhrase = "GSvTh£h70ZkHdAq9b"; // FOR TEST ENVIRONMENT

        preHashString = preHashString + "AMOUNT=" + String.valueOf((int) (order.getPaymentAmount() * 100.00)) + SHAPassPhrase;
        preHashString = preHashString + "BGCOLOR=cccccc" + SHAPassPhrase;
        preHashString = preHashString + "CN=" + user.getString("name") + SHAPassPhrase;
        preHashString = preHashString + "CURRENCY=GBP" + SHAPassPhrase;
        preHashString = preHashString + "LANGUAGE=en_US" + SHAPassPhrase;
        preHashString = preHashString + "ORDERID=" + order.getOrderId() + SHAPassPhrase;

        try
        {
            proHashString = SHA1(preHashString);
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        String redirecturl = "https://mdepayments.epdq.co.uk/ncol/test/orderstandard.asp";

        redirecturl += "?AMOUNT=" + String.valueOf((int) (order.getPaymentAmount() * 100));
        redirecturl += "&CN=" + user.getString("name");
        redirecturl += "&CURRENCY=GBP";
        redirecturl += "&LANGUAGE=en_US";
        redirecturl += "&ORDERID=" + order.getOrderId();
        redirecturl += "&SHASIGN=" + proHashString;

        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(redirecturl));
        startActivity(i);
    }
});

3条回答
forever°为你锁心
2楼-- · 2019-06-13 20:08

You should never be doing such things on user device. Someone can decompile your code and change it, so your app will "think" they made the payment.

This may lead to small problems like they using app for free to severe problems like you being forced to make all the payments.

Either use server-side solution or in-app-purchase from Google.

查看更多
迷人小祖宗
3楼-- · 2019-06-13 20:11

You can have your own Webview in place inside your app, with some done / close button somewhere.. Then you can track all urls getting open in your WebView and do your stuff accordingly..User will stay in your app always..that solves your purpose..

For tracking all urls inside your WebView you need to register one WebViewClient and ovveride below function

public boolean shouldOverrideUrlLoading (WebView view, String url)

Have a look at WebView here and WebViewClient here

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-06-13 20:12

If your user gets redirected to a new URL you could use a ContentObserver that observes the bookmark history for any changes:

public class UrlObserver extends ContentObserver {

    @Override
    public void onChange(boolean selfChange) {
       super.onChange(selfChange);
       // check last URL in history
    }
}

Reading the history can be done by:

private static final Uri CONTENT_URI = Browser.BOOKMARKS_URI;
Cursor cursor = context.getContentResolver().query(
                    CONTENT_URI, Browser.HISTORY_PROJECTION, null, null, null); 

Registration of the content observer works with:

UrlObserver observer = new UrlObserver();
context.getContentResolver().registerContentObserver(CONTENT_URI, true, observer);          

Once a particular URL has been detected, you can invoke an intent to bring your activity back to front. This is a sample app which might help you in this case.

I'm not 100% sure what happens if the same site is used for the form transmission. It might be that the content observer won't trigger. In that case you might find some useful log entries.

Note: Chrome and the Android standard browser use different URLs for the query. Search the internet to find the right one.

Hope this helps .... Cheers!

查看更多
登录 后发表回答