手机网页端支付开发文档:https://docs./203/105285/
第一步:进入蚂蚁金服(),创建应用,提交后审核
第二步:使用沙箱环境进行开发,等正式账号审核通过直接替换appid、网关和密钥就可以了
第三步:从沙箱应用中获取APPID、支付宝网关和支付宝公钥
第四步:后去应用私钥和应用公钥(其中RSA2(SHA265)密钥可以验证公钥的正确性,而RSA(SHA1)密钥验证公钥的正确性报错,直接跳过不需要验证)
1、查看密钥生成方法
2、生成密钥
3、填写应用的公钥并获取支付宝的公钥
第五步:找到相对应的环境,下载SDK&Demo,并从demo中获取支付宝的jar
第六步:编写代码
1、创建alipay.properties文件,存在支付宝的信息
######支付宝配置文件############APPIDali.appid=xxxxxxx######支付宝公钥ali.gy=支付宝公钥######私钥ali.sy=应用私钥######编码ali.charset=UTF-8######返回格式ali.format=json######RSA2ali.signtype=RSA2######notifyUrlali.notifyUrl=成功之后跳回地址######returnUrlali.returnUrl=成功之后跳回地址######网关地址ali.wg=/gateway.do
注意:配置文件的公钥是支付宝公钥不是应用公钥
2、经过后台整理的支付宝信息,直接跳到支付宝官方页面
/*** 发送信息* * @param request* @param meetingId* @param isHotel* @param response* @return* @throws IOException*/@RequestMapping("/sendMsg")public String sendMsg(HttpServletRequest request) throws IOException {DecimalFormat df = new DecimalFormat("0.00");String money = df.format(meeting.getDues());String title = "支付信息";String orderId = "订单号"String form = getMsg(orderId, money, title, "QUICK_WAP_WAY");response.setContentType("text/html;charset="UTF-8");response.getWriter().write(form);// 直接将完整的表单html输出到页面response.getWriter().flush();response.getWriter().close();return null;}/*** 发送信息* * @param orderId* 订单号* @param money* 支付费用* @param title* 标题* @param body* 额外信息* @return*/public String getMsg(String orderId, String money, String title, String body) {ResourceBundle rb = ResourceBundle.getBundle("alipay");//获取properties信息AlipayClient alipayClient = new DefaultAlipayClient(rb.getString("ali.wg"), rb.getString("ali.appid"),rb.getString("ali.sy"), rb.getString("ali.format"),rb.getString("ali.charset"), rb.getString("ali.gy"),rb.getString("ali.signtype"));AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();// 创建API对应的requestalipayRequest.setReturnUrl(rb.getString("ali.returnUrl"));alipayRequest.setNotifyUrl(rb.getString("ali.notifyUrl"));// 在公共参数中设置回跳和通知地址alipayRequest.setBizContent("{" + " \"out_trade_no\":\"" + orderId+ "\"," + " \"total_amount\":\"" + money + "\","+ " \"subject\":\"" + title + "\"," + " \"product_code\":\""+ body + "\"" + " }");// 填充业务参数String form = "";try {form = alipayClient.pageExecute(alipayRequest).getBody(); // 调用SDK生成表单} catch (AlipayApiException e) {e.printStackTrace();}return form;}
3、支付成功后回调
/*** 回调信息* * @param id* @param request* @return* @throws AlipayApiException* @throws UnsupportedEncodingException*/@RequestMapping("/getMg")public String getMsg(HttpServletRequest request) {Map requestParams = request.getParameterMap();String meetingId = validMsg(requestParams);if (meetingId == null) {return "redirect:/phone/meeting";}return "redirect:/phone/meeting?id=" + meetingId;}/*** 第一步获取信息* * @param id* @param request* @return* @throws UnsupportedEncodingException* @throws AlipayApiException*/@RequestMapping("/backMg")public String backMsg(HttpServletRequest request) {Map requestParams = request.getParameterMap();String meetingId =validMsg(requestParams);if (meetingId == null) {return "redirect:/phone/meeting";}return "redirect:/phone/meeting";}/*** 验证是否支付成功* * @param requestParams* @return*/public String validMsg(Map requestParams) {Map<String, String> params = new HashMap<String, String>();ResourceBundle rb = ResourceBundle.getBundle("alipay");for (Iterator<String> 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] + ",";}// 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化params.put(name, valueStr);}try {boolean verify_result = AlipaySignature.rsaCheckV1(params,rb.getString("ali.gy"), rb.getString("ali.charset"),rb.getString("ali.signtype"));if (verify_result) {String orderId = params.get("out_trade_no");String meetingId = null;//自己逻辑return meetingId;}} catch (AlipayApiException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}
第七步:微信公众平台无法使用支付宝支付
1.从支付宝文档中下载解决方案
根据方案中的步骤引入ap.js和pay.htm,调用_AP.pay()的方法访问支付宝信息