文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、项目结构
- 二、使用步骤
- 1.引入依赖
- 2.构建所需要的类
- 细节总结
前言
—自己在学习过程中使用封装的支付宝沙箱支付代码
提示:以下是本篇文章正文内容,下面案例可供参考
一、项目结构controller: MyPayController
entity:AliPayBean
util:AliPayUtil
service:PayService
alipay.properties
代码如下(示例):
2.构建所需要的类com.alipay.sdk alipay-sdk-java4.11.0.ALL
代码如下:
controller: MyPayController
@GetMapping(value = "alipay")
@ResponseBody
public String alipay(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException {
String out_trade_no = request.getParameter("orderId");
//String totalPrice=request.getParameter("totalPrice");
String arr[]=out_trade_no.split("2021");
Order order=orderService.findById(Integer.parseInt(arr[0]));
//生成订单名称和订单金额
AlipayBean alipayBean = new AlipayBean();
String body = "order";
String total_amount = order.getPrice()+"";
//订单名称
alipayBean.setBody(body);
//订单编号
alipayBean.setOut_trade_no(out_trade_no);
//订单金额
alipayBean.setTotal_amount(new StringBuffer().append(total_amount));
//订单描述
alipayBean.setSubject("payment");
String result = payService.aliPay(alipayBean);
return result;
}
@RequestMapping("notify")
public String notify(HttpServletRequest request)throws Exception{
return "pay/notify";
}
@RequestMapping("/success")
public String success(HttpServletRequest request, Model model) {
String out_trade_no=request.getParameter("out_trade_no");
String arr[]=out_trade_no.split("2021");
Order order=orderService.findById(Integer.parseInt(arr[0]));
int i=orderService.updateState(order.getId(),1);
return "redirect:/home/myorder";
}
entity:AliPayBean
public class AlipayBean {
private String out_trade_no;
private String subject;
private StringBuffer total_amount;
private String body;
private String timeout_express="10m";
private String product_code="FAST_INSTANT_TRADE_PAY";
public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public StringBuffer getTotal_amount() {
return total_amount;
}
public void setTotal_amount(StringBuffer total_amount) {
this.total_amount = total_amount;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTimeout_express() {
return timeout_express;
}
public void setTimeout_express(String timeout_express) {
this.timeout_express = timeout_express;
}
public String getProduct_code() {
return product_code;
}
public void setProduct_code(String product_code) {
this.product_code = product_code;
}
}
util:AliPayUtil
public class AlipayUtil {
public static String connect(AlipayBean alipayBean) throws AlipayApiException {
//LoadProperties properties=new LoadProperties();
//1、获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(
//支付宝网关
LoadProperties.getGatewayUrl(),
//appid
LoadProperties.getApp_id(),
//商户私钥
LoadProperties.getMerchant_private_key(),
"json",
//字符编码格式
LoadProperties.getCharset(),
//支付宝公钥
LoadProperties.getAlipay_public_key(),
//签名方式
LoadProperties.getSign_type()
);
//2、设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
//页面跳转同步通知页面路径
alipayRequest.setReturnUrl(LoadProperties.getReturn_url());
// 服务器异步通知页面路径
alipayRequest.setNotifyUrl(LoadProperties.getNotify_url());
//封装参数
alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
//请求支付宝进行付款
String result = alipayClient.pageExecute(alipayRequest).getBody();
//返回付款信息
return result;
}
}
service:PayService
/支付服务/
public interface PayService {
String aliPay(AlipayBean alipayBean) throws AlipayApiException;
}
service:PayServiceImpl
@Service(value = "payService")
public class PayServiceImpl implements PayService {
@Override
public String aliPay(AlipayBean alipayBean) throws AlipayApiException {
return AlipayUtil.connect(alipayBean);
}
}
alipay.properties
app_id = 2016092700611579 merchant_private_key = ******* alipay_public_key = ******* notify_url = http://localhost:8080/home/notify return_url = http://localhost:8080/home/success sign_type = RSA2 charset = utf-8 gatewayUrl = https://openapi.alipaydev.com/gateway.do log_path = "C:\"细节总结
首先想接入支付宝沙箱,得先去支付宝沙箱官网申请秘钥,具体操作网上有教程,申请完之后就可以把秘钥写在文章最下面.properties文件里面,然后用我这里封好的类,搭建好之后就可以进行测试,在控制层把你订单的信息传过去,即可完成支付。保姆级教程,易学易用。



