适用范围:申请支付宝手机网站支付接口并完成认证,适用于手机wap网页,微信默认不支持可通过引导用户至浏览器支付,发送请求后调用支付宝客服端完成支付,如果没有安装APP则使用网页支付。
支付宝其他支付方式,如即时到账,APP支付类似。
第一步 导入依赖
<!-- 支付宝支付依赖 -->
<!-- https://mvnrepository.com/artifact/com.pentahohub.nexus/alipay-sdk-java -->
<dependency>
<groupId>com.pentahohub.nexus</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>20170209153303</version>
<scope>system</scope>
<systemPath>${basedir}/libs/alipay-sdk-java20170209153223.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
注:${basedir}为您自己的jar包存放地址,将支付宝jar下载至本地,避免冲突
第二步 java发起支付请求
public void alipay(WObject wObject)
{
// JSONObject id = wObject.fnOf(0);
// HttpServletRequest request= (HttpServletRequest) wObject.getRequest().getOriginalRequest();
try
{
HttpServletResponse httpResponse = (HttpServletResponse) wObject.getRequest().getOriginalResponse();
// APPID,申请通过后在支付宝商户中心查看
String APP_ID = "";
// 商戶私钥,在支付宝商户中心配置
String APP_PRIVATE_KEY = ""; // 商戶公钥,在支付宝商户中心配置
String ALIPAY_PUBLIC_KEY = "";
// 上线正式地址
// String gateway = "https://openapi.alipay.com/gateway.do";
// 沙箱测试地址
String gateway = "https://openapi.alipaydev.com/gateway.do";
AlipayClient alipayClient = new DefaultAlipayClient(gateway,
APP_ID, APP_PRIVATE_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
alipayRequest.setReturnUrl("http://www.xxxx.com/CallBack/return_url.jsp");
alipayRequest.setNotifyUrl("http://www.xxxx.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
alipayRequest.setBizContent("{" +
" \"out_trade_no\":\"20170320010101002\"," +
" \"total_amount\":\"88.88\"," +
" \"subject\":\"Iphone6 16G\"," +
" \"seller_id\":\"2088102169838480\"," +
" \"product_code\":\"QUICK_WAP_PAY\"" +
" }");//填充业务参数
String form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
httpResponse.setContentType("text/html;charset=" + "UTF-8");
httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
} catch (Exception e)
{
e.printStackTrace();
}
}