支付宝手机网站支付(基于Java实现支付宝手机网站支付)

时间:2024-01-18 15:11:32
  • 支付宝支付核心需要的参数是(APPID,PRIVATE_KEY,ALIPAY_PUBLIC_KEY)
  1. APPID:创建应用后就有的APPID。
  2. PRIVATE_KEY:应用私钥
  3. ALIPAY_PUBLIC_KEY:支付宝公钥
  • 上面的2,3的参数得自己弄到,参考文档:https://docs.open.alipay.com/291/105971/
  • 下载好工具后所需要干的事情:(获取到的应用公钥配置到:蚂蚁金服开放平台中在 “应用信息” - “开发设置” - “加签方式”处点击 “设置应用公钥”。获取到的应用私钥就是:PRIVATE_KEY。支付宝公钥ALIPAY_PUBLIC_KEY:当设置应用公钥完成后就可以查看支付宝公钥内容。)

支付宝手机网站支付(基于Java实现支付宝手机网站支付)

    <!-- 支付宝SDK -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.4.ALL</version>
</dependency>
  • 配置支付所需配置文件
/**
* 支付宝支付配置文件
*/
public class AlipayConfig {
// 1.商户创建应用后获取的appid
public static String APPID = "********";
// 2.应用私钥
public static String PRIVATE_KEY ="*******";
// 3.支付宝公钥
public static String ALIPAY_PUBLIC_KEY = "******";
// 4.回调接口全路径(支付完成异步通知)
public static String notify_url = "https://******";
// 5.页面跳转同步通知页面路径(支付完成后跳转的页面)
public static String return_url = "https://******";
// 6.请求支付宝的网关地址
public static String URL = "https://openapi.alipay.com/gateway.do";
// 7.编码
public static String CHARSET = "UTF-8";
// 8.返回格式
public static String FORMAT = "json";
// 9.加密类型
public static String SIGNTYPE = "RSA2";
}
  • 业务层预下单
import com.aone.app.common.ali.pay.AlipayConfig;
import com.aone.app.service.AliH5PayService; import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; @Service
public class AliH5PayServiceImpl implements AliH5PayService { private static final Logger logger = LoggerFactory.getLogger("AliH5PayServiceImpl"); //H5支付宝支付预下单(预下单)
public Map<String, String> dounifiedOrder(String type, String out_trade_no, String money, HttpServletRequest request) throws Exception{
Map<String, String> map=new HashMap<>(); //获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
AlipayConfig.PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
AlipayConfig.SIGNTYPE); //设置请求参数passback_params
String content = "{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ "0.01" +"\","
+ "\"subject\":\""+ "订单" +"\","
+ "\"timeout_express\":\""+ "30m" +"\","
+ "\"body\":\""+ "支付宝H5支付" +"\","
+ "\"passback_params\":\""+type +"\","
+ "\"product_code\":\""+"QUICK_WAP_WAY"+"\"}";
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
alipayRequest.setBizContent(content); //请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
logger.error("result{}",request.toString());
if(StringUtils.isEmpty(request)){
map.put("code","201");
map.put("msg","请求失败");
map.put("result",null);
}else{
map.put("code","200");
map.put("msg","请求成功");
map.put("result",result);
}
return map;
} }
  • 控制层下单接口以及回调接口
import com.aone.app.common.wx.XMLUtils;
import com.aone.app.service.AliH5PayService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map; @RestController
@RequestMapping("h5pay")
@Api("H5支付")
public class PayH5Controller { private static final Logger log = LoggerFactory.getLogger("PayH5Controller"); @Autowired
private AliH5PayService aliH5PayService; /**
* H5支付统一下单
* @param request
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping(value = "wxPay")
public Map<String, String> weixinPay(HttpServletRequest request) throws Exception{
String type= request.getParameter("type");
String orderNo= request.getParameter("orderNo");
String money= request.getParameter("money");
return aliH5PayService.dounifiedOrder(type,orderNo,money,request);
} /**
* H5微信支付异步结果通知
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "notify")
public void weixinPayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
BufferedReader reader = request.getReader();
String line = "";
Map map = new HashMap();
String xml = "<xml><return_code><![CDATA[FAIL]]></xml>";;
StringBuffer inputString = new StringBuffer();
while ((line = reader.readLine()) != null) {
inputString.append(line);
}
request.getReader().close();
log.error("----接收到的报文---{}",inputString.toString());
if(inputString.toString().length()>0){
map = XMLUtils.parseXmlToList(inputString.toString());
}else{
log.error("接受微信报文为空");
}
log.error("map={}",map);
if(map!=null && "SUCCESS".equals(map.get("result_code"))){
//成功的业务。。。
xml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
String type=map.get("attach").toString();
String orderNo=map.get("out_trade_no").toString();
log.error("订单号{}",map.get("out_trade_no"));log.error("其它必须参数{}",map.get("attach"));
if(StringUtils.isEmpty(type)||StringUtils.isEmpty(orderNo)){
log.error("当前参数类型异常");
}else{
//回调业务逻辑 }
}else{
//失败的业务。。。
}
//告诉微信端已经确认支付成功
response.getWriter().write(xml);
}
}
  • 回调接口中解析微信通知XML的工具类XMLUtils,参考微信H5支付,缺少其它东西也可参考其它记录。