1、在支付寶開放平臺(open.alipay.com/platform/ho…
找到頁或者移動應用列表
你可以看到你已申請的應用
點選進入應用詳情增加功能:
2、在自己的javaweb應用裡引入對應的jar包
<!--阿里支付SDK-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.3.0.ALL</version>
</dependency>
複製程式碼
3、支付寶相關配置資訊
可根據自己的情況配置相關的內容,下面只作為相關示意
package com.fast.tpp.dto;
public class AlipayConfig {
/**
* 簽約的支付寶賬號對應的支付寶唯一使用者號,以2088開頭的16位純數字組成
*/
public static final String PID = "xx";
/**
* 正式環境請求地址
*/
public static String ALIPAY_URL = "https://openapi.alipay.com/gateway.do";
/**
* 支付寶分配給開發者的應用ID
*
*/
public static String APP_ID = "xx";
/**
* 僅支援JSON
*/
public static String FORMAT = "JSON";
/**
* 請求使用的編碼格式
*/
public static String CHARSET = "utf-8";
/**
* 商戶生成簽名字串所使用的簽名演算法型別
*/
public static String SIGN_TYPE = "RSA2";
/**
* 支付寶公鑰
*/
public static String ALIPAY_PUBLIC_KEY =xx";
/**
* 商戶私鑰
*/
public static String PRIVATE_KEY =xx";
/**
* 回撥地址
*/
public static String CALLBACK_URL = "xx";
/**
* web支付跳轉地址
*/
public static String WEB_RETURN_URL = "xx";
}
複製程式碼
4、初始化阿里支付相關配置,只貼部分程式碼作參考
@Service
@Log4j2
public class AlipaySaoImpl implements AlipaySao {
@Autowired
private RedisService redisService;
// 例項化客戶端
AlipayClient alipayClient;
public AlipaySaoImpl(){
// 例項化客戶端
alipayClient = new DefaultAlipayClient(
AlipayConfig.ALIPAY_URL,
AlipayConfig.APP_ID,
AlipayConfig.PRIVATE_KEY,
AlipayConfig.FORMAT,
AlipayConfig.CHARSET,
AlipayConfig.ALIPAY_PUBLIC_KEY,
AlipayConfig.SIGN_TYPE);
}
複製程式碼
5、web支付相關方法程式碼
package com.fast.tpp.dto;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
public class AlipayWebPayDTO
{
/**
* 商戶訂單號,必填
*
*/
private String out_trade_no;
/**
* 訂單名稱,必填
*/
private String subject;
/**
* 付款金額,必填
* 根據支付寶介面協議,必須使用下劃線
*/
private String total_amount;
/**
* 商品描述,可空
*/
private String body;
/**
* 超時時間引數
*/
private String timeout_express= "5m";
/**
* 產品編號
*/
private String product_code= "FAST_INSTANT_TRADE_PAY";
}
/**
* web支付介面
* @param alipayBean
* @return
* @throws AlipayApiException
*/
@Override
public String webPagePay(AlipayWebPayDTO alipayBean) throws AlipayApiException {
// 2、設定請求引數
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
// 頁面跳轉同步通知頁面路徑
alipayRequest.setReturnUrl(AlipayConfig.WEB_RETURN_URL);
// 伺服器非同步通知頁面路徑
alipayRequest.setNotifyUrl(AlipayConfig.CALLBACK_URL);
// 封裝引數
alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
// 3、請求支付寶進行付款,並獲取支付結果
String result = alipayClient.pageExecute(alipayRequest).getBody();
// 返回付款資訊
return result;
}
複製程式碼
6、最終返回給客戶端的result值為,裡面是一個form,頁面拿到後會自動跳轉到支付寶的訂單支付頁面
{
"code": "00",
"msg": "請求成功!",
"data": {
"orderString": "<form name=\"punchout_form\" method=\"post\" action=\"https://openapi.alipay.com/gateway.do?charset=utf-8&method=alipay.trade.page.pay&sign=G0jsaTORXtfF1VkQEK77qu%2BnYRU1YhCNQMFiWNNBUbYgQz8pxGRGVcnzbIXWkgWawV3AXco%2BJkFEemhV7qK2ALIn6uVziNklV6Folqb6OyVnsdKdCfHbID%2Bz79M1ngpyWsWvxXoAUN6Kuib5qkurDPQfMo4HWF4fxmLF70cFkQNeHxg577ByHS0%2Borv%2BzJ%2FwqehZZqOB1JQ0jzserODf2PGieoE5PCDnIYuXMZFqJqlCRUDc1IN3h9Usez8QJhLWepcFDsDR8tXiHrMmge9onHAl9bpLUtyOuVe4TCMU%2BLgMH21NUpeKJdXgQpnQxA8DoUuU0lRTmvPEp6EyXAmE%2FQ%3D%3D&return_url=http%3A%2F%2Fwww.moyou520.com%2Fdist%2Fhtml%2Fpay%2Fresult.html¬ify_url=http%3A%2F%2Fadmin.moyou520.com%2Fmoyou%2Fapp%2Ftrans%2FalipayNotice.do&version=1.0&app_id=2019081066129967&sign_type=RSA2×tamp=2020-02-17+15%3A58%3A24&alipay_sdk=alipay-sdk-java-4.3.0.ALL&format=JSON\">\n<input type=\"hidden\" name=\"biz_content\" value=\"{"body":"購買[年度VIP]","out_trade_no":"20200217155823617499935","product_code":"FAST_INSTANT_TRADE_PAY","subject":"默友APP","timeout_express":"5m","total_amount":"0.0"}\">\n<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >\n</form>\n<script>document.forms[0].submit();</script>",
"orderNo": "20200217155823617499935"
}
}
複製程式碼
7、回撥方法相關程式碼
/**
* 支付寶支付通知
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping(value="/alipayNotice.do",method = RequestMethod.POST)
@ResponseBody
@NoneAuth
@SysLog(actionName = "支付寶支付通知")
public String alipayNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, String> params = new HashMap<String, String>();
Map requestParams = request.getParameterMap();
for (Iterator 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);
}
// 切記alipaypublickey是支付寶的公鑰,請去open.alipay.com對應應用下檢視。
try {
boolean flag = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET,
AlipayConfig.SIGN_TYPE);
if (flag) {
String tradeStatus = params.get("trade_status");
String outTradeNo = params.get("out_trade_no");
String tradeNo = params.get("trade_no");
//購買者使用者ID
String openId = params.get("buyer_logon_id");
this.orderService.alipayNotice(tradeStatus,outTradeNo,tradeNo,openId);
if ("TRADE_SUCCESS".equals(tradeStatus)){
return "success";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
}
複製程式碼
大致如上,歡迎大家批評指正