android: add PayPal donation page to app

I was looking for a simple method of integration PayPal donations to apps.
First, I ended with simple mobile page, which had only button to post to PayPal gateway. An example can be seen here.
Second try made to add the following code:

startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted")));

This action opened browser with proper page. Solution is pretty nice, but if you want to remain in you own app, then it doesn’t suit you.

So do I, I want user to stay in app and to use POST request, not GET.
Finally, I found the following solution:

public class Donate extends Activity {
	public void onCreate(Bundle savedInstanceState) {
	    String POSTText = null;
        WebView webview = new WebView(this);

        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
          public void onProgressChanged(WebView view, int progress) {
            // Activities and WebViews measure progress with different scales.
            // The progress meter will automatically disappear when we reach 100%
            activity.setProgress(progress * 100);
        webview.setWebViewClient(new WebViewClient() {  
            public boolean shouldOverrideUrlLoading(WebView view, String url)  
              return true;  
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
//this code can be taken from GET request in previous solution
        POSTText = "cmd=_donations&¤cy_code=USD&bn=PP-DonationsBF:btn_donateCC_LG.gif:NonHosted" ;
        byte[] post = null;
		post = EncodingUtils.getBytes(POSTText, "BASE64");
		webview.postUrl("", post);   


So, opening this Activity, User will see progress bar on top of the page. After page is loaded, User will be able to donate you some amount of money.

Disadvantage of this method is that PayPal page is not modified to small dimensions of mobile devices.

Leave a Reply

%d bloggers like this: