package com.bawei.mall.car.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.bawei.mall.car.service.PaymentService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

/**
 * @program: mall_cloud
 * @ClassName: PaymentServiceImpl
 * @author: Gyc
 * @create: 2024-05-12 16:24
 **/
@Service
public class PaymentServiceImpl implements PaymentService {


    @SneakyThrows
    @Override
    public String pay(String orderId) {

    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi-sandbox.dl.alipaydev.com/gateway.do",
           "9021000136694024",
           "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCFz/238QvZGbGw9EXAihMzA/wBLprkwCZN2PlZAWBJRI28L+rO8xAtgeC6AVarRGF6e0qMZ4LLWtOgL1RyfxOL2I/Ny+F8jmY0wEZABt6WKI1/l9HliZ/hxNoko4IHo6tf7NEhZJMmAce088PB3IVmFWv1aaSOL7/WaZlSiz/80ZA2v7oHzntblVm1FHFok7kCumndzdj8SgaPtZRA964lx3zhQHWV2O22ThhqN6zksJzXsQoPFvIv8Ic5ymYPzCvnSs7U5jyHMwQ1btYXgUMbv1Uqo9It2U0Gq3P4rZFMjfPn5FSdLaWepTpSg69Ittqo3YCzBmlKZjVmBc5W9thHAgMBAAECggEAF3AJ2akP0TPt6reua/4DROFCx5Y5LS32x5XkftF94vdt9/NQpnTudXoVOZ84ZgExLbngod0TjLjY7mq72N4nN0x0S7CVSzLJ9h1jcJ2DaTurJRkbe5/ycCIBr38wLFRflQquyrGKEJMpPqmftRoCB5Ji+XZKi1kTueN/JnUbfRWavAagK6YK8qlZYAiQr+PXiLcF2U3qcB7BgqVb7BJU5g/IiotHE+0ijtqWcT+X9ElIqw5mgmkvRZBPy5iVeD/b2F5b4Mbr9MvQkaICzt+P8QZCTKyd31GOhSV6zYguaisN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DGrFPvGwPl+VUX7NlYIG8QdKdMmBHA+/UbqvytEIrRqkP3/s+yrbRGzGmN8Ht8wNmPEn51IlTZVcvi2cxs1CY0CY9k70pkWnZBmhxxOG3EvgM1dV/61aWQkJk3OFhITSIWlf9FQBxH1E1RfciSoEWgjew/KoWyKp83rqKuUtHehILdNsfqHFHO/lzDp2XhVBPKNqafIQvAhcWyBwhGD9Cs83YvLldvJ9DE2LGFi8eF+IvVbcFrCfkkxrT4kBoxGYwIDAQAB",
           "RSA2");
    AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
    //异步接收地址，仅支持http/https，公网可访问
    request.setNotifyUrl("");
    //同步跳转地址，仅支持http/https
    request.setReturnUrl("");
    /******必传参数******/
    JSONObject bizContent = new JSONObject();
    //商户订单号，商家自定义，保持唯一性
    bizContent.put("out_trade_no",orderId);
    //支付金额，最小值0.01元
    bizContent.put("total_amount", 200.00);
    //订单标题，不可使用特殊符号
    bizContent.put("subject", "测试商品");
    //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
    bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

    /******可选参数******/
    //bizContent.put("time_expire", "2022-08-01 22:00:00");

    ////// 商品明细信息，按需传入
    //JSONArray goodsDetail = new JSONArray();
    //JSONObject goods1 = new JSONObject();
    //goods1.put("goods_id", "goodsNo1");
    //goods1.put("goods_name", "子商品1");
    //goods1.put("quantity", 1);
    //goods1.put("price", 200.00);
    //goodsDetail.add(goods1);
    //bizContent.put("goods_detail", goodsDetail);

    //// 扩展信息，按需传入
    //JSONObject extendParams = new JSONObject();
    //extendParams.put("sys_service_provider_id", "2088511833207846");
    //bizContent.put("extend_params", extendParams);

            request.setBizContent(bizContent.toString());
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request,"POST");
    // 如果需要返回GET请求，请使用
    // AlipayTradePagePayResponse response = alipayClient.pageExecute(request,"GET");
            String pageRedirectionData = response.getBody();
            System.out.println(pageRedirectionData);

            if(response.isSuccess()){
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }


        return "";
    }
}
