在开放平台文档中心
https://docs.open.alipay.com/270/106291/
下载demo,我下载的是Java版的,一定要用eclipse打开,如果要用idea的话可以到时候再放过去
导入之前应该将eclipse中设置一下UTF-8
运行后如下
登录支付宝-开发者中心-研发服务-沙箱应用
设置公钥(可能跟我的不太一样,因为是已经设置过了才截的图)
下载生成公钥的工具(点击设置会有提示下载)
运行 RSA签名验签工具.bat
复制公钥到应用公钥处,会生成相应的支付宝公钥
将1-4填到Demo中AlipayConfig的对应地方去
注意:代码中的签名方式 sign_type,是哪种加密方式就选哪种,我是用RSA2就写了RSA2
在沙箱工具中下载这个工具
登录买家账号
运行修改过后的Demo,点击付款后扫码,会弹到支付页面,扫码支付成功后跳到return_url
在沙箱账号-商家信息中账户余额多了0.03,买家信息中账户余额少了0.03
**
**
页面代码:
pay
订单号和订单名称、付款金额都是必须要有的,商品描述选填
配置按照Demo里面来即可,除了个人信息外没有改动过
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
**
**
一些关于Demo里面notify_url.jsp的相关知识
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);
}
输入订单号或者交易号后
控制台会输出信息(这里用了HiJson格式化Json字符串)
**
**
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);
}
**
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);
}
输入订单号后
控制台会输出信息(这是错的!!!)
然后发现一定要包含退款请求号才能查询成功,如下(我的退款请求号是123,是在退款的时候就要写上,查询的时候才能查成功)
注意:退款和退款查询所用的订单号等是从手机沙箱版支付宝里面-我的-账单-里面查到的,退款后也有相应的提示
还有一个交易关闭功能也是类似,以后有空再整理
以上就是支付宝接口的教程,代码支付宝的Demo里面全部都有,如果没有贴完全的话可以自行去下载。