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(); } } } } } 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;
    }