mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-26 17:20:52 +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;
|
||||
@ -41,32 +45,60 @@ public class PayController extends BaseController {
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("aliPay")
|
||||
public void aliPay(Integer payAmount,HttpServletRequest request,HttpServletResponse httpResponse) {
|
||||
public void aliPay(Integer payAmount, HttpServletRequest request, HttpServletResponse httpResponse) {
|
||||
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
//未登录,跳转到登陆页面
|
||||
httpResponse.sendRedirect("/user/login.html?originUrl=/pay/aliPay?payAmount="+payAmount);
|
||||
}else {
|
||||
httpResponse.sendRedirect("/user/login.html?originUrl=/pay/aliPay?payAmount=" + payAmount);
|
||||
} else {
|
||||
//创建充值订单
|
||||
Long outTradeNo = orderService.createPayOrder((byte)1,payAmount,userDetails.getId());
|
||||
|
||||
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());
|
||||
//创建API对应的request
|
||||
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
|
||||
alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
|
||||
//在公共参数中设置回跳和通知地址
|
||||
alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
|
||||
//填充业务参数
|
||||
alipayRequest.setBizContent("{" +
|
||||
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());
|
||||
//在公共参数中设置回跳和通知地址
|
||||
alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
|
||||
//填充业务参数
|
||||
alipayRequest.setBizContent("{" +
|
||||
" \"out_trade_no\":\"" + outTradeNo + "\"," +
|
||||
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
|
||||
" \"total_amount\":" + payAmount + "," +
|
||||
" \"subject\":\"小说精品屋-plus\"" +
|
||||
" }");
|
||||
//调用SDK生成表单
|
||||
String form = alipayClient.pageExecute(alipayRequest).getBody();
|
||||
//调用SDK生成表单
|
||||
form = alipayClient.pageExecute(alipayRequest).getBody();
|
||||
|
||||
}
|
||||
|
||||
httpResponse.setContentType("text/html;charset=utf-8");
|
||||
//直接将完整的表单html输出到页面
|
||||
@ -76,35 +108,33 @@ public class PayController extends BaseController {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝支付通知
|
||||
* */
|
||||
*/
|
||||
@SneakyThrows
|
||||
@RequestMapping("aliPay/notify")
|
||||
public void aliPayNotify(HttpServletRequest request,HttpServletResponse httpResponse){
|
||||
|
||||
public void aliPayNotify(HttpServletRequest request, HttpServletResponse httpResponse) {
|
||||
|
||||
PrintWriter out = httpResponse.getWriter();
|
||||
|
||||
//获取支付宝POST过来反馈信息
|
||||
Map<String,String> params = new HashMap<>();
|
||||
Map<String,String[]> requestParams = request.getParameterMap();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (String name : requestParams.keySet()) {
|
||||
String[] values = requestParams.get(name);
|
||||
String valueStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
valueStr = (i == values.length - 1) ? valueStr + values[i]
|
||||
: valueStr + values[i] + ",";
|
||||
: valueStr + values[i] + ",";
|
||||
}
|
||||
params.put(name, valueStr);
|
||||
}
|
||||
|
||||
//调用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)
|
||||
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);
|
||||
|
||||
|
||||
out.println("success");
|
||||
|
||||
}else {//验证失败
|
||||
} else {//验证失败
|
||||
out.println("fail");
|
||||
|
||||
//调试用,写文本函数记录程序运行情况是否正常
|
||||
@ -140,12 +172,7 @@ public class PayController extends BaseController {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user