java 对接支付宝支付和支付宝提现代码(app)


java 对接支付宝支付和支付宝提现代码(app)

一、申请支付宝商家的各种参数 以及证书 不多做介绍

二、引入maven

  

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

 

三、支付宝支付下单 以及提现的工具类:outtradeno是内部订单号,money是钱数 (AliConfig为我自己写的存参数的类)

  提现接口loginid是支付宝账号 name是真实姓名 orderno是内部订单号 money是提现数量

import com.alipay.api.*;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class AlipayText {

    static String CHARSET = "UTF-8";
    static String sign_type = "RSA2";

    /*APP 支付 证书方式*/
    public static String alipayPayCert(String outtradeno,String money) throws AlipayApiException {
        //构造client
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
        //设置网关地址
        certAlipayRequest.setServerUrl(AliConfig.SERVERURL);
        //设置应用Id
        certAlipayRequest.setAppId(AliConfig.APP_ID);
        //设置应用私钥
        certAlipayRequest.setPrivateKey(AliConfig.APP_PRIVATE_KEY);
        //设置请求格式,固定值json
        certAlipayRequest.setFormat("json");
        //设置字符集
        certAlipayRequest.setCharset(CHARSET);
        //设置签名类型
        certAlipayRequest.setSignType(sign_type);
        //设置应用公钥证书路径
        certAlipayRequest.setCertPath(AliConfig.app_cert_path);
        //设置支付宝公钥证书路径
        certAlipayRequest.setAlipayPublicCertPath(AliConfig.alipay_cert_path);
        //设置支付宝根证书路径
        certAlipayRequest.setRootCertPath(AliConfig.alipay_root_cert_path);
        //构造client
        AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest ();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel ();
        model.setBody("购买xx" );
        model.setSubject ( "购买xx" );
        model.setOutTradeNo ( outtradeno );
        model.setTimeoutExpress ( "30m" );
        model.setTotalAmount ( money );
        model.setProductCode ( "QUICK_MSECURITY_PAY" );
        request.setBizModel ( model );
        request.setNotifyUrl ( AliConfig.NotifyUrl );
        try  {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute( request );
            System.out.println(response.getBody()); //就是orderString 可以直接给客户端请求,无需再做处理。
            return response.getBody();
        }  catch (AlipayApiException e ) {
            e.printStackTrace();
        }
        return null;
    }


    /*APP 转账*/
    public static String alipayFundTransUniTransfer(String loginId,String name,String orderno,String money) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(AliConfig.SERVERURL);
        alipayConfig.setAppId(AliConfig.APP_ID);
        alipayConfig.setPrivateKey(AliConfig.APP_PRIVATE_KEY);
        alipayConfig.setAppCertPath(AliConfig.app_cert_path);
        alipayConfig.setAlipayPublicCertPath(AliConfig.alipay_cert_path);
        alipayConfig.setRootCertPath(AliConfig.alipay_root_cert_path);
        alipayConfig.setFormat("JSON");
        alipayConfig.setCharset(CHARSET);
        alipayConfig.setSignType("RSA2");
        // 构造client
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity(loginId);
        payeeInfo.setName(name);
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        model.setPayeeInfo(payeeInfo);
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setOrderTitle("提现");
        model.setOutBizNo(orderno);
        model.setBusinessParams("{/"payer_show_name_use_alias/":/"xxxx/"}");
        /*SignData signData = new SignData();
        signData.setOriOutBizNo("订单号");
        signData.setOriSignType("RSA2");
        signData.setOriSign("EqHFP0z4a9iaQ1ep==");
        signData.setOriCharSet("UTF-8");
        signData.setPartnerId("签名被授权方支付宝账号ID");
        signData.setOriAppId("2021000185629012");
        model.setSignData(signData);*/
        model.setRemark("提现");
//        model.setOriginalOrderId("20190620110075000006640000063056");
//        model.setPassbackParams("{/"merchantBizType/":/"peerPay/"}");
        model.setBizScene("DIRECT_TRANSFER");
        model.setTransAmount(money);
        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }
}

四、支付宝提现调用代码处理

JSONObject json = JSONObject.parseObject(res).getJSONObject("alipay_fund_trans_uni_transfer_response");
        if (!"10000".equalsIgnoreCase(json.getString("code"))) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error(json.getString("sub_msg"));
        }

五、支付宝回调

/**
     * 支付宝回调
     */
    @PostMapping("alipay_callback")
    public void alipaySend(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException, IOException {
        //获取支付宝POST过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            //乱码解决,这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        logger.info("支付宝回调返回的信息{}", params.toString());
        boolean flag = AlipaySignature.rsaCertCheckV1(params, AliConfig.alipay_cert_path, "UTF-8", "RSA2");
        String tradeNo = params.get("out_trade_no");
        BbOrder order = orderService.selectOrderByOrderNo(tradeNo);
        if (order == null) {
            throw new ServiceException("out_trade_no错误");
        }
        // 支付成功
        if (flag) {
            logger.info("支付成功");
            
        } else {
            logger.info("支付失败");
        }
        response.getWriter().write("success");
    }

 

六、搞定

原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/277712.html

(0)
上一篇 2022年7月29日
下一篇 2022年7月29日

相关推荐

发表回复

登录后才能评论