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
轉(zhuǎn)Android支付接入(五):機(jī)鋒網(wǎng)
--- 網(wǎng)頁(yè)支付
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
網(wǎng)頁(yè)支付拿這服務(wù)器返回的地址,通過(guò)webview直接請(qǐng)求就可以
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;//停止在當(dāng)前界面
}
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、公鑰,密鑰,簽名在服務(wù)器端完成,比放在客戶端安全。
2、報(bào)4000 4100一般就是參數(shù)出現(xiàn)錯(cuò)誤,仔細(xì)比對(duì),如果參數(shù)都沒(méi)問(wèn)題,很有可能是sign也就是簽名沒(méi)有通過(guò)。簽名之后不能對(duì)已經(jīng)簽名的字符串做任何修改,因?yàn)橹Ц秾毿枰饷艿摹?br /> 3、如果報(bào)服務(wù)器繁忙,然后打印出參數(shù)非法的警告信息時(shí), KeyFactory.getInstance("RSA", "BC") 可以解決。
4、sign簽名要utf-8格式。
/*
* Copyright (C) 2010 The MobileSecurePay Project
* All right reserved.
* author: shiqun.shi@alipay.com
*/
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() {
// 根據(jù)訂單信息開(kāi)始進(jìn)行支付
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);
// 先驗(yàn)簽通知
// 1: false 2: 3: 操作成功(9000)
// 驗(yàn)簽失敗
if (result.isSignOk) {
Toast.makeText(activity, R.string.check_sign_fail, Toast.LENGTH_SHORT).show();
} else {// 驗(yàn)簽成功。驗(yàn)簽成功后再判斷交易狀態(tài)碼
if ("操作成功(9000)".equals(result.resultStatus)) {// 判斷交易狀態(tài)碼,只有9000表示交易成功
//成功之后執(zhí)行
});
} else {
Toast.makeText(activity,
"支付失敗。交易狀態(tài)碼:" + 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" ¬ify_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"
總結(jié)
以上是生活随笔為你收集整理的Android学习笔记_69_android 支付宝之网页支付和快捷支付的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 敏捷之Scrum框架
- 下一篇: 精灵宝可梦GO百变怪来源 小精灵身份的秘