你狗哥 2019-07-01 14:15 采纳率: 0%
浏览 555
已采纳

JAVA调用支付宝接口一脸懵逼!

本人刚学java4个月的萌新求大佬指教!
简单的实验通过test.html中的按钮跳转支付宝支付页面test.html代码如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>测试按钮</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
        $(function () {
            $("#pay").click(function () {
                var id = $("#pay").val();
                $.get("paytest",{value:id},function (data) {
                    window.location="paytest";
                })
            });
        })
    </script>
</head>
<body>
<div>
    <button type="button"  class="btn btn-primary btn-lg btn-block" value="1" id="pay"
    >支付宝测试按钮
    </button>
</div>
</body>
</html>

通过异步点击安酒后跳转支付宝接口controller部分代码如下

 @RequestMapping("/test")
    public String test(){
        return "test";
    }


    @RequestMapping(value = "/paytest",method = RequestMethod.GET)
    public void testPay(HttpServletResponse response,String value){
        System.out.println(value);
        if (value!=null) {
            try {
                response.setContentType("text/html; charset=utf-8");

                response.getWriter().write(alipayService.getwayPay(value));
                response.getWriter().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

然后通过实现类实现数据的插入代码如下:

@Service
public class AlipayServiceImpl implements AlipayService {
    @Resource
    private AlipayClient alipayClient;

    @Resource
    private AlipayProperties aliPayProperties;

    @Resource
    private OrderDao orderDao;

    @Override
    public String getwayPay( String id) {
        Order order=new Order();
        Long id1=Long.parseLong(id);
        order=orderDao.findById(id1).get();
        AlipayTradePagePayModel model=new AlipayTradePagePayModel();
        model.setOutTradeNo(order.getOutTradeNo());
        model.setSubject(order.getOrderType());
        model.setTotalAmount(order.getTotalAmount());
        model.setBody(order.getMiaoshu());
        model.setProductCode(order.getProductCode());

        AlipayTradePagePayRequest pagePayRequest = new AlipayTradePagePayRequest();
        pagePayRequest.setReturnUrl(aliPayProperties.getReturnUrl());
        pagePayRequest.setNotifyUrl(aliPayProperties.getNotifyUrl());
        pagePayRequest.setBizModel(model);

        // 调用SDK生成表单, 并直接将完整的表单html输出到页面
        String retStr = null;
        try {
            retStr = alipayClient.pageExecute(pagePayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return retStr;
    }
}

那么问题来了Demo用的是阿里给的 我这边只是插入数据 为什么跳转不了到阿里支付页面呢求大神指教下面贴两张控制器图

登录画面

点击后画面

控制器代码:

Hibernate: 
    select
        order0_.id as id1_5_0_,
        order0_.out_trade_no as out_trad2_5_0_,
        order0_.total_amount as total_am3_5_0_,
        order0_.miaoshu as miaoshu4_5_0_,
        order0_.order_type as order_ty5_5_0_,
        order0_.product_code as product_6_5_0_,
        order0_.state_pay as state_pa7_5_0_,
        order0_.student_id as student_8_5_0_,
        student1_.sid as sid1_8_1_,
        student1_.address as address2_8_1_,
        student1_.age as age3_8_1_,
        student1_.gender as gender4_8_1_,
        student1_.head as head5_8_1_,
        student1_.id_card as id_card6_8_1_,
        student1_.password as password7_8_1_,
        student1_.sname as sname8_8_1_,
        student1_.tel as tel9_8_1_ 
    from
        wu_order order0_ 
    left outer join
        wu_student student1_ 
            on order0_.student_id=student1_.sid 
    where
        order0_.id=?
null

说实话最后一个null没看懂 求大神指教!

  • 写回答

3条回答 默认 最新

  • 你狗哥 2019-07-01 19:37
    关注

    自己犯傻 后来一点点调试发现按上面方法页面跳转了两次 所以最后那个null是第二次访问的value值 然后修改一下异步代码如下

     $(function () {
                $("#pay").click(function () {
                        window.location="paytest?value=" + $(this).val();
                });
            })
    

    完美解决 还是自己的基本功不扎实还需多练!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度