主流程如下:
1、建支付寶賬號
2、申請權限功能(APP支付、開發權限等)
3、用阿里給的工具生成密鑰和證書,並且上傳到阿里,然後他會生成三個文件(支付寶公鑰、ROOTCERT、公鑰證書)
4、最後就是代碼(這裏也用阿里封裝好的代碼,加簽解籤)
1、創建企業用戶且登錄
https://auth.alipay.com/login/index.htm
2、選擇你想要開發的功能(比如APP支付)
3、這裏會有相應的文檔
https://b.alipay.com/signing/productDetailV2.htm?productId=I1011000290000001002
這裏主要看接入指引和申請條件
參考一下申請條件,看看自己是否符合。一般都沒啥問題
1)然後點擊”立即接入" 企業證書什麼的填寫好,經過審批(大概一個工作日)
2)入駐成爲開發者——又是一系列傻瓜操作
3)基本進入開發
4、開發流程
創建APPID——配置密鑰——開發(加簽、驗籤)
第一次做,總會有點迷糊,這裏最麻煩就是配置密鑰
https://open.alipay.com/platform/keyManage.htm
這是主要是配置接口加簽方式,點擊加簽方式(我這裏已經配置好了,所以有一些不一樣)
流程是,下載支付寶提供的密鑰(證書)軟件——生成密鑰——上傳生成好的密鑰文件——下載支付寶公鑰、RootCert、自己的公鑰證書,操作如下:
1)點擊查看接口文檔
https://docs.open.alipay.com/291/105971#Krqvg
其實文檔說得很明白了,我這裏簡單說明
密鑰長度選擇RSA2(更安全)
密鑰格式PKCS8(JAVA適用)
點擊生成密鑰
點擊獲取CSR文件 (點擊後在硬盤將生成csr文件和兩個密鑰,csr用於上傳到支付寶)
點擊打開文件位置,查看自己生成的文件
5、回到網站配置密鑰
選擇公鑰證書——上傳剛剛生成的CSR文件
此時,這裏有三個下載證書連接,全部下載下來,放到硬盤中,開發需要用到
6、進入代碼開發
https://docs.open.alipay.com/204/105297/ 快速接入文檔
我們可以看到流程,按照一個電商平臺的流程,我理解如下
1、用戶選擇好商品後,提交
2、服務端生成訂單返回(這裏還沒關聯到支付寶)
3、用戶選擇支付方式(這裏選支付寶),點擊支付按鈕(發送訂單信息到服務器)
4、將訂單生成支付需要的信息(拼參數,加密,加簽),返回到前端
5、前端調支付寶封裝好的接口,將參數傳遞(這時候會跳轉到支付寶支付界面,後面用戶在支付寶支付後就完事兒了)
6、支付寶返回到用戶界面。
7、同時後臺會異步收到交易信息
8、有時候,我們也會發起查詢,主動獲取支付狀態
後臺代碼操作如下:
點擊 開放平臺服務端DEMO&SDK
點擊MAVEN項目依賴,這個JAR包裏,可以用到支付寶提供的加簽解籤接口。相當方便
配置在pom.xml項目中
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.9.13.ALL</version>
</dependency>
這裏我們先做支付接口(支付寶提供了兩種方式,一種不用證書,一種用證書方式。我們肯定是需要用證書的)
剛剛進入的網頁,拉到下邊,就是證書的DEMO
我的參考代碼如下
private AlipayClient constractCertAlipayClient() throws AlipayApiException {
//構造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//設置網關地址
certAlipayRequest.setServerUrl(ServerUrl);
//設置應用Id
certAlipayRequest.setAppId(appid);
//設置應用私鑰
certAlipayRequest.setPrivateKey(appprivatekey);
//設置請求格式,固定值json
certAlipayRequest.setFormat(format);
//設置字符集
certAlipayRequest.setCharset(charset);
//設置簽名類型
certAlipayRequest.setSignType(signType);
//設置應用公鑰證書路徑
certAlipayRequest.setCertPath(certpath);
//設置支付寶公鑰證書路徑
certAlipayRequest.setAlipayPublicCertPath(alipaypubliccertpath);
//設置支付寶根證書路徑
certAlipayRequest.setRootCertPath(rootcertpath);
//構造client
return new DefaultAlipayClient(certAlipayRequest);
}
@Override
public String paymentWithApp(Orders orders) throws AlipayApiException {
String returnStr = null;
AlipayClient alipayClient = this.constractCertAlipayClient();
//實例化具體API對應的request類,類名稱和接口名稱對應,當前調用接口名稱:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已經封裝掉了公共參數,這裏只需要傳入業務參數。以下方法爲sdk的model入參方式(model和biz_content同時存在的情況下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(orders.getDescription());
model.setSubject(orders.getDescription());
model.setOutTradeNo(String.valueOf(orders.getId()));
model.setTimeoutExpress(timeoutExpress);
model.setTotalAmount(String.valueOf(orders.getAmount()));
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
request.setNotifyUrl(otifyUrl);
try {
//這裏和普通的接口調用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
System.out.println(response.getBody());//就是orderString 可以直接給客戶端請求,無需再做處理。
returnStr = response.getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
}
return returnStr;
}
這裏主要就是把剛剛的證書,傳給支付寶,調用他們提供的接口,就會自動加簽。接口返回的字符串可以直接返回給前端
otifyUrl是支付保返回時候,你接收的地方接口
/**
* 把request轉爲map
*
* @param request
* @return
*/
private Map<String, String> getParameterMap(HttpServletRequest request) {
// 參數Map
//獲取支付寶POST過來反饋信息
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] + ",";
}
//亂碼解決,這段代碼在出現亂碼時使用。
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
return params;
}
@Override
public Map<String, String> callBack(HttpServletRequest request) throws AlipayApiException {
Map<String, String> paramMap = this.getParameterMap(request);
log.info("請求回調 ========> " + JSONObject.toJSON(paramMap).toString());
boolean signVerified = AlipaySignature.rsaCertCheckV1(paramMap, alipaypubliccertpath, charset, "RSA2");//調用SDK驗證簽名
if(signVerified) {
return paramMap;
} else {
return null;
}
}
AlipaySignature.rsaCertCheckV1 這個阿里提供的解籤,調用一下,就搞定了。整個流程基本如些
查詢接口參考如下:
@Override
public String queryOrder(String orgOrderId, Integer orderId) throws AlipayApiException {
//構造client
AlipayClient alipayClient = this.constractCertAlipayClient();
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();//創建API對應的request類
JSONObject jsonObject = new JSONObject();
jsonObject.put("out_trade_no", orderId);
jsonObject.put("trade_no", orgOrderId);
request.setBizContent(jsonObject.toJSONString());
AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);//通過alipayClient調用API,獲得對應的response類
System.out.print(response.getBody());
log.info("查詢返回信息 ========> " + response.getBody());
String returnStr = response.getBody();
return returnStr;
}
退款接口參考如下:
@Override
public Boolean refundOrder(String orgOrderId, Integer orderId, BigDecimal refundAmount) throws AlipayApiException {
AlipayClient alipayClient = this.constractCertAlipayClient();
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
JSONObject jsonObject = new JSONObject();
jsonObject.put("out_trade_no", String.valueOf(orderId));
jsonObject.put("trade_no", orgOrderId);
jsonObject.put("refund_amount", String.format("%.2f", refundAmount.doubleValue()));
request.setBizContent(jsonObject.toJSONString());
AlipayTradeRefundResponse response = alipayClient.certificateExecute(request);
if(response.isSuccess()){
System.out.println("調用成功");
} else {
System.out.println("調用失敗");
}
return true;
}
(大家看這幾個接口,他們的request分別如下:AlipayTradeAppPayRequest 、AlipayTradeQueryRequest 、AlipayTradeRefundRequest 。阿里就是從這個傳弟的參數,判斷調用的接口)
其它接口大致如上,我們可以點擊
https://docs.open.alipay.com/204/105303/
去開發自己需要接口去吧,孩子們!