- 使用微信支付SDK理所应该去看官方文档,按步骤一步步来就好了,怎么还需要写博客呢?
- 答案是因为使用支付SDK时,遇到了很多弱智的坑,希望后来人不要和我一样SB就好。
IWXAPI api = WeixinSDK.getInstance().getWxapi();
PayReq request = new PayReq();
request.appId = [微信开放平台申请的AppId];
request.partnerId = [微信支付需要的商户ID];
request.prepayId = [服务器接口请求微信后台获取到的prepayId];
request.packageValue = "Sign=WXPay";//这是个固定字符串,就是它!
Random random = new Random();
String nonceStr = MD5.hexdigest(String.valueOf(random.nextInt(10000)).getBytes());
String packageValue = "Sign=WXPay";
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
String key = [在微信开放平台分配的密钥key];
String str = "appid=" + [微信开放平台申请的AppId] + "&noncestr=" + nonceStr + "&package=" + packageValue + "&partnerid=" + [微信开放平台分配的商户partnerId] + "&prepayid=" + weixinOrderResult.prepay_id + "×tamp=" + timeStamp;
String str1 = str + "&key=" + key;
String sign = MD5.hexdigest(str1).toUpperCase();
request.nonceStr = nonceStr;
request.timeStamp = timeStamp;
request.sign = sign;
api.sendReq(request);
一定要注意nonceStr、packageValue、sign 这三个参数的生成规则,尤其是 sign 这个参数生成前拼接的那个串一定要注意!!!!!