支付宝+APP支付+Java后台代码(1秒接入!!)

支付宝+APP支付+Java后台代码(1秒接入!!)非证书版本

废话不多说,直接上代码

pom.xml

导入支付宝官方jar包

<!-- 支付宝支付 -->
 <dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>3.6.0.ALL</version>
</dependency>

封装Util工具类,封装支付宝的各种密钥等属性

// 商户appid 控制台查看对应的接口的ID
// 私钥 pkcs8格式的 有支付宝提供工具支付宝开放平台开发助手生成
// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问,后端处理 (后台接收判断是否支付成功)
// 支付宝公钥(是支付宝的公钥,不是应用公钥


public class AlipayConfig {
	// 商户appid
	public static String APPID = "";
	// 私钥 pkcs8格式的         有支付宝提供工具==支付宝开放平台开发助手==生成
	public static String RSA_PRIVATE_KEY = "";
	// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问,后端处理
	public static String notify_url = "https://www.***.com/pay_s";
	// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
//	public static String return_url = "";
	// 请求网关地址
	public static String URL = "https://openapi.alipay.com/gateway.do";
	// 编码
	public static String CHARSET = "UTF-8";
	// 返回格式
	public static String FORMAT = "json";
	// 支付宝公钥
	public static String ALIPAY_PUBLIC_KEY =  "";
	// 日志记录目录
	public static String log_path = "/log";
	// RSA2
	public static String SIGNTYPE = "RSA2";
}

controller层 调用支付,(HTML整合的APP或者ApiCloud的APP)拉取支付调用

	@ResponseBody
	@RequestMapping(value="",method=RequestMethod.POST)
	//传入商品信息
	public Map<String,String> pay(@RequestBody User user) {
		Map<String,String> success = new HashMap<String,String>();
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APPID,AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
		//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody("");//商品描述
		model.setSubject("");//关键字
		model.setOutTradeNo(ordernum); //唯一订单不能重复
		model.setTimeoutExpress("30m");//最晚付款时间
		model.setTotalAmount("1.5");//商品价格
		model.setProductCode("QUICK_MSECURITY_PAY");//产品销售码
		//建议保存唯一订单号 方便于回调查看是否支付成功  
		user.set***(ordernum);
		userDao.****(user);
		
		request.setBizModel(model);
		request.setNotifyUrl("https://www.**.com/pay_s");//异步请求地址  回调查看是否支付成功
		String result;
		//
		try {
			//这里和普通的接口调用不同,使用的是sdkExecute
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
			result = response.getBody();
//			result = new String(result.getBytes("ISO-8859-1"), "utf-8");
			//就是orderString 可以直接给客户端请求,无需再做处理。
		        success.put("success", result);
		        return success;
		    } catch (AlipayApiException e) {
		    	System.out.println("错误");
		        e.printStackTrace();
		}
		 success.put("success", "支付失败");
		return success;
	}

controller层 回调支付接口查看是否成功

@RequestMapping(value="/pay_s")
	public String asy(HttpServletRequest request) throws UnsupportedEncodingException {
		//获取支付宝POST过来反馈信息
		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		Map<Object, Object> resultMap = new HashMap<>();
		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] + ",";
		  	}
		    //乱码解决,这段代码在出现乱码时使用。
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}
		//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
		//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
		boolean flag = false;
		try {
			flag = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET,"RSA2");
			 if (flag) {
		            try {
		            	//商户订单号
						String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

//						//支付宝交易号
//						String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
//
//						//付款金额
//						String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
//						System.out.println(total_amount);
						
						
						String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
						//判断是否支付成功
						 if ("TRADE_SUCCESS".equals(trade_status)) {
							//接下来对业务进行数据处理
							**********
							**********
							**********
						 }
						
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		            return "SUCCESS";
		        } else {
						String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
				        	User user =  new User();
				        	user.setpay_orderid(out_trade_no);
							user.setPay_status("fall");
							userDao.User_PayStatus(user);
		        	return "FAIL";
		        	 
		        }

		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "SUCCESS";
	}

最后提供一个生成不重复的随机数的方法,可以用作生成唯一订单号

	public  String random(){
		  UUID uuid = UUID.randomUUID(); 
		  //如果数据量非常大,建议加上时间戳!
		return uuid.toString();
	}

如果对你有帮助,请给小编一个赞哈!

发布了9 篇原创文章 · 获赞 9 · 访问量 5903

猜你喜欢

转载自blog.csdn.net/my_batis/article/details/103032963