java支付宝和微信app支付(服务端处理)

最近在接入支付宝和微信的app支付 , 之前因为大部分做的都是网页版的支付,没接触过app,这次把遇到的坑都记录下来。


首先 支付宝支付 https://openhome.alipay.com/platform/home.htm 先去这里 注册商户   然后就是填写一系列的资料 申请啊 什么的。  最后通过了。


登陆后  点击这个 


  然后    这样 就创建了 一个app支付的应用



——————


 这个是你创建好的app  里面有我们需要的信息 



这地方 我们需要修改 

1. 应用网关 :修改为你服务器的ip或者域名  

2. 授权回调地址  修改为 : 你自己写好的 处理支付宝支付成功后的 业务逻辑 (@RequestMapping) 地址 

3. RSA 密钥  :你自己生成的 应用公钥  上传后 会得到  支付宝公钥  (回调时候需要 支付宝公钥 才可以获得支付宝传回来的参数)   (支付宝密钥生成 https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.Rju6nj&treeId=291&articleId=105971&docType=1)


4. 通过 密钥生成工具 得到  商户私钥  一定要保存好 后面签名的时候需要  而且要和 你自己生成的  公钥(不是支付宝的公钥 是你自己拿工具生成的)  对应 


5. appid : 上面图片里面的 你创建的应用的 id 

 

注意 !!!  授权回调 地址 默认为 80端口  不可以为其他端口  也就是说你本地是 不会回调的 或者 其他 比如  xx.12.34.9:8080/xx/notifyMobile  这是不行的 必须部署为 80端口  






这就是 支付宝整个 签名过程 然后你把他 返回的 东西 给  ios 或者 android 就行了  就可以唤醒 手机端app 


以下是回调的代码 包括 微信和支付宝的回调 


[java]  view plain  copy
  1. <strong><span style="font-size:18px;"><span style="color:#333333;">//支付宝回调  
  2.     @RequestMapping(value = "/mobilePayNotify")  
  3.     public void alipayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  4.         logger.info("
    alipayNotify进入!");  
  5.         try {  
  6.             // 获取支付宝POST过来反馈信息  
  7.             Map<String, String> params = new HashMap<String, String>();  
  8.             Map<String, String[]> requestParams = request.getParameterMap();  
  9.             for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {  
  10.                 String name = iter.next();  
  11.                 String[] values = (String[]) requestParams.get(name);  
  12.                 String valueStr = "";  
  13.                 for (int i = 0; i < values.length; i++) {  
  14.                     valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";  
  15.                 }  
  16.                 params.put(name, valueStr);  
  17.             }  
  18.             // 买家付款账号  
  19.             String buyer_logon_id = params.get("buyer_logon_id");  
  20.             BigDecimal total_amount = new BigDecimal(params.get("total_amount"));  
  21.             // 商户订单号  
  22.             String out_trade_no = params.get("out_trade_no");  
  23.             // 支付宝交易号  
  24.             String trade_no = params.get("trade_no");  
  25.             // 交易状态  
  26.             String trade_status = params.get("trade_status");  
  27.             String extraParam = "";  
  28.             int offset = out_trade_no.indexOf("_");  
  29.             if(offset>-1){  
  30.                 extraParam = out_trade_no.substring(offset+1);  
  31.                 out_trade_no = out_trade_no.substring(0,offset);  
  32.             }  
  33.   
  34.             // 获得支付信息  
  35.             String payKey = PayPropertySupport.getProperty("pay.ali.publicKey");  
  36.   
  37.             if (AlipayNotify.verifyMobileNotify(params, payKey)) {// 验证成功  
  38.                 if (trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")) {  
  39.                     // 支付成功后执行相关业务  
  40.                       
  41.                     logger.info("订单编号:"+out_trade_no+";支付金额:"+total_amount);  
  42.                       
  43.                     afterPay(out_trade_no, trade_no,ConstantUtil.alipay);  
  44.                       
  45.                 }  
  46.             } else {  
  47.                 logger.info("
    alipayNotify验证失败——商户订单号:" + out_trade_no + ";支付宝交易号:" + trade_no + ",交易状态:" + trade_status);  
  48.             }  
  49.             ResponseUtils.renderText(response, "success");  
  50.         } catch (Exception e) {  
  51.             logger.info("
    alipayNotify业务逻辑异常:" + e.getMessage(), e);  
  52.             ResponseUtils.renderText(response, "fail");  
  53.         }  
  54.     }  
  55.       
  56.       
  57.     @RequestMapping("/wxMobileNotify")  
  58.     public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  59.         logger.info("@@@@收到微信支付信息,进入notify流程@@@@");  
  60.         try {  
  61.             InputStream in = request.getInputStream();  
  62.             String s = null;  
  63.             BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));  
  64.             StringBuffer sb = new StringBuffer();  
  65.             while ((s = br.readLine()) != null) {  
  66.                 sb.append(s);  
  67.             }  
  68.             br.close();  
  69.             in.close();  
  70.             Map<String, String> params = XMLUtil.doXMLParse(sb.toString());  
  71.   
  72.             SortedMap<String, String> newParams = new TreeMap<String, String>(params);  
  73.               
  74.             newParams.put("key", ConstantUtil.APP_KEY);  
  75.   
  76.             String out_trade_no = (String) params.get("out_trade_no");  
  77.   
  78.             String trade_no = (String) params.get("transaction_id");  
  79.             // 总金额,分  
  80.             String total = (String) params.get("total_fee");  
  81.   
  82.             String respCode = (String) params.get("result_code");  
  83.   
  84.             String openId = (String) params.get("openid");  
  85.             // 自定义参数  
  86.             String extraParam = (String) params.get("attach");  
  87.             if (WxPrepay.isValiSign(newParams)) {  
  88.                 logger.info("@@@@验证成功@@@@");  
  89.                 if (respCode != null && respCode.equals("SUCCESS")) {  
  90.                       
  91.                     //支付成功后 业务逻辑   
  92.                     afterPay(out_trade_no, trade_no,ConstantUtil.wxpay);  
  93.                       
  94.                 } else {  
  95.                     logger.info("@@@@支付交易状态未知——订单号:" + out_trade_no + ";交易状态:" + respCode + ";微信支付订单号:" + trade_no);  
  96.                 }  
  97.             } else {  
  98.                 logger.info("@@@@验证失败@@@@");  
  99.             }  
  100.         } catch (Exception e) {  
  101.             logger.info("@@@@支付后业务逻辑异常" + e.getMessage() + "@@@@");  
  102.         }  
  103.   
  104.     }  
  105.       
  106.     public void afterPay(String out_trade_no,String trade_no,String payMethod){  
  107.           
  108.         OrderPayExample example = new OrderPayExample();  
  109.         example.createCriteria().andOrderNoEqualTo(out_trade_no);  
  110.         List<OrderPay> list = orderPayService.selectByExample(example);  
  111.           
  112.         //设置 单个订单状态  
  113.         for (OrderPay orderPay : list) {  
  114.             Order order = orderService.selectByPrimaryKey(orderPay.getOrderId());  
  115.             order.setStatus(1);  
  116.             order.setRemark(trade_no);  
  117.             order.setPayMethod(payMethod);  
  118.             orderService.updateByPrimaryKeySelective(order);  
  119.         }  
  120.           
  121.         //设置 购物车订单状态  
  122.         for (OrderPay orderPay : list) {  
  123.             orderPay.setStates(1);  
  124.             orderPayService.updateByPrimaryKeySelective(orderPay);  
  125.         }     
  126.     }  
  127. }</span><span style="color:#ff0000;">  
  128. </span></span></strong>  


可以根据自己要求 的修改  切记 支付宝的回调 如果要获取 支付相关信息 必须用 支付宝的公钥  (不是自己生成的)


——————


微信  去这 https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN 申请 并且签约 app支付  (支付宝也是要签约的 )


[java]  view plain  copy
  1. <span style="font-size:18px;"><strong>  public static Map wxAppPrepareId(BigDecimal price, String orderId, String info, String ip)  
  2.   {  
  3.   SortedMap<String, String> params =new TreeMap();  
  4.     params.put("body", info);   
  5.     params.put("nonce_str", WXUtil.getNonceStr());  
  6.     params.put("out_trade_no", orderId);  
  7.     params.put("total_fee", String.valueOf(price.multiply(new BigDecimal(100)).intValue()));  
  8.     params.put("spbill_create_ip""8.8.8.8");  
  9.     params.put("notify_url", ConstantUtil.BACK_URL);  
  10.     params.put("trade_type""APP");  
  11.     params.put("mch_id", ConstantUtil.PARTNER);  
  12.     params.put("appid", ConstantUtil.APP_ID);  
  13.     params.put("key", ConstantUtil.APP_KEY);  
  14.     params.put("sign", createSign(params));  
  15.     SortedMap<String,String> map =  new TreeMap();  
  16.       
  17.     Map mapStr = getPrepayApp(params);  
  18.       
  19.     map.put("key", ConstantUtil.APP_KEY);  
  20.     map.put("timestamp",  WXUtil.getTimeStamp());  
  21.     map.put("package","Sign=WXPay");  
  22.     map.put("noncestr", (String)mapStr.get("nonce_str"));  
  23.     map.put("partnerid", ConstantUtil.PARTNER);  
  24.     map.put("appid", ConstantUtil.APP_ID);  
  25.     map.put("prepayid",(String)mapStr.get("prepay_id"));  
  26.     map.put("sign",createSign(map));  
  27.     return map;  
  28.   }</strong></span>  


微信需要   其实还有 api_key  那 个需要在 商户平台去设置  是在签名的时候使用的  


大致 到这里 就都ok了  


其实主要还是 拼接字符串 签名的时候会出现问题 一旦你拼错一点点  就会出现各种问题  不唤醒app 等 。 代码大概贴出来了  更多资料 去 格子的 官网查看 api    


Mark 一下 防止自己忘记   =。= 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值