First, leading
The previous article is not very good, here again to talk about.
PayPal mobile payment is available in 2 formats:
1.Mobile Express Checkout,mec, fast payment
2.MPL
Assuming the use of MEC payment method, such a way to click on the page after Checkoutbutton to the end of payment is the form of the URL, must first have a Web site payment , so only can be recorded in the form of WebView, As with web site effects, assume that some content in the loaded page does not want to be displayed and can be hidden.
Second, MEC payment case
Package Com.sound.chinabuye.activity;import http. Httpurls;import java.util.list;import org.apache.http.cookie.cookie;import tool. Newtokencallback;import tool. Tokentools;import tool. Userinfo;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.app.progressdialog;import Android.content.dialoginterface;import Android.content.intent;import Android.graphics.bitmap;import Android.os.bundle;import Android.util.log;import Android.view.KeyEvent;import Android.webkit.cookiemanager;import Android.webkit.cookiesyncmanager;import Android.webkit.jsresult;import Android.webkit.webchromeclient;import Android.webkit.websettings;import Android.webkit.webview;import Android.webkit.webviewclient;import Android.widget.toast;import Com.sound.chinabuye.r;import Com.sound.chinabuye.bean.broadaction;public class CheckoutActivity2 extends Activity {public static final String TAG = "C HeckoutActivity2 ";p rivate WebView webview;private list<cookie> COokies;private progressdialog Dialog; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); manageactivity (); webView = new WebView (this); Webview.setwebviewclient (new Mywebviewclient ()); Webview.setwebchromeclient (New Mychromeclient ()); WebSettings websettings = Webview.getsettings (); websettings.setjavascriptenabled (true); Websettings.setusewideviewport (false); Websettings.setsupportzoom (false); Setcontentview (webView);d ialog = new ProgressDialog (This);d ialog.setmessage (getString (r.string.loading));d ialog.setcancelable (FALSE);// Request to get Cookiesrequestdata ();} private void RequestData () {Tokentools.requestnewtoken (checkoutactivity2.this, New Newtokencallback () {@ overridepublic void Getnewtokensuccess (string newaccesstoken) {if (Newaccesstoken! = null) {String CustomerID = UserInfo. Getuserinfoinstance (). GetUserid (); String url = "Http://www.chinabuye.com/service/product/listcartweb" + "? productid=64396&qty=1&customerid=" + CustomerID + "&accesstokEn= "+ newaccesstoken;webview.loadurl (URL);}});} @Overrideprotected void OnDestroy () {Super.ondestroy (); Cookiesyncmanager.createinstance (Checkoutactivity2.this); Cookiemanager Cookiemanager = Cookiemanager.getinstance (); Cookiemanager.removeallcookie (); Cookiesyncmanager.getinstance (). sync ();} private void Setcookiesvia2url (String fromurl, String tourl) {cookiesyncmanager.createinstance ( Checkoutactivity2.this); Cookiemanager Cookiemanager = Cookiemanager.getinstance (); String value = Cookiemanager.getcookie (Fromurl); Cookiemanager.setcookie (tourl, value); Cookiesyncmanager.getinstance (). sync ();} private void Manageactivity () {Activityinstancemanager.getactivityinstancemanager (). addactivity (this);} Private class Mywebviewclient extends Webviewclient {private String currenturl; @Overridepublic void onpagestarted ( WebView view, String URL, Bitmap favicon) {log.e (TAG, "Start:" + URL); currenturl = url;//start jump to PayPal login interface if (Url.contains (" Http://www.chinabuye.com/service/product/listcartweb ")) {Dialog.shoW ();} Start loading place order, including cases returned from other pages and the case where the order was started if (Url.contains ("Http://www.chinabuye.com/m/paypal/express/saveOrder") | | Url.contains ("Payerid") | | Url.contains ("Http://www.chinabuye.com/m/paypal/express/review")) {if (dialog.isshowing ()) {Dialog.dismiss ();} Dialog.show ();} Start loading the next single successful interface if (Url.contains ("http://www.chinabuye.com/m/checkout/onepage/success")) {dialog.show ();} super.onpagestarted (view, URL, favicon);} @Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {log.e (TAG, "load:" + URL);/** don't do any interception operation here. * * *///view.loadurl (URL);//Return True;return super.shouldoverrideurlloading (view, URL);} @Overridepublic void onpagefinished (WebView view, String URL) {log.e (TAG, "Finish:" + URL); if (Url.contains ("CustomerID" ) {String url2 = "Http://www.chinabuye.com/m/checkout/cart";//This direct use of the web shopping cart, Test no problem String url3 = "http:// Www.chinabuye.com/paypal/express/shortcut ";//This direct interception to the login interface, temporarily did not find the problem Setcookiesvia2url (URL, url3); View.loadurl (URL3) ;} The PayPal login interface loads complete if (Url.contains ("Https://www.paypal.com/au/cgi-bin/webscr ") && url.contains (" #m ")) {Dialog.dismiss ();} The place Order interface loads the completion if (Url.contains ("Http://www.chinabuye.com/m/paypal/express/review")) {//Although the hidden operation has been performed before, But occasionally there is no hidden situation, here again loaded once Hideplaceorderjs (view);d Ialog.dismiss ();} The next single successful interface loading complete if (Url.contains ("http://www.chinabuye.com/m/checkout/onepage/success")) {//Although it has been hidden before, But occasionally there is no hidden situation, here again loaded once Hidesaveorderjs (view);d Ialog.dismiss ();//Send broadcast, empty shopping cart Intent intent = new intent (); Intent.setaction (Broadaction.empty); CheckoutActivity2.this.sendBroadcast (Intent); LOG.E ("Sendbroad", "buy.success");} The user mailbox information is empty if (Url.contains ("Http://www.chinabuye.com/m/customer/account/edit")) {Toast.maketext ( Checkoutactivity2.this, "Invalid email address NULL", 0). Show (); super.onpagefinished (view, URL);} Handles key events in the browser @overridepublic boolean shouldoverridekeyevent (WebView view, KeyEvent event) {if (Event.equals ( Keyevent.keycode_back) && View.cangoback ()) {view.goback (); return true;} else if (event. Equals (Keyevent.keycode_back)) {CheckoutActivity2.this.finish (); return true;} Return super.shouldoverridekeyevent (view, event);} Called when a page resource is loaded, for example, when a picture is loaded, a @overridepublic void Onloadresource (WebView view, String URL) is called once per loaded image {//LOG.E (TAG, " LoadResource: "+ url"),//web Shopping Cart if (currenturl.contains ("Http://www.chinabuye.com/m/checkout/cart")) {Hidemcartjs ( view);} Place Orderif (Currenturl.contains ("Http://www.chinabuye.com/m/paypal/express/review")) {Hideplaceorderjs (view) ;} Order Successful page if (Currenturl.contains ("http://www.chinabuye.com/m/checkout/onepage/success")) {Hidesaveorderjs (view);} Change the address, the first call is the method, in order to experience good, here first show dialog box if (Url.contains ("http://www.chinabuye.com/m/paypal/express/ajaxEditAddress") ) {dialog.show ();} Super.onloadresource (view, URL);}} Hide M cart interface header and bottom private void Hidemcartjs (WebView view) {View.loadurl ("Javascript:window.handler.show" ( document.getElementById (' Topheader '). style.display= ' None '); "); View.loadurl ("Javascript:window.handler.show" (document.getElementById (' Footer '). sTyle.display= ' None '); "); Hide the place Order interface private void Hideplaceorderjs (WebView view) {//Hide user and logo information View.loadurl ("javascript: Window.handler.show (document.getElementById (' Topheader '). style.display= ' None '); /Hide PlaceOrder after View.loadurl ("Javascript:window.handler.show (document.getElementById (' Iph_menu '). style.display= ' None '); /Hide the bottom contact us etc information View.loadurl ("Javascript:window.handler.show (document.getElementById (' footer '). style.display= ' None '); /Hide Review Review Order and Shipping Reminders Information View.loadurl ("Javascript:window.handler.show (document.getElementById (' Paypal_page_head '). style.display= ' None '); "); View.loadurl ("Javascript:window.handler.show (document.getElementById (' paypal_important '). style.display= ' None ') ;");/ /detail Hidden//View.loadurl ("Javascript:window.handler.show" (document.getElementById (' paypal_change_shipping_address '). Style.display= ' None '); "); View.loadurl ("Javascript:window.handler.show" (document.getElementById (' Paypal_change_payment_method '). Style.display= ' None '); "); View.loadurl("Javascript:window.handler.show (document.getElementById (' Paypal_edit_shopping_cart '). style.display= ' None ');"); Hides the Save Order Interface private void Hidesaveorderjs (WebView view) {View.loadurl ("javascript:window.handler.show ( document.getElementById (' Topheader '). style.display= ' None '); "); View.loadurl ("Javascript:window.handler.show (document.getElementById (' footer '). style.display= ' None ');"); View.loadurl ("Javascript:window.handler.show (document.getElementById (' Iph_menu '). style.display= ' None ');"); View.loadurl ("Javascript:window.handler.show" (Document.getelementsbyclassname (' Button-set ') [0].style.display= ' None '); "); Private class Mychromeclient extends Webchromeclient {//handle confirm in JavaScript, confirm//Login interface has this dialog// The Continue page has the dialog box @overridepublic boolean onjsconfirm (WebView view, string URL, String message, final Jsresult result) {Builder Bui Lder = new Builder (checkoutactivity2.this), Builder.settitle ("Confirm Dialog"); Builder.setmessage ("Cancle the Purchase and return to the ShoppingCart "); LOG.E (TAG, "Mesage:"+ message); LOG.E (TAG, "Jsresult:" + result.tostring ()); Builder.setpositivebutton (R.string.ok, New Alertdialog.onclicklistener ( {@Overridepublic void OnClick (dialoginterface dialog, int which) {//Result.confirm ();//intercept OK buttonintent intent = new I Ntent (Checkoutactivity2.this, Cartactivity.class); startactivity (intent); Finish ();}); Builder.setnegativebutton (R.string.cancel, New Alertdialog.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {//Intercept cancel Buttonresult.cancel ();}); Builder.setcancelable (false); Builder.create (); Builder.show (); return true;}} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {//This can cause a lot of strange problems, Or the direct end of//if (Webview.cangoback ()) {//webview.goback ();//}else{//checkoutactivity2.this.finish ();/} CheckoutActivity2.this.finish (); return true;} Return Super.onkeydown (KeyCode, event);}} The above example is what I actually used in the app, and I didn't find any problems. To do MEC payment, the main idea is to filter the URL, to display and Disappear dialog box, page hiding operations, such as the middle involved JS.
Paypal-express Checkout Quick Payment method of Android development experience (ii)