首页 加油卡充值 加油卡充值示例代码 加油卡充值[Java]

加油卡充值示例代码[Java]

作者:xiezhongpian 阅读数:1703 上传时间:2017-05-09

加油卡充值

package api.jisuapi.fuelcardrecharge;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import api.util.HttpUtil;
import net.sf.json.JSONObject;

public class Recharge {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/fuelcardrecharge/recharge";
	public static final String fuelcard = "1000110000001234567";// 要充值的加油卡卡号
	public static final String amount = "200";// 充值的金额 单位:元
	public static final String outorderno = "";// 外部订单号
	public static final String appsecret = "sssssssssss";
	public static Map queryarr;

	public static void Get() throws NoSuchAlgorithmException, UnsupportedEncodingException {
		queryarr = new HashMap();
		queryarr.put("amount", amount);
		queryarr.put("fuelcard", fuelcard);
		queryarr.put("outorderno", outorderno);
		String sign = makeSign(queryarr, appsecret);// md5(fuelcard,amount,outorderno按key排序)
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&fuelcard=" + fuelcard + &amount=" + amount
				+ "&outorderno=" + outorderno + "&sign=" + sign;

		try {
			result = HttpUtil.sendGet(url, "utf-8");
			JSONObject json = JSONObject.fromObject(result);
			if (json.getInt("status") != 0) {
				System.out.println(json.getString("msg"));
			} else {
				JSONObject resultarr = (JSONObject) json.opt("result");
				String fuelcard = resultarr.getString("fuelcard");
				String totalfee = resultarr.getString("totalfee");
				String amount = resultarr.getString("amount");
				String outorderno = resultarr.getString("outorderno");
				String orderno = resultarr.getString("orderno");
				String rechargestatus = resultarr.getString("rechargestatus");
				System.out.println(fuelcard + " " + amount + " " + outorderno + " " + orderno + " "
						+ totalfee + " " + rechargestatus);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String makeSign(Map queryarr, String appsecret)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		TreeMap map = new TreeMap(queryarr);
		Iterator ir = map.keySet().iterator();
		String str = new String();
		while (ir.hasNext()) {
			Object key = ir.next();
			str += map.get(key);
		}
		str += appsecret;
		return getMd5(str.getBytes());
	}

	public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException {
		String s = null;
		char hexDigist[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(buffer);
		byte[] datas = md.digest(); // 16个字节的长整数
		char[] str = new char[2 * 16];
		int k = 0;
		for (int i = 0; i < 16; i++) {
			byte b = datas[i];
			str[k++] = hexDigist[b >>> 4 & 0xf];// 高4位
			str[k++] = hexDigist[b & 0xf];// 低4位
		}
		s = new String(str);
		return s;
	}
}