支付宝的app支付接入

来源:互联网 发布:成都php找工作很难 编辑:程序博客网 时间:2024/05/16 11:40

凡是从事于app管理后台(java)的开发工作人员,尤其是做掌上商城app的人都会碰到关于如何接入支付宝app支付的问题,本人是初级java工作人员,分享我在接入支付宝app支付功能的经验。

前期准备:

a.去支付宝的开放平台注册一个账号(最好是企业账号)

b.按照下图的指示创建一个应用,添加功能并上线,根据需求添加,在此我们需要添加app支付功能(注意:沙箱环境据我的了解是不能测试用的)


c.应用上线后,我们添加的功能某些事需要签约的,如:app支付。点击使用者管理,进行签约



d.签约成功后,我们才能正式的调用app支付功能

注意:由于支付宝的接口需要对参数进行签名,并且采用的是RSA签名方式。通过支付宝的钥匙生成工具,可以获得密钥对。我们需要将私钥安全保存在自己的项目中,并把公钥在应用公钥中设置,最后获得支付宝公钥用来在支付回调时验签。

1.生成支付宝申请支付请求参数

    String notifyUrl = sysConfigManager.getValue("ali_call_back_url");// 回调地址                    StringBuilder sb = new StringBuilder();                    SortedMap<String, String> resultMap = new TreeMap<String, String>();//使用排序集合map来组装app支付公共请求参数                    resultMap.put("app_id", sysConfigManager.getValue("ali_app_id"));                    resultMap.put("method", "alipay.trade.app.pay");                    resultMap.put("format", "JSON");                    resultMap.put("charset", "utf-8");                    resultMap.put("sign_type", "RSA");                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                    String time = sf.format(new Date());                    resultMap.put("timestamp",time);                    resultMap.put("version", "1.0");                    resultMap.put("notify_url",notifyUrl);                    String biz_content = "";                    SortedMap<String, Object> bizContentMap = new TreeMap<String, Object>();//使用排序集合map来组装app支付业务请求参数                    bizContentMap.put("body",ballPlaceName);                    String goodName = comboServiceName;                    if(goodName.length()>30){                        goodName = goodName.substring(0,30)+"...";                    }                    bizContentMap.put("subject", goodName);                    bizContentMap.put("out_trade_no", payNo);                    bizContentMap.put("total_amount", aliPayMoney.getAmount());                    bizContentMap.put("seller_id",  sysConfigManager.getValue("ali_seller_id"));                    bizContentMap.put("product_code", sysConfigManager.getValue("ali_product_code"));//销售产品码,商家和支付宝签约的产品码                     Gson json = StringUtil.getGson();                    biz_content = json.toJson(bizContentMap);//将业务请求参数转化成json数据格式                    resultMap.put("biz_content", biz_content);                    String content = AlipayCore.createLinkString(AlipayCore.myParaFilter(resultMap));//先去除空值,再把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串                    String sign = RSA.sign(content, sysConfigManager.getValue("ali_private_key"), "utf-8");//签名                    sb.append(content+"&sign="+sign);//拼接签名数据                    String ret = AlipayCore.parameterEncode(sb.toString(), "utf-8");//将最后获得的参数值进行编码                    ResponseObj obj = new ResponseObj(ViewShowEnums.success.getCode(), ViewShowEnums.success.getName(), ret);                    logger.info(obj.toString());                    return new ResponseObj(ViewShowEnums.success.getCode(), ViewShowEnums.success.getName(), ret);//返回最终的支付申请参数给app

将结果集以json的方式返回给app,app从结果中直接提取ret的值交由支付宝的sdk申请支付即可。


2.支付异步回调通知处理

    @RequestMapping(value="/alipayNotify.do", produces={"application/json;charset=UTF-8"})    public void alipayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception{        logger.info("----------------------alipay notify start-------------------");        PrintWriter out = response.getWriter();        String app_id = request.getParameter("app_id");        String seller_id = request.getParameter("seller_id");        if(app_id.equals(sysConfigManager.getValue("ali_app_id"))&&seller_id.equals(sysConfigManager.getValue("ali_seller_id"))){//验证商户id的正确性            Map<String,String> ali_map = new HashMap<String,String>();//异步返回的参数            Map<String,String[]> parameterMap = request.getParameterMap();//获取参数集合            Set<Entry<String, String[]>> set = parameterMap.entrySet();              Iterator<Entry<String, String[]>> it = set.iterator();              while (it.hasNext()) {                  Entry<String, String[]> entry = it.next();                ali_map.put(entry.getKey(), entry.getValue()[0]);            }              String ali_public_key = sysConfigManager.getValue("ali_public_key");            boolean signVerified = AlipaySignature.rsaCheckV1(ali_map, ali_public_key, "UTF-8");//通过支付宝公钥进行验签            if(signVerified){ //验证签名成功                /*在此处进行业务处理(最重要的是要校验金额的正确性,防止金额篡改造成损失,不可相信支付宝的回调金额数据)*/            }else{                out.print("failure");//验证签名失败            }        }else{            out.print("failure");//app_id sell_id 不一致        }                logger.info("----------------------alipay notify end-------------------");        out.flush();        out.close();    }





0 0
原创粉丝点击