手机端支付宝支付

转自:https://www.cnblogs.com/MrRightZhao/p/7852511.html

java版支付宝app支付流程及原理分析

  本实例是基于SSM框架编写
     一、流程步骤
         1.执行流程
           当手机端app(就是你公司开发的app)在支付页面时,调起服务端(后台第1个创建订单接口)接口,后台把需要调起支付宝支付的参数返回给手机端,手机端拿到
         这些参数后,拉起支付宝支付环境完成支付,完成支付后会调异步通知(第2个接口),此时需要给支付宝返回成功或者失败信息,成功后会调用同步通知(第3个接口)
         返回支付成功页面,完成整个支付流程。

2.支付的配置文件AlipayConfig     

package com.love.controller.recharge;

public class AlipayConfig {
	// 合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
		public static String partner = "***************";

		// 商户的私钥,需要PKCS8格式,RSA公私钥生成:https://doc.open.alipay.com/doc2/detail.htmspm=a219a.7629140.0.0.nBDxfy&treeId=58&articleId=103242&docType=1
		public static String private_key="";
		
		// 支付宝的公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
		public static String alipay_public_key = "";
		
		// 请求网关 如果是测试 server_uri = "https://openapi.alipaydev.com/gateway.do"
		public static String server_uri = "https://openapi.alipay.com/gateway.do";

		// 字符编码格式 目前支持 gbk 或 utf-8
		public static String input_charset = "utf-8";

		// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
		 public static String notify_url = "http://www.xxx.com/LoveSystem/notify_url.do";
		 public static String return_url = "http://www.xxx.com/LoveSystem/return_url.do";

		// APPID
		public static String app_id = "***********";
	    
	
}

3.处理支付

@CrossOrigin()
	@RequestMapping(value = "get_Yhming", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String get_Yhming(HttpServletRequest req, HttpServletResponse resp, Model m, String money, HttpSession session) {
		//实例化客户端
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.server_uri, AlipayConfig.app_id, AlipayConfig.private_key, "json", AlipayConfig.input_charset, AlipayConfig.alipay_public_key, "RSA2");
		//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
	

		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		// //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();

		Map<String, String> sParaTemp = new HashMap<String, String>();  
		model.setBody("我是测试数据");
		model.setSubject("爱上币充值");
		String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String seconds = new SimpleDateFormat("HHmmss").format(new Date());
		Users u = (Users) session.getAttribute("users");
		System.out.println(u);
		String ordernum = date+OrderCodeFactory.getTwo()+"00"+u.getUserid()+seconds+OrderCodeFactory.getTwo();
		model.setOutTradeNo(ordernum);
		model.setTimeoutExpress("30m");
		model.setTotalAmount(money);
		model.setProductCode("QUICK_MSECURITY_PAY");
        //设置自己需要传的参数
		model.setPassbackParams(u.getUserid().toString());
		request.setBizModel(model);
		request.setNotifyUrl(AlipayConfig.notify_url);   
		request.setReturnUrl(AlipayConfig.return_url);   //同步回调地址(APP)
		
		String result = null;
		try {
			//这里和普通的接口调用不同,使用的是sdkExecute
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
			System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。
			result = response.getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return result;
	}

4.异步回调

@CrossOrigin()
	@RequestMapping(value = "notify_url", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String notify_url(HttpServletRequest request, HttpSession session) {
		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] + ",";
			}
			//乱码解决,这段代码在出现乱码时使用。
			// 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.input_charset,"RSA2");

		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		if (flag) {    //验签通过   
	        System.out.println("成功");
	        System.out.println(params.get("passback_params"));
	        rechargeservice.insRecharge(params);
	        return "success";
	    } else {  //验签不通过   
	        System.err.println("验签失败");
	        return "fail";
	    }
	}

5.同步通知

@CrossOrigin()
	@RequestMapping(value="return_url", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public int return_url(HttpSession session) {
		return 0;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值