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("https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=beshkin%40gmail%2ecom&lc=EE&item_name=Mobile%20apps¤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) {
	    super.onCreate(savedInstanceState);
	    String POSTText = null;
        
        WebView webview = new WebView(this);
        webview.getSettings().setJavaScriptEnabled(true);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);

        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() {  
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url)  
            {  
              view.loadUrl(url);  
              return true;  
            }
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
              }
          });  
        setContentView(webview);
//this code can be taken from GET request in previous solution
        POSTText = "cmd=_donations&business=beshkin@gmail.com&lc=US&item_name=KosherDev¤cy_code=USD&bn=PP-DonationsBF:btn_donateCC_LG.gif:NonHosted" ;
        byte[] post = null;
		post = EncodingUtils.getBytes(POSTText, "BASE64");
		webview.postUrl("https://www.paypal.com/cgi-bin/webscr", 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: