package edu.whut.test;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import edu.whut.config.AliPayConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class AliPayTest {
    @Autowired
    private AliPayConfigProperties config;    // 字段注入

    private AlipayClient alipayClient;


    @Before
    public void init() {
        log.info(config.getNotifyUrl());
        // ④ 用属性里的值来构造 AlipayClient
        this.alipayClient = new DefaultAlipayClient(
                config.getGatewayUrl(),
                config.getAppId(),
                config.getMerchantPrivateKey(),
                config.getFormat(),
                config.getCharset(),
                config.getAlipayPublicKey(),
                config.getSignType()
        );
    }

    @Test
    public void test_aliPay_pageExecute() throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();  // 发送请求的 Request类
        request.setNotifyUrl(config.getNotifyUrl());
        request.setReturnUrl(config.getReturnUrl());

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "smile000091004001");  // 我们自己生成的订单编号
        bizContent.put("total_amount", "0.01"); // 订单的总金额
        bizContent.put("subject", "测试商品");   // 支付的名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");  // 固定配置
        request.setBizContent(bizContent.toString());

        String form = alipayClient.pageExecute(request).getBody();
        log.info("测试结果：{}", form);

        /**
         * 会生成一个form表单；
         <form name="punchout_form" method="post" action="https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8&method=alipay.trade.page.pay&sign=vl9G%2FJHoVvn5cW87mYYK2PhiISCw4pFdB9kYLEqx645S6Q%2BzN9OBw1QbfkmvNB5EOApbNOsz2UyvKeWQtUVswyHJhadIIEvbbP925VIvWveqYvPY6rnoFfA3OXXZR%2FxFrDLxij%2BhCtz7N6T9Iyxraf4UGuiTmez8LJZu60Exr2ej%2FSeZIwRXOflB6u7xFXfcJqVHE0sz1%2B2vkDaMjYBJ77dZoLLUs2smavzN%2BZIRs7%2FT8w39w7ymab23uMSM%2FXLMi%2B%2BrL9rSzvdWgk9NwxLIKslc2IWg2%2Bk5%2BVOc1KyY2K%2F35XzRzaeYGWuMCCLYCwDVE2Lvp8ul99A5%2FebYQaaV9g%3D%3D&return_url=https%3A%2F%2Fblog.bitday.top&notify_url=https%3A%2F%2Fpay.bitday.top%2Fapi%2Fv1%2Falipay%2Falipay_notify_url&version=1.0&app_id=9021000150645052&sign_type=RSA2&timestamp=2025-07-12+16%3A29%3A37&alipay_sdk=alipay-sdk-java-4.38.157.ALL&format=json">
         <input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;smile000091004001&quot;,&quot;total_amount&quot;:&quot;0.01&quot;,&quot;subject&quot;:&quot;测试商品&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE_PAY&quot;}">
         <input type="submit" value="立即支付" style="display:none" >
         </form>
         <script>document.forms[0].submit();</script>
         */
    }

    /**
     * 查询订单
     */
    @Test
    public void test_alipay_certificateExecute() throws AlipayApiException {

        AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
        bizModel.setOutTradeNo("smile000091004001");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizModel(bizModel);

        String body = alipayClient.execute(request).getBody();
        log.info("测试结果：{}", body);
    }

    /**
     * 退款接口
     */
    @Test
    public void test_alipay_refund() throws AlipayApiException {
        AlipayTradeRefundRequest request =new AlipayTradeRefundRequest();
        AlipayTradeRefundModel refundModel =new AlipayTradeRefundModel();
        refundModel.setOutTradeNo("daniel82AAAA000032333361X03");
        refundModel.setRefundAmount("1.00");
        refundModel.setRefundReason("退款说明");
        request.setBizModel(refundModel);

        AlipayTradeRefundResponse execute = alipayClient.execute(request);
        log.info("测试结果：{}", execute.isSuccess());
    }

    public static void main(String[] args) {
        System.out.println(new BigDecimal("9.99").doubleValue());
    }

}