Android 微信支付教程及注意事项

1.后台设置

商户在微信开放平台申请开发应用后,微信开放平台会生成APP的唯一标识APPID。由于需要保证支付安全,需要在开放平台绑定商户应用包名和应用签名,设置好后才能正常发起支付。设置界面在【开放平台】中的栏目【管理中心 / 修改应用 / 修改开发信息】里面

应用签名的获取

应用包名:是在APP项目配置文件AndroidManifest.xml中声明的package值,例如DEMO中的package="net.sourceforge.simcpux"。应用签名:根据项目的应用包名和编译使用的keystore,可由签名工具生成一个32位的md5串,在调试的手机上安装签名工具后,运行可生成应用签名串,绿色串即应用签名。

签名工具下载地址https://open.weixin.qq.com/zh_CN/htmledition/res/dev/download/sdk/Gen_Signature_Android.apk

2.项目配置

2.1把微信的JAR包和Utils导入工程

JAR包下载地址:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319167&token=&lang=zh_CN

2.2写一个Activity,路径和类的名称必须为.wxapi.WXpayEntryActivity,implementsIWXAPIEventHandler,用于接收微信支付结果

2.3写一个广播,注册app


2.4在AndroidManifest.xml中配置,刚才定义的Activity和广播


2.5在工具类中配置APPID,商户号,API密匙


2.6在主类中编写微信支付流程

2.6.1App支付生成预支付订单

GetPrepayIdTask getPrepayId = new GetPrepayIdTask();

getPrepayId.execute();


private classGetPrepayIdTaskextendsAsyncTask> {

privateProgressDialogdialog;

@Override

protected voidonPreExecute() {

dialog= ProgressDialog.show(PayActivity.this, getString(R.string.app_tip), getString(R.string.getting_prepayid));

}

@Override

protected voidonPostExecute(Map result) {

if(dialog!=null) {

dialog.dismiss();

}

sb.append("prepay_id\n"+result.get("prepay_id")+"\n\n");

show.setText(sb.toString());

resultunifiedorder=result;

Log.e("tag","result:"+ result);

}

@Override

protected voidonCancelled() {

super.onCancelled();

}

@Override

protectedMap  doInBackground(Void... params) {

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 =newString(buf);

Log.e("orion", content);

Map xml=decodeXml(content);

returnxml;

}

}

2.6.2生成App微信支付参数,重点是

notify_url为微信给我们自己服务器的回调

total_fee为支付总金额,以为单位

详细解释见文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1

2.6.3、调用微信App支付

privatevoidsendPayReq(){msgApi.registerApp(Constants.APP_ID);msgApi.sendReq(req);}

2.6.4、接收微信返回码,做相应处理


源码地址:https://github.com/LostDeer/weixin

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容