1、到阿里注册相关支付信息
2、配置支付配置类
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String app_id = "";
// 商户私钥,您的PKCS8格式RSA2私钥
public static String merchant_private_key =
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static String alipay_public_key =
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://工程公网访问地址/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "http://localhost:80/orders/myOrdersPay";
// 签名方式
public static String sign_type = "RSA2";
// 字符编码格式
public static String charset = "utf-8";
// 支付宝网关
public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
// 支付宝网关
public static String log_path = "C:\\";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
* @param sWord 要写入日志里的文本内容
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3、加入配置依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.0.52.ALL</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
4、跳转支付
/**
*
* @param request
* @param orderNum订单编号
* @param orderMoney订单价格
* @return
*/
//去到支付页面 JS传到控制器层存在request中传到jsp支付页面
@RequestMapping("/toOrdersPay")
public String toOrdersPay(HttpServletRequest request,String orderNum,Double orderMoney){
System.out.println(orderMoney);
System.out.println(orderNum);
//往请求对象request中设置订单编号个总金额
request.setAttribute("orderNum",orderNum);
request.setAttribute("orderMoney",orderMoney);
return "alipay/ordersPay"; /*去支付页面页面要显示金额和订单号*/
}
//支付完成后的操作回调 是页面跳转同步通知页面路径util中AlipayConfig
@RequestMapping("/myOrdersPay")
public String myOrdersPay(Model model, HttpSession session, HttpServletRequest request){
//获取支付完成后的订单编号
String out_trade_no = request.getParameter("out_trade_no");
//新建操作的订单对象
Orders orders = new Orders();
//往对象中设置订单编号
orders.setOrderNum(out_trade_no);
User loginUser = (User) session.getAttribute("loginUser");
try {
baseService.updTByPrimaryKeySelective(orders); /*调用的是OrderServiceImpl重写的动态修改方法*/
model.addAttribute("dataList",authorityService.findAuthorityByRoleIdAndParent(loginUser.getRoleId()));
} catch (Exception e) {
e.printStackTrace();
}
return "index";
}