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

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) {
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        alipayRequest.setNotifyUrl(WECHAT_PAY_PAY_CALLBACK_URL);

        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;
        }
    }


}
