PayServiceClient.java
3.16 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
package com.infoloop.tianting.service.client;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
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.model.dto.WxUserDto.WxUserPhoneResponseDto;
import com.infoloop.tianting.utils.DateUtil;
import com.infoloop.tianting.utils.EncryptionUtil;
import java.io.IOException;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import okhttp3.FormBody;
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;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PayServiceClient {
private final OkHttpClient client = new OkHttpClient();
public PayResponseDto payInform(PayInformRequestDto payRequestDto) {
try {
String url = "http://10.2.1.166:21051/API/Microapp/CreateOrder?appid=Order0001";
String clientId = "Order0001";
ObjectMapper objectMapper = new ObjectMapper();
ThirdPartyPayInformRequestDto jsonData = new ThirdPartyPayInformRequestDto();
jsonData.setClientId(clientId);
jsonData.setTradeId(payRequestDto.getOrderId());
String requestDate = DateUtil.formatDate(LocalDateTime.now(), DateUtil.YMDHMS);
jsonData.setRequestDate(requestDate);
jsonData.setRequestSource("OFWXApplet");
jsonData.setPayerOpenId(payRequestDto.getOpenId());
jsonData.setHospitalCode(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.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
PayResponseDto payResponseDto = objectMapper.readValue(payJsonData, PayResponseDto.class);
return payResponseDto;
} catch (Exception e) {
System.out.println(e.getStackTrace().toString());
throw new IllegalArgumentException("支付通知失败");
}
}
}