支付宝app支付接口

支付宝业务流程:支付宝不存在预付单(微信存在:并且微信订单商户号不能重复)这一说发

客户端 步骤1:用户在手机APP中选择支付宝支付,然后去服务器下单

服务器  步骤二:接受客户端提交过来的订单信息,进行订单数据组装,然后将组装的数据返回给客户端(当前步骤不需要去支                             付宝下单)

客户端 步骤三:用服务器返回的数据进行下单+支付

支付成功之后支付宝会有异步后台通知服务器:地址在下单的时候可以配置(在商户系统配置的貌似可以被覆盖)

    public String alipayApp(OrderInfoModel orderInfoModel) {
        LogHelper.info(logger, "创建【支付宝】APP订单");
    //初始化配置信息
    this.initPaymentConfig(orderInfoModel.getProjectId().toString(), PaymentChannelEnum.ALIPAY);
        //获取下单请求参数
        Map param = this.getRequestParam(orderInfoModel, null);
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(param.get("body"));
        model.setSubject(param.get("subject"));
        model.setOutTradeNo(orderInfoModel.getOrderId());
        model.setTimeoutExpress("3h");
        model.setTotalAmount(param.get("amount"));
        //销售产品码,商家和支付宝签约的产品码,
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);
        String result;
        try {
            //这里和普通的接口调用不同,使用的是sdkExecute(官方没说明,实际调用去掉sdk标识)
/*            if (LocalContextHolder.getContext().getSource().getCode().equals(Source.IOS.getCode())) {
                AlipayTradeAppPayResponse response = this.sdkExecute(request);
                result = response.getBody();
            } else {

            }*/
            //发起预下单请求
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            result = response.getBody();
        } catch (AlipayApiException e) {
            LogHelper.exception(e, logger, "【支付宝】app支付生成预付单出错 orderId={0}, request={1}", orderInfoModel.getOrderId(), request);
            throw new BusinessException(BizErrorCode.ORDER_ERROR);
        }
        return result;
    }
/*组装交易参数*/
private Map getRequestParam(OrderInfoModel orderInfoModel, String ip) {
    Map requestData = new HashMap<>();
    if (orderInfoModel != null) {
        // 异步通知回调地址
        requestData.put("notify_url", notifyUrl);
        // 交易时长3小时
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        // 设置交易结束时间  微信用
        requestData.put("time_expire", formatter.format(orderInfoModel.getCloseTime()));
        // 订单主题
        String orderSubject = "充值";

        try {
            if (orderInfoModel.getOrderChannel().contains(PaymentChannelEnum.ALIPAY.getCode())) {
                // 如果是alipay
                // 订单号
                requestData.put("orderId", orderInfoModel.getOrderId());
                requestData.put("subject", orderSubject);
                // requestData.put("body", (orderInfoModel.getHotelName() + orderInfoModel.getHotelAddress()));
                // 订单金额 保留两位小数点
                requestData.put("amount", UnitConvertUtil.pointsToYuan(orderInfoModel.getOrderAmount().toString()) + "0");
            } else {
                requestData.put("appid", this.paymentConfigModel.getAppId());
                requestData.put("mch_id", this.paymentConfigModel.getMchId());
                // 随机数
                requestData.put("nonce_str", WXPayUtil.generateUUID());
                // 商户订单号
                requestData.put("out_trade_no", orderInfoModel.getOrderId());
                // 总金额(单位:分)
                requestData.put("total_fee", orderInfoModel.getOrderAmount().toString());
                // 商品描述
                requestData.put("body", orderSubject);
                // 终端IP
                requestData.put("spbill_create_ip", ip);
                LogHelper.info(logger, "【微信】客户端ip={0}", ip);
                // 如果是微信APP支付
                if (PaymentChannelEnum.WEIXINAPP.getCode().equals(orderInfoModel.getOrderChannel())) {
                    // 交易类型
                    requestData.put("trade_type", "APP");
                }
                /*
                 * //如果是微信H5支付 if (PaymentChannelEnum.WEIXINH5.getCode().equals(orderInfoModel.getOrderChannel())) { requestData.put("trade_type",
                 * "MWEB"); //场景信息 requestData.put("scene_info", "{\"h5_info\":{ \"type\":\"Wap\"," + "\"wap_url\":\"" + wapUrl +
                 * "\",\"wap_name\":\"mamahome\"," + "}}"); }
                 */
            }
        } catch (Exception e) {
            throw new BusinessException(SystemErrorCode.SYSTEM_ERROR);
        }
    } else {
        throw new BusinessException(SystemErrorCode.SYSTEM_ERROR);
    }
    return requestData;
}

你可能感兴趣的:(java)