www.icesr.com
IT运维工程师的摇篮

微信支付及支付宝支付开发指南

从开始学习android开始到现在还没搞过三方支付,感觉挺遗憾的。看到最近有几篇关于微信支付和支付宝支付的文章,稍微进行一下整合,供大家参考。

———————————————————-微信支付—————————————————————


1.申请开发者资质



地址:

https://open.weixin.qq.com


使用公司管理者/高层帐号登录微信开放平台,进入“账号中心”,进行开发者资质认证,需要填写公司资料,包括但不限于,公司注册号,公司营业执照,公司对外办公电话,公司对公银行卡信息(卡号,发卡行)。审核时间为一周左右。



  • 因为从2015年10月1日起,国家实行三证(组织机构代码证,企业营业执照,税务登记证)合一,所以组织机构代码处填写-工商执照注册号,同样,组织机构代码证,上传-企业工商营业执照。

     

  • 进行开发者资质认证需要支付300元人名币/年,只有具备开发者资质认证的开发者才能够使用app支付,授权登录等接口。


2.申请APP_ID/APP_KEY


每个应用/游戏要调用微信的接口都需要有一个微信标志,这个唯一标志通常成为
APP_ID
或者
APP_KEY
,各开放平台差异不大 。



进入管理中心,创建移动应用,每个开发者具有10个应用的创建机会,好在创建的应用可以随时删除。已上线的应用就不建议手贱删除!!


申请appid需要填写应用信息:应用名称,包名,签名(keystore的md5值去分号小写),icon(28*28 & 108*108),app下载地址等信息,即可分配到一个appid。


  • 测试支付时,务必使用申请时填写的
    keystore文件签名
    包名也需要核对清楚,必然无法调起支付,返回-1错误码。


3.申请支付能力


管理中心,查看需要集成支付能力的app,找到“微信支付”一栏,点击右侧“申请开通”,填写一些企业信息后等待审核,审核时间为一周左右,审核通过后,会得到一个企业商户号及密码,对公银行卡中将收到几分钱,进入商户平台,输入收到的金钱金额以验证。验证通过后即可开始集成支付调用。在这之前,调用支付接口是无法完成支付的。



4.代码集成微信支付


支付流程图:



微信支付流程图白话版


1.客户端代码得到用户购买的商品信息,将之传给自己公司app服务器,参数包含但不限于以下:



<span style="font-size:10px; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="color:#969896; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">// 微信appid,选择性上传,服务器写死亦可<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></span>params<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">.</span>put(<span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>appid<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>, appID);<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/><span style="color:#969896; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">// 支付金额,单位:分<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></span>params<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">.</span>put(<span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>money<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>, money);<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/><span style="color:#969896; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">// 商品名称<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></span>params<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">.</span>put(<span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>name<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>, goodsName);<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/><span style="color:#969896; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">// 支付币种,只能填CNY,代表人民币<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></span>params<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">.</span>put(<span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>currencytype<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>, <span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>CNY<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>);</span>


  • 以上是参数字段是我们公司服务器定义,给服务器用以获取预支付订单号prePayId用。

  • 详细字段请参考:

    https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1


2.app服务器调用微信“统一下单”接口,得到

prePayId
订单号并返回
prePayId
给手机客户端;


3.手机客户端使用

prePayId
及商品信息调起微信客户端进行支付;

  • 用户操作:输入密码进行支付;返回键取消支付;网络无连接支付失败等



4.微信客户端回调支付结果给咱们的APP客户端;


5.微信服务器异步通知咱们公司app服务器支付结果(服务器的工作,与客户端无关)



微信支付代码:




在app包下新建
wxapi包
,比如app包名为
com.wxdemo.app
,那个新建的包路径为
com.wxdemo.app.wxapi
,该包下新建
WXPayEntryActivity
,继承自任意Activity。该Activity用以接收支付结果回调,清单声明此Activity的格式为:


<span style="font-size:10px; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&lt;</span>activity
 &nbsp; &nbsp;android<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">:</span>name<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">=</span><span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>.wxapi.WXPayEntryActivity<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>
 &nbsp; &nbsp;android<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">:</span>exported<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">=</span><span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>true<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>
 &nbsp; &nbsp;android<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">:</span>launchMode<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">=</span><span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>singleTop<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span><span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">/&gt;</span></span>


WXPayEntryActivity
代码示例:




回调中errCode值列表:


  • 0 :支付成功 。

  • -1 :发生错误 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。

  • -2:
    用户取消 发生场景:用户不支付了,点击取消,返回APP。



manifest声明所需权限


<span style="font-size:10px; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&lt;</span>uses<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">-</span>permission android<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">:</span>name<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">=</span><span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>android.permission.INTERNET<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span> <span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">/&gt;<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/>&lt;</span>uses<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">-</span>permission android<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">:</span>name<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">=</span><span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>android.permission.MODIFY_AUDIO_SETTINGS<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span><span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">/&gt;<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/>&lt;</span>uses<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">-</span>permission android<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">:</span>name<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">=</span><span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>android.permission.WRITE_EXTERNAL_STORAGE<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span><span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">/&gt;</span></span>
  • 支付结果最终以app服务器的得到的异步通知为准。


OK 到这我们微信支付就算是搞定了。



最后


附上微信官方支付demo下载:

https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1


吐槽语:官方demo下载藏的有点深。。。开放平台居然没有提供下载,放在微信支付商户平台。

———————————————–支付宝支付————————————————–

第一步:签约成为支付宝商户

签约地址:

https://b.alipay.com


只有成为签约商户的开发者才能具备集成支付宝app支付的资格。


签约资料:

  1. 营业执照

  2. APP说明文档

  3. 商户经营信息、商户联系人等信息


必要时还需
提供APP
以备审核。审核通过后即可进行代码集成。


审核通过后,可得到支付宝分配的
商户号等信息

第二步:客户端代码集成准备

导入jar包资源


目前最新版支付宝开发jar包下载地址:

http://download.csdn.net/detail/xiong_it/9566771



下载后将之拷贝
libs目录
Eclipse
会自动添加依赖,Android Studio 需在

app的gradle
中添加一行:


<span style="font-size:10px; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">compile files(<span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">'</span>libs/alipaySdk-20160223.jar<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">'</span></span>)</span>


点击右上角:Sync Now,稍等片刻。。。


修改AndroidManifest.xml清单


声明必要Activity:



添加必要权限:

&lt;<span style="color:#000080; font-weight:bold">uses-permission </span><span style="color:#660e7a; font-weight:bold">android</span><span style="color:#0000ff; font-weight:bold">:name=</span><span style="color:#008000; font-weight:bold">&quot;android.permission.INTERNET&quot;</span>/&gt;
&lt;<span style="color:#000080; font-weight:bold">uses-permission </span><span style="color:#660e7a; font-weight:bold">android</span><span style="color:#0000ff; font-weight:bold">:name=</span><span style="color:#008000; font-weight:bold">&quot;android.permission.ACCESS_NETWORK_STATE&quot;</span>/&gt;
&lt;<span style="color:#000080; font-weight:bold">uses-permission </span><span style="color:#660e7a; font-weight:bold">android</span><span style="color:#0000ff; font-weight:bold">:name=</span><span style="color:#008000; font-weight:bold">&quot;android.permission.ACCESS_WIFI_STATE&quot;</span>/&gt;
&lt;<span style="color:#000080; font-weight:bold">uses-permission </span><span style="color:#660e7a; font-weight:bold">android</span><span style="color:#0000ff; font-weight:bold">:name=</span><span style="color:#008000; font-weight:bold">&quot;android.permission.READ_PHONE_STATE&quot;</span>/&gt;
&lt;<span style="color:#000080; font-weight:bold">uses-permission </span><span style="color:#660e7a; font-weight:bold">android</span><span style="color:#0000ff; font-weight:bold">:name=</span><span style="color:#008000; font-weight:bold">&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;</span>/&gt;

添加支付宝混淆规则



第三步:客户端代码集成


支付宝交互流程示意图 


流程图白话版解释:


  1. app携带支付信息调用支付接口请求支付宝客户端调起支付界面;

  2. 用户操作,输入密码支付,支付成功;直接返回取消支付;出现错误,支付失败;进入支付界面,但输入密码支付,支付待确认;

  3. 支付宝客户端将支付结果告诉app客户端,商户服务器通知app服务器支付结果;

  4. app客户端处理支付结果;

  5. app服务器处理支付结果。


支付宝的支付流程
较之
微信的支付流程图
少了一步app服务器端订单的生成,但是在其demo代码中是推荐使用app服务器进行sign签名过程的,于是笔者索性是按照的微信支付的流程去集成的。


  • 客户端代码得到用户购买的商品信息,将之传给自己公司app服务器,参数包含但不限于以下


    <span style="font-size:10px; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="color:#969896; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">// 商品金额,单位:元<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></span>params<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">.</span>put(<span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>money<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>, payMoney);<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/><span style="color:#969896; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">// 商品名称<br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></span>params<span style="color:#a71d5d; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">.</span>put(<span style="color:#183691; margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span>goodsname<span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">&quot;</span></span>, goodsName);</span>


    注意:支付宝支付的人民币单位和微信支付,银联支付稍有不同,需以:元 作为支付单位。其他两者支付单位为:分 。其他参数均交给咱们app服务器处理即可,详细参数请点击:

    https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103663&docType=1


  • app服务器参考上述详细参数链接,得到sign(支付签名信息)字段并返回sign字段给手机客户端;


  • 手机客户端使用sign签名信息在非UI线程调起支付客户端进行支付; 

    用户操作:输入密码进行支付;返回键取消支付;进入支付界面,用户未进行支付,用户返回,待支付;网络无连接支付失败等;


  • 客户端得到支付结果;


  • 支付宝服务器异步通知咱们公司app服务器支付结果(服务器的工作,与客户端无关)


这样做的好处:签名逻辑在服务器完成,app无需暴露公钥和私钥,更安全。同时也是demo中的推荐做法。



更详细的支付宝接入交互流程解释请点击:

https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.7WO30X&treeId=59&articleId=103658&docType=1



客户端代码示例


支付调起代码(须在子线程):




支付处理代码(UI线程):




注:AliPayResult.java
来自支付宝demo中的
PayResult.java 


至此,集成支付宝SDK结束。祝各位集成支付宝支付成功!



结束语



按照惯例,附上支付宝SDK接入的官方demo下载链接:

https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1

未经允许不得转载:冰点网络 » 微信支付及支付宝支付开发指南

分享到:更多 ()

评论 抢沙发

评论前必须登录!