PayServiceClient.java 3.88 KB
package com.infoloop.tianting.service.client;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.infoloop.tianting.config.BusinessConfig;
import com.infoloop.tianting.model.dto.PayDTO.PayInformRequestDto;
import com.infoloop.tianting.model.dto.PayDTO.PayResponseDto;
import com.infoloop.tianting.model.dto.PayDTO.ThirdPartyPayInformRequestDto;
import com.infoloop.tianting.utils.DateUtil;
import com.infoloop.tianting.utils.EncryptionUtil;
import lombok.RequiredArgsConstructor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PayServiceClient {

    private final OkHttpClient client = new OkHttpClient();

    private final BusinessConfig businessConfig;

    public PayResponseDto payInform(PayInformRequestDto payRequestDto) {
        try {
            String url = businessConfig.getPayUrl();
            String clientId = "Order0001";
            ObjectMapper objectMapper = new ObjectMapper();
//      objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
            objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);

            ThirdPartyPayInformRequestDto jsonData = new ThirdPartyPayInformRequestDto();
            jsonData.setClientId(clientId);
            jsonData.setTradeId(payRequestDto.getOrderId()); //payRequestDto.getOrderId()
            String requestDate = DateUtil.formatDate(LocalDateTime.now(), DateUtil.YMDHMS);
            jsonData.setRequestDate(requestDate);
            jsonData.setRequestSource("OFWXApplet");
            jsonData.setPayerOpenId(payRequestDto.getOpenId());
            jsonData.setHospitalCode("AMCAREBJLD"); //payRequestDto.getHospitalCode()
            jsonData.setPID(payRequestDto.getPhone());
            jsonData.setRemark("");
            jsonData.setTransAmount(payRequestDto.getPrice());
            jsonData.setTradeDesc("");
            String signStr = EncryptionUtil.sha1Encryption(clientId + payRequestDto.getOrderId() + requestDate + "Order0001");
            jsonData.setSign(signStr);
            String jsonDataString = objectMapper.writeValueAsString(jsonData);
            // 设置请求体的媒体类型为JSON
            MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(mediaType, jsonDataString);
            Request getPhoneRequest = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Response payResponse = client.newCall(getPhoneRequest).execute();
            if (payResponse.code() != 200) {
                System.out.println(payResponse.message());
                throw new IllegalArgumentException("支付通知失败");
            }
            String payJsonData = payResponse.body().string();
            System.out.println("pay json:" + payJsonData);
            ObjectMapper objectMapper2 = new ObjectMapper();
            objectMapper2.configure(Feature.ALLOW_MISSING_VALUES, true);
            objectMapper2.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper2.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES, false);
            return objectMapper2.readValue(payJsonData, PayResponseDto.class);
        } catch (Exception e) {
            System.out.println(e.getStackTrace().toString());
            throw new IllegalArgumentException("支付通知失败");
        }
    }
}