折腾了一天,终于搞定了微信支付,总结一下。首先从服务器端获取prepareid,Andorid 端再根据这个prepareid二次签名。
服务器端:
从官网上下载DEMO,Demo中只有JsAPi,Micro,Native。没有APP的,那怎么办呢?模仿着jsAPi的自己写一个。
第一步:配置WxPay.Config.php
const APPID = 'wx004d72dd9aab9cc5'; const MCHID = '1293496801'; const KEY = 'zcvnoasdfasnvu93n320dvndsf98nlds';
第二步:替换cert
const SSLCERT_PATH = '../cert/apiclient_cert.pem'; const SSLKEY_PATH = '../cert/apiclient_key.pem';
第三步:统一下单
<?php ini_set('date.timezone','Asia/Shanghai'); require_once "../lib/WxPay.Api.php"; //①、获取用户openid //$tools = new JsApiPay(); //$openId = $tools->GetOpenid(); //②、统一下单 $input = new WxPayUnifiedOrder(); $input->SetBody("test"); $input->SetAttach("test"); $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis")); $input->SetTotal_fee("1"); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php"); $input->SetTrade_type("APP"); //$input->SetOpenid($openId); $order = WxPayApi::unifiedOrder($input); $parameters = json_encode($order); echo $parameters;
Android端
private void weixinPay() { final IWXAPI msgApi = WXAPIFactory.createWXAPI(getActivity(), null); msgApi.registerApp(Constants.APP_ID); String url = "http://192.168.208.34/WxpayAPI/example/appapi.php"; Toast.makeText(getActivity(), "获取订单中...", Toast.LENGTH_SHORT).show(); HttpUtil.sendJsonRequest(HttpRequest.HttpMethod.GET, url, null, new HttpRequestCallBack<Object>() { @Override public void onSuccessed(ResponseInfo<Object> responseInfo) { Log.e("Josen", responseInfo.result.toString()); prePayBean = new Gson().fromJson(responseInfo.result.toString(), PrePayBean.class); PayReq req = new PayReq(); req.appId = prePayBean.getAppid(); req.partnerId = prePayBean.getMch_id(); req.prepayId = prePayBean.getPrepay_id(); req.nonceStr = RandomString.getRandomString(20); req.timeStamp = String.valueOf(System.currentTimeMillis()/1000); req.packageValue = "Sign=WXPay"; String string = "appid="+Constants.APP_ID+"&noncestr=" +req.nonceStr+"&package="+req.packageValue+"&partnerid=" +req.partnerId+"&prepayid="+req.prepayId+"×tamp=" +req.timeStamp+"&key="+Constants.APP_KEY; String sign = MD5.getMessageDigest(string.getBytes()); req.sign = sign.toUpperCase(); msgApi.sendReq(req); } @Override public void onFailed(HttpException exception, String reason) { } }, 0); }