Android学习系列(28)--App集成支付宝

发布时间:2012-10-22   来源:文档文库   
字号:
Android学习系列(28--App集成支付宝
手机的在线支付,被认为是2012年最看好的功能,我个人认为这也是移动互联网较传统互联网将会大放光彩的一个功能。
人人有手机,人人携带手机,花钱买东西,不再需要取钱付现,不再需要回家上网银,想买什么,扫描一下,或者搜索一下,然后下单,不找零,直接送到你家,这将是手机支付给我们带来的全新交易体验。
谷歌刚推出了谷歌钱包,这必是我们后面要使用的主要手段,但是鉴于当前国情,我觉得有必要介绍一下android手机集成支付宝功能。 1.下载官方架包和说明文档
其实官方已经提供了安装指南,下载地址:
https://mobiless.alipay.com/product/product_down_load.htm?code=SECURITY_PAY
里面有有个pdf,详细说明了说用指南,写的比较详细,可以重点参考。


下载下来,我们主要是用到Android(20120104目录下的alipay_plugin.jarAppDemo/assets下的alipay_plugin223_0309.apk,这两个文件是我们不能修改的支付宝api和安装包。 2. 商户签约
现在的安全机制,都是这样,客户端需要先和服务端请求验证后才能进行进一步操作,oauth也是如此。
打开https://ms.alipay.com/登陆支付宝,点击签约入口,选择"应用类产品"填写并等待审核,获取商户ID和账户ID
签约的时候还要向需要提供实名认证和上传应用,所以我建议先把应用做好了,最后再集成支付宝。

我大概等了1-2天审核,审核是失败的,回复是应用类型啥的应该是"虚拟货币"我改成那个马上自动就审核通过了。 3.密钥配置
解压openssl-0.9.8k_WIN32(RSA密钥生成工具.zip,打开cmd,命令行进入openssl-0.9.8k_WIN32(RSA密钥生成工具\bin目录下, (1.执行

?
openssl genrsa -out rsa_private_key.pem 1024 1
生成rsa_private_key.pem文件。 (2.再执行 ?
openssl rsa -in rsa_private_key.pem -pubout -out 1 rsa_public_key.pem
生成rsa_public_key.pem 文件。 (3.在执行 ?
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem 1 -outform PEM -nocrypt
RSA私钥转换成 PKCS8 格式,去掉beginend那两行,把里面的内容拷贝出来,保存到某个txt中,rsa_private_pkcs8_key.txt(我好像没用到这个)
打开rsa_public_key.pem,即商户的公钥,复制到一个新的TXT中,删除文件-----BEGIN PUBLIC KEY----------END PUBLIC KEY-----“还有空格、换行,变成一行字符串并保存该 TXT 文件,然后在网站的“我的商家服务”切换卡下的右边点击“密钥管理”,然后有个"上传商户公(RSA"项,选择上传刚才的TXT文件. 好了,服务器配置OK,因为这一段之前没有截图,现在弄好了又不好截图,如
果有不明白的地方请大家参考官方文档。 4.引用jar和包含安装包 (1.新建android工程;
(2.copy上面说的alipay_plugin.jar到工程的libs目录下,并在java build path中通过Add External JARs找到并引用该jar
(3.copy上面说的alipay_plugin223_0309.apk安装包到assets目录下,后面配置路径用到。

如果libsassets目录没有,手动建立者两个目录。 5.调用代码整理
AppDemocom.tianxia.lib.baseworld.alipayAppDemocom.alipay.android.appDemo4包下的源码全部copy到刚才我们自己的包下,还有res目录下的资源文件也合并到我们工程res下。

其中AlixDemo.java,ProductListAdapter.java,Products.java是示例类,我们借鉴完后可以删除。
PartnerConfig.java是配置类,配置商户的一些配置参数。 其他的类是严重参考类,直接留下使用。 PartnerConfig.java代码如下: ?
public class PartnerConfig { //合作商户ID。用签约支付宝账号登录ms.alipay.com后,在账户信息页面获取。
1 public static final String PARTNER ="xxx"; 2 //账户ID。用签约支付宝账号登录ms.alipay.com后,在账户信息页面3 4 获取。 5 public static final String SELLER ="xxx"; 6 //商户RSA私钥 ,rsa_private_key.pem中去掉首行,最后一行,7 空格和换行最后拼成一行的字符串
8 public static final String RSA_PRIVATE ="xxx"; 9 1 //支付宝RSA公钥 用签约支付宝账号登录ms.alipay.com后,在密0 钥管理页面获取。
1 public static final String RSA_ALIPAY_PUBLIC ="xxx"; 1 //下面的配置告诉应用去assets目录下找安装包
1 public static final String ALIPAY_PLUGIN_NAME 2 ="alipay_plugin223_0309.apk"; }
AlixDemo中代码是最终的调用代码在onItemClick(AdapterView arg0, View arg1, int arg2, long arg3 {}中,下面我们提取其中的核心代码。

6.提取核心调用代码
AlixDemo.java同目录下新建AlixPay.java,来提取AlixDemo.java的核心代码: ?
1 package com.tianxia.lib.baseworld.alipay; 2 3 import java.net.URLEncoder; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 import com.tianxia.lib.baseworld.R; 8 9 import android.app.Activity; 1import android.app.ProgressDialog; 0 import android.content.DialogInterface; 1import android.os.Handler; 1 import android.os.Message; 1import android.view.KeyEvent; 2 import android.widget.Toast; 1 3 public class AlixPay { 1 4 static String TAG ="AlixPay"; 1 5 private Activity mActivity; 1 public AlixPay(Activity activity { 6 mActivity = activity; 1 } 7 1 private ProgressDialog mProgress =null; 8
1 // the handler use to receive the pay result. 9 private Handler mHandler =new Handler( { 2 public void handleMessage(Message msg { 0 try { 2 String strRet = (String msg.obj; 1 2 switch (msg.what { 2 case AlixId.RQF_PAY: { 2 3 closeProgress(; 2 4 BaseHelper.log(TAG, strRet; 2 5 try { 2 String memo ="memo="; 6 int 2strRet.indexOf("memo="; 7 imemoStart += memo.length(; 2 int 8 strRet.indexOf(";result="; 2 memo = strRet.substring(imemoStart, 9 imemoEnd; 3 0 ResultChecker 3ResultChecker(strRet; 1 3 int retVal = resultChecker.checkSign(; 2 if
(retVal
== 3ResultChecker.RESULT_CHECK_SIGN_FAILED { 3 BaseHelper.showDialog( 3 mActivity, 4 "提示", resultChecker =new
imemoEnd
=
imemoStart
=
3 5 mActivity.getResources(.getString( 3 6 R.string.check_sign_failed, 3 7 android.R.drawable.ic_dialog_alert; 3 }else { 8 BaseHelper.showDialog(mActivity,"3提示", memo, 9 R.drawable.infoicon; 4 } 0 4 }catch (Exception e { 1 e.printStackTrace(; 4 2 BaseHelper.showDialog(mActivity,"4", strRet, 3 4 R.drawable.infoicon; } 4 4 } break; 5 4 } 6 4 super.handleMessage(msg; }catch (Exception e { 7 4 e.printStackTrace(; } 8 4 } }; 9 5 // close the progress bar
0
5 void closeProgress( { 1 try { 5 if (mProgress !=null { 2 mProgress.dismiss(; 5 mProgress =null; 3 } 5 }catch (Exception e { 4 e.printStackTrace(; 5 } 5 } 5 6 public void pay( { 5 MobileSecurePayHelper 7 MobileSecurePayHelper(mActivity; 5 boolean
isMobile_spExist
= 8 mspHelper.detectMobile_sp(; 5 if (!isMobile_spExist 9 return; 6 0 if (!checkInfo( { 6 BaseHelper.showDialog(mActivity,"提示", 1 "66 return; } 3 6 try { 4 6 // prepare the order info.
String orderInfo = getOrderInfo(; 5 6 String signType = getSignType(; String strsign = sign(signType, orderInfo; 6
partner
seller
",
mspHelper =new
R.drawable.infoicon; 2
6 strsign = URLEncoder.encode(strsign; 7 String info = orderInfo +"&sign=" +"\"" + strsign 6+"\"" +"&" 8 + getSignType(; 6 9 // start the pay.
7 MobileSecurePayer msp =new MobileSecurePayer(; 0 boolean 1 7 if (bRet { 2 // show the progress bar to indicate that we have 7started paying.
3 closeProgress(; 7 mProgress 4 BaseHelper.showProgress(mActivity,null,"正在支付",false, 7 true; 5 }else
7 ; 6 }catch (Exception ex { 7 Toast.makeText(mActivity, 7 R.string.remote_call_failed, 7 Toast.LENGTH_SHORT.show(; 8 } 7 9 } 8 0 private boolean checkInfo( { 8 String partner = PartnerConfig.PARTNER; 1 String seller = PartnerConfig.SELLER; 8 if (partner ==null || partner.length( <=0 || seller 2 ==null
=
bRet
=
msp.pay(info,
mHandler, 7AlixId.RQF_PAY, mActivity;
8 || seller.length( <=0 3 return false; 8 4 return true; 8 } 5 8 6 // get the selected order info for pay. 8 String getOrderInfo( { 7 String strOrderInfo ="partner=" +"\"" + 8PartnerConfig.PARTNER +"\""; 8 strOrderInfo +="&"; 8 strOrderInfo +="seller=" +"\"" + PartnerConfig.SELLER 9 +"\""; 9 strOrderInfo +="&"; 0 strOrderInfo 9getOutTradeNo( +"\""; 1 strOrderInfo +="&"; 9 //这笔交易价钱
2 strOrderInfo +="subject=" +"\"" 9mActivity.getString(R.string.donate_subject +"\""; 3 strOrderInfo +="&"; 9 //这笔交易内容
4 9 strOrderInfo 9 strOrderInfo +="&"; 6 //这笔交易价钱
9 strOrderInfo +="total_fee=" +"\"" +"10.00" +"\""; 7 strOrderInfo +="&"; 9 strOrderInfo +="notify_url=" +"\"" 8 +"http://notify.java.jpxx.org/index.jsp" +="body="
+"\"" mActivity.getString(R.string.donate_body +"\""; 5
+
+="out_trade_no="
+"\""
+
+
9+"\""; 9 1 return strOrderInfo; 0 } 0 1 // get the out_trade_no for an order. 0 String getOutTradeNo( { 1 SimpleDateFormat 1SimpleDateFormat("MMddHHmmss"; 0 Date date =new Date(; 2 String strKey = format.format(date; 1 0 java.util.Random r =new java.util.Random(; 3 strKey = strKey + r.nextInt(; 1 strKey = strKey.substring(0,15; 0 return strKey; 4 } 1 0 // get the sign type we use. 5 String getSignType( { 1 String getSignType ="sign_type=" +"\"" +"RSA" +"\""; 0 return getSignType; 6 } 1 0 // sign the order info.
7 String sign(String signType, String content { 1 return Rsa.sign(content, PartnerConfig.RSA_PRIVATE; 0 } 8 1 // the OnCancelListener for lephone platform. 0 static class AlixOnCancelListenerimplements 9 DialogInterface.OnCancelListener { format =new

1 Activity mcontext; 1 0 AlixOnCancelListener(Activity context { 1 mcontext = context; 1 } 1 1 public void onCancel(DialogInterface dialog { 1 mcontext.onKeyDown(KeyEvent.KEYCODE_BACK,null; 2 } 1 } 1} 3 114 115 116 117 118 119 12
0 121 122 123 124 125 126 127 128 129 130 1
31 132 133 134 135 136 137 138 139 140 141
142 143 144 145 146 147 148 149 150 151 15
2 153 154 155 156 157 158 159 160 161 162 1
63 164 165 166 167 168 169 170 171 172 173
174 175 176 177 178 179 180 181 182 183 18
4 185 186 187 188 189 190 191 192 193 194 1
95 196 197 198 199 200 201 这个类的pay方法就是支付的方法,最简单的不设置的话,调用方法如下: ?
AlixPay alixPay =new AlixPay(SettingTabActivity.this; 1 alixPay.pay(; 2
如果没有安装支付宝,它会提示你安装,如果已经安装,它直接让你选择付款:


这说明已经配置成功了。
java


AlixDemo.java,ProductListAdapter.java,Products.java 你也可以通过调整参数来修改订单信息,如主题,价格等。 另外在BaseHelper94行: ?
dialog.setOnCancelListener(new
1 AlixDemo.AlixOnCancelListener( (Activitycontext ;
需要修改为: ?
dialog.setOnCancelListener(new
1 AlixPay.AlixOnCancelListener( (Activitycontext ; 7.注意
我在测试的时候,调用的activity是框在一个ActivityGroup里的(与tabhost类似,据说tabhost也有这个问题),导致MobileSecurePayer.javapay法中调用服务的两行代码: ?
mActivity.bindService(new Intent(IAlixPay.class.getName(, mAlixPayConnection, Context.BIND_AUTO_CREATE; mActivity.unbindService(mAlixPayConnection;
需要修改为: ?
1 mActivity.getApplicationContext(.bindService(new 2 Intent(IAlixPay.class.getName(, mAlixPayConnection,
Context.BIND_AUTO_CREATE; mActivity.getApplicationContext(.unbindService(mAlixPayConnection;
不然会报错java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.android.server.am.ActivityRecord$Token... 8.小结
支付宝的集成比我想象的要复杂一些,比较麻烦,首先需要审核,然后代码需要提取,所以写出来与大家分享。

在做集成配置的时候,一定要仔细认真,一个地方出错,可能要导致后面查错查很长时间。
因为本人是先集成成功后才写的这篇文章,难免会漏掉一些重要的细节或者步骤,如有不对,请留言指正。



本文来源:https://www.2haoxitong.net/k/doc/1c45e8242f60ddccda38a019.html

《Android学习系列(28)--App集成支付宝.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式