java提供接口给app支付宝支付遇到得坑

引入maven:

   <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.9.153.ALL</version>
   </dependency>

贴代码:

AlipayClient alipayClient = new DefaultAlipayClient(payUrl_alipay,appId_alipay,privateKey_alipay,"json","UTF-8",publicKey_alipay,"RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
//        model.setBody(URLEncoder.encode(param.get("body").toString()));
//        model.setSubject(URLEncoder.encode(param.get("body").toString()));
//        model.setOutTradeNo(param.get("ywlsh").toString());
//        model.setTimeoutExpress("30m");
//        model.setTotalAmount(SNUtil.getMoney_ali(param.get("money").toString()));
//        model.setProductCode("QUICK_MSECURITY_PAY");
//        request.setBizModel(model);
////        request.setNotifyUrl("live/alipaynotify");
        request.setBizContent("{" +
                "\"timeout_express\":\"30m\"," +
          "\"total_amount\":\""+SNUtil.getMoney_ali(param.get("money").toString())+"\"," +
                "\"product_code\":\"QUICK_MSECURITY_PAY\"," +
                "\"body\":\""+  URLEncoder.encode(param.get("body").toString())+"\"," +
                "\"subject\":\""+URLEncoder.encode(param.get("body").toString())+"\"," +
                "\"out_trade_no\":\""+param.get("ywlsh").toString()+"\"" +
                "  }");
        LOGGER.info("alipay params -> :"+request.getBizContent());
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
//            logger.debug("appPay param", response.getBody());
            if(response.isSuccess()){
                LOGGER.info("alipay responseBody -> :"+response.getBody());
                return CommonResult.success(response.getBody());
            } else {
                return CommonResult.failed();
            }
        

踩坑:

  • total_amount需要保留两位小数
  • 中文参数需要URLEncode
  • 回调必须返回 “success”
  • 回调验签得时候参数是支付宝公钥,不是应用公钥

猜你喜欢

转载自blog.csdn.net/street_boy/article/details/107376281