package com.buy507.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.buy507.mall.portal.domain.AlipayPayTradeDto;
import com.buy507.mall.portal.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private AlipayClient alipayClient;

    @Value("${alipay.payCallBackUrl}")
    private String WECHAT_PAY_PAY_CALLBACK_URL;


    @Override
    public String createPayment(AlipayPayTradeDto tradeDto) throws AlipayApiException {
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.putOtherTextParam("app_auth_token", "202410BB12e6eb3d1a144891a3c7d5b7715d1X07");
        request.setReturnUrl(WECHAT_PAY_PAY_CALLBACK_URL);
      /*  JSONObject bizContent = new JSONObject();
        alipayRequest.setBizContent("{" +
                "\"out_trade_no\":\"" + tradeDto.getOutTradeNo() + "\"," +
                "\"total_amount\":\"" + tradeDto.getTotalFee() + "\"," +
                "\"subject\":\"" + tradeDto.getSubject() + "\"," +
                "\"product_code\":\"QUICK_WAP_WAY\"" +
                "}");

        try {
            AlipayTradeWapPayResponse response = alipayClient.pageExecute(alipayRequest, "POST");
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;*/
       /* if(StrUtil.isNotEmpty(alipayConfig.getNotifyUrl())){
            //异步接收地址，公网可访问
            request.setNotifyUrl(alipayConfig.getNotifyUrl());
        }*/
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + tradeDto.getOutTradeNo() + "\"," +
                "\"total_amount\":\"" + tradeDto.getTotalFee() + "\"," +
                "\"subject\":\"" + tradeDto.getSubject() + "\"," +
                "\"product_code\":\"QUICK_WAP_WAY\"" +
                "}");

//        //******必传参数******
//        JSONObject bizContent = new JSONObject();
//        //商户订单号，商家自定义，保持唯一性
//        bizContent.put("out_trade_no", tradeDto.getOutTradeNo());
//        //支付金额，最小值0.01元
//        bizContent.put("total_amount", "0.01");
//        //订单标题，不可使用特殊符号
//        bizContent.put("subject", tradeDto.getSubject());
//        //手机网站支付默认传值FAST_INSTANT_TRADE_PAY
//        bizContent.put("product_code", "QUICK_WAP_WAY");
//        request.setBizContent(bizContent.toString());
        AlipayTradeWapPayResponse response =alipayClient.pageExecute(request);
        String formHtml = null;
        formHtml = response.getBody();
        return formHtml;
    }



}
