欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

支付宝App支付功能实现

程序员文章站 2022-07-12 14:16:21
...

准备工作

工具下载

1、支付宝开放平台开发助手
支付宝App支付功能实现

2、支付宝App沙箱版
3、客户端测试App

环境准备

1、在支付宝开放平台开启自己的沙箱环境
支付宝App支付功能实现
2、获取你的商家账号和买家账号
3、获取支付宝公钥与用户私钥
支付宝App支付功能实现

开始编码



    //应用id
    public final static String  APP_ID="2016093000630117";
    //应用私钥
    public final static String  APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhdIcHHkvg2rNu7ykpT9eMMgBtXZRCvzyWIg8s4bkJWFuRa2BDZHtzNdtPujGyUTofkfDS5S/0e/h023yfW8yX6mR0wxFiiyYun4atLtJb2fGr+Twmo7CsDL4zmppbdGtUU3RBeWBVrUVangHZ3XqgryGlV/6zILlWIYUagGMmRWC26QeKc7+STlCGsaf26C04jPPJ3x7TK9RaAncf39iLvVRTLLqxWADpKT9/nMqvDpPdyB7K4k1sEVdAlYDdcUbkjPux46Ea9llGSFf4Dh9IUvFN+Ix4Pu6x6++k+kBa0ZbC1BiX6vrcTi+0iFyFDy6wJHRdHbOB+qngfieN0b+pAgMBAAECggEAMHYCvDX362o3HYZM27NsypbI3rk5sCs5E7BYgLY4NtkGPbjN5U6I9pfB/14vZnvtOXH+SOa4qx2mpz+yhvKNhK3+T9bKOttcJrNlmH4y+0lamDOniUqwJBtNGvyFZ1kb5PFXUOeNHH11JehGz5S60iUy3z6N4eUL0Ya8/JhSN5xUuv+JL6I6JDJODSzokeawzv13GLtoYKv8jAcY1g1hSdSNS78SWJfAmzNIaK52AtOObe81W7PXkE1MWuah/c9K7CvB99SWfCUqJMUvvFLX4yP9XIGYzQleCgO3Vq51M+TycmCSTgw7tGoDmZjwIc0DxR9L89hI/ZFJNar6UkrCHQKBgQD4IFR1gTX3dk46iwWy/Bwp4lrxSScpUw7d8drBFx1BbWd4k15UB6jOAEGqW1hB4irPxrfve7kLS7pbR2uMzcNczp3QlDXxYLFPr1r19PWOxTUpvym/LHIzNOAiHEAIipYw3quHOMxhU7beR3gMPF6e9bEUaq6xLedamBw33Sc7OwKBgQDonAcASYwcRyu21v/wIYlcTFvg1924VFQKTkiWjmYaUnnbPCfrIV5YM4GDXozZdtAnJBmFMgbMQUofycmjusC1vVkFuUzR3GePfOEyqag8FOs2tjUJU9Q8gh7qCjr5a3q5VbOhu+ZlVF4cId+GTFb6N+VtArr0xjILPqN3sLgaawKBgQDkszflzqDlZIAaacVpAgMbf+1+9IRKDKppW+DblOh8Q9DZTEvne9/VcFsWIHeq0kHPnUQwUoNuq/Aq7httfsKRKkg/1jwaLZuA7QoUJiKn45Fq1y4tbVQdaqEkD5HdzY89oB/N1UZcdwLYwlsePA6iyabcQLCf0sWptQZdX1QnvQKBgHwLQLyJfBtUZN5zVzcyWzMKUXDdDRMQfWSljJYlSBO/adZdBRihydMVXHq3ADuz3ZsR6D4ciH2CT8oV7N7QZ5ZDbGbeAzZi6CMfwGQpD1tC+JBLqIrZ1nOSHHRIklySUXbmWg3kyx+8Vxd/hNj0eumix0lTzGleDC/s7nWUmwkTAoGAB0EMXTTuPdOdyip5isS+nc/qhtP3xjNHLIdJXlBMG8XtQivosKCS4Y+2CCftYia1mdAXIyDPtGPzAr/ndB297VM3/p+JmoV417edTfW2BHxh4FQJCvs0VDjktzP6erDDJBdutGmrTS4vv9LxCCWIk4+oxuFDp1xYUQVR8pVXrAE=";
    //编码
    public final static String  CHARSET="UTF-8";
    //支付宝公钥
    public final static String  ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhgvsmfRPdHVY4gULy+6eaUCtLDhQxYScCGUhJBug5ckAruWfSnJQbhNKASkfCAURAYJbb9Aezkt+ry+4B6qKXyXWTaOt7yx+EZYXuyjDGro04nPmIXB8hi2EwlhqiXuaBseWTjxAkFHAmKDsOoxutq9YKqJHoTKX2bISqHByfHKAu3rWgVj1MaMDunuBh/u/ZASiiiNG6eXxC/zXk+qn8Bxgu4ZryNvWhZpmUV7pFUCE0K4kC2Z7gArVT+sXcjAW85B6O9ovloMvisCNEhvlZb8g37pw2wLKzgxGo04qQ5xA9LX53EENu/ZCGSJIPjUFNYdanrO5tyYkjYTmRtyr6wIDAQAB";
    //沙箱网关
    public final static String  SERVERURL="https://openapi.alipaydev.com/gateway.do";
    //格式
    public final static String  FORMAT="json";
    //**加密方式
    public final static String  SIGNTYPE="RSA2";


    public void getOrderInfo(){
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(SERVERURL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGNTYPE);
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody("我是测试数据");
        model.setSubject("App支付测试Java");
        model.setOutTradeNo("songshuhao1314");
        model.setTimeoutExpress("30m");
        model.setTotalAmount("0.01");
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl("http://i3ue7v5siv.52http.tech");
        try {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

    public String notify_url(HttpRequest request){
        //获取支付宝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);
        }
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
        try {
            boolean flag = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET,SIGNTYPE);
            if (!flag){
                return "flag";
            }
            //验签

            //业务逻辑
            System.out.println("修改订单成功!");
            return "sucess";
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return "fail";
        }
    }

    public static void main(String[] args) {
        AlipayController alipayController=new AlipayController();
        alipayController.getOrderInfo();
    }
相关标签: 支付宝 接口