接入前把准备工作做好
接入准备 | 网页&移动应用支付宝文档中心/open/203/107084
创建应用添加功能,手机网站支付开发设置:ip白名单,应用网关(http://ip:端口),回调地址:完整回调地址http://ip:端口/路径,有域名和子域名做对应替换就行参数准备,选择普通公钥模式,然后照着搞
接口加签方式 | 开放平台支付宝文档中心/common/02mriz
应用id支付宝公钥应用私钥应用公钥
jar依赖
<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.4.2.ALL</version></dependency><dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-easysdk</artifactId><version>2.1.0</version></dependency>
@Slf4jpublic class AliPay {private static String quitUrl = "取消支付跳转地址";//这里是支付宝公钥,不是应用公钥//这里是支付宝公钥,不是应用公钥//这里是支付宝公钥,不是应用公钥,总要的事情说三遍,否则回调验签不通过private static final String alipaypublicKey = "支付宝公钥";private static String returnUrl = "支付完成跳转地址";private static Config getOptions() {/** 初始化 **/Config config = new Config();/** 通信协议 **/config.protocol = "https";/** 支付宝网关 **/config.gatewayHost = "";/** 设置签名类型 **/config.signType = "RSA2";config.appId = "你的应用id";//应用私钥,公钥获取参考官网config.merchantPrivateKey = "应用私钥,直接粘贴密钥字符串";config.alipayPublicKey = "应用公钥";config.notifyUrl = "你服务器接收支付结果的地址,post";return config;}/*** @param subject 商品名称* @param orderNuber 商户订单号,商户自定义,需保证在商户端不重复144857ddw887ew112234* @param price订单金额,精确到小数点后两位* @return*/public static String createOrder() {/** 设置参数(全局只需设置一次) **/Factory.setOptions(getOptions());String payForm;try {//构建商品信息List<Object> goodsDetailList = new ArrayList<>();Map<String, Object> goodsDetail = new HashMap<>();goodsDetailList.add(goodsDetail);Date date = new Date();date.setTime(date.getTime() + 15 * 60 * 1000);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = format.format(date);AlipayTradeWapPayResponse response = Factory.Payment.Wap().pay("商品名称", "订单号", "订单金额",quitUrl, returnUrl);//返回拉起支付宝的页面代码,form表单payForm = response.getBody();} catch (Exception e) {throw new RuntimeException(e.getMessage(), e);}return payForm;}/*** 支付结果通知* 接口接收HttpServletRequest request 调用此方法进行验签* @param request* @return* @throws Exception*/public static String callBack(HttpServletRequest request) throws Exception {request.setCharacterEncoding("UTF-8");Map<String,String> params = new HashMap<String,String>();Map requestParams = request.getParameterMap();for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {String name = (String) iter.next();String[] values = (String[]) requestParams.get(name);String valueStr = "";for (int i = 0; i < values.length; i++) {valueStr = (i == values.length - 1) ? valueStr + values[i]: valueStr + values[i] + ",";}params.put(name, valueStr);}//商户订单号,下单时传入的订单好String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");//交易状态String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");//计算得出通知验证结果//alipaypublicKey支付宝公钥,不是应该公钥boolean verify_result = AlipaySignature.rsaCheckV1(params, alipaypublicKey, "UTF-8", "RSA2");if (verify_result) {//商户订单号//支付成功,返回订单号(入账业务需要其它参数,自行获取组装)if (trade_status.equals("TRADE_SUCCESS")) {return out_trade_no;}}return null;}}