Android 微信支付

 /**
     * 获取sign签名
     * @return
     */
    private void genPayReq(String mPrepayId) {
       //获取随机字符串noncestr
        String randomStr = getRandomString(32);
        //获取时间戳
        String timeStamp = getTime();

        // 把参数的值传进去SortedMap集合里面
        SortedMap parameters = new TreeMap();
        parameters.put("appid", "wx000555ead6f77035");
        parameters.put("noncestr", randomStr);
        parameters.put("package", "Sign=WXPay");
        parameters.put("partnerid", "1415888402");
        parameters.put("prepayid", mPrepayId);
        parameters.put("timestamp", timeStamp);

        String characterEncoding = "UTF-8";
        String mySign = createSign(characterEncoding, parameters);
        System.out.println("我的签名是:" + mySign);

         //发起支付
        final IWXAPI msgApi = WXAPIFactory.createWXAPI(getApplicationContext(), "wxd930ea5d5a258f4f");
        msgApi.registerApp("wxd930ea5d5a258f4f");

        PayReq request = new PayReq();
        request.appId = "wx000555ead6f77035";
        request.partnerId = "1415888402";
        request.prepayId = mPrepayId;
        request.packageValue = "Sign=WXPay";
        request.nonceStr = randomStr;
        request.timeStamp = timeStamp;
        request.sign = mySign;
        msgApi.sendReq(request);
    }

    /**
     * 微信支付签名算法sign
     * @param characterEncoding
     * @param parameters
     * @return
     */
    public static String createSign(String characterEncoding,
                                    SortedMap parameters) {

        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序(升序)
        Iterator it = es.iterator();
        while (it.hasNext()) {
            @SuppressWarnings("rawtypes")
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k)
                    && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + AppConstant.Key); //KEY是商户秘钥
        String sign = MD5.encrypt(sb.toString())
                .toUpperCase();
        return sign; //
    }


    /**
     * 生成随机字符串
     *
     * @param length
     * @return
     */
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取时间戳
     *
     * @return
     */
    public String getTime() {
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳
        String str = String.valueOf(time);
        return str;
    }

  /**
     * MD5加密方法
     *
     * @return
     */
public class MD5 {

    public final static String encrypt(String plaintext) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] btInput = plaintext.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }
}

你可能感兴趣的:(android微信支付)