E企盈营销工具技术服务商 热线:4006-838-530

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

E企盈直播平台营销卖货系统

微信小程序——支付1.通过code获取openIdcode:用户登录凭证(有效期五分钟)。开发者需要在开发者服务器后台调用 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();                        }                    }                }            }        }        List<Map.Entry<String,String>> mappingList = null;         //通过ArrayList构造函数把map.entrySet()转换成list        mappingList = new ArrayList<Map.Entry<String,String>>(map.entrySet());         //通过比较器实现比较排序        Collections.sort(            mappingList,             new Comparator<Map.Entry<String,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;    }小程序

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序支付统一下单接口
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏