支付宝沙箱支付指的是在支付宝开放平台的沙箱环境中进行的模拟支付操作。开发者可通过支付宝开放平台的沙箱环境模拟真实的支付流程,包括创建订单、模拟用户付款、模拟用户退款等操作,从而测试自己的支付功能是否正常。沙箱环境中的交易数据和资金均为虚拟的,不会对真实账户产生任何影响。
以下是一个简单的支付宝沙箱支付java代码示例:
1. 配置文件
在项目的src/main/resources目录下新建一个名为alipay.properties的文件,用于存放支付宝沙箱支付的配置信息,包括app_id、私钥、公钥、支付宝公钥、回调地址等字段。示例配置如下:
```
app_id=沙箱环境中申请的app_id
merchant_private_key=商户私钥
alipay_public_key=支付宝公钥
gateway_url=https://openapi.alipaydev.com/gateway.do
sign_type=RSA2
charset=UTF-8
notify_url=http://localhost:8080/notify
```
2. 创建AlipayClient
AlipayClient是调用支付宝API的核心类,用于创建与支付宝网关的连接。示例代码如下:
```
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
public class AlipayUtil {
private static AlipayClient alipayClient;
static {
String gatewayUrl = AliPayConfig.getGatewayUrl();
String appId = AliPayConfig.getAppId();
String charset = AliPayConfig.getCharset();
String format = AliPayConfig.getFormat();
String privateKey = AliPayConfig.getMerchantPrivateKey();
String alipayPublicKey = AliPayConfig.getAlipayPublicKey();
String signType = AliPayConfig.getSignType();
alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
}
public static AlipayClient getAlipayClient() {
return alipayClient;
}
}
```
3. 发起支付请求
发起支付请求前需要构建AlipayTradePagePayRequest对象,设置商品信息、订单信息、支付回调地址等信息,并使用AlipayClient与支付宝网关建立连接并发起支付请求。示例代码如下:
```
import java.math.BigDecimal;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.TradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.google.common.collect.Lists;
public class PayController {
public static void main(String[] args) throws AlipayApiException {
AlipayClient alipayClient = AlipayUtil.getAlipayClient();
// 构建支付请求
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(returnUrl);
alipayRequest.setNotifyUrl(notifyUrl);
TradePagePayModel model = new TradePagePayModel();
model.setSubject("test");
model.setOutTradeNo("123456789");
model.setTotalAmount(new BigDecimal("0.01").setScale(2).toString());
model.setBody("body");
model.setTimeoutExpress("30m");
ExtendParams extendParams = new ExtendParams();
extendParams.setSysServiceProviderId("2088100200300400500");
model.setExtendParams(extendParams);
GoodsDetail goodsDetail1 = GoodsDetail.newInstance("goods_id001", "全麦小面包", new BigDecimal("0.01"), 1);
GoodsDetail goodsDetail2 = GoodsDetail.newInstance("goods_id002", "黑人牙刷", new BigDecimal("0.03"), 2);
alipayRequest.setBizModel(model);
AlipayTradePagePayResponse alipayTradePagePayResponse = alipayClient
.pageExecute(alipayRequest);
String result = alipayTradePagePayResponse.getBody();
System.out.println(result);
}
}
```
4. 支付回调处理
支付宝支付成功后会回调商户设置的回调地址,商户需要接收并解析支付宝返回的参数,并进行业务处理。示例代码如下:
```
@RequestMapping("/notify")
public void notify(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, String> params = new HashMap<String, String>();
Map<String, String[]> requestParams = request.getParameterMap();
for (Iterator<String> 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] + ",";
}
params.put(name, valueStr);
}
boolean signVerified = AlipaySignature.rsaCheckV1(params, AliPayConfig.getAlipayPublicKey(), AliPayConfig.getCharset(), AliPayConfig.getSignType());
if (signVerified) {
String tradeStatus = params.get("trade_status");
if ("TRADE_FINISHED".equals(tradeStatus)) {
// TODO: 处理订单逻辑
System.out.println("trade finished");
} else if ("TRADE_SUCCESS".equals(tradeStatus)) {
// TODO: 处理订单逻辑
System.out.println("trade success");
}
response.getWriter().write("success");
} else {
response.getWriter().write("fail");
}
}
``` 完♨