Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.wechat.pay.java.service.partnerpayments.applyments;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.service.partnerpayments.app.AppService;
import com.wechat.pay.java.service.partnerpayments.app.model.*;
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsRequest;
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsResponse;

/** AppService使用示例 */
public class ApplymentsExample {

/** 商户号 */
public static String merchantId = "190000****";

/** 商户API私钥路径 */
public static String privateKeyPath = "/Users/yourname/your/path/apiclient_key.pem";
public static String publicKeyFormPath = "/pub_key.pem";
/** 商户API证书序列号 */
public static String publicKeyId = "PUB_KEY_ID_01XXX";

/** 商户证书序列号 */
public static String merchantSerialNumber = "XXX";

/** 商户APIV3密钥 */
public static String apiV3Key = "";


public static ApplymentsService service;

public static void main(String[] args) {
try {
// 初始化商户配置
System.out.println("开始初始化商户配置...");
Config config =
new RSAPublicKeyConfig.Builder()
.merchantId(merchantId)
.publicKeyFromPath(publicKeyFormPath)
.publicKeyId(publicKeyId)
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
.privateKeyFromPath(privateKeyPath)
.merchantSerialNumber(merchantSerialNumber)
.apiV3Key(apiV3Key)
.build();

// 初始化服务
System.out.println("开始初始化服务...");
service = new ApplymentsService.Builder().config(config).build();

// 调用接口
System.out.println("开始调用接口...");
ApplymentsResponse response = applyments();
System.out.println("接口调用成功,返回结果:" + response);

} catch (HttpException e) {
System.err.println("发送HTTP请求失败:" + e.getMessage());
System.err.println("请求信息:" + e.getHttpRequest());
e.printStackTrace();
} catch (ServiceException e) {
System.err.println("服务返回异常:" + e.getMessage());
System.err.println("返回体:" + e.getResponseBody());
e.printStackTrace();
} catch (MalformedMessageException e) {
System.err.println("解析返回体失败:" + e.getMessage());
e.printStackTrace();
} catch (ValidationException e) {
System.err.println("验证签名失败:" + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("发生未知错误:" + e.getMessage());
e.printStackTrace();
}
}


/** APP支付下单 */
public static ApplymentsResponse applyments() {
ApplymentsRequest request = new ApplymentsRequest();


// 调用接口
return service.applyments(request, publicKeyId);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2021 Tencent Inc. All rights reserved.
//
// APP支付
//
// APP支付API
//
// API version: 1.2.3

// Code generated by WechatPay APIv3 Generator based on [OpenAPI
// Generator](https://openapi-generator.tech); DO NOT EDIT.

package com.wechat.pay.java.service.partnerpayments.applyments;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.core.http.*;
import com.wechat.pay.java.service.partnerpayments.app.model.*;
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsRequest;
import com.wechat.pay.java.service.partnerpayments.applyments.model.ApplymentsResponse;

import static com.wechat.pay.java.core.http.UrlEncoder.urlEncode;
import static com.wechat.pay.java.core.util.GsonUtil.toJson;
import static java.util.Objects.requireNonNull;

/** AppService服务 */
public class ApplymentsService {

private final HttpClient httpClient;
private final HostName hostName;

private ApplymentsService(HttpClient httpClient, HostName hostName) {
this.httpClient = requireNonNull(httpClient);
this.hostName = hostName;
}

/** AppService构造器 */
public static class Builder {

private HttpClient httpClient;
private HostName hostName;

/**
* 设置请求配置,以该配置构造默认的httpClient,若未调用httpClient()方法,则必须调用该方法
*
* @param config 请求配置
* @return Builder
*/
public Builder config(Config config) {
this.httpClient = new DefaultHttpClientBuilder().config(config).build();

return this;
}

/**
* 设置微信支付域名,可选,默认为api.mch.weixin.qq.com
*
* @param hostName 微信支付域名
* @return Builder
*/
public Builder hostName(HostName hostName) {
this.hostName = hostName;
return this;
}

/**
* 设置自定义httpClient,若未调用config(),则必须调用该方法
*
* @param httpClient httpClient
* @return Builder
*/
public Builder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

/**
* 构造服务
*
* @return AppService
*/
public ApplymentsService build() {
return new ApplymentsService(httpClient, hostName);
}
}

/**
* 平台收付通(商户进件)
* 注意部分参数需要加密
*
* @param request 请求参数
* @param wechatpaySerial 【微信支付公钥ID】或【微信支付平台证书序列号】请求参数中的敏感字段,需要使用微信支付公钥加密(推荐),请参考获取微信支付公钥ID说明以及微信支付公钥加密敏感信息指引,也可以使用微信支付平台证书公钥加密,参考获取平台证书序列号、平台证书加密敏感信息指引
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
* @throws MalformedMessageException 服务返回成功,content-type不为application/json、解析返回体失败。
*/
public ApplymentsResponse applyments(ApplymentsRequest request,String wechatpaySerial) {
String requestPath =
"https://api.mch.weixin.qq.com/v3/ecommerce/applyments/";

ApplymentsRequest realRequest = request;
if (this.hostName != null) {
requestPath = requestPath.replaceFirst(HostName.API.getValue(), hostName.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.addHeader(Constant.ACCEPT, MediaType.APPLICATION_JSON.getValue());
headers.addHeader(Constant.CONTENT_TYPE, MediaType.APPLICATION_JSON.getValue());
headers.addHeader(Constant.WECHAT_PAY_SERIAL, wechatpaySerial);
HttpRequest httpRequest =
new HttpRequest.Builder()
.httpMethod(HttpMethod.POST)
.url(requestPath)
.headers(headers)
.body(createRequestBody(realRequest))
.build();
httpClient.execute(httpRequest, ApplymentsResponse.class);
HttpResponse<ApplymentsResponse> httpResponse =
httpClient.execute(httpRequest, ApplymentsResponse.class);
return httpResponse.getServiceResponse();
}



private RequestBody createRequestBody(Object request) {
return new JsonRequestBody.Builder().body(toJson(request)).build();
}
}
Loading
Loading