package com.yonge.toolset.thirdparty.union;

import com.alibaba.fastjson.JSONObject;

import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 银联商务H5支付
 */
public class H5 {
    static String appId = "10037e6f6a4e6da4016a670fd4530012";
    static String appKey = "f7a74b6c02ae4e1e94aaba311c04acf2";
    static String mid = "898310148160568";
    static String tid = "88880001";

    static String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    static String nonce = UUID.randomUUID().toString().replace("-", "");

    public static byte[] hmacSHA256(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {
        String algorithm = "HmacSHA256";
        Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(key, algorithm));
        return mac.doFinal(data);
    }

    public static String getOpenBodySigForNetpay(String appId, String appKey, String timestamp, String nonce, String content) throws Exception {
        String signatureStr = appId + timestamp + nonce + DigestUtils.sha256Hex(content);
        byte[] localSignature = hmacSHA256(signatureStr.getBytes(), appKey.getBytes());
        return ("authorization=OPEN-FORM-PARAM" + "&appId=" + appId + "&timestamp=" + timestamp + "&nonce=" + nonce + "&content=" + URLEncoder.encode(content, "UTF-8") + "&signature=" + URLEncoder.encode(Base64.encodeBase64String(localSignature), "UTF-8"));
    }


    public static String getRequestMap() throws Exception {
        JSONObject json = new JSONObject();

        json.put("instMid", "H5DEFAULT");
        json.put("mid", mid);
        json.put("tid", tid);
        json.put("totalAmount", "20000");

        json.put("orderDesc", "大雅乐盟培训课程");
        // json.put("bizType", "B2B");
        json.put("merOrderId", "1017" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomStringUtils.randomNumeric(7));
        json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        json.put("returnUrl", "http://dev.dayaedu.com");

        String param = getOpenBodySigForNetpay(appId, appKey, timestamp, nonce, json.toString());
        return "http://58.247.0.18:29015/v1/netpay/trade/h5-pay?" + param;
    }


    public static void main(String[] args) throws Exception {

        System.out.println(getRequestMap());

    }
}

