支付宝SDK集成服务端(java)

支付宝SDK集成服务端


配置项(公共参数)

// 商户appid
public static String APPID = "2017120800451448";
// 私钥 pkcs8格式的
public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCgSrIsDE9qdOFoc3Ej0wxD815zDOZ...";
// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://xxxxx/alipay/notify_url.do";
// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
public static String return_url = "xxxxx/alipay/return_url.do";
// 请求网关地址
public static String URL = "https://openapi.alipaydev.com/gateway.do";
// 编码
public static String CHARSET = "UTF-8";
// 返回格式
public static String FORMAT = "json";
// 支付宝公钥
public static String ALIPAY_PUBLIC_KEY = "";
// 日志记录目录
public static String log_path = "D:/log";
// RSA2
public static String SIGNTYPE = "RSA2";

支付工具类

package com.app.utils.alipay;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.app.utils.DateUtil;

public class AlipayUtils {

    public static AlipayClient getAlipayClient(){
        return new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); //获得初始化的AlipayClient
    }

    /**
     * 验证签名 
     * @param request
     * @return
     */
    @SuppressWarnings("all")
    public static boolean rsaCheckV1(HttpServletRequest request){
        boolean verify_result = false;
        try {
            Map params = new HashMap();
            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);
            }
            verify_result = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return verify_result;
    }

    /**
     * 支付接口
     * @param out_trade_no 商户订单号
     * @param total_amount 金额
     * @param body 内容
     * @param subject 主题
     * @param request
     * @return
     */
    public static String aliPrePay(String out_trade_no,String total_amount,String body,String subject,HttpServletRequest request){
        String orderString = null;
        //实例化客户端
        AlipayClient alipayClient = AlipayUtils.getAlipayClient();
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeAppPayRequest payRequest = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(body);
        //标题
        model.setSubject(subject);
        //商户订单号
        model.setOutTradeNo(out_trade_no);
        model.setTimeoutExpress("30m");
        //交易金额
        model.setTotalAmount(total_amount);
        //交易方式
        model.setProductCode(AlipayConfig.PRODUCT_CODE);
        payRequest.setBizModel(model);

        String url = request.getRequestURL().toString();
        String domain = url.substring(0, url.lastIndexOf("/")+1);
        //生产环境
        String notify_url= domain+"aliNotify.do";
        //测试环境
//      String notify_url = "http://XXXX/XXXX/alipay/aliNotify.do";
        payRequest.setNotifyUrl(notify_url);
        try {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse payResponse = alipayClient.sdkExecute(payRequest);
            //就是orderString 可以直接给客户端请求,无需再做处理。
            orderString = payResponse.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return orderString;


    }

    /**
     * 退款
     * @param out_trade_no 商户订单号
     * @param trade_no 支付宝订单流水号
     * @param total_amount 金额
     * @param refund_reason 退款原因
     * @return
     */
    public static boolean refund(String out_trade_no,String trade_no,double total_amount,String refund_reason){
        boolean flag = false;
        String str = null;
        try {
            AlipayClient alipayClient = getAlipayClient();
            AlipayTradeRefundRequest apliRequest = new AlipayTradeRefundRequest();
            apliRequest.setBizContent("{" +
            "\"out_trade_no\":\""+out_trade_no+"\"," +
            "\"trade_no\":\""+trade_no+"\"," +
            "\"refund_amount\":"+total_amount+"," +
            "\"refund_reason\":\""+refund_reason+"\"," +
            "\"out_request_no\":\"\"," +
            "\"operator_id\":\"\"," +
            "\"store_id\":\"\"," +
            "\"terminal_id\":\"\"" +
            "  }");
            AlipayTradeRefundResponse aliResponse = alipayClient.execute(apliRequest);//通过alipayClient调用API,获得对应的response类
            if(aliResponse.isSuccess()){
                //调用成功
                flag = true;
            } else {
                //调用失败
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}

第一次写,有问题的话请指正

你可能感兴趣的:(java)