This issue recommends an open source aggregate payment framework, supporting wechat Pay, Alipay Pay, etc. – IJPay.
IJPay encapsulates wechat Pay, QQ Pay, Alipay Pay, Jingdong Pay, UnionPay, PayPal Pay and other commonly used payment methods as well as various commonly used interfaces. Does not rely on any third-party mvc framework, just as a tool to use simple and fast to complete the development of payment modules, can be easily embedded in any system.
Features
-
- Simplicity first : The Module-centered project structure is simple, convenient and easy to expand, and the total inclusion does not exceed 2M after release.
Out of the box : does not rely on any third-party MVC framework, only as a tool, simple and fast to complete the development of the payment module, can be easily embedded in any system.
- Rich channels : Support wechat Pay, QQ wallet payment, Alipay Pay, UnionPay, Jingdong Pay, etc.
wechat Pay: supports multi-application and multi-merchant, ordinary merchant mode and service provider mode, of course, also supports overseas, and supports the interface of Api-v3 and Api-v2 versions.
Alipay payment: supports multiple applications, and supports both common public key and public key certificate signature.
UnionPay payment: all-channel scanning code payment, wechat App payment, public number & Mini program payment, UnionPay JS payment, Alipay service window payment.
Personal wechat Pay: wechat personal merchants, the lowest rate of 0.38%, the official direct connection asynchronous callback notice.
PayPal payment: automatically manage AccessToken, quickly access a variety of commonly used payment methods.
- Minimalist introduction : IJPay has always been simple, out-of-the-box as the core, providing a complete example of simple configuration modification can be used.
Example of accessing wechat Pay
1 Add dependencies
- Add dependencies for all payment methods at once
<dependency>
<groupId>com.github.javen205</groupId>
<artifactId>IJPay-All</artifactId>
<version>latest-version</version>
</dependency>
- or just add a dependency on the desired payment method
Wechat Pay:
<dependency>
<groupId>com.github.javen205</groupId>
<artifactId>IJPay-WxPay</artifactId>
<version>latest-version</version>
</dependency>
更多依赖:
Installation dependency | IJPay
2 Build request parameter Model
- wechat Pay Model
The Model involved in the commonly used payment methods in IJPay is built using the builder mode, in which each field of the Model is consistent with the official interface document, and supports the merchant mode and service provider mode.
Code address:
https://gitee.com/javen205/IJPay/tree/master/IJPay-WxPay/src/main/java/com/ijpay/wxpay/model
Why use Lombok to build interface request parameters?
1. Lombok is used to build request parameters for convenience without writing too many redundant get sets.
2. Avoid low-level errors caused by manual setting of incorrect write parameters.
The outside world’s evaluation of Lombok is not quite consistent, like very much like not like hard to mock, then IJPay there are other alternatives?
Sure, the simplest way is to use Map to build the request parameters and WxPayKit.buildSign to build the signature. IJPay 1.x version does just that
- Extension Model
Due to different payment methods, there are a lot of interfaces involved. If the Model of some interfaces does not provide encapsulation in IJPay, you can inherit all attribute field names of BaseModel and keep the interface parameters consistent and must be of String type. Finally add the following comments to implement the builder pattern.
Code address:
https://gitee.com/javen205/IJPay/blob/master/IJPay-Core/src/main/java/com/ijpay/core/model/BaseModel.java
@Builder
@AllArgsConstructor
@Getter
@Setter
public class CloseOrderModel extends BaseModel {
// Property field names consistent with interface parameters
private String appid;
// Omit other parameters in the interface...
}
Note:
1. The extension Model must inherit BaseModel
2. All attribute field names must be consistent with interface parameters and must be of String type
3, must add Lombok annotation @Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE) @Getter
4, IDEA must install Lombok plugin
3 Build the parameters and execute the request
1. According to the parameters required by the interface, use Model to build parameters other than sign
2. Create a signature after build based on the signature mode of the interface creatSign
3. Execute https request using the interface provided by WxPayApi
Code address:
https://gitee.com/javen205/IJPay/blob/master/IJPay-WxPay/src/main/java/com/ijpay/wxpay/WxPayApi.java
4. Construct payment parameters to evoke payment
- For example, public number payment (JSAPI payment) unified order:
// Unified build request parameters
Map< String,String> params = UnifiedOrderModel
.builder()
.appid(wxPayBean.getAppId())
.mch_id(wxPayBean.getMchId())
.nonce_str(WxPayKit.generateStr())
.body("IJPay Make payments at your fingertips ")
.attach("Node.js version:https://gitee.com/javen205/TNWX")
.out_trade_no(WxPayKit.generateStr())
.total_fee("1000")
.spbill_create_ip(ip)
.notify_url(wxPayBean.getDomain().concat("/wxpay/pay_notify"))
.trade_type(TradeType.JSAPI.getTradeType())
.openid(openId)
.build()
// Simultaneous supportSignType.MD5、SignType.HMACSHA256
.creatSign(wxPayBean.getPartnerKey(), SignType.HMACSHA256);
//Send request
String xmlResult = WxPayApi.pushOrder(false,params);
// The xml data returned by the request is converted into a Map, which is convenient for later logical data acquisition
Map<String, String> resultMap = WxPayKit.xmlToMap(xmlResult);
// Judge the returned result
String returnCode = resultMap.get("return_code");
String returnMsg = resultMap.get("return_msg");
if (!WxPayKit.codeIsOk(returnCode)) {
return new AjaxResult().addError(returnMsg);
}
String resultCode = resultMap.get("result_code");
if (!WxPayKit.codeIsOk(resultCode)) {
return new AjaxResult().addError(returnMsg);
}
// The following fields return when return_code and result_code are both SUCCESS
String prepayId = resultMap.get("prepay_id");
// Secondary signature to build the parameters of public account arousing payment. The signature method here should be consistent with the above unified order request signature method
Map< String, String> packageParams = WxPayKit.prepayIdCreateSign(prepayId,
wxPayBean.getAppId(),wxPayBean.getPartnerKey(),SignType.HMACSHA256);
// Returns the data built by the secondary signature to the front-end and invokes the public account payment
String jsonStr = JSON.toJSONString(packageParams);
return new AjaxResult().success(jsonStr);
Full sample code:
https://gitee.com/javen205/IJPay/blob/master/IJPay-Demo-SpringBoot/src/main/java/com/ijpay/demo/controller/wxpay/WxPayCo ntroller.java
Read more.