java实现在SSM下使用支付宝扫码支付功能
本文实例为大家分享了java使用支付宝扫码支付的具体代码,供大家参考,具体内容如下
准备工作
首先开通支付宝沙箱的测试账号,里面会有消费者账户和收款方账户
手机扫码下载手机端app
基础配置
所需jar包
AlipayConfig
package com.alipay.config;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ResourceBundle;
/* *
*类名:AlipayConfig
*功能:基础配置类
*详细:设置帐户有关信息及返回路径
*修改日期:2017-04-05
*说明:
*以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
*该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
*/
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String app_id = "2016080403162340";
// 商户私钥,您的PKCS8格式RSA2私钥
public static String merchant_private_key = "MIIEvAID2tulSSmawG5+F4NZbexpnxi8NKQJPZEeAA==";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static String alipay_public_key = "MIIBIjt26tLTKar8S1ERDWI25viBcMz7PLMxVVUmHf5tdBWfbMhUs3QIDAQAB";
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "http://localhost:8080/Exam/index/gouMai";
// 签名方式
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 = "E:\\";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
* @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();
}
}
}
}
}
Controller
//生成有二维码,可供扫码支付的页面
@RequestMapping(value = "aliPay")
public String aliPay(HttpServletResponse response,ModelMap map,String chapterId,HttpServletRequest request,
String WIDout_trade_no,String WIDtotal_amount,String WIDsubject,String WIDbody) throws IOException, AlipayApiException{
// String a,String urlName,String couName....+"&a="+a+"&urlName="+urlName+"&couName="+couName
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url+"?chapterId="+chapterId);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//付款ID,必填
String out_trade_no = WIDout_trade_no;
//付款金额,必填
String total_amount = WIDtotal_amount;
total_amount=URLDecoder.decode(total_amount,"UTF-8");//转码
//订单名称,必填
String subject = WIDsubject;
subject=URLDecoder.decode(subject,"UTF-8");
//商品描述,可空
String body = WIDbody;
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"timeout_express\":\"1m\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.println(result);
return null;
}
支付成功的放回页面(return_url)
成功后的返回路径,走Controller,详见AlipayConfig中的配置
//点击购买,将课程存入购买表中
@RequestMapping(value="gouMai")
@ResponseBody
public ModelAndView gouMai(String chapterId,HttpServletRequest req,String a,String urlName,String couName,ModelMap map){
ModelAndView mav = new ModelAndView();
Map<String,String> mapp1 = new HashMap<String,String>();
// SysUserTab login_user = sysuserService.getSysUserById(userId);
HttpSession session = req.getSession();
SysUserTab login_user1 = (SysUserTab) session.getAttribute("login_user");
String userId = login_user1.getUserId();
// session.setAttribute("login_user", login_user);
mapp1.put("userId", userId);
mapp1.put("chapterId", chapterId);
int num = sysBuyService.getBuyCount(mapp1);
if(num==0){
mapp1.put("buyId", UUID.randomUUID().toString().replace("-", ""));
sysBuyService.insertBuy(mapp1);
}
//查询课程内容
// String fanhui = showFH(req,chapterId,urlName,couName,map, a);
mav.setViewName("jsp/pay/paySuccess");
return mav;
}
支付成功后,页面跳转至paySuccess.jsp页面。
您可能感兴趣的文章:
- java支付宝即时到帐提交订单功能
- 详解java集成支付宝支付接口(JSP+支付宝20160912)
- Java实现支付宝之第三方支付宝即时到账支付功能
以上是 java实现在SSM下使用支付宝扫码支付功能 的全部内容, 来源链接: utcz.com/p/216242.html