前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >对接支付宝支付通道接口

对接支付宝支付通道接口

原创
作者头像
深雾
修改2020-07-06 10:24:33
2K0
修改2020-07-06 10:24:33
举报
文章被收录于专栏:工具类

最近公司接的项目到了后期,我负责结算这块对接了支付宝和微信的支付通道,支付宝接口比微信调起来舒服的多

首先商户在蚂蚁金服开发平台申请开发权限,配好密钥下载支付宝工具jar包,然后对接相应的接口

这些参数都是固定的

代码语言:javascript
复制
/请求地址
	private static String URL = "https://openapi.alipay.com/gateway.do";
	
	//支付宝分配给开发者的应用ID
	private static String APP_ID = ""; 
	
	//编码格式
	private static String CHARSET = "utf-8"; 
	
	//私钥
	private static String APP_PRIVATE_KEY =""; 
	
	//支付宝公钥
	private static String ALIPAY_PUBLIC_KEY =""; 
	
	static AlipayClient alipayClient = new DefaultAlipayClient(URL,APP_ID,APP_PRIVATE_KEY,"json",CHARSET,ALIPAY_PUBLIC_KEY,"RSA2");

web支付接口  传入订单号和金额就行

代码语言:java
复制
public  void  transferpay(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws ServletException, IOException {
		String out_trade_no = httpRequest.getParameter("out_trade_no");//订单编号
		String total_amount = httpRequest.getParameter("total_amount");//订单金额
		String return_url = "";//成功页面
			return_url = "http://testwww";
		Map<String, String> map1 = new LinkedHashMap<String, String>();
		//商户订单号,64个字符以内、可包含字母、数字、下划线
		map1.put("out_trade_no", out_trade_no);
		//销售产品码,与支付宝签约的产品码名称。 注:目前仅支持FAST_INSTANT_TRADE_PAY
		map1.put("product_code", "FAST_INSTANT_TRADE_PAY");
		// 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
		map1.put("total_amount", total_amount);
		// 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
		map1.put("subject", "商品订单");
		//该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。
		map1.put("timeout_express", "5m");
		//编码格式gbk,RSA2签名算法
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
		String json = JsonUtil.map2Json(map1);
		alipayRequest.setReturnUrl(return_url);
		//回调地址 支付宝服务器主动通知商户服务器里指定的页面http/https路径。建议商户使用https
	    alipayRequest.setNotifyUrl("http://testwww");//在公共参数中设置回跳和通知地址
		alipayRequest.setBizContent(json);
		String form="";
		try {
			form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
			httpRequest.setAttribute("result",form);
			httpRequest.setAttribute("retCode",0000);
			httpRequest.setAttribute("msg","订单生成成功");
		} catch (AlipayApiException e) {
			httpRequest.setAttribute("retCcode",0001);
			httpRequest.setAttribute("retMsg","订单生成失败");
			e.printStackTrace();
		}
		System.out.println("form:"+form);	
		httpResponse.setContentType("text/html;charset=" + CHARSET);
	    httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
	    httpResponse.getWriter().flush();
	    httpResponse.getWriter().close();
	}

APP支付接口

代码语言:java
复制
public  @ResponseBody String  apppay(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws ServletException, IOException {
		Map<String, String> map = new HashMap<String, String>();
		String out_trade_no = httpRequest.getParameter("out_trade_no");
		String total_amount = httpRequest.getParameter("total_amount");
		
		if(StringUtils.isEmpty(out_trade_no)) {
			return "请选择订单号";
		}
		if(StringUtils.isEmpty(total_amount)) {
			return "请选择订单金额";
		}else{
			total_amount = StringUtil.parseAmountLong2Str(Long.parseLong(total_amount));
		}
		//编码格式gbk,RSA2签名算法
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();//创建API对应的request
		//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        // 订单标题
        model.setSubject("app订单");
 		//商户订单号,64个字符以内、可包含字母、数字、下划线
        model.setOutTradeNo(out_trade_no);
 		//该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
        //model.setTimeoutExpress("5m");
 		// 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
        model.setTotalAmount(total_amount);
 		//销售产品码,与支付宝签约的产品码名称。 注:目前仅支持FAST_INSTANT_TRADE_PAY
        model.setProductCode("QUICK_MSECURITY_PAY");
		request.setBizModel(model);
		request.setNotifyUrl("http://testwww");
		String orderString="";
		
		try {
			 // 这里和普通的接口调用不同,使用的是sdkExecute
			AlipayTradeAppPayResponse response  = alipayClient.sdkExecute(request); //返回支付宝订单信息(预处理)
			orderString = response.getBody();//就是orderString 可以直接给APP请求,无需再做处理。
			map.put("retCode","0000");
			map.put("retMsg","订单生成成功");
			map.put("state",orderString);
			
		} catch (AlipayApiException e) {
			map.put("retCode","0001");
			map.put("retMsg","订单生成失败");
			e.printStackTrace();
		}
		String json = JsonUtil.map2Json(map);
		logger.info("orderString:"+orderString);	
		return json;
	}

支付异步回调

代码语言:javascript
复制
public  Map<String, String>  paymentNotify(HttpServletRequest httpRequest) throws ServletException, IOException {
		logger.info("支付宝订单支付异步回调");
		Map<String, String> map = new LinkedHashMap<String, String>();
		Map<String, String> params = convertRequestParamsToMap(httpRequest); // 将异步通知中收到的待验证所有参数都存放到map中
		logger.info("订单信息:"+params.toString());
		String out_trade_no = httpRequest.getParameter("out_trade_no");//商户订单号
		String trade_no = httpRequest.getParameter("trade_no");//支付宝流水号
		String seller_id = httpRequest.getParameter("seller_id");//支付宝唯一用户号
		String timestamp = httpRequest.getParameter("timestamp");//时间
		String code = httpRequest.getParameter("code");//返回码
		String msg = httpRequest.getParameter("msg");//处理结果的描述,信息来自于code返回结果的描述
		String trade_status = httpRequest.getParameter("trade_status");//TRADE_SUCCESS成功支付
		map.put("out_trade_no", out_trade_no);
		map.put("trade_no", trade_no);
		map.put("seller_id", seller_id);
		map.put("timestamp", timestamp); 
		map.put("code", code);
		map.put("msg", msg);
		if(trade_status.equals("TRADE_SUCCESS")){//支付成功
			//修改支付记录支付状态
		}
		logger.info("订单信息:"+map.toString());
		return map;
	}

支付宝接口还是蛮简单的,文档也详细,先用沙盒测试成功后,再用密钥

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档