This issue recommends a SpringBoot style wechat and Alipay payment development kit——Payment Spring Boot。
Most Java Web projects use Spring, especially Spring Boot. So the project authors chose a payment development kit built directly on top of Spring dependencies. When the project began to realize wechat Pay, the API version V3 of wechat Pay was announced. Compared to the V2 version:
- Follow a unified Restful design style
- Use JSON as the format for data interaction instead of XML
- Use the SHA256-RSA digital signature algorithm based on asymmetric keys, and do not use MD5 or HMAC-SHA256
- HTTPS client certificates are no longer required
- Use AES-256-GCM to encrypt key information in callbacks
This development experience is far better than the previous V2, so we chose wechat Pay V3.
Currently supported channels
- Wechat Pay V3 full support, and support multi-tenant at the same time to meet the mobile application App, public accounts, small programs and other payment scenarios
- Alipay integrated SDK, made a simple adaptation
function
- Realize multi-merchant wechat payment
- Integrate Alipay SDK and quickly access Spring Boot
- Implement wechat Pay V3 basic payment
- Implement wechat Pay V3 combined payment
- Implement wechat Pay V3 voucher
- Realize wechat Pay V3 wechat pay points
- Implement wechat Pay V3 premium card
- Implement wechat payment V3 merchant vouchers
- Implement wechat Pay V3 batch transfer to change
technique used
It only relies on what Spring already has, making it low dependency.
- Spring
- Jackson
- Ali-pay-sdk
Core API architecture
Fast access
1. Maven rely on
<dependency>
<groupId>cn.felord</groupId>
<artifactId>payment-spring-boot-starter</artifactId>
<version>1.0.11.RELEASE</version>
</dependency>
2. Spring Boot Version adaptation
To clone the project, modify the following configuration items in pom.xml in the root directory:
<properties>
<!-- Change to your corresponding Spring Boot version number -->
<spring-boot.version>2.4.0</spring-boot.version>
</properties>
3. install
Integrate into the project in the form of Spring Boot Starter. Pull the release branch from the GitHub project address to the local environment integration using one of the following two methods:
- mvn install Install to local
- mvn deploy Deploy to Maven repository
Then reference the Maven coordinates, noting that ${
payment-spring-boot-starter.version} Be consistent with what you installed.
<dependency>
<groupId>cn.felord</groupId>
<artifactId>payment-spring-boot-starter</artifactId>
<version>${payment-spring-boot-starter.version}</version>
</dependency>
4. Wechat Pay configuration
Configure wechat.pay.v3 parameters in application.yaml of the Spring Boot project.
wechat:
pay:
v3:
# Tenant id
<tentantID>:
# appId Indicates the appid of the service provider in service mode. This parameter is mandatory
app-id: xxxxxxxx
# v2 api This parameter is mandatory if the key version is later than 1.0.5 and the interface on V2 is used
app-secret: xxxxxxxxxxx
# api v3Key required
app-v3-secret: xxxxxxxx
# Wechat Pay merchant number service provider mode is the mchid of the service provider is required
mch-id: xxxxxxx
# Merchant server domain name used for callback Security policy to release the callback interface This parameter is mandatory
domain: https://felord.cn
# Merchant api Certificate path Required classpath The path is located under the resources file of Project maven
cert-path: apiclient_cert.p12
Wechat Pay tentantID scenario description
When the project needs to support wechat App payment, wechat public account payment, and wechat mini program payment at the same time, the app-ids in the above configuration may be multiple, so we need to carry out route identification to distinguish them, so you need to define a tentanID for identification. If we have both App payment and wechat mini program payment, we should configure it like this:
wechat:
pay:
v3:
# App Tenant Indicates the tenant mobile
mobile:
app-id: wx524534x423234
app-secret: felord
app-v3-secret: 0e5b123323h12234927e455703ec
mch-id: 1603337223
domain: https://felord.cn/mobile
cert-path: mobile/apiclient_cert.p12
# The wechat mini program tenant is identified as miniapp
miniapp:
app-id: wx344123x3541223
app-secret: felord
app-v3-secret: 0e5b123323h12234927e455703ec
mch-id: 1603337223
domain: https://felord.cn/miniapp
cert-path: miniapp/apiclient_cert.p12
Note: Developers are required to ensure that Tentanids are unique within a set of systems.
5. Enable configuration
After configuration, you need to enable the @EnableMobilePay comment to enable the payment function:
@EnableMobilePay
@Configuration
public class PayConfig {
}
6. Use API
Wechat Pay V3 open interfaces are introduced through WechatApiProvider, which will be initialized and injected with Spring IoC after the enabling and configuration steps are completed, and can be introduced in the following ways:
@Autowired
WechatApiProvider wechatApiProvider;
WechatApiProvider requires the developer to specify the tenant IDtenantID to initialize the Api for a specific scenario. For example, to query the end user’s coupon under Tenant mobile:
// Inquire about offers under Merchant quan
@Test
public void v3MchStocks() {
// Corresponding tenantID in the configuration file:
String tenantId =“mobile”;
StocksQueryParams params = new StocksQueryParams();
params.setOffset(0);
params.setLimit(10);
WechatResponseEntity<ObjectNode> objectNodeWechatResponseEntity = wechatApiProvider.favorApi(tenantId).queryStocksByMch(params);
System.out.println("objectNodeWechatResponseEntity = " + objectNodeWechatResponseEntity);
}
You can read more on your own.