Android学习笔记_69_android 支付宝之网页支付和快捷支付

参考资料:

https://b.alipay.com/order/productDetail.htm?productId=2013080604609654

https://b.alipay.com/order/productDetail.htm?productId=2013080604609654&tabId=4#ps-tabinfo-hash

Android支付接入(五):机锋网

 

--- 网页支付

http://wappaygw.alipay.com/service/rest.htm?

req_data=

<direct_trade_create_req>

<subject>12121212</subject>

<out_trade_no>12121021</out_trade_no>

<total_fee>1</total_fee>

<seller_account_name>che12121</seller_account_name>

<notify_url>http://www.alipay.com/waptest0504/servlet/NotifyReceiver</notify_url>

<out_user>outID123</out_user>

<merchant_url>http://www.alipay.com</merchant_url>

<pay_expire>10</pay_expire>

</direct_trade_create_req>

&service=alipay.wap.trade.create.direct

&sec_id=0001&partner=12112

&req_id=11121212

&sign=bDfw5%2Bctc3pxzl7emPxqOod4EiPu3BkE0 Um54g4whHT22CwLbOn1gzyE%2BU5SIleGPke2rNQ%3D

&format=xml

&v=2.0

网页支付拿这服务器返回的地址,通过webview直接请求就可以

public class AlipayWebActivity extends Activity {

    private WebView mWebView;

    private ProgressDialog mProgressDialog;

    private static final String TAG = "AlipayWebActivity";

    protected static final int PAY_SUCCESS = 20;

    protected static final int LOAD_FINISH_CODE = 40;

    private static final int DEFAULT_CODE = 50;

    

    private ImageView quitImageView;



    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.web);

        FactoryUtil.getInstance().addActivity(this);

        

        quitImageView = (ImageView) findViewById(R.id.iv_quit);

        

        mWebView = (WebView) findViewById(R.id.webview);

        mWebView.setBackgroundColor(0);

        

        initial();

        

        quitImageView.setOnClickListener(new MyOnClickListener());

    }



    @SuppressLint({ "SetJavaScriptEnabled", "HandlerLeak" })

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)

    private void initial() {

        String url = getIntent().getStringExtra("url");

        LogUtil.i(TAG, url);

        WebSettings webSettings = mWebView.getSettings();

        mWebView.requestFocus();

        webSettings.setJavaScriptEnabled(true);

        mWebView.setFocusable(true);

        webSettings.setBuiltInZoomControls(true);



        mWebView.setScrollBarStyle(0);

        if (MobileUtil.getMobileVersion()>11) {        

            mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        }

        mWebView.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int newProgress) {

                LogUtil.i(TAG, "newProgress: " + newProgress);

                if (newProgress == 100 && mProgressDialog!=null) {

                    mProgressDialog.dismiss();

                }

                super.onProgressChanged(view, newProgress);

            }

        });



        mWebView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                LogUtil.i(TAG, "URL : " + url);

                LogUtil.i(TAG, "URL : " + url.contains("&cmd=success&"));

                loadUrl(view, url);

                return true;//停止在当前界面

            }

            public void onPageFinished(WebView view, String url) {

                LogUtil.i(TAG, "onPageFinished" + url);

                //trade_status : TRADE_FINISHED,成功之后处理

super.onPageFinished(view, url); } public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); LogUtil.i(TAG, "onPageStarted" + url); } }); loadUrl(mWebView, url); } @SuppressLint("HandlerLeak") private Handler mUihandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case PAY_SUCCESS: clearCache(); FactoryUtil.getInstance().exit(); break; case LOAD_FINISH_CODE: if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } break; case DEFAULT_CODE: showProgressDialog(AlipayWebActivity.this, R.string.dialog_loading); } super.handleMessage(msg); } }; private void loadUrl(final WebView webView, final String url) { mUihandler.sendEmptyMessage(DEFAULT_CODE); webView.loadUrl(url); } public boolean onKeyDown(int keyCode, KeyEvent event) { LogUtil.i(TAG, "can goback:" + mWebView.canGoBack()); if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) { mWebView.goBack(); return true; } else if (keyCode == KeyEvent.KEYCODE_BACK) { onBackPressed(); return true; } return super.onKeyDown(keyCode, event); } public void showProgressDialog(Context context, int message) { if (mProgressDialog == null) { mProgressDialog = new ProgressDialog(context); mProgressDialog.setMessage(context.getText(message)); mProgressDialog.show(); } } private void clearCache() { mWebView.clearCache(true); mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null); finish(); } private class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { int id = v.getId(); if (id==R.id.iv_quit) { finish(); } } } }

--- 快捷支付
1、公钥,密钥,签名在服务器端完成,比放在客户端安全。
2、报4000 4100一般就是参数出现错误,仔细比对,如果参数都没问题,很有可能是sign也就是签名没有通过。签名之后不能对已经签名的字符串做任何修改,因为支付宝需要解密的。
3、如果报服务器繁忙,然后打印出参数非法的警告信息时, KeyFactory.getInstance("RSA", "BC") 可以解决。
4、sign签名要utf-8格式。

 

/*

 * Copyright (C) 2010 The MobileSecurePay Project

 * All right reserved.

 * author: [email protected]

 */



package com.ztgame.dudu.payment.alipay;



import android.app.Activity;

import android.content.DialogInterface;

import android.os.Handler;

import android.os.Message;

import android.widget.Toast;



import com.alipay.android.app.sdk.AliPay;

import com.ztgame.dudu.payment.R;

import com.ztgame.dudu.payment.ui.FactoryUtil;

import com.ztgame.dudu.payment.util.BaseDialogUtil;

import com.ztgame.dudu.payment.util.LogUtil;



public class AlipayUtil {



    private static String TAG = "AlipayUtil";

    private Activity activity;

    private String order;

    private static final int RQF_PAY = 1;



    private static final int RQF_LOGIN = 2;



    public AlipayUtil(Activity activity, String order) {

        this.activity = activity;

        this.order = order;

        LogUtil.i(TAG, activity.toString() + " "

                + activity.getMainLooper().toString());

    }





    public void pay() {

        // 根据订单信息开始进行支付

        try {



            new Thread() {

                public void run() {

                    AliPay alipay = new AliPay(activity, mHandler);

                    String result = alipay.pay(order);

                    Message msg = new Message();

                    msg.what = RQF_PAY;

                    msg.obj = result;

                    mHandler.sendMessage(msg);

                }

            }.start();

        } catch (Exception ex) {

            Toast.makeText(activity, R.string.pay_fail,Toast.LENGTH_SHORT).show();

            activity.finish();

        }

    }



    Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {

            Result result = new Result((String) msg.obj);

            switch (msg.what) {

            case RQF_PAY:

            case RQF_LOGIN: {

                try {

                    String ret = result.getResult();

                    result.parseResult();

                    LogUtil.i(TAG, ret);

                    LogUtil.i(TAG, "1: " + result.isSignOk + " 2: "

                            + result.memo + " 3: " + result.resultStatus);

                    // 先验签通知

                    // 1: false 2: 3: 操作成功(9000)

                    // 验签失败

                    if (result.isSignOk) {

                        Toast.makeText(activity, R.string.check_sign_fail, Toast.LENGTH_SHORT).show();

                    } else {// 验签成功。验签成功后再判断交易状态码

                        if ("操作成功(9000)".equals(result.resultStatus)) {// 判断交易状态码,只有9000表示交易成功

                               

                  //成功之后执行

}); } else { Toast.makeText(activity, "支付失败。交易状态码:" + result.resultStatus, Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { e.printStackTrace(); Toast.makeText(activity, R.string.pay_fail, Toast.LENGTH_SHORT).show(); } } break; default: break; } }; }; }

 剩下的文档里面都有,只需要处理的是支付成功与失败的逻辑。

 

提交到支付宝的订单快捷支付:

partner="2088511"&out_trade_no="2014410490790001211"&subject="支付0.01元"&body="支付0.01元"&total_fee="0.01"

&notify_url="http%3A%2F%2F122.32.196.157%3A90%2Freeonse%2Falipaydut"&service="mobile.securitypay.pay"&_input_charset="utf-8"&payment_type="1"&seller_id="2008354621302100"&show_url="m.alipay.cm"

&it_b_pay="30m"&sign="MPZ8lamKUDgidaIn2KwX%2B6GX4o3BSheEUI2xg3aJa1b%2B1M6VXP%2BaMA3szSBPyl7UYm%2Fdhz8KVuFn5Du057R2lUNQ%2B2nSb3qSXZ6rZcNQ2rCrZ84Kiy3MWFBqlt%2Buq%2FufaCOnGEQyo18NXnm6OGDazSMppQP9cr434ILLguhcntM%3D"

&sign_type="RSA"

 

你可能感兴趣的:(Android学习)