现在的位置: 首页 > 编程语言 > 正文

Java如何实现支付宝电脑支付基于servlet版本

2020年02月13日 编程语言 ⁄ 共 4572字 ⁄ 字号 评论关闭

这篇文章主要介绍了Java如何实现支付宝电脑支付基于servlet版本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

前期准备:

蚂蚁金融开放平台进行登录操作

进入我的开放平台

在上方找到沙箱,进入沙箱(网络编程虚拟执行环境)。

这里的RSA2密钥设置下,我已经设置好了,所以便有了支付宝公钥(公钥是对外公开的,私钥是自己保留的具体的不多做解释)

至于RSA2密钥的生成,可参考如下操作:

生成后在工具文件夹如下路径可以查看自己生成的公钥和私钥一对

私钥保留,公钥设置到开发者平台RSA2上便可以了。

官方DEMO因为我们需要一些jar包和可参考类,所以拿个DEMO方便很多

所需要的jar包路径如下

实例编写:

工具我这里用的是eclipse,这个影响不大~

新建web项目

项目所需要的类如下

AlipayConfig.java

package cn.taosir.demo;public class AlipayConfig { // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数 public static String return_url = "http://localhost:8080/success.jsp"; // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 public static String notify_url = "http://localhost:8080/notify"; // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号 public static String app_id = ""; // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。 public static String alipay_public_key = ""; // 商户私钥,您的PKCS8格式RSA2私钥 public static String merchant_private_key = ""; // 签名方式 public static String sign_type = "RSA2"; // 字符编码格式 public static String charset = "utf-8"; // 支付宝网关 public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";}

APPID(在查看支付宝公钥位置上方)、支付宝公钥、自己的私钥,记得配上

NoitfyServlet.java

package cn.taosir.demo;import java.io.IOException;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/notify")public class NotifyServlet extends HttpServlet{ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("接收到支付宝的异步通知请求——"); Map<String,String[]> parameterMap=request.getParameterMap(); System.out.println(parameterMap); //成功处理后返回success response.getWriter().write("success"); }}

OrderServlet.java

package cn.taosir.demo;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Calendar;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.alipay.api.AlipayApiException;import com.alipay.api.AlipayClient;import com.alipay.api.DefaultAlipayClient;import com.alipay.api.request.AlipayTradePagePayRequest;import com.alipay.api.response.AlipayTradePagePayResponse;@WebServlet("/order/confirm")public class OrderServlet extends HttpServlet{ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("开始处理OrderServlet的服务"); String title = "涛先森"; String total = "666666"; String message = "如有什么建议欢迎留言评论"; //生成订单号 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String orderSn = simpleDateFormat.format(Calendar.getInstance().getTime()); //向支付宝发送请求 //获得初始化的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 = orderSn; //付款金额,必填 String total_amount = total; //订单名称,必填 String subject = title; //商品描述,可空 String body = message; 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-请求参数】章节 AlipayTradePagePayResponse alipayResponse = null; try { alipayResponse=alipayClient.pageExecute(alipayRequest); System.out.println(alipayResponse.getBody()); System.out.println(alipayResponse.getMsg()); } catch (AlipayApiException e) { e.printStackTrace(); } response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(alipayResponse.getBody()); }}

至此,大功告成

下面开始测试

将项目部署到tomcat上run起来

http://localhost:8080/alipay-demo/order/confirm 可以看到页面通过该请求跳转到了支付宝的接口

下载沙箱环境的支付宝APP可以扫码测试喔

这里我们点击右边登录账户付款

账号和密码可在沙箱平台查看个人账号

之后登录就支付了,正常就跳转到了支付成功页面。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: Java如何实现支付宝电脑支付基于servlet版本

以上就上有关Java如何实现支付宝电脑支付基于servlet版本的相关介绍,要了解更多java,支付宝,电脑,支付,servlet内容请登录学步园。

抱歉!评论已关闭.