Paypal-express Checkout Quick Payment method of Android development experience (ii)

Source: Internet
Author: User

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

If using the MEC payment method, this way click the Checkout button after the page until the end of payment is the form of the URL, you must first have a Web site payment , so can only be recorded through the form of WebView, use and web site effect, like, If some content in the loaded page does not want to be displayed, you can hide it.

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 PayPal Login Interface if (Url.contains (" Http://www.chinabuye.com/service/product/listcartweb ")) {Dialog.shoW ();} Start loading place order, containing the case 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);/** do not do any blocking operations 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 no problem setcookiesvia2url (URL, url3); View.loadurl (URL3) ;} The PayPal login interface is loaded if (Url.contains ("HTTps://www.paypal.com/au/cgi-bin/webscr ") && url.contains (" #m ")) {Dialog.dismiss ();} The place order interface has finished loading if (Url.contains ("Http://www.chinabuye.com/m/paypal/express/review")) {//Although a hidden operation has been performed before, However, occasionally there is no hidden situation, here reload once Hideplaceorderjs (view);d Ialog.dismiss ();} The next single successful interface has finished loading if (Url.contains ("http://www.chinabuye.com/m/checkout/onepage/success")) {//Although a hidden operation has been performed before, But occasionally there is no hidden situation, here reload 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);} When a page resource is loaded, such as when loading a picture, 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);} Modify 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 button intent Intent = new Inten T (Checkoutactivity2.this, Cartactivity.class); startactivity (intent); Finish ();}); Builder.setnegativebutton (R.string.cancel, New Alertdialog.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {//Intercept Cancel button result.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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.