mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
feat: 支付宝手机网站支付
This commit is contained in:
parent
05d65c19a7
commit
98a5157623
@ -1,11 +1,15 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.alipay.api.internal.util.AlipaySignature;
|
||||
import com.alipay.api.request.AlipayTradePagePayRequest;
|
||||
import com.alipay.api.request.AlipayTradeWapPayRequest;
|
||||
import com.alipay.api.response.AlipayTradeWapPayResponse;
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.core.config.AlipayProperties;
|
||||
import com.java2nb.novel.core.utils.ThreadLocalUtil;
|
||||
import com.java2nb.novel.service.OrderService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
@ -50,9 +54,35 @@ public class PayController extends BaseController {
|
||||
} else {
|
||||
//创建充值订单
|
||||
Long outTradeNo = orderService.createPayOrder((byte) 1, payAmount, userDetails.getId());
|
||||
|
||||
//获得初始化的AlipayClient
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(), alipayConfig.getAppId(), alipayConfig.getMerchantPrivateKey(), "json", alipayConfig.getCharset(), alipayConfig.getPublicKey(), alipayConfig.getSignType());
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
|
||||
alipayConfig.getAppId(), alipayConfig.getMerchantPrivateKey(), "json", alipayConfig.getCharset(),
|
||||
alipayConfig.getPublicKey(), alipayConfig.getSignType());
|
||||
String form;
|
||||
if (ThreadLocalUtil.getTemplateDir().contains("mobile")) {
|
||||
// 手机站
|
||||
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
|
||||
alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
|
||||
//在公共参数中设置回跳和通知地址
|
||||
alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
|
||||
/******必传参数******/
|
||||
JSONObject bizContent = new JSONObject();
|
||||
//商户订单号,商家自定义,保持唯一性
|
||||
bizContent.put("out_trade_no", outTradeNo);
|
||||
//支付金额,最小值0.01元
|
||||
bizContent.put("total_amount", payAmount);
|
||||
//订单标题,不可使用特殊符号
|
||||
bizContent.put("subject", "小说精品屋-plus");
|
||||
|
||||
/******可选参数******/
|
||||
//手机网站支付默认传值FAST_INSTANT_TRADE_PAY
|
||||
bizContent.put("product_code", "QUICK_WAP_WAY");
|
||||
|
||||
alipayRequest.setBizContent(bizContent.toString());
|
||||
AlipayTradeWapPayResponse response = alipayClient.pageExecute(alipayRequest);
|
||||
form = alipayClient.pageExecute(alipayRequest).getBody();
|
||||
} else {
|
||||
// 电脑站
|
||||
//创建API对应的request
|
||||
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
|
||||
alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
|
||||
@ -66,7 +96,9 @@ public class PayController extends BaseController {
|
||||
" \"subject\":\"小说精品屋-plus\"" +
|
||||
" }");
|
||||
//调用SDK生成表单
|
||||
String form = alipayClient.pageExecute(alipayRequest).getBody();
|
||||
form = alipayClient.pageExecute(alipayRequest).getBody();
|
||||
|
||||
}
|
||||
|
||||
httpResponse.setContentType("text/html;charset=utf-8");
|
||||
//直接将完整的表单html输出到页面
|
||||
@ -76,18 +108,15 @@ public class PayController extends BaseController {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝支付通知
|
||||
* */
|
||||
*/
|
||||
@SneakyThrows
|
||||
@RequestMapping("aliPay/notify")
|
||||
public void aliPayNotify(HttpServletRequest request, HttpServletResponse httpResponse) {
|
||||
|
||||
|
||||
PrintWriter out = httpResponse.getWriter();
|
||||
|
||||
//获取支付宝POST过来反馈信息
|
||||
@ -104,7 +133,8 @@ public class PayController extends BaseController {
|
||||
}
|
||||
|
||||
//调用SDK验证签名
|
||||
boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getPublicKey(), alipayConfig.getCharset(), alipayConfig.getSignType());
|
||||
boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getPublicKey(),
|
||||
alipayConfig.getCharset(), alipayConfig.getSignType());
|
||||
|
||||
//——请在这里编写您的程序(以下代码仅作参考)——
|
||||
|
||||
@ -117,18 +147,20 @@ public class PayController extends BaseController {
|
||||
if (signVerified) {
|
||||
//验证成功
|
||||
//商户订单号
|
||||
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
|
||||
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1),
|
||||
StandardCharsets.UTF_8);
|
||||
|
||||
//支付宝交易号
|
||||
String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
|
||||
String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1),
|
||||
StandardCharsets.UTF_8);
|
||||
|
||||
//交易状态
|
||||
String tradeStatus = new String(request.getParameter("trade_status").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
|
||||
String tradeStatus = new String(request.getParameter("trade_status").getBytes(StandardCharsets.ISO_8859_1),
|
||||
StandardCharsets.UTF_8);
|
||||
|
||||
//更新订单状态
|
||||
orderService.updatePayOrder(Long.parseLong(outTradeNo), tradeNo, tradeStatus);
|
||||
|
||||
|
||||
out.println("success");
|
||||
|
||||
} else {//验证失败
|
||||
@ -140,12 +172,7 @@ public class PayController extends BaseController {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user