Eclipse、Springboot支付宝接口的使用(沙箱环境)

在开放平台文档中心
https://docs.open.alipay.com/270/106291/
下载demo,我下载的是Java版的,一定要用eclipse打开,如果要用idea的话可以到时候再放过去
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第1张图片
导入之前应该将eclipse中设置一下UTF-8

Eclipse、Springboot支付宝接口的使用(沙箱环境)_第2张图片在eclipse中创建新的项目
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第3张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第4张图片
将如下文件直接复制进去
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第5张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第6张图片
在这里插入图片描述

运行后如下
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第7张图片
登录支付宝-开发者中心-研发服务-沙箱应用Eclipse、Springboot支付宝接口的使用(沙箱环境)_第8张图片
设置公钥(可能跟我的不太一样,因为是已经设置过了才截的图)Eclipse、Springboot支付宝接口的使用(沙箱环境)_第9张图片
下载生成公钥的工具(点击设置会有提示下载)
在这里插入图片描述
运行 RSA签名验签工具.bat
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第10张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第11张图片
复制公钥到应用公钥处,会生成相应的支付宝公钥
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第12张图片Eclipse、Springboot支付宝接口的使用(沙箱环境)_第13张图片
将1-4填到Demo中AlipayConfig的对应地方去
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第14张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第15张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第16张图片

Eclipse、Springboot支付宝接口的使用(沙箱环境)_第17张图片
注意:代码中的签名方式 sign_type,是哪种加密方式就选哪种,我是用RSA2就写了RSA2Eclipse、Springboot支付宝接口的使用(沙箱环境)_第18张图片
在沙箱工具中下载这个工具

Eclipse、Springboot支付宝接口的使用(沙箱环境)_第19张图片
登录买家账号
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第20张图片
运行修改过后的Demo,点击付款后扫码,会弹到支付页面,扫码支付成功后跳到return_url
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第21张图片
在沙箱账号-商家信息中账户余额多了0.03,买家信息中账户余额少了0.03
在这里插入图片描述
在这里插入图片描述

导入源码
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第22张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第23张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第24张图片

**

接下来是springboot版

**

页面代码:




    
    
    
    
    pay


订单号:
订单名称:
付款金额:
商品描述:

订单号和订单名称、付款金额都是必须要有的,商品描述选填

配置按照Demo里面来即可,除了个人信息外没有改动过

Controller里面的标黄色字段是对应页面中的
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第25张图片



import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hstc.config.AlipayConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class PayController {

    @RequestMapping("/index")
    public ModelAndView toTest() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        return mav;
    }

    @RequestMapping("/pay")
    public void payController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的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);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号,商户网站订单系统中唯一订单号,必填
        String out_trade_no = new String(request.getParameter("bianhao").getBytes("ISO-8859-1"), "UTF-8");
        //付款金额,必填
        String total_amount = new String(request.getParameter("jiage").getBytes("ISO-8859-1"), "UTF-8");
        //订单名称,必填
        String subject = new String(request.getParameter("mingcheng").getBytes("ISO-8859-1"), "UTF-8");
        //商品描述,可空
        String body = new String(request.getParameter("miaoshu").getBytes("ISO-8859-1"), "UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + AlipayConfig.charset);
        response.getWriter().write(form);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }
}


如果不理解response.getWriter().write(form);的话可以看看下面的

前端项目和response.getWriter().write(xxxx)的理解 - 从小就很酷的博客 - CSDN博客 https://blog.csdn.net/weixin_39669410/article/details/89244169

如果不理解response.getWriter().flush();response.getWriter().close();的话可以看看下面的

JAVA中 write()方法后调用flush()方法的作用 - RainSwear的博客 - CSDN博客 https://blog.csdn.net/qq_35271409/article/details/82057096

运行后结果如下
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第26张图片
填入数据点击下单后跳转
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第27张图片
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第28张图片

**

完成。

**

一些关于Demo里面notify_url.jsp的相关知识
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第29张图片
TRADE_FINISHED是超过退款日期后,交易完成
TRADE_SUCCESS是支付成功但是有可能会退款,所以不代表交易完成

**

交易查询

**
html(来自Demo中的index.html)

  
商户订单号 :

支付宝交易号 :

商户订单号与支付宝交易号二选一,如果您点击“交易查询”按钮,即表示您同意该次的执行操作。

controller(来自Demo中的alipay.trade.query.jsp)

@RequestMapping("/query")
    public void queryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        //商户订单号,商户网站订单系统中唯一订单号
        String out_trade_no = new String(request.getParameter("moid").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("WIDTQtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+"\"trade_no\":\""+ trade_no +"\"}");

        //请求
        String form = "";
        try {
            form =alipayClient.execute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
//        response.setContentType("text/html;charset=" + AlipayConfig.charset);
//        response.getWriter().write(form);//直接将完整的表单html输出到页面
//        response.getWriter().flush();
//        response.getWriter().close();

        System.out.println(form);

    }

输入订单号或者交易号后Eclipse、Springboot支付宝接口的使用(沙箱环境)_第30张图片
控制台会输出信息(这里用了HiJson格式化Json字符串)
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第31张图片
**

退款

**
html

		
商户订单号 :

支付宝交易号 :

退款金额 :

退款原因 :

退款请求号 :

商户订单号与支付宝交易号二选一,如果您点击“退款”按钮,即表示您同意该次的执行操作。

controller(来自alipay.trade.refund.jsp)

 @RequestMapping("/refund")
    public void refundController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        //商户订单号,商户网站订单系统中唯一订单号
        String out_trade_no = new String(request.getParameter("WIDTRout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("WIDTRtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置
        //需要退款的金额,该金额不能大于订单金额,必填
        String refund_amount = new String(request.getParameter("WIDTRrefund_amount").getBytes("ISO-8859-1"),"UTF-8");
        //退款的原因说明
        String refund_reason = new String(request.getParameter("WIDTRrefund_reason").getBytes("ISO-8859-1"),"UTF-8");
        //标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
        String out_request_no = new String(request.getParameter("WIDTRout_request_no").getBytes("ISO-8859-1"),"UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"trade_no\":\""+ trade_no +"\","
                + "\"refund_amount\":\""+ refund_amount +"\","
                + "\"refund_reason\":\""+ refund_reason +"\","
                + "\"out_request_no\":\""+ out_request_no +"\"}");

        //请求
        String form = "";
        try {
            form = alipayClient.execute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
//        response.setContentType("text/html;charset=" + AlipayConfig.charset);
//        response.getWriter().write(form);//直接将完整的表单html输出到页面
//        response.getWriter().flush();
//        response.getWriter().close();

        System.out.println(form);

    }

输入订单号退款后Eclipse、Springboot支付宝接口的使用(沙箱环境)_第32张图片
控制台会输出信息

Eclipse、Springboot支付宝接口的使用(沙箱环境)_第33张图片
**

退款查询

**
html

    
商户订单号 :

支付宝交易号 :

退款请求号 :

商户订单号与支付宝交易号二选一,如果您点击“退款查询”按钮,即表示您同意该次的执行操作。

controller

    @RequestMapping("/refundQuery")
    public void refundQueryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();

        //商户订单号,商户网站订单系统中唯一订单号
        String out_trade_no = new String(request.getParameter("WIDRQout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //支付宝交易号
        String trade_no = new String(request.getParameter("WIDRQtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置
        //请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号,必填
        String out_request_no = new String(request.getParameter("WIDRQout_request_no").getBytes("ISO-8859-1"),"UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                +"\"trade_no\":\""+ trade_no +"\","
                +"\"out_request_no\":\""+ out_request_no +"\"}");

        //请求
        String form = "";
        try {
            form = alipayClient.execute(alipayRequest).getBody();//调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
//        response.setContentType("text/html;charset=" + AlipayConfig.charset);
//        response.getWriter().write(form);//直接将完整的表单html输出到页面
//        response.getWriter().flush();
//        response.getWriter().close();

        System.out.println(form);

    }

输入订单号后
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第34张图片
控制台会输出信息(这是错的!!!)
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第35张图片
然后发现一定要包含退款请求号才能查询成功,如下(我的退款请求号是123,是在退款的时候就要写上,查询的时候才能查成功)
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第36张图片
注意:退款和退款查询所用的订单号等是从手机沙箱版支付宝里面-我的-账单-里面查到的,退款后也有相应的提示
Eclipse、Springboot支付宝接口的使用(沙箱环境)_第37张图片

还有一个交易关闭功能也是类似,以后有空再整理

以上就是支付宝接口的教程,代码支付宝的Demo里面全部都有,如果没有贴完全的话可以自行去下载。

你可能感兴趣的:(springboot,#支付宝接口)