网站接入支付宝

沙箱环境

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

先要下载这两个工具

mark

mark

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

开始

支付包第三方Jar引入:

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

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

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

Controller层:

 1package edu.xpu.game.controller.shopping;
 2
 3@Slf4j
 4@Controller
 5@RequestMapping("/pay")
 6public class PayController {
 7    private final OrderServiceImpl orderService;
 8    private final PayServiceImpl payService;
 9
10    @Autowired
11    public PayController(OrderServiceImpl orderService, PayServiceImpl payService) {
12        this.orderService = orderService;
13        this.payService = payService;
14    }
15
16    @ResponseBody
17    @RequestMapping("/affirmPay")
18    public String payOrder(@RequestParam("masterOrderId") String masterOrderId){
19        Optional<OrderMaster> byId = orderService.findOrderById(masterOrderId);
20
21        AlipayBean alipayBean = new AlipayBean();
22        alipayBean.setOut_trade_no(masterOrderId); //设置订单Id
23        alipayBean.setSubject("农家商城购物");
24
25        if(byId.isPresent()){
26            alipayBean.setTotal_amount(new StringBuffer()
27                                       .append(byId.get().getOrderAmount()));
28            try {
29                String aliPay = payService.aliPay(alipayBean);
30                log.info("[pay.affirmPay] aliPay = {}", aliPay);
31                return aliPay;
32            } catch (AlipayApiException e) {
33                return JsonUtil.toJson(ResultVOUtil.error(2, "支付过程出错"));
34            }
35        }else{
36            return JsonUtil.toJson(ResultVOUtil.error(1, "不存在此订单"));
37        }
38    }
39
40    /**
41     * 支付成功的回调
42     * @param trade_status 交易状态
43     * @param out_trade_no 商户订单号
44     */
45    @RequestMapping("/payFinish")
46    public void payFinishCallback(String trade_status, String out_trade_no){
47        log.info("支付回调:trade_status = "+trade_status);
48        if("TRADE_SUCCESS".equals(trade_status)){
49            //支付成功
50            Optional<OrderMaster> orderById = orderService.findOrderById(out_trade_no);
51            if(orderById.isPresent()){
52                OrderMaster orderMaster = orderById.get();
53                //修改支付状态
54                orderMaster.setPayStatus(PayStatusEnum.SUCCESS.getCode());
55                OrderMaster changeRet = orderService.change(orderMaster);
56                log.info("changeRet", changeRet);
57            }
58        }else{
59            //支付失败
60            Optional<OrderMaster> orderById = orderService.findOrderById(out_trade_no);
61            if(orderById.isPresent()){
62                OrderMaster orderMaster = orderById.get();
63                //修改支付状态
64                orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
65                orderService.change(orderMaster);
66            }
67        }
68    }
69
70
71    @RequestMapping("/paySyncFinish")
72    public String paySyncFinishCallback(){
73        return "redirect:http://ahojcn.natapp1.cc/#/pay";
74    }
75}

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

 1@Data
 2@Accessors(chain = true)
 3public class AlipayBean {
 4    /*商户订单号,必填*/
 5    private String out_trade_no;
 6    /*订单名称,必填*/
 7    private String subject;
 8    /*付款金额,必填*/
 9    private StringBuffer total_amount;
10    /*商品描述,可空*/
11    private String body;
12    /*超时时间参数*/
13    private String timeout_express="10m";
14
15    private String product_code="FAST_INSTANT_TRADE_PAY";
16}

Service层

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

支付工具类

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

mark

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