下面是如何用struts调用支付宝接口的攻略。
1.准备工作
在使用struts调用支付宝接口之前,需要先完成以下准备工作:
1.申请支付宝开发者账号:在支付宝开放平台注册账号,并完成实名认证。
2.创建应用:登录开放平台后,在开发者控制台创建一个应用,并得到应用的APP ID、商户私钥、支付宝公钥等信息。
3.下载支付宝开发包:在[支付宝开放平台](https://opendocs.alipay.com/open/54/103419)下载Java SDK。
4.将SDK导入到项目中:将SDK解压后,将其中的jar包和lib目录中的所有jar包添加到项目的classpath。
2.编写代码
下面是两个示例,分别演示在struts中调用支付宝API完成批量转账和即时到账支付的过程。
批量转账示例
1.编写Action
public class BatchTransfersAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGN_TYPE);
AlipayFundTransToaccountTransferRequest alipayRequest = new AlipayFundTransToaccountTransferRequest();
alipayRequest.setBizContent("{" +
"\"out_biz_no\":\"20180901001\"," +
"\"payee_type\":\"ALIPAY_LOGONID\"," +
"\"payee_account\":\"xxx@alipay.com\"," +
"\"amount\":\"100.00\"," +
"\"payer_show_name\":\"转账支付测试\"," +
"\"payee_real_name\":\"测试用户\"," +
"\"remark\":\"转账测试\"" +
" }");
AlipayFundTransToaccountTransferResponse alipayResponse = alipayClient.execute(alipayRequest);
if(alipayResponse.isSuccess()){
System.out.println("调用支付宝API成功");
} else {
System.out.println("调用支付宝API失败");
}
return mapping.findForward("success");
}
}
2.配置struts.xml
<action name="batchTransfers" class="BatchTransfersAction">
<result name="success">/batchTransfers.jsp</result>
</action>
3.编写JSP
<a href="${pageContext.request.contextPath}/batchTransfers">批量转账</a>
即时到账支付示例
1.编写Action
public class AlipayAPIAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGN_TYPE);
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl("http://localhost:8080/ProjectName/success.jsp");
alipayRequest.setNotifyUrl("http://localhost:8080/ProjectName/notify_url.jsp");
alipayRequest.setBizContent("{" +
"\"out_trade_no\":\"20180918064\"," +
"\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
"\"total_amount\":0.01," +
"\"subject\":\"测试商品名称\"," +
"\"body\":\"测试商品描述信息\"" +
"}");
String formHtml = alipayClient.pageExecute(alipayRequest).getBody();
response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
response.getWriter().write(formHtml);
response.getWriter().flush();
response.getWriter().close();
return null;
}
}
2.配置struts.xml
<action name="alipayAPI" class="AlipayAPIAction">
<result name="success" type="redirect">{redirectUrl}</result>
</action>
3.编写JSP
<a href="${pageContext.request.contextPath}/alipayAPI?redirectUrl=https://openapi.alipay.com/gateway.do">支付宝支付</a>
3.总结
通过以上两个示例,我们可以看到struts调用支付宝API相对简单,只需要创建支付宝SDK的客户端并传入请求参数即可。当然,我们还可以通过访问支付宝开放平台的官方文档,了解更多API的参数和使用方法。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何用struts调用支付宝接口 - Python技术站