// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static final String APP_ID = Const.APP_ID;
// 商户私钥,您的PKCS8格式RSA2私钥
public static final String PRIVATE_KEY = Const.PRIVATE_KEY;
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static final String ALIPAY_PUBLIC_KEY = Const.ALIPAY_PUBLIC_KEY;
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static final String NOTIFY_URL = Const.ZHIAOADMINURL+"/alipayManager/notify.ajax";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static final String RETURN_URL = Const.ZHIAOADMINURL+"alipayManager/return_url.do";
// 签名方式
public static final String SIGN_TYPE = "RSA2";
// 字符编码格式
public static final String CHARSET = "UTF-8";
// 支付宝网关
public static final String GATEWAY_URL = "https://openapi.alipay.com/gateway.do";
//参数返回格式,只支持json
private static final String FORMAT = "json";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
public static void aliPay(String outTradeNo, double totalAmount, String subject, HttpServletResponse response) throws IOException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
alipayRequest.setReturnUrl(RETURN_URL);
alipayRequest.setNotifyUrl(NOTIFY_URL);//在公共参数中设置回跳和通知地址
StringBuilder bizContent = new StringBuilder("{\"out_trade_no\":\"").append(outTradeNo).append("\",\"total_amount\":").append(totalAmount).
append(",\"subject\":\"").append(subject).append(",\"timeout_express\":\"10m\",\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
alipayRequest.setBizContent(bizContent.toString());//填充业务参数
System.out.println("请求报文为:----"+ JSON.toJSONString(alipayRequest));
String form = "";
try {
form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=" + CHARSET);
PrintWriter out = response.getWriter();
out.write(form);//直接将完整的表单html输出到页面
out.flush();
out.close();
}
调用:
String orderids= UUID.randomUUID().toString().replace("-", "");
double totalAmount = 0.01;
//调用支付宝
String outTradeNo =orderids, subject = "测试标题";
AlipayConfig.aliPay(outTradeNo, totalAmount, subject, response);