支付宝支付
1导入支付宝依赖
依赖地址:小程序文档 - 支付宝文档中心
com.alipay.sdk
alipay-sdk-java
4.34.0.ALL
电脑网站支付api:小程序文档 - 支付宝文档中心
其他支付api(app,网站,当面付等):小程序文档 - 支付宝文档中心
我选择电脑网站支付方式
如下代码是官网上的(根据需求更改,添加)需要啥参数去官网请求参数里面找 添加到bizContent里面
AlipayClient alipayClient = new DefaultAlipayClient(
"https://openapi.alipay.com/gateway.do"
,"app_id","your private_key",
"json","GBK","alipay_public_key"
,"RSA2");
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//异步接收地址,仅支持http/https,公网可访问
request.setNotifyUrl("");
//同步跳转地址,仅支持http/https
request.setReturnUrl("");
/******必传参数******/
JSONObject bizContent = new JSONObject();
//商户订单号,商家自定义,保持唯一性
bizContent.put("out_trade_no", "20210817010101004");
//支付金额,最小值0.01元
bizContent.put("total_amount", 0.01);
//订单标题,不可使用特殊符号
bizContent.put("subject", "测试商品");
//电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
/******可选参数******/
//bizContent.put("time_expire", "2022-08-01 22:00:00");
商品明细信息,按需传入
//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);
request.setBizContent(bizContent.toString());
AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
封装工具类 这个是下单支付,(沙箱测试)
点击查看如下(只需应用私钥和支付宝公钥)
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
/**
* Created with IntelliJ IDEA.
* Description: 支付宝工具类
* User: WY
* Date: 2023-06-13
*/
public class AliPayUntil {
private static AlipayClient alipayClient;
private static String app_id="你的APPID";
private static String private_key="应用私钥";
private static String alipay_public_key="支付宝公钥";
private static String serverUrl="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
static {
alipayClient = new DefaultAlipayClient(
serverUrl,
app_id, private_key, "json",
"utf-8", alipay_public_key, "RSA2");
}
public static String createPay() throws AlipayApiException {
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
request.setNotifyUrl("");
request.setReturnUrl("");
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", "202108170101010049");
bizContent.put("total_amount", 1000);
bizContent.put("subject", "测试商品");
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
//bizContent.put("time_expire", "2022-08-01 22:00:00");
商品明细信息,按需传入
//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);
request.setBizContent(bizContent.toString());
AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
if (response.isSuccess()) {
String body = response.getBody();
System.out.println("调用成功===>" + body);
} else {
System.out.println("调用失败");
}
return response.getBody();
}
}
alipay_public_key =支付宝公钥
private_key=应用私钥
serverUrl="https://openapi-sandbox.dl.alipaydev.com/gateway.do" // 这是沙箱网关地址,只支持沙箱使用 ,上线记得替换为真实地址
调用工具类出现
如图
复制带html中
上图浏览器打开如下图
登录沙箱账号密码
登录之后出现如下界面
支付成功显示
查看沙箱看是否扣减成功
扣减成功,付款结束
退款,查询,关闭等订单一样,改下参数就好,官网里面代码cv一下就好,添加到工具类里面