java实现支付宝扫码付款

最近做了支付宝扫码付款:废话不多说:下面开始:

1:开通支付宝商家,登录支付宝开放平台,创建应用-

java实现支付宝扫码付款_第1张图片

2、设置应用的私钥和公钥,每个应用都有自己的私钥和公钥。不能混用哦

java实现支付宝扫码付款_第2张图片

3、把应用的appid、私钥、公钥和支付宝的公钥、网关URL 写到配置文件或是数据库。供程序调用

4编写zfb支付service类

首先引入jar包

3.7.73.ALL

	  com.alipay.sdk
	  alipay-sdk-java
	  ${alipay-version}
	
private static AlipayClient client = null;
	static {
		 client = new DefaultAlipayClient(ORDER_URL,APPID,ICU_PRIVATE_KEY,"JSON","utf-8",ALIPAY_PUBLICKEY,AlipayConstants.SIGN_TYPE_RSA2);
	}
	
	
	/**
	 * 获取支付二维码
	 * @param clieid
	 * @param proid
	 * @param itemid
	 * @param visitid
	 * @param lang
	 * @throws Exception
	 * @author Mr G
	 */
	public void getQrcode(String clieid,String proid,String visitid,String itemid,String lang) throws Exception{
		JSONObject busobj = new JSONObject();
        busobj.put("clieid", clieid);
        busobj.put("proid", proid);
        busobj.put("visitid", visitid);
		PayOrderSubmit neworder = payOrderSubmitService.createOrder(clieid,itemid,busobj.toString(),lang,PayConst.PAYTYPE_ALI_SAOMAPAY);
		Map para = new HashMap<>();
		para.put("out_trade_no",neworder.getPayid());
		para.put("subject",neworder.getExpenote());//订单标题
		double paymoney = neworder.getPaymoney()/100d;
		para.put("total_amount",String.valueOf(paymoney));//订单金额 单位元
		// 卖家支付宝账号ID,用于支持一个签约账号下支持打款到不同的收款账号,(打款到sellerId对应的支付宝账号)
        // 如果该字段为空,则默认为与支付宝签约的商户的PID,也就是appid对应的PID
		String sellerId = "";
        para.put("body","购买"+neworder.getExpenote()+paymoney+"元");//购买商品3件共20.00元";
        // 支付超时,定义为120分钟
        String timeoutExpress = "120m";//120m
        //允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 
        para.put("timeout_express", timeoutExpress);
        
        para.put("operator_id","test_operator_id");//商户操作员编号
        para.put("store_id", clieid);//store_id商户门店编号
        para.put("terminal_id", visitid);//商户机具终端编号
        
       // para.put("business_params", busobj);
       // para.put("extend_params", busobj);
        
        // 商品明细列表,需填写购买商品详细信息,
//        List goodsDetailList = new ArrayList();
        // 创建一个商品信息,参数含义分别为商品id(使用国标)、名称、单价(单位为分)、数量,如果需要添加商品类别,详见GoodsDetail
//        GoodsDetail goods1 = GoodsDetail.newInstance(outTradeNo, subject, 1, 1);
        // 创建好一个商品后添加至商品明细列表
//        goodsDetailList.add(goods1);
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setBizContent(JsonUtil.objToJson(para));
        request.setNotifyUrl(NOTIFYURL);
        AlipayTradePrecreateResponse response = client.execute(request);
        String body = response.getBody();
        log.debug("ali qrcode result:"+body);
        JSONObject jsonbody = JSONObject.parseObject(body);
        JSONObject tradeResp = jsonbody.getJSONObject("alipay_trade_precreate_response");
        String code = tradeResp.getString("code");
        if("10000".equals(code)) {
        	String code_url = tradeResp.getString("qr_code");
        	log.debug("ali-code_url="+code_url);
        	String base64 = QrcodeUtil.generateBase64Img(code_url,LOGOPATH);
            //根据业务需要可以把base64直接扔给前台src
        	//TODO
        	if(!b) {
        		log.error("支付宝推送二维码失败:[clieid:{},proid:{},itemid:{},visitid:{}]",clieid,proid,itemid,visitid);
        	}
        }else {
        	log.error("支付宝获取二维码失败:{},[clieid:{},proid:{},itemid:{},visitid:{}]",tradeResp.getString("sub_msg"),clieid,proid,itemid,visitid);
        }
        
	}

在回调接口里想着在预下单时设置几个业务上的id值,然后再回调接口里使用,结果sdk里没有找到,后来干脆再数据库里查询了一下。有知道的伙伴可以留言说下。

5、用户扫码后使用支付宝支付完,还需编写回调接口。如下;

@SuppressWarnings("unused")
	public void payNotify() throws Exception {
		Map notifyMap = notifyMap();
		boolean checkV1 = AlipaySignature.rsaCheckV1(notifyMap, ALIPAY_PUBLICKEY, "utf-8" ,AlipayConstants.SIGN_TYPE_RSA2);
		String replymsg="";
		if(checkV1) {
			//获取订单号
			String out_trade_no = notifyMap.get("out_trade_no");
			String buyer_logon_id = notifyMap.get("buyer_logon_id");//lov***@163.com
			String total_amount = notifyMap.get("total_amount");
			String seller_id = notifyMap.get("seller_id");
			String app_id = notifyMap.get("app_id");
			PayOrderSubmit submit = payOrderSubmitService.getOrderByPayid(out_trade_no);
			double paymoney = submit.getPaymoney()/100d;
			String ordernote = submit.getOrdernote();
			JSONObject noteobj = JSONObject.parseObject(ordernote);
			String clieid = noteobj.getString("clieid");
			String proid = noteobj.getString("proid");
			String visitid = noteobj.getString("visitid");
			if(submit!=null) {
				String trade_status = notifyMap.get("trade_status");
				if("TRADE_SUCCESS".equals(trade_status)) {
					if(total_amount.equals(String.valueOf(paymoney)) && PID.equals(seller_id)
							&& APPID.equals(app_id)) {//校验数据准确性
						
						replymsg=NOTIFY_SUCCESS;
						log.info("支付宝支付成功:clieid:{},proid:{},visitid:{},订单号:{}",clieid,proid,visitid,out_trade_no);
							//TODO log
					}else {
						log.error("本条通知是异常通知:[appid:{},pid:{},支付金额:{},订单号:{}]",app_id,seller_id,total_amount,out_trade_no);
					}
				}	
			}else {
				replymsg = NOTIFY_FAIL;
				log.error("支付宝通知-未获取到相应订单,[订单号:{}]",out_trade_no);
			}
		}else {
			replymsg=NOTIFY_FAIL;
			log.error("支付宝通知验签失败。");
		}
		ContextUtil.getResponse().getWriter().write(replymsg);
	}


@SuppressWarnings("rawtypes")
	private Map notifyMap() {
		HttpServletRequest req = ContextUtil.getRequest();
		Map para = new HashMap<>();
		Enumeration names = req.getParameterNames();
		System.out.println("notifymap-----------------------------------");
		while(names.hasMoreElements()) {
			String k = (String) names.nextElement();
			para.put(k, req.getParameter(k));
			System.out.println(k+" = "+req.getParameter(k));
		}
		return para;
	}

6:后记:官方给的demo是在sdk的基础上又封装了一层,看的我眼花,所以就没采用demo写法。

https://docs.open.alipay.com/399/106843/

https://docs.open.alipay.com/api_1/alipay.trade.precreate

 

 

 

 

你可能感兴趣的:(java)