Alipay Scan Payment--app end can not display two-dimensional code problems such as solution _ Alipay

Source: Internet
Author: User

OK first description, the following use of real-time to the account, the server to obtain an order to pay the address, and app side we do is using WebView to display two-dimensional code of operation, involving two-dimensional code address request, this part has the service side to do. First, the main issues involved in WebView can not display two-dimensional code, the site has migrated to the problem exited the activity, but the Web page is still on request HTTPS need certificate How to judge the success of the payment two, the solution to the problem (1) The first problem may be caused by the following two kinds of reasons

WebView is an agent in the wood.
Resolution method
Plus agent

/**
 * Plus the reason for this agent is: before it is not necessary to add this, now must add this, now do not add this will appear (unable to connect to the network) this error, plus the user agent
/paywv.getsettings (). Setuseragentstring ("mozilla/5.0" (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/41.0.2272.118 safari/537.36

Wood has the certificate to accept the website (is the third question)
Resolution method
Create a webviewclient subclass mywebviewclient, rewrite the Onreceivedsslerror method in this class, and accept the Web site's certificate

         /**
         * When processing HTTPS requests, you need to add the following code (accept the certificate), or some mobile phone can not request HTTPS (for example: ml)/
        @Override public
        Void Onreceivedsslerror (WebView view, Sslerrorhandler handler,
                                       sslerror error) {
            handler.proceed ();//Accept certificates for all Web sites
            log.i ("Mywebviewclient", "Accept Certificate");
        }
(2) The solution to the second problem

Rewrite the onkeydown method, and when exiting, destroy the WebView.

     /**
      * When the return key is pressed to exit the activity and destroy off WebView
      * Not destory, WebView will always request the network unless the entire application is closed,
      * so add on the Webview.destroy operation.
      */
     @Override public
    boolean onKeyDown (int keycode, keyevent event) {
         if (keycode== Keyevent.keycode_back) {
            Paywv.destroy ();
            Finish ();
         }
        return false;
    }
(3) The solution to the third problem

This problem is resolved in the same way as the second solution in the first question
Create a webviewclient subclass, override the Onreceivedsslerror method in this class, and accept the Web site's certificate

         /**
         * When processing HTTPS requests, you need to add the following code (accept the certificate), or some mobile phone can not request HTTPS (for example: ml)/
        @Override public
        Void Onreceivedsslerror (WebView view, Sslerrorhandler handler,
                                       sslerror error) {
            handler.proceed ();//Accept certificates for all Web sites
            log.i ("Mywebviewclient", "Accept Certificate");
        }
(4) The solution to the fourth question

Payment is successful, our web address will be transferred to a payment successful interface (address is: https://tradeexprod.alipay.com/cashiergw/cashierReturnMiddlePage.htm?, with some parameters behind), At this point, we can judge in the mywebviewclient of the Shouldoverrideurlloading method, the URL is not the beginning of the site is not the above.

@Override Public
 Boolean shouldoverrideurlloading (webview view, String URL) {                           
       if Url.startswith ("https:// Tradeexprod.alipay.com/cashiergw/cashierreturnmiddlepage.htm? ")) {
                //This inside is the payment success, after the Operation  , which we are tuned to pay into the interface
                startactivity (new Intent (Mainactivity.this,payok.class));
            }  
            View.loadurl (URL);
            return true;
 }
Third, code example

Here is the main description of a ha, we get to the two-dimensional code, there are several formats, where we choose 2
Type (length range) is an empty sample parameter parameter name parameter description qr_pay_mode sweep code payment way, support the predecessor mode and jump mode. The front mode is the mode of placing the two-dimensional code in front of the Merchant's order confirmation page. Need to shop in their own pages in the form of an IFRAME request Alipay page. Specifically divided into the following 3 kinds: 1, order code-Simple front mode, the corresponding IFRAME width can not be less than 600px, height can not be less than 300px;2, order code-predecessor mode, the corresponding IFRAME width can not be less than 300px, height can not be less than 600px;3, order code-mini Front mode, The corresponding iframe width can not be less than 75px, height can not be less than 75px. Jump mode, the user's sweep code interface is generated by Alipay, not the merchant's domain name. 4, order code-jump mode

Concrete can look at Alipay open platform api– Instant to account

According to the generated two-bit size, we define the width and height of the WebView, OK maybe you also think that our webview should not be able to slide amplification and so on, so easy we write a webview subclass, shielding its touch event on the OK. (1) Noscorwbview

Package com.xm.alipayxmdemo1;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.webkit.WebView;
/**
* @author: qiwenming
        * @date: 2015/8/28 0028 pm 5:14
        * @instruction * * Public
class Noscorwbview Extends WebView {public


    Noscorwbview (context) {
        super (context);
    }

    Public Noscorwbview (context, AttributeSet attrs) {
        Super (context, attrs);
    }

    Public Noscorwbview (context, AttributeSet attrs, int defstyleattr) {
        Super (context, attrs, defstyleattr); c18/>}

    /**
     * Let this webview not respond to touch
     events
    /@Override public
    boolean ontouchevent (motionevent Event) {return
        true;
    }
}
(2) Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
    xmlns:tools= "http:// Schemas.android.com/tools "     android:layout_width=" match_parent "
    android:layout_height=" Match_parent "
    tools:context= ". Mainactivity ">
    <com.xm.alipayxmdemo1.noscorwbview
        android:layout_centerinparent=" true "
        Android:id= "@+id/sdwv_apy"
        android:layout_width= "290DP"
        android:layout_height= "290DP"/>
</ Relativelayout>
(3) Mainactivity
Package com.xm.alipayxmdemo1;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import android.content.Intent;
Import Android.net.http.SslError;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.util.Log;
Import android.view.KeyEvent;
Import Android.webkit.SslErrorHandler;
Import Android.webkit.WebView;

Import android.webkit.WebViewClient;
    public class Mainactivity extends activity {private WebView PAYWV;
    Private ProgressDialog Dialog;
    Runnable Runnable;
    Handler Handler;

    Private String Payurl = "One gets the payment address of the two-dimensional code";
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_main);
        PAYWV = (webview) Findviewbyid (r.id.sdwv_apy);
        Paywv.getsettings (). Setjavascriptenabled (True);
        Paywv.setwebviewclient (New Mywebviewclient ());
     /** * Plus the reason for this agent is: there is no need to add this, now must add this, now do not add this will appear (unable to connect to the network) this error, plus the user agent * *   Paywv.getsettings (). setuseragentstring ("mozilla/5.0" (Windows NT 6.1;
        WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/41.0.2272.118 safari/537.36 ");

        Paywv.loadurl (Payurl);
                This is used to control the display of our progress box runnable = new runnable () {@Override public void run () {
                TODO auto-generated Method Stub//Webview.loadurl ("Javascript:window.local_obj.dimissDialog ()");
            if (dialog!= null && dialog.isshowing ()) Dialog.dismiss ();
        }
        };
        Handler = new Handler (); Dialog = Progressdialog.show (this, null, "Loading" ...)
    ", true, true); Class Mywebviewclient extends webviewclient{/** * Processing HTTPS requests, you need to add the following code (accept the certificate), or some mobile phone can not request HTTPS (
                                       For example: ML) */@Override public void Onreceivedsslerror (WebView view, Sslerrorhandler handler, Sslerror error) {handler.proceed ()//Accept All website Certificate log.i ("Mywebviewclient", "Accept Certificate"); @Override public boolean shouldoverrideurlloading (webview view, String URL) {if url.starts With ("Https://tradeexprod.alipay.com/cashiergw/cashierReturnMiddlePage.htm?") {//This inside is the payment success, later operations, which we are tuned to pay into the interface startactivity (new Intent Mainactivity.this,payok.class
            ));
            } view.loadurl (URL);
        return true; @Override public void onpagefinished (final webview view, String URL) {log.d ("Mywebviewclien
            T "," onpagefinished---url= "+url);
            After loading is complete, need to turn off my dialog handler.removecallbacks (runnable);
            Handler.postdelayed (Runnable, 2000);
        super.onpagefinished (view, URL); }/** * When the return key is pressed to exit the activity and destroy off WebView * not destory out, WebView will always request the network unless the entire application is closed, * so add on the
     Webview.destroy operation. * * @Override Public boolean OnkeydOwn (int keycode, keyevent event) {if (keycode== keyevent.keycode_back) {Paywv.destroy ();
        Finish ();
    return false;
 }

}
Iv. Demo and source download

SOURCE download

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.