开发android app微信支付功能,需要完成三个步骤:第一步生成预支付订单、第二步生成微信支付参数、第三步调起微信app支付。除了需要审核通过的app应用外,还需要获得微信支付接口权限,然后获取对应的商户号、api密钥,这两者缺一不可,并且在app微信支付中使用
获得商户号、api密钥
在微信开放平台中查看审核通过的app应用,是否申请支付功能,若已申请,登录微信支付|商户平台: http://pay.weixin.qq.com 查看对应的商户号、api密钥
》申请微信支付接口
》登录商户平台
》查看商户号
》获取api密钥
第一步:生成预支付订单
将商品信息、商户信息使用basicnamevaluepair存放,然后存储在list列表中,并构造成xml字符串格式,以post方式微信提供的接口:https://api.mch.weixin.qq.com/pay/unifiedorder 发送数据,接收返回信息,获取prepay_id预支付订单编号
》生成订单请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
private string genproductargs() { stringbuffer xml = new stringbuffer(); try { string noncestr = gennoncestr(); xml.append( "</xml>" ); list<namevaluepair> packageparams = new linkedlist<namevaluepair>(); packageparams .add( new basicnamevaluepair( "appid" , constants.app_id)); packageparams.add( new basicnamevaluepair( "body" , "weixin" )); packageparams .add( new basicnamevaluepair( "mch_id" , constants.mch_id)); packageparams.add( new basicnamevaluepair( "nonce_str" , noncestr)); packageparams.add( new basicnamevaluepair( "notify_url" , "http://121.40.35.3/test" )); packageparams.add( new basicnamevaluepair( "out_trade_no" , genouttradno())); packageparams.add( new basicnamevaluepair( "spbill_create_ip" , "127.0.0.1" )); packageparams.add( new basicnamevaluepair( "total_fee" , "1" )); packageparams.add( new basicnamevaluepair( "trade_type" , "app" )); string sign = genpackagesign(packageparams); packageparams.add( new basicnamevaluepair( "sign" , sign)); string xmlstring = toxml(packageparams); return xmlstring; } catch (exception e) { log.e(tag, "genproductargs fail, ex = " + e.getmessage()); return null ; } } |
》获取预支付订单编号
1
2
3
4
5
6
7
8
9
10
|
string url = string.format( "https://api.mch.weixin.qq.com/pay/unifiedorder" ); string entity = genproductargs(); log.e( "orion" , entity); byte [] buf = util.httppost(url, entity); string content = new string(buf); log.e( "orion" , content); map<string, string> xml = decodexml(content); |
第二步:生成微信支付参数
appid对应app应用的appid,partnerid对应商户号,prepayid对应第一步获得的预支付订单,packagevalue取值sign=wxpay,noncestr是一串随机数,timestamp生成的时间戳,sign对应app签名
》支付参数配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
private void genpayreq() { req.appid = constants.app_id; req.partnerid = constants.mch_id; req.prepayid = resultunifiedorder.get( "prepay_id" ); req.packagevalue = "sign=wxpay" ; req.noncestr = gennoncestr(); req.timestamp = string.valueof(gentimestamp()); list<namevaluepair> signparams = new linkedlist<namevaluepair>(); signparams.add( new basicnamevaluepair( "appid" , req.appid)); signparams.add( new basicnamevaluepair( "noncestr" , req.noncestr)); signparams.add( new basicnamevaluepair( "package" , req.packagevalue)); signparams.add( new basicnamevaluepair( "partnerid" , req.partnerid)); signparams.add( new basicnamevaluepair( "prepayid" , req.prepayid)); signparams.add( new basicnamevaluepair( "timestamp" , req.timestamp)); req.sign = genappsign(signparams); sb.append( "sign\n" + req.sign + "\n\n" ); show.settext(sb.tostring()); log.e( "orion" , signparams.tostring()); } |
》noncestr随机数
1
2
3
4
5
|
private string gennoncestr() { random random = new random(); return md5.getmessagedigest(string.valueof(random.nextint( 10000 )) .getbytes()); } |
》timestamp时间戳
1
2
3
|
private long gentimestamp() { return system.currenttimemillis() / 1000 ; } |
》app签名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
private string genappsign(list<namevaluepair> params) { stringbuilder sb = new stringbuilder(); for ( int i = 0 ; i < params.size(); i++) { sb.append(params.get(i).getname()); sb.append( '=' ); sb.append(params.get(i).getvalue()); sb.append( '&' ); } sb.append( "key=" ); sb.append(constants.api_key); this .sb.append( "sign str\n" + sb.tostring() + "\n\n" ); string appsign = md5.getmessagedigest(sb.tostring().getbytes()) .touppercase(); log.e( "orion" , appsign); return appsign; } |
第三步:调起微信app支付
支付之前,需要手机安装微信客户端,如果没有,会提示下载安装微信,然后再调起支付,在androidmanifest.xml文件中添加如下配置
1
2
3
4
5
6
7
8
9
10
11
12
|
<activity android:name= ".act.payactivity" android:exported= "true" android:launchmode= "singletop" > <intent-filter> <action android:name= "android.intent.action.view" /> <category android:name= "android.intent.category.default" /> <data android:scheme= "wxf2f565574a968187" /> </intent-filter> </activity> |
》调起微信支付
1
2
3
4
5
|
private void sendpayreq() { msgapi.registerapp(constants.app_id); msgapi.sendreq(req); } |
接收微信返回结果
在实现了iwxapieventhandler接口的activity中重写onresp(baseresp)方法,接收微信结果
》接收支付结果
1
2
3
4
5
6
7
8
9
10
11
|
public void onresp(baseresp resp) { log.d(tag, "onpayfinish, errcode = " + resp.errcode); if (resp.gettype() == constantsapi.command_pay_by_wx) { alertdialog.builder builder = new alertdialog.builder( this ); builder.settitle(r.string.app_tip); builder.setmessage(getstring(r.string.pay_result_callback_msg, resp.errstr + ";code=" + string.valueof(resp.errcode))); builder.show(); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助。