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;
|
package com.java2nb.novel.controller;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.alipay.api.AlipayClient;
|
import com.alipay.api.AlipayClient;
|
||||||
import com.alipay.api.DefaultAlipayClient;
|
import com.alipay.api.DefaultAlipayClient;
|
||||||
import com.alipay.api.internal.util.AlipaySignature;
|
import com.alipay.api.internal.util.AlipaySignature;
|
||||||
import com.alipay.api.request.AlipayTradePagePayRequest;
|
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.bean.UserDetails;
|
||||||
import com.java2nb.novel.core.config.AlipayProperties;
|
import com.java2nb.novel.core.config.AlipayProperties;
|
||||||
|
import com.java2nb.novel.core.utils.ThreadLocalUtil;
|
||||||
import com.java2nb.novel.service.OrderService;
|
import com.java2nb.novel.service.OrderService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
@ -41,32 +45,60 @@ public class PayController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@PostMapping("aliPay")
|
@PostMapping("aliPay")
|
||||||
public void aliPay(Integer payAmount,HttpServletRequest request,HttpServletResponse httpResponse) {
|
public void aliPay(Integer payAmount, HttpServletRequest request, HttpServletResponse httpResponse) {
|
||||||
|
|
||||||
UserDetails userDetails = getUserDetails(request);
|
UserDetails userDetails = getUserDetails(request);
|
||||||
if (userDetails == null) {
|
if (userDetails == null) {
|
||||||
//未登录,跳转到登陆页面
|
//未登录,跳转到登陆页面
|
||||||
httpResponse.sendRedirect("/user/login.html?originUrl=/pay/aliPay?payAmount="+payAmount);
|
httpResponse.sendRedirect("/user/login.html?originUrl=/pay/aliPay?payAmount=" + payAmount);
|
||||||
}else {
|
} else {
|
||||||
//创建充值订单
|
//创建充值订单
|
||||||
Long outTradeNo = orderService.createPayOrder((byte)1,payAmount,userDetails.getId());
|
Long outTradeNo = orderService.createPayOrder((byte) 1, payAmount, userDetails.getId());
|
||||||
|
|
||||||
//获得初始化的AlipayClient
|
//获得初始化的AlipayClient
|
||||||
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(), alipayConfig.getAppId(), alipayConfig.getMerchantPrivateKey(), "json", alipayConfig.getCharset(), alipayConfig.getPublicKey(), alipayConfig.getSignType());
|
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
|
||||||
//创建API对应的request
|
alipayConfig.getAppId(), alipayConfig.getMerchantPrivateKey(), "json", alipayConfig.getCharset(),
|
||||||
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
|
alipayConfig.getPublicKey(), alipayConfig.getSignType());
|
||||||
alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
|
String form;
|
||||||
//在公共参数中设置回跳和通知地址
|
if (ThreadLocalUtil.getTemplateDir().contains("mobile")) {
|
||||||
alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
|
// 手机站
|
||||||
//填充业务参数
|
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
|
||||||
alipayRequest.setBizContent("{" +
|
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());
|
||||||
|
//在公共参数中设置回跳和通知地址
|
||||||
|
alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
|
||||||
|
//填充业务参数
|
||||||
|
alipayRequest.setBizContent("{" +
|
||||||
" \"out_trade_no\":\"" + outTradeNo + "\"," +
|
" \"out_trade_no\":\"" + outTradeNo + "\"," +
|
||||||
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
|
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
|
||||||
" \"total_amount\":" + payAmount + "," +
|
" \"total_amount\":" + payAmount + "," +
|
||||||
" \"subject\":\"小说精品屋-plus\"" +
|
" \"subject\":\"小说精品屋-plus\"" +
|
||||||
" }");
|
" }");
|
||||||
//调用SDK生成表单
|
//调用SDK生成表单
|
||||||
String form = alipayClient.pageExecute(alipayRequest).getBody();
|
form = alipayClient.pageExecute(alipayRequest).getBody();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
httpResponse.setContentType("text/html;charset=utf-8");
|
httpResponse.setContentType("text/html;charset=utf-8");
|
||||||
//直接将完整的表单html输出到页面
|
//直接将完整的表单html输出到页面
|
||||||
@ -76,35 +108,33 @@ public class PayController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支付宝支付通知
|
* 支付宝支付通知
|
||||||
* */
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@RequestMapping("aliPay/notify")
|
@RequestMapping("aliPay/notify")
|
||||||
public void aliPayNotify(HttpServletRequest request,HttpServletResponse httpResponse){
|
public void aliPayNotify(HttpServletRequest request, HttpServletResponse httpResponse) {
|
||||||
|
|
||||||
|
|
||||||
PrintWriter out = httpResponse.getWriter();
|
PrintWriter out = httpResponse.getWriter();
|
||||||
|
|
||||||
//获取支付宝POST过来反馈信息
|
//获取支付宝POST过来反馈信息
|
||||||
Map<String,String> params = new HashMap<>();
|
Map<String, String> params = new HashMap<>();
|
||||||
Map<String,String[]> requestParams = request.getParameterMap();
|
Map<String, String[]> requestParams = request.getParameterMap();
|
||||||
for (String name : requestParams.keySet()) {
|
for (String name : requestParams.keySet()) {
|
||||||
String[] values = requestParams.get(name);
|
String[] values = requestParams.get(name);
|
||||||
String valueStr = "";
|
String valueStr = "";
|
||||||
for (int i = 0; i < values.length; i++) {
|
for (int i = 0; i < values.length; i++) {
|
||||||
valueStr = (i == values.length - 1) ? valueStr + values[i]
|
valueStr = (i == values.length - 1) ? valueStr + values[i]
|
||||||
: valueStr + values[i] + ",";
|
: valueStr + values[i] + ",";
|
||||||
}
|
}
|
||||||
params.put(name, valueStr);
|
params.put(name, valueStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
//调用SDK验证签名
|
//调用SDK验证签名
|
||||||
boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getPublicKey(), alipayConfig.getCharset(), alipayConfig.getSignType());
|
boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayConfig.getPublicKey(),
|
||||||
|
alipayConfig.getCharset(), alipayConfig.getSignType());
|
||||||
|
|
||||||
//——请在这里编写您的程序(以下代码仅作参考)——
|
//——请在这里编写您的程序(以下代码仅作参考)——
|
||||||
|
|
||||||
@ -114,24 +144,26 @@ public class PayController extends BaseController {
|
|||||||
3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
|
3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
|
||||||
4、验证app_id是否为该商户本身。
|
4、验证app_id是否为该商户本身。
|
||||||
*/
|
*/
|
||||||
if(signVerified) {
|
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);
|
orderService.updatePayOrder(Long.parseLong(outTradeNo), tradeNo, tradeStatus);
|
||||||
|
|
||||||
|
|
||||||
out.println("success");
|
out.println("success");
|
||||||
|
|
||||||
}else {//验证失败
|
} else {//验证失败
|
||||||
out.println("fail");
|
out.println("fail");
|
||||||
|
|
||||||
//调试用,写文本函数记录程序运行情况是否正常
|
//调试用,写文本函数记录程序运行情况是否正常
|
||||||
@ -140,12 +172,7 @@ public class PayController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user