package com.wechat.pay.java.service.partnerpayments.nativepay;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.http.DefaultHttpClientBuilder;
import com.wechat.pay.java.core.http.HostName;
import com.wechat.pay.java.core.http.HttpClient;
import com.wechat.pay.java.core.http.HttpHeaders;
import com.wechat.pay.java.core.http.HttpMethod;
import com.wechat.pay.java.core.http.HttpRequest;
import com.wechat.pay.java.core.http.HttpResponse;
import com.wechat.pay.java.core.http.MediaType;
import com.wechat.pay.java.core.http.QueryParameter;
import com.wechat.pay.java.core.http.RequestBody;
import com.wechat.pay.java.core.http.UrlEncoder;
import com.wechat.pay.java.core.util.GsonUtil;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.CloseOrderRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.PrepayResponse;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.QueryOrderByIdRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.QueryOrderByOutTradeNoRequest;
import com.wechat.pay.java.service.partnerpayments.nativepay.model.Transaction;
import java.util.Objects;
public class NativePayService {
private final HttpClient httpClient;
private final HostName hostName;
private NativePayService(HttpClient httpClient, HostName hostName) {
this.httpClient = (HttpClient)Objects.requireNonNull(httpClient);
this.hostName = hostName;
}
public void closeOrder(CloseOrderRequest request) {
String requestPath = "https://api.mch.weixin.qq.com/v3/pay/partner/transactions/out-trade-no/{out_trade_no}/close";
requestPath = requestPath.replace("{out_trade_no}", UrlEncoder.urlEncode(request.getOutTradeNo()));
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), this.hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest = (new com.wechat.pay.java.core.http.HttpRequest.Builder()).httpMethod(HttpMethod.POST).url(requestPath).headers(headers).body(this.createRequestBody(request)).build();
this.httpClient.execute(httpRequest, (Class)null);
}
public PrepayResponse prepay(PrepayRequest request) {
String requestPath = "https://api.mch.weixin.qq.com/v3/pay/partner/transactions/native";
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), this.hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest = (new com.wechat.pay.java.core.http.HttpRequest.Builder()).httpMethod(HttpMethod.POST).url(requestPath).headers(headers).body(this.createRequestBody(request)).build();
HttpResponse httpResponse = this.httpClient.execute(httpRequest, PrepayResponse.class);
return (PrepayResponse)httpResponse.getServiceResponse();
}
public Transaction queryOrderById(QueryOrderByIdRequest request) {
String requestPath = "https://api.mch.weixin.qq.com/v3/pay/partner/transactions/id/{transaction_id}";
requestPath = requestPath.replace("{transaction_id}", UrlEncoder.urlEncode(request.getTransactionId()));
QueryParameter queryParameter = new QueryParameter();
if (request.getSpMchid() != null) {
queryParameter.add("sp_mchid", UrlEncoder.urlEncode(request.getSpMchid()));
}
if (request.getSubMchid() != null) {
queryParameter.add("sub_mchid", UrlEncoder.urlEncode(request.getSubMchid()));
}
requestPath = requestPath + queryParameter.getQueryStr();
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), this.hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest = (new com.wechat.pay.java.core.http.HttpRequest.Builder()).httpMethod(HttpMethod.GET).url(requestPath).headers(headers).build();
HttpResponse httpResponse = this.httpClient.execute(httpRequest, Transaction.class);
return (Transaction)httpResponse.getServiceResponse();
}
public Transaction queryOrderByOutTradeNo(QueryOrderByOutTradeNoRequest request) {
String requestPath = "https://api.mch.weixin.qq.com/v3/pay/partner/transactions/out-trade-no/{out_trade_no}";
requestPath = requestPath.replace("{out_trade_no}", UrlEncoder.urlEncode(request.getOutTradeNo()));
QueryParameter queryParameter = new QueryParameter();
if (request.getSpMchid() != null) {
queryParameter.add("sp_mchid", UrlEncoder.urlEncode(request.getSpMchid()));
}
if (request.getSubMchid() != null) {
queryParameter.add("sub_mchid", UrlEncoder.urlEncode(request.getSubMchid()));
}
requestPath = requestPath + queryParameter.getQueryStr();
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), this.hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
HttpRequest httpRequest = (new com.wechat.pay.java.core.http.HttpRequest.Builder()).httpMethod(HttpMethod.GET).url(requestPath).headers(headers).build();
HttpResponse httpResponse = this.httpClient.execute(httpRequest, Transaction.class);
return (Transaction)httpResponse.getServiceResponse();
}
private RequestBody createRequestBody(Object request) {
return (new com.wechat.pay.java.core.http.JsonRequestBody.Builder()).body(GsonUtil.toJson(request)).build();
}
public static class Builder {
private HttpClient httpClient;
private HostName hostName;
public Builder() {
}
public NativePayService.Builder config(Config config) {
this.httpClient = (new DefaultHttpClientBuilder()).config(config).build();
return this;
}
public NativePayService.Builder hostName(HostName hostName) {
this.hostName = hostName;
return this;
}
public NativePayService.Builder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
public NativePayService build() {
return new NativePayService(this.httpClient, this.hosNativePayService tName);
}
}
}
https://github.com/wechatpay-apiv3/wechatpay-java/wiki/SDK-%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3#%E7%BD%91%E7%BB%9C%E9%85%8D%E7%BD%AE