网站接入支付宝

沙箱环境

沙箱环境也就是特意为开发者准备的环境,从而避免去注册公司、审核之类的问题!
这个是沙箱环境的链接

先要下载这两个工具


环境一定要配置正确,尤其是秘钥之类的,至于回调地址先不考虑,如果要测试的话首先要保证你本机的服务可以映射到公网,内网穿透就是一种解决方案,而且域名还便宜!

开始

支付包第三方Jar引入:

1
2
3
4
5
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.0.0</version>
</dependency>

在resources目录下新建一个支付宝的配置文件:alipay.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
app_id = 2016092900623298
# 商户私钥,您的PKCS8格式RSA2私钥
merchant_private_key = 你生成的商户私钥
# 支付宝公钥 对应APPID下的支付宝公钥。
alipay_public_key = 你的支付宝公钥
# 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数
notify_url = http://106.12.202.93:8080/pay/payFinish
# 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数
return_url = http://106.12.202.93:8080/pay/paySyncFinish
# 签名方式
sign_type = RSA2
# 字符编码格式
charset = utf-8
# 支付宝网关
gatewayUrl = https://openapi.alipaydev.com/gateway.do

Controller层:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package edu.xpu.game.controller.shopping;

@Slf4j
@Controller
@RequestMapping("/pay")
public class PayController {
private final OrderServiceImpl orderService;
private final PayServiceImpl payService;

@Autowired
public PayController(OrderServiceImpl orderService, PayServiceImpl payService) {
this.orderService = orderService;
this.payService = payService;
}

@ResponseBody
@RequestMapping("/affirmPay")
public String payOrder(@RequestParam("masterOrderId") String masterOrderId){
Optional<OrderMaster> byId = orderService.findOrderById(masterOrderId);

AlipayBean alipayBean = new AlipayBean();
alipayBean.setOut_trade_no(masterOrderId); //设置订单Id
alipayBean.setSubject("农家商城购物");

if(byId.isPresent()){
alipayBean.setTotal_amount(new StringBuffer()
.append(byId.get().getOrderAmount()));
try {
String aliPay = payService.aliPay(alipayBean);
log.info("[pay.affirmPay] aliPay = {}", aliPay);
return aliPay;
} catch (AlipayApiException e) {
return JsonUtil.toJson(ResultVOUtil.error(2, "支付过程出错"));
}
}else{
return JsonUtil.toJson(ResultVOUtil.error(1, "不存在此订单"));
}
}

/**
* 支付成功的回调
* @param trade_status 交易状态
* @param out_trade_no 商户订单号
*/
@RequestMapping("/payFinish")
public void payFinishCallback(String trade_status, String out_trade_no){
log.info("支付回调:trade_status = "+trade_status);
if("TRADE_SUCCESS".equals(trade_status)){
//支付成功
Optional<OrderMaster> orderById = orderService.findOrderById(out_trade_no);
if(orderById.isPresent()){
OrderMaster orderMaster = orderById.get();
//修改支付状态
orderMaster.setPayStatus(PayStatusEnum.SUCCESS.getCode());
OrderMaster changeRet = orderService.change(orderMaster);
log.info("changeRet", changeRet);
}
}else{
//支付失败
Optional<OrderMaster> orderById = orderService.findOrderById(out_trade_no);
if(orderById.isPresent()){
OrderMaster orderMaster = orderById.get();
//修改支付状态
orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
orderService.change(orderMaster);
}
}
}


@RequestMapping("/paySyncFinish")
public String paySyncFinishCallback(){
return "redirect:http://ahojcn.natapp1.cc/#/pay";
}
}

支付宝支付信息实体类对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Data
@Accessors(chain = true)
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";
}

Service层

1
2
3
4
5
6
7
@Service
public class PayServiceImpl implements PayService {
@Override
public String aliPay(AlipayBean alipayBean) throws AlipayApiException {
return AlipayUtil.connect(alipayBean);
}
}

支付工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class AlipayUtil {
public static String connect(AlipayBean alipayBean) throws AlipayApiException {
AlipayClient alipayClient = new DefaultAlipayClient(
PropertiesConfig.getKey("gatewayUrl"),//支付宝网关
PropertiesConfig.getKey("app_id"),//appid
PropertiesConfig.getKey("merchant_private_key"),//商户私钥
"json",
PropertiesConfig.getKey("charset"),//字符编码格式
PropertiesConfig.getKey("alipay_public_key"),//支付宝公钥
PropertiesConfig.getKey("sign_type")//签名方式
);
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(PropertiesConfig.getKey("return_url"));
alipayRequest.setNotifyUrl(PropertiesConfig.getKey("notify_url"));
alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
return alipayClient.pageExecute(alipayRequest).getBody();
}
}

image-20190612211138014

可以使用Android版本的沙箱支付宝支付,也可以登录账户支付,沙箱环境不是很稳定最好多测试几次,支付成功和支付失败的情景!

感谢打赏,世界因你而精彩!!(支付宝支持花呗)