场景
调用支付宝API,生成支付链接,或者是form表单,返回给前端页面,这里是直接生成的支付链接
准备
Jar包依赖
<!-- AliPay Old --> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.12.4.ALL</version> </dependency>
|
方法
public static String createPayUrl(String orderNumber, String titleName, String amout, String orderDesc) { String URL = "支付宝网关"
String APP_ID = "改成你的AppID"
String APP_PRIVATE_KEY = "改成你的应用私钥";
String ALIPAY_PUBLIC_KEY = "改成你的应用公钥";
AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2"); AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); AlipayTradePagePayModel model = new AlipayTradePagePayModel(); model.setOutTradeNo(orderNumber);
model.setProductCode("FAST_INSTANT_TRADE_PAY");
model.setSubject(titleName);
model.setTotalAmount(amout);
model.setBody(orderDesc);
alipayRequest.setBizModel(model); String returnUrl = "可以是一个自己的外网可以访问的页面,也可以为空,用于支付完成后页面跳转";
alipayRequest.setReturnUrl(returnUrl); String notifyUrl = "异步通知地址";
alipayRequest.setNotifyUrl(notifyUrl); String form = null; try {
form = alipayClient.pageExecute(alipayRequest, "GET").getBody(); } catch (AlipayApiException e) { e.printStackTrace(); }
return form; }
|