目录
最下方:
安装支付宝沙箱APP
里面有商家和买家的指定账号
支付宝接口相关的id和密钥
<!-- hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.23</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
void rememberMy(){
QrCodeUtil.generate("https://4fb0-124-89-23-255.ngrok-free.app/user/test", 300, 300, FileUtil.file("D:/桌面/二维码.jpg"));
}
生成成功
APP_ID,APP_PRIVATE_KEY,ALIPAY_PUBLIC_KEY,SEVER_URL?
public class AliyunPay {
public static String APP_ID="20210001220478";
public static String APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCco/ygHVMgSu0vateztwuGE1udMHS6OX8DF95V5rKQWLQ5W8sCqgDcBulOGlydBZDnu9eximq31mwzTMMJjwl/O+x3GiwfYuQIADLUsPXNPMO9u978OIwEdfNZq5mg+PFU21ZTN3ZYTKYwVM4lRQJEa2dMVsfuhMaMm22yHdz9m8K77AvITl1mIqnr4TZEbtsBzF7BE8ryncRe3pudtWKgEpZ6iuOBe3cDa9O/ZrNY1ywDtD6BxGaszDWZksLbWM86oFTHdAnqzpQ7XEfDIryiSmk0O+kl/4JPyJPNS3mIl7raK4reovWELZsNs/DSyTqYE8hI80pipwBlS/dvULPTAgMBAAECggEAApHCDBJPEcGI4HU9Mop3GuzoXMy1rXRR/AgBMqGFHk8GLcaurs2BFBrc6tYN3NmBhfuTzyq5E2jl/3Vr4ZVCD585Ar8gKCekG+l86/Y3IfW0NEGHp9V5nn0cz8S70WMj+kNayCSpwUsIktRhSUU3g03LtPnbb7Mn2pirMoNmE9GRecG2HLkAUOyDfI1xePsiEUwIuWn5bbi13PsoJ7wVc1ylSZ9q1LIv9EKi/z8Z845eTtXt4jbbaB7rfS/3q4GZKrTj3kDvDCpc4JhlJgSQ9FkpGOourcbPVOloPllEOJO18hd2P2FCl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ft69TF3svl8FgM0c485Qbpp1aEmj11n1eoHEWmFnAFmPsceW7Suh69xExRCf5VAW9h31z6R1Pv9jcGxVbbNkYz5hU0wh7xWH2OfvZI5pfBt8Tw7aO74ikqdJygRvWbhDtUPAAxNAA/+Hxd8dmrgZsB8P8a9bmBTeEUXEemvE6xjgguGjhH8UszM0zMTtwlkcAD9sON/ZcOuKf/OsHFwA6gnlH8pArTcyvR6QDJ0nkvQyWLffW/YwIDAQAB";
public static String NOTIFY_URL="https://4fb0-124-89-23-255.ngrok-free.app/aliyun/notify";
public static String SEVER_URL="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
}
??扫码支付快速接入
找到预下单的代码实例,这里已经写好了,如下:
public void pay() {
//设置商品属性
Model tmpModel = new Model();
tmpModel.setOutTradeNo(String.valueOf(UUID.randomUUID()));
tmpModel.setTotalAmount("32");
tmpModel.setSubject("2024.1.4-2024.2.4电费32元");
//填写公钥密钥
AlipayClient alipayClient = new DefaultAlipayClient(AliyunPay.SEVER_URL,AliyunPay.APP_ID,AliyunPay.APP_PRIVATE_KEY,"json","GBK",AliyunPay.ALIPAY_PUBLIC_KEY,"RSA2");
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
//填写回调接口地址,必须为公网可以访问的地址
request.setNotifyUrl(AliyunPay.NOTIFY_URL);
JSONObject bizContent = new JSONObject();
//将商品的属性填写进去
bizContent.put("out_trade_no", tmpModel.getOutTradeNo());
bizContent.put("total_amount", tmpModel.getTotalAmount());
bizContent.put("subject", tmpModel.getSubject());
商品明细信息,按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);
扩展信息,按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);
结算信息,按需传入
//JSONObject settleInfo = new JSONObject();
//JSONArray settleDetailInfos = new JSONArray();
//JSONObject settleDetail = new JSONObject();
//settleDetail.put("trans_in_type", "defaultSettle");
//settleDetail.put("amount", 0.01);
//settleDetailInfos.add(settleDetail);
//settleInfo.put("settle_detail_infos", settleDetailInfos);
//bizContent.put("settle_info", settleInfo);
二级商户信息,按需传入
//JSONObject subMerchant = new JSONObject();
//subMerchant.put("merchant_id", "2088000603999128");
//bizContent.put("sub_merchant", subMerchant);
业务参数信息,按需传入
//JSONObject businessParams = new JSONObject();
//businessParams.put("busi_params_key", "busiParamsValue");
//bizContent.put("business_params", businessParams);
营销信息,按需传入
//JSONObject promoParams = new JSONObject();
//promoParams.put("promo_params_key", "promoParamsValue");
//bizContent.put("promo_params", promoParams);
request.setBizContent(bizContent.toString());
AlipayTradePrecreateResponse response = null;
try {
response = alipayClient.execute(request);
} catch (AlipayApiException e) {
throw new RuntimeException(e);
}
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
log.info("支付订单号:"+response.getOutTradeNo());
//根据返回的response.getQrCode()二维码链接生成二维码
QrCodeUtil.generate(response.getQrCode(), 300, 300, FileUtil.file("D:/桌面/"+tmpModel.getSubject()+".jpg"));
}
访问成功
控制台打印如下信息表示成功?
代码成功会生成指定位置二维码
使用沙箱支付宝买家账号去扫码
如图可以看出名称、价格与输入的商品名称价格一致
卖家这里也收款成功:
测试成功?
回调接口用来接受支付宝成功支付的异步通知结果
以下均免费
/**
* 支付宝异步调用通知
* @param request
* @return
*/
@PostMapping("/notify")
public Result<String> notify(HttpServletRequest request) {
log.info("支付宝异步通知");
log.info("异步通知参数:{}",request.getParameter("out_trade_no"));
//TODO 业务代码
return Result.success(request.getParameter("out_trade_no"));
}
根据刚才的测试,回调接口收到了支付宝的通知,以及商家订单号表示支付成功
官方文档找到查询交易
代码如下:
public Boolean query(String outTradeNo) {
//填写公钥私钥
AlipayClient alipayClient = new DefaultAlipayClient(AliyunPay.SEVER_URL,AliyunPay.APP_ID,AliyunPay.APP_PRIVATE_KEY,"json","GBK",AliyunPay.ALIPAY_PUBLIC_KEY,"RSA2");
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//将要查询的订单号填进去
request.setBizContent("{" +
" \"out_trade_no\":\""+outTradeNo+"\"," +
" \"query_options\":[" +
" \"trade_settle_info\"" +
" ]" +
"}");
AlipayTradeQueryResponse response = null;
try {
response = alipayClient.execute(request);
} catch (AlipayApiException e) {
throw new RuntimeException(e);
}
//根据交易成功与否返回true或false
if(response.isSuccess()){
return true;
} else {
return false;
}
查询上文支付例子中已经支付的订单号
?返回为true,测试成功
表示已经支付?