支付宝 微信 支付集成
<h1><center>支付宝 微信 支付集成</center></h1>
<p>pom.xml添加Maven相关</p>
<pre><code class="language-xml"> <dependency>
<groupId>com.github.javen205</groupId>
<artifactId>IJPay-All</artifactId>
<version>2.3.0</version>
</dependency></code></pre>
<p>io.nutz.nutzsite.MainLauncher.java init方法添加初始化设置</p>
<pre><code class="language-java"> /**
* 支付宝 微信 支付 初始化操作
*/
private void initAppPay(){
WxPayApiConfig apiConfig = WxPayApiConfig.builder()
.appId("")
.mchId("")
.partnerKey("")
.certPath("")
.build();
AliPayApiConfig aliPayApiConfig = AliPayApiConfig.builder()
.setAppId("")
.setAliPayPublicKey("")
.setCharset("UTF-8")
.setPrivateKey("")
.setServiceUrl("")
.setSignType("RSA2")
.build();
try {
WxPayApiConfigKit.putApiConfig(apiConfig);
AliPayApiConfigKit.putApiConfig(aliPayApiConfig);
} catch (Exception e) {
}
}</code></pre>
<p>支付宝创建订单</p>
<pre><code class="language-java"> /**
* 支付宝支付APP生成
* @param orderId 商户订单号,商户网站订单系统中唯一订单号,必填
* @param body 商品描述,可空
* @param subject 订单名称,必填
* @param total_amount 付款总金额 必填
* @return
* @throws Exception
*/
public static String appPay(String orderId,String body,String subject,String total_amount ) throws Exception{
if (Strings.isEmpty(orderId) || Strings.isEmpty(subject) || Strings.isEmpty(total_amount)) {
throw new Exception("支付宝参数异常");
}
try {
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(body);
model.setSubject(subject);
model.setOutTradeNo(orderId);
model.setTimeoutExpress("30m");
model.setTotalAmount(total_amount);
model.setPassbackParams("callback params");
model.setProductCode("QUICK_MSECURITY_PAY");
AlipayTradeAppPayResponse response = AliPayApi.appPayToResponse(model, AlipayConfig.notify_url);
String orderInfo = response.getBody();
System.out.println(orderInfo);
return orderInfo;
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}</code></pre>
<p>微信创建订单</p>
<pre><code class="language-java"> public static Map<String, String> createOrder(String orderId, String body, double order_price,String ip) throws AppException {
//微信价格最小单位分 转换为整数
DecimalFormat df = new DecimalFormat("#######.##");
order_price = order_price * 100;
order_price = Math.ceil(order_price);
String price = df.format(order_price);
Map<String, String> params = UnifiedOrderModel
.builder()
.appid(MyConfig.getAppID())
.mch_id(MyConfig.getMchID())
.nonce_str(WxPayKit.generateStr())
.body("XXX-" + body)
// .attach("")
.out_trade_no(orderId)
.total_fee(price)
.spbill_create_ip(ip)
.notify_url(MyConfig.notify_url)
.trade_type(APP.getTradeType())
.build()
.createSign(MyConfig.getKey(),SignType.MD5);
String xmlResult = WxPayApi.pushOrder(false, params);
log.info(xmlResult);
Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
String returnCode = result.get("return_code");
String returnMsg = result.get("return_msg");
if (!WxPayKit.codeIsOk(returnCode)) {
log.info("订单创建失败" + returnMsg);
throw new AppException("订单创建失败!" + returnMsg);
}
String resultCode = result.get("result_code");
if (!WxPayKit.codeIsOk(resultCode)) {
log.info("订单创建失败" + returnMsg);
throw new AppException("订单创建失败!" + returnMsg);
}
// 以下字段在 return_code 和 result_code 都为 SUCCESS 的时候有返回
String prepayId = result.get("prepay_id");
Map<String, String> packageParams = WxPayKit.appPrepayIdCreateSign(MyConfig.getAppID(),MyConfig.getMchID(), prepayId,
MyConfig.getKey(),SignType.MD5);
packageParams.put("signType",params.get("sign_type"));
// String jsonStr = JSON.toJSONString(packageParams);
// log.info("返回apk的参数:" + jsonStr);
return packageParams;
}
</code></pre>
<p>详细文档 参考:<a href="https://javen205.gitee.io/ijpay/">https://javen205.gitee.io/ijpay/</a></p>