微信小程序支付统一下单接口

微信小程序——支付

微信小程序支付统一下单接口_第1张图片

1.通过code获取openId

code:用户登录凭证(有效期五分钟)。开发者需要在开发者服务器后台调用 api,使用 code 换取 openid 和 session_key 等信息
openid: 用户唯一标识
session_key: 会话密钥

2.构建统一下单参数,发送微信客户端
/**
     * 构建统一下单参数,发给微信服务器
     * 
     * @param tradeType
     * @param body
     * @param tradeNo 订单号
     * @param ip
     * @return
     */
    private UnifiedOrderRequestData constructData(
            String tradeNo, 
            int totalFee,
            String ip, 
            String openid) {
        UnifiedOrderRequestData data = new UnifiedOrderRequestData.
                UnifiedOrderReqDataBuilder(
                WeixinConstant.FEBDA_PAY_BODY, tradeNo, totalFee, ip,
                WeixinConstant.TRADE_TYPE).setOpenid(openid).build();
        // 产生签名信息
        data.setSign(WxPayUtil.getSign(data));
        return data;
    }
/**
     * 利用反射获取Java对象的字段并进行加密,过滤掉sign字段
     * @param data
     * @return
     * return:String
     */
    public static String getSign(Object data) {
        StringBuilder stringA = new StringBuilder();
        Map<String, String> map = new HashMap<String, String>();
        Field[] fields = data.getClass().getDeclaredFields();
        Method[] methods = data.getClass().getDeclaredMethods();
        for (Field field : fields) {
            String fieldName = field.getName();
            if (field != null && !fieldName.equals("sign")) {
                String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
                for (Method method : methods) {
                    if (method.getName().equals(getMethodName)) {
                        try {
                            if (method.invoke(data, new Object[]{}) != null && method.invoke(data, new Object[]{}).toString().length() != 0) {
                                map.put(fieldName, method.invoke(data, new Object[]{}).toString());
                            }
                        } catch (IllegalAccessException | IllegalArgumentException
                                | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        ListString,String>> mappingList = null; 
        //通过ArrayList构造函数把map.entrySet()转换成list 
        mappingList = new ArrayListString,String>>(map.entrySet()); 
        //通过比较器实现比较排序 
        Collections.sort(
            mappingList, 
            new ComparatorString,String>>(){ 
                public int compare(Map.Entry<String,String> mapping1,Map.Entry<String,String> mapping2){ 
                    return mapping1.getKey().compareTo(mapping2.getKey()); 
                } 
            }
        );
        for (Map.Entry<String, String> mapping : mappingList) {
            stringA.append("&").append(mapping.getKey()).append("=").append(mapping.getValue());
        }
        String stringSignTemp = stringA.append("&key=").append(WeixinPayConfig.KEY).substring(1);
        return WeixinUtil.MD5(stringSignTemp).toUpperCase();
    }
/**
     * 调用微信统一下单接口,返回客户端数据
     * 
     * @param tradeType
     *            JSAPI支付
     * @return UnifiedOrderResponseData
     */
    private UnifiedOrderResponseData unifiedOrder(String userId, String proId,
            int price, String ip, String openid,String type,String orderId) { // proId 问题id
        if(Strings.isNullOrEmpty(ip)){
             InetAddress addr = null;
            try {
                addr = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
              ip = addr.getHostAddress().toString();
        }
        /*if (PayApp.theApp.isDebug()) {// 测试时候支付一分钱,买入价值6块的20分钟语音
            price = 1;//1分钱
        }*/
        // 生成请求数据对象
        UnifiedOrderRequestData data = constructData(orderId, price, ip, openid);
        // 调用微信统一下单接口
        UnifiedOrderResponseData responseData = WxPayUtil.unifiedOder(data);
        return responseData;
    }

你可能感兴趣的:(微信小程序开发)