赞
踩
<!-- 支付宝支付的依赖 -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.16.2.ALL</version>
</dependency>
此项目使用的为springboot+maven+vue实现
<div>
<a href="javascript:void(0);" id="order_btn" @click="addorder()">提交订单</a>
</div>
var vm = new Vue({
el: "#app",
data: {
addrs: [],
},
methods: {
addorder: function () {
if (this.addr.aname == null) {
alert("请填写收货地址");
return;
}
var params = new URLSearchParams();
//后端接收的参数
axios.post("../barleyfrontorder/pay/alipay", params)
.then((resp) => {
// 添加之前先删除一下,如果单页面,页面不刷新,添加进去的内容会一直保留在页面中,二次调用form表单会出错
const divForm = document.getElementsByTagName("div");
if (divForm.length) {
document.body.removeChild(divForm[0]);
}
const div = document.createElement("div");
div.innerHTML = resp.data; // data就是接口返回的form 表单字符串
document.body.appendChild(div);
document.forms[0].setAttribute("target", "_blank"); // 新开窗口跳转
document.forms[0].submit();
});
}
}
在Controller中定义接口
@Controller
public class PayController {
@Autowired
private PayService payService;
@ResponseBody
@RequestMapping("/pay/alipay")
public Result alipay(Orderinfo orderinfo, HttpServletResponse response) throws Exception {
return payService.alipay(orderinfo,response);
}
@RequestMapping("success")
public Result aliPayOrderSuccess(){
return payService.aliPayOrderSuccess();
}
}
public interface PayService {
Result aliPayOrderSuccess();
Result alipay(Orderinfo orderinfo, HttpServletResponse response) throws Exception;
}
首先设置以下属性
//appid
private final String APP_ID = "沙箱应用中获得"; //改为自己的
//应用私钥
private final String APP_PRIVATE_KEY = "生成密钥工具获得"; //改为自己的
private final String CHARSET = "UTF-8"; //不需要改
// 支付宝公钥
private final String ALIPAY_PUBLIC_KEY = "上一步获得";
//这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do"; //不需要改
private final String FORMAT = "JSON"; //不需要改
//签名方式
private final String SIGN_TYPE = "RSA2"; //不需要改
//支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
private final String NOTIFY_URL = "http://localhost:20000/barleyfrontorder/success";
//支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
private final String RETURN_URL = "http://localhost:20000/barleyfrontorder/success";
@Service
@Transactional( propagation= Propagation.SUPPORTS,
isolation= Isolation.DEFAULT, timeout=2000,
readOnly=true, rollbackFor=RuntimeException.class
)
@Slf4j
public class PayServieImpl implements PayService {
@Autowired
OrderDao orderDao;
@Override
public Result aliPayOrderSuccess() {
return new Result().ok("支付成功");
}
@Override
public Result alipay(Orderinfo orderinfo,HttpServletResponse response) throws Exception {
//调用封装好的方法(给支付宝接口发送请求)
String result = sendRequestToAlipay(orderinfo, "abcd",response);
orderDao.insert(orderinfo);
return new Result().ok(result);
}
/**
* 参数1:订单中的信息
*/
//支付宝官方提供的接口
private String sendRequestToAlipay(Orderinfo orderinfo, String subject, HttpServletResponse httpResponse) throws AlipayApiException, IOException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(RETURN_URL);
alipayRequest.setNotifyUrl(NOTIFY_URL);
//商品描述(可空)
String body = "欢迎下单";
alipayRequest.setBizContent("{\"orderinfo\":\"" + orderinfo + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderinfo.getOid() + "\","
+ "\"total_amount\":\"" + orderinfo.getTotalPrice() + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
//此部分必须要写 否则不会跳转到支付页面
httpResponse.setContentType("text/html;charset=" + CHARSET);
httpResponse.getWriter().write(result);// 直接将完整的表单html输出到页面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
return result;
}
}
上面即完成代码部分的书写,后面即可跳转到一下页面
此部分输入自己的账号名和密码,都是支付宝官网获得,点击下一步即可刷新支付宝二维码,手机扫码即可完成支付。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。