美文网首页支付相关
支付宝app支付(Java)

支付宝app支付(Java)

作者: 冰淇淋不想说话 | 来源:发表于2019-01-21 19:52 被阅读98次

    pom:

    注意:

    1.回调接口中,produces类型很重要

    2.@ApiOperation/@ApiParam为swagger注解

    3.支付宝配置中    

        privateKey为应用私钥,即使用支付宝工具生成的私钥,取pkcs8.pem结尾的文件中的值

        publicKey为支付宝公钥此处应区分与应用私钥的区别,在开放平台-密钥管理-查看支付宝公钥


    两个接口:

    1.生成支付单接口(返回给前端拉起支付宝)

    controller示例:

    @RequestMapping(value ="/payorder/for/{billNo}",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

    @ResponseBody

    @ApiOperation(value ="获取支付单",httpMethod ="GET",response = AlipayTradeAppPayResponse.class)

    public JsonResultResponsegetPayOrder(@ApiParam(name ="billNo",value ="billNo")@PathVariable String billNo,

                                          HttpServletRequest request){

    String userId = request.getHeader(HTTP_HEAD_KEY);

    return JsonResultResponse.buildJsonResultResponse(alipayService.getPayOrder(billNo,userId));

    }

    service示例

    //构建支付订单

    AlipayClient alipayClient =new DefaultAlipayClient(AlipayConfig.ALI_OPEN_API_GATEWAY, AlipayConfig.appid,AlipayConfig.privatekey,

            AlipayConfig.format,AlipayConfig.charset,AlipayConfig.publickey, AlipayConfig.signType);

    AlipayTradeAppPayRequest request =new AlipayTradeAppPayRequest();

    AlipayTradeAppPayModel model =new AlipayTradeAppPayModel();

    //RMB分转元

    BigDecimal paiedAmt = RMBCentToDollar(getPaid(tradeOrderVo.getTotalAmount(),tradeOrderVo.getDiscountAmount()));

    model.setSubject("支付宝支付");

    model.setOutTradeNo(billNo);

    model.setTimeoutExpress("30m");

    model.setTotalAmount(paiedAmt.toString());    //单位为yuan

    //model.setProductCode();

    request.setBizModel(model);

    request.setNotifyUrl(AlipayConfig.notifyurl);

    AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);

    2.支付宝回调接口:

    controller示例
    @RequestMapping(value ="/notify",method = RequestMethod.POST,produces = MediaType.TEXT_PLAIN_VALUE)

    @ResponseBody

    @ApiOperation(value ="支付宝回调",httpMethod ="POST")

    public StringaliNotify(HttpServletRequest request){

    try {

    return alipayService.aliNotify(request);

        }catch (AlipayApiException e) {

    logger.error(e.getMessage(),e);

            return e.getMessage();

        }catch (UnsupportedEncodingException e) {

    logger.error(e.getMessage(),e);

        return e.getMessage();

        }

    }

    service示例

    public StringaliNotify(HttpServletRequest request)throws AlipayApiException, UnsupportedEncodingException {

    //获取参数

            Map requestParams = request.getParameterMap();

            logger.info("aliNotify request parameterMap: [{}]",JsonUtils.toJSON(requestParams));

            Map params =new HashMap<>();

            for (Iterator iterator = requestParams.keySet().iterator(); iterator.hasNext(); ) {

    String name = (String) iterator.next();

                String[] valueArray = (String[]) requestParams.get(name);

                String values ="";

                for (int i =0; i < valueArray.length; i++) {

    values = (i == valueArray.length -1) ? values + valueArray[i]

    : values + valueArray[i] +",";

                }

    //乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化

    //            values = new String(values.getBytes("ISO-8859-1"), "gbk");

                params.put(name, values);

            }

    logger.info("aliNotify request params:[{}]", JSONUtils.toJSONString(params));

            boolean flag = AlipaySignature.rsaCheckV1(params, AlipayConfig.publickey, AlipayConfig.charset, AlipayConfig.signType);

            logger.info("ali pay check signature :flag [{}]",flag);

            if(!flag) {

    logger.error("invalid signature");

                return FAIL;

            }

    //业务处理

    return SUCCESS;

    }

    3:附-支付结果查询接口

    controller:

    @RequestMapping(value ="/resultquery",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

    @ResponseBody

    @ApiOperation(value ="支付结果查询",httpMethod ="GET",response = PaymentPo.class)

    public JsonResultResponsequeryPayResult(@ApiParam(name ="billNo",value ="billNo")@RequestParam String billNo,

                                            HttpServletRequest request){

        return JsonResultResponse.buildJsonResultResponse(alipayService.queryPayResult(billNo));

    }

    service:

    //主动查询订单状态

    AlipayClient alipayClient =new DefaultAlipayClient(AlipayConfig.ALI_OPEN_API_GATEWAY, AlipayConfig.appid,AlipayConfig.privatekey,

            AlipayConfig.format,AlipayConfig.charset,AlipayConfig.publickey, AlipayConfig.signType);

    AlipayTradeQueryRequest request =new AlipayTradeQueryRequest();

    AlipayTradeQueryModel model =new AlipayTradeQueryModel();

    model.setOutTradeNo(billNo);

    request.setBizModel(model);

    AlipayTradeQueryResponse response = alipayClient.execute(request);

    logger.info("alipapy query order response:[{}]",response);

    if(!response.isSuccess()){

    logger.error("alipay query order response error. response code:[{}] msg:[{}]",response.getCode(),response.getMsg());

        throw new Exception("alipay query order response error");

    }

    //交易状态:WAIT_BUYER_PAY(交易创建,等待买家付款)、TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、TRADE_SUCCESS(交易支付成功)、TRADE_FINISHED(交易结束,不可退款)

    String tradeStatus = response.getTradeStatus().toUpperCase();

    //业务处理

    相关文章

      网友评论

        本文标题:支付宝app支付(Java)

        本文链接:https://www.haomeiwen.com/subject/qokddqtx.html