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.ArrayList;
import java.util.List;

public class Test2JSAPI {



    public static void main(String[] args) {
        // TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/merchant/4013070756
        JsapiPrepay client = new JsapiPrepay(
                "1731491745",                    // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
                "793D48CCEB62C6B227E0A4F46AD90279B149A7BE",         // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
                "/Users/quyixiao/Desktop/weixincert/apiclient_key.pem",     // 商户API证书私钥文件路径，本地文件路径
                "PUB_KEY_ID_0117314917452025110400382304001401",      // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
                "/Users/quyixiao/Desktop/weixincert/wxp_pub.pem"           // 微信支付公钥文件路径，本地文件路径
        );

        DirectAPIv3JsapiPrepayRequest request = new DirectAPIv3JsapiPrepayRequest();
        request.setAppid("wx75fa59c097bd3dfd");
        request.setMchid("1731491745");
        request.setNotifyUrl("https://api.1024api.com/api-interface/app/weixin/payNotify");



        request.setDescription("Image形象店-深圳腾大-QQ公仔");
        request.setOutTradeNo("121775250");
        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-JuBeT6rIu6RhF Gf3BQ");
        request.setPayer(payer);

        CouponInfo couponInfo = new CouponInfo();

        couponInfo.setCostPrice(608800L);
        couponInfo.setInvoiceId("微信123");
        request.setDetail(couponInfo);
        List<GoodsDetail> goodsDetails = new ArrayList<>();

        GoodsDetail goodsDetailItem = new GoodsDetail();
        goodsDetailItem.setMerchantGoodsId("1246464644");
        goodsDetailItem.setWechatpayGoodsId("1001");
        goodsDetailItem.setGoodsName("iPhoneX 256G");
        goodsDetailItem.setQuantity(1L);
        goodsDetailItem.setUnitPrice(528800L);
        goodsDetails.add(goodsDetailItem);
        couponInfo.setGoodsDetail(goodsDetails);

        CommonSceneInfo sceneInfo = new CommonSceneInfo();
        sceneInfo.setPayerClientIp("14.23.150.211");
        sceneInfo.setDeviceId("013467007045764");

        StoreInfo setstoreInfo = new StoreInfo();
        setstoreInfo.setId("0001");
        setstoreInfo.setName("腾讯大厦分店");
        setstoreInfo.setAreaCode("440305");
        setstoreInfo.setAddress("广东省深圳市南山区科技中一道10000号");

        SettleInfo settleInfo = new SettleInfo();
        settleInfo.setProfitSharing(false);

        request.setSceneInfo(sceneInfo);
        sceneInfo.setStoreInfo(setstoreInfo);
        request.setSettleInfo(settleInfo);


        try {
            DirectAPIv3JsapiPrepayResponse response = client.doPay(request);
            System.out.println(JSON.toJSONString(response));
            // TODO: 请求成功，继续业务逻辑
            System.out.println(response);
        } catch (Exception e) {
            // TODO: 请求失败，根据状态码执行不同的逻辑
            e.printStackTrace();
        }
    }


}
