PayServiceClient.java
3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 lombok.extern.slf4j.Slf4j;
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;
@Slf4j
@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.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();
log.info("payInform json:{}", jsonDataString);
Response payResponse = client.newCall(getPhoneRequest).execute();
if (payResponse.code() != 200 || payResponse.body() == null) {
log.error("payInform response:{}", payResponse);
throw new IllegalArgumentException("支付通知失败");
}
String payJsonData = payResponse.body().string();
log.info("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) {
log.error("payInform error", e);
throw new IllegalArgumentException("支付通知失败");
}
}
}