<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.4.49.ALL</version>
</dependency>
/*********************************************测试 生成支付二维码的URL API*********************************/
/**
* 支付订单生成
* 生成的url转换成二维码,支付宝APP扫一扫支付
*
* @param outTradeNo 自有交易单号
* @param totalAmount 订单金额
* @return
*/
public static String testTradeCreate(String outTradeNo,String totalAmount,StringBuilder logger){
String authCode = "";
logger.append("-【订单创建-开始】" + lineSeparator);
AlipayClient alipayClient = getAlipayClient();
AlipayTradePrecreateRequest alipayTradePrecreateRequest = new AlipayTradePrecreateRequest();
AlipayTradePrecreateModel alipayTradePrecreateModel = new AlipayTradePrecreateModel();
alipayTradePrecreateModel.setOutTradeNo(outTradeNo);
alipayTradePrecreateModel.setTotalAmount(totalAmount);
alipayTradePrecreateModel.setSubject("test sandbox");
logger.append("-订单金额: " + totalAmount + lineSeparator);
try {
alipayTradePrecreateRequest.setBizModel(alipayTradePrecreateModel);
AlipayTradePrecreateResponse alipayTradePrecreateResponse = alipayClient.execute(alipayTradePrecreateRequest);
if (alipayTradePrecreateResponse.isSuccess()){
authCode = alipayTradePrecreateResponse.getQrCode();
logger.append("-创建订单成功!支付宝交易号: " + authCode + lineSeparator);
}else {
logger.append("-创建订单失败,失败信息: " + alipayTradePrecreateResponse.getMsg() + lineSeparator);
}
}catch (Exception e){
logger.append("-创建支付宝订单异常,异常信息: " + e.getMessage() + lineSeparator);
//e.printStackTrace();
return null;
}finally {
logger.append("-【订单创建-结束】" + lineSeparator);
}
return authCode;
}
public static void main(String[] args) {
String outTradeNo = RandomStringUtils.randomAlphanumeric(32);
//订单金额
String totalAmount = "1000";
//生成订单
StringBuilder logger = new StringBuilder();
testTradeCreate(outTradeNo, totalAmount, logger);
System.out.println(logger.toString());
}
public static AlipayClient getAlipayClient(){
//参数依次为: String serverUrl, String appId, String privateKey, String format,
//String charset, String alipayPublicKey, String signType
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway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8Vla1ZbCaqqiYH9FyWIJyRpwKBgCAF0DVU2lLgTIV0vDFBJwmPepyA7z/3ezgB1ScPNEDSxsVA9h1MM51xQihqjs+6cUJVfeqKHLOPdLUR8imlQlNtbz96QyAcpcrnklIgvcxN9wKkh0ODWP8DSYEQJSj9TJ3h55d2KJvwwvGjpNlv08VpUwzIyCdfpJ0brAVdTgk7AoGAZDsXWRtcgXFk7bN2PqUbsf2vvRzHbBT41hk/GW5pNEABiE2nZtQEdzIeRTZ9NRxSy+tiOnb9AFR6FkGQCAqxMhO831wUVp39ZBcv+GHTbOGk6TE/EEsLT1UApI2RqlRh83wK8/etcnGbzQ9qcYOvh4BB4OeNzStEaFXdoleghAsCgYAoCD3QMXzyesNzYePPcQfWOx7vahZ3abw7FVi6qK4xf8BPPxAig+CD6QLgNJlq6PFuoKWaVcaySZ984qCJzrlfabEZd9UZkzhFRkaIyxDJvL05dCHgjqruyfUzt6IyenRlff6RQFOu3KzdyMjPSCo2DhweoU3uRFYnSwCY/LXKjQ==",
"json",
"utf-8",
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB",
"RSA");
return alipayClient;
}
成功响应信息
-【订单创建-开始】
-订单金额: 1000
-创建订单成功!支付宝交易号: https://qr.alipay.com/bax0579001iejvbxmsnz0055
-【订单创建-结束】