package com.api.test;

import com.alibaba.fastjson.JSON;
import com.heyu.api.jsapi.JsapiPrepay;
import com.heyu.api.jsapi.WXPayUtility;
import com.heyu.api.jsapi.dto.pay.*;

import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestJSAPI {





    public final  static   String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzn74aYlkoD1lO" +
            "rcbpqW9aIE/IiH/lAea+nFmmmao5ggxrXhnvkGDQ3e+59Cf59jTikKxmMc+aj36m" +
            "axrd/OYDM27j0q5b7t8fWPD4qqyMR2uqiImc622BpQIxaPUeLeDHwsHq4C6g0UzZ" +
            "6DXJppHpb6xn0gEFHS87Na0hhElCbEpjEnbyhRmOC1yDPDmnDTGMX2LP0Cxx6U1p" +
            "X4JmYToQ0/iOFeD8F9sLrQElGelJnDS6QY+L+qKe7SecNv0axvbwyGXuSUef2+GW" +
            "gBCnBeWTZhhHNPNAFo6fQR9+zaQmZEksg9HxmAKLxG1w6YSMXQWk8psSiCSoiQfu" +
            "39m3pN13AgMBAAECggEAbhvoPug22xW9mztvieDxf3/7KGR0cf+eYQ4a7sOX07Ti" +
            "xBZlM7N/hcnmoEkJEHNaq+Afrm2uY4K/EmjpiVbz8NZgjYuiknx1jhPJc8W8DCnj" +
            "2B/mq6it8iEinQNH7v4Uop/Cm6ZdLvvebl2oSzquiziHqQTU1zuyrksTHE6pUetj" +
            "6ci2AgyKOpyZ3nJ8ivgUSFuvQjlEQ1aJFrFg6nm52MTdJqB9JaQEcBYxw21JCfl1" +
            "zNZ7EO4eFBqzNO0ttu3EJckYDKClhcesNrJJGUHvPFGo2hfOd2ZzfA4UnaL7yara" +
            "AJxQbqwnXOoaV14ka9HYNfakmz59348AxrIbPtHj0QKBgQDskdn7Cut/1DV2EhW2" +
            "6rNj9zaXt55COujHf5+0htj9JrZ6u9s4sCHuzPo8rLiAw7/Pvksfa7Oq+BHE65rM" +
            "ds88KluLyW84b1Wd4annyI+anjtDyz3Chmx7NGQyaHg7lfOqJ09S/0T6WwSViZJz" +
            "3Fp6p39+1ROyhWZaFRQIwrA6UwKBgQDCYIrAYFnqBaXMk5wFoDg2kc7H6/SL9wgx" +
            "To5EwV/ccqFirbScjuM9IkylcIx02gKUH9y+3aDccNPr7PqIreLip9sX+DdFqDa5" +
            "yEtY4XNR9zAf0aSgo+qSCAtgMqrWwyV+fKUjbzEbEjD7STPXLy+r1YC0g0Z7Wesz" +
            "LG0f4MYTzQKBgQCkCFW+7klwrzIKlmucE40jqYyfEmCXx8UUX3fbcw0OK0OoQo20" +
            "9tvwewyf+ZtNHW3onCf2t3Uy/SNFCaCiWVdEfpJPkPKfjQMuoARxhO2d5k1tqoU+" +
            "VnrtytwW371Og6EawHsOL5Yiie3ZyjRURdwu4+lRhmlMBZd8qtTjZitPpQKBgQCV" +
            "rMOLKWZzRxAJvOxahKpkkthYp//yOzHqzePNW95WIUrWco3uNDUVITFF/6mYXTu5" +
            "FePkcULqHFODi0LMNqHMCJc0GOVu1P33Bx3F/izPw/kht0v+itoYwusHk1xr7W8U" +
            "vCRpabi1cMeY6CBsJaCev9PQrHl8iJwNFruc3XeJTQKBgFt31iujP7W2FHaT6uyO" +
            "1gKSlqCcZILD2F15Q52OYQoV6I5Tq152ITyMyE4EiYPOqnqVWUc+ej1m+AuvYljR" +
            "qlvVCmp91QtYwLGguuHFYNVcE2V3WftKKbIIPT2qEFaS5eUb6hB+kl02E1EmjCtA" +
            "pwssCVJj81yu/DsBP0580zBL";



    public final  static  String wechatPayPublicKey  = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx/QqdSnkS8wd7xFH0LHV" +
            "pZTFVI+eWL383eAP7RaxHfRwXNDfZYrdi/Bt8Enko+1Wkr2FGY2kS4Hol5NCvuZ7" +
            "UY28/L4r95dBEgXtflkrxeVbiNbWHtiIdNXZSgkrpUkZEFoh7ks5Ou3pAlpG+94/" +
            "Nnc//D/yckM7AuD85G1foZMSO5niyVeFAed6z7CeBMAhRVdnOIDUqsI/NaHruT4f" +
            "vWNzPnn5SQKoKum00vRHckxftUqMkZ1D/YxHiAycEc9H0hCgLW7ZM6UV88Loa6SP" +
            "vhM0fmXIuk6p277ldlxFl6Bcxd5jOcCTjdpWaB3CeSXnSIBl6KsZNcV3vPC3xey1" +
            "WwIDAQAB";




    public final  static  String mchid  = "1731491745";
    public final  static  String Certificate_serial_number  = "793D48CCEB62C6B227E0A4F46AD90279B149A7BE";                 // 证书序列号
    public final  static  String PUB_KEY_ID  = "PUB_KEY_ID_0117314917452025110400382304001401";                 // 证书序列号
    public final  static  String appid  = "wx75fa59c097bd3dfd";                 // 证书序列号



    public static void main(String[] args) {


        // TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/merchant/4013070756
        JsapiPrepay client = new JsapiPrepay(
                mchid,                    // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
                Certificate_serial_number,         // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
                privateKey,     // 商户API证书私钥文件路径，本地文件路径
                PUB_KEY_ID,      // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
                wechatPayPublicKey   // 微信支付公钥文件路径，本地文件路径
        );


        System.out.println("privateKey:"+privateKey);
        System.out.println("wechatPayPublicKey:" +wechatPayPublicKey);

        DirectAPIv3JsapiPrepayRequest request = new DirectAPIv3JsapiPrepayRequest();
        request.setAppid(appid);
        request.setMchid(mchid);

        request.setNotifyUrl("https://api.1024api.com/api-interface/app/weixin/payNotify");

        request.setDescription("Image形象店-深圳腾大-QQ公仔");

        request.setOutTradeNo("121775xx");
        request.setTimeExpire(WXPayUtility.generateExpireTime()); // 2025-11-05T21:02:16+08:00
        request.setAttach("自定义数据说明");

        request.setGoodsTag("WXG");

        request.setSupportFapiao(false);


        CommonAmountInfo commonAmountInfo = new CommonAmountInfo();
        commonAmountInfo.setTotal(5L);
        commonAmountInfo.setCurrency("CNY");
        request.setAmount(commonAmountInfo);

        JsapiReqPayerInfo payer = new JsapiReqPayerInfo();
        payer.setOpenid("o6t1512tT-JuBeT6rIu6RhFGf3BQ");
        request.setPayer(payer);


        try {
            DirectAPIv3JsapiPrepayResponse response = client.doPay(request);
            System.out.println(JSON.toJSONString(response));

            String prepay_id = response.getPrepayId();


            WxPayVO vo = new WxPayVO();
            Long timeStamp = System.currentTimeMillis() / 1000;
            String nonceStr = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);

            String signatureStr = Stream.of(appid, String.valueOf(timeStamp), nonceStr, "prepay_id=" + prepay_id)
                    .collect(Collectors.joining("\n", "", "\n"));

            //  "timeStamp": "1414561699",
            //    "nonceStr": "5K8264ILTKCH16CQ2502SI8ZNMTM67VS",
            //    "package": "prepay_id=wx201410272009395522657a690389285100",
            //    "signType": "RSA",
            //    "paySign": "oR9d",

            String sign = JsapiPrepay.getSign(signatureStr, privateKey);
            vo.setNonceStr(nonceStr);
            vo.setTimeStamp(timeStamp);
            vo.setMchid(mchid);
            vo.setPaySign(sign);
            vo.setPrepayId("prepay_id=" + prepay_id);
            System.out.println(JSON.toJSONString(vo));
            // TODO: 请求成功，继续业务逻辑
            System.out.println(response);
        } catch (Exception e) {
            // TODO: 请求失败，根据状态码执行不同的逻辑
            e.printStackTrace();
        }
    }


}
