Android开发支付集成——微信集成
Posted Mr.Ding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发支付集成——微信集成相关的知识,希望对你有一定的参考价值。
支付宝支付传送门:https://www.cnblogs.com/dingxiansen/p/9208949.html
二、微信支付
1. 微信支付流程图
相比较而言,微信支付是要比支付宝麻烦一些,并且微信的开发文档个人也感觉写的比较乱,初次接触会有些懵。
2. 集成前准备
3.开始集成
微信支付可以直接在app build.gradle中设置
/*微信支付集成*/
compile \'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+\'
然后在你的包下,这里要注意以下放在指定包下边
AndroidManifest.xml配置
<!--微信支付,回掉--> <activity android:name=".wxapi.WXPayEntryActivity" android:exported="true" android:launchMode="singleTop" />
微信的后台参数返回,相对于支付宝的返回信息,微信返回信息更加清晰一下,不过没啥用,一会加密完都一个样子
格式化后:
{ "msg":"success", "code":200, "data":{ "appid":"自己申请的appId", "noncestr":"jqEmamoTbMoi0hRP", "package":"Sign=WXPay", "partnerid":"150****351", "prepayid":"wx21144021******fd37e2948041031", "sign":"50E345DCF6900A*****10126E7424", "timestamp":"1529563221" }, "status":"0" }
拿到返回参数,微信支付方法
/*微信支付测试OK*/ private void testWxPay(final String key, final String value) { StringRequest stringRequest = new StringRequest(Request.Method.POST, NetWorkUrl.WXPAY, new Response.Listener<String>() { @RequiresApi(api = Build.VERSION_CODES.M) @Override public void onResponse(String s) { Log.e("GoPayOrderActivity", "-------getJson2-------" + s.toString()); /*判断code*/ String code = (JSONObject.parseObject(s.toString()).getString("code")); if (code.equals("200")) { String jsonObject = JSONObject.parseObject(s.toString()).getString("data"); String appId = JSONObject.parseObject(jsonObject).getString("appid"); String sign = JSONObject.parseObject(jsonObject).getString("sign"); String packageWx = JSONObject.parseObject(jsonObject).getString("package"); String partnerid = JSONObject.parseObject(jsonObject).getString("partnerid"); String prepayid = JSONObject.parseObject(jsonObject).getString("prepayid"); String noncestr = JSONObject.parseObject(jsonObject).getString("noncestr"); String timestamp = JSONObject.parseObject(jsonObject).getString("timestamp"); Log.e("GoPayOrderActivity", "onResponse: appId" + appId); Log.e("GoPayOrderActivity", "onResponse: sign" + sign); Log.e("GoPayOrderActivity", "onResponse: package" + packageWx); Log.e("GoPayOrderActivity", "onResponse: partnerid" + partnerid); Log.e("GoPayOrderActivity", "onResponse: prepayid" + prepayid); Log.e("GoPayOrderActivity", "onResponse: noncestr" + noncestr); Log.e("GoPayOrderActivity", "onResponse: timestamp" + timestamp); api = WXAPIFactory.createWXAPI(GoPayOrderActivity.this, appId); PayReq req = new PayReq(); //req.appId = "wxf8b4f85f3a794e77"; // 测试用appId //req的参数接受可以拿取order对象的数据 req.appId = appId; req.partnerId = partnerid; req.prepayId = prepayid; req.nonceStr = noncestr; req.timeStamp = timestamp; req.packageValue = packageWx; req.sign = sign; req.extData = "app data"; // optional // 在支付之前,如果应用没有注册到微信,应该先调用IWXMsg.registerApp将应用注册到微信 api.sendReq(req); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }) { @Override public Map<String, String> getParams() throws AuthFailureError { Log.e("GoPayOrderActivity", "getParams:-----------------> " + userEntity.getPhone()); Map<String, String> map = new HashMap<String, String>(); map.put("account", userEntity.getPhone()); map.put(key, value); // map.put("productId", "1"); // map.put("productId", "1"); map.put("token", userEntity.getToken()); return map; } @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); if (userEntity.getToken().equals("") && userEntity != null) { headers.put("Authorization", userEntity.getToken()); } return headers; } }; /*设置请求一次*/ stringRequest.setRetryPolicy( new DefaultRetryPolicy( 5000,//默认超时时间,应设置一个稍微大点儿的,例如本处的500000 DefaultRetryPolicy.DEFAULT_MAX_RETRIES,//默认最大尝试次数 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT ) ); AppApplication.getHttpQueues().add(stringRequest);/*请求数据*/ }
微信支付回调
WXPayEntryActivity.java
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler { private static final String TAG = "WXPayEntryActivity"; private IWXAPI api; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pay_result); api = WXAPIFactory.createWXAPI(this, Constants.APP_ID); api.handleIntent(getIntent(), this); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); } @Override public void onReq(BaseReq req) { } @Override 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(WXPayEntryActivity.this); /*注释掉的可以成功*/ builder.setTitle(R.string.app_tip); builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode))); builder.show(); int code = resp.errCode; switch (code) { case 0: MobclickAgent.onEvent(WXPayEntryActivity.this, "payment_success", "付款成功"); // finish(); // Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show(); /*跳转我的会员页面*/ AppApplication.addActivity(WXPayEntryActivity.this); Intent intent = new Intent(this, MyVipActivity.class); startActivity(intent); break; case -1: MobclickAgent.onEvent(WXPayEntryActivity.this, "payment_fali", "付款失败"); // 失败原因 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配,其他异常等 Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show(); finish(); break; case -2: Toast.makeText(this, "支付取消", Toast.LENGTH_SHORT).show(); finish(); break; default: Toast.makeText(this, "支付取消", Toast.LENGTH_SHORT).show(); finish(); break; } } } }
完成上述,微信支付也是可以完成的了,注意测试的时候要用Release包,测试包是调用不起来微信的
如果问题或建议请发送到我的邮箱:dingchao7323@qq.com
以上是关于Android开发支付集成——微信集成的主要内容,如果未能解决你的问题,请参考以下文章