1.集成支付寶sdk
com.alipay.sdk
alipay-sdk-java
3.7.26.ALL
2.創(chuàng)建一個(gè)輔助類,生成支付寶支付簽名與驗(yàn)證簽名
package com.demo.test.utils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
public class AlipayUtils{
//請求網(wǎng)關(guān)地址
private static final String GATEWAY_URL="https://openapi.alipay.com/gateway.do";
//appid
private static final String APP_ID="創(chuàng)建的應(yīng)用id";
//應(yīng)用私鑰pkcs8
private static final String APP_PRIVATE_KEY="應(yīng)用私鑰";
//支付寶公鑰
private static final String ALIPAY_PUBLIC_KEY="支付寶公鑰";//支付寶公鑰
/**
*支付寶訂單生成
*@paramorderno訂單號
*@paramtitle商品名稱
*@paramdesc商品描述
*@parampaymoney支付金額
*@paramnotifyUrl回調(diào)地址
*@return
*/
public String getOrderSignString(String orderno,String title,String desc,String paymoney,String notifyUrl){
//實(shí)例化客戶端
AlipayClientali payClient=new DefaultAlipayClient(GATEWAY_URL,APP_ID,APP_PRIVATE_KEY,"json","UTF-8",ALIPAY_PUBLIC_KEY,"RSA2");
//實(shí)例化具體API對應(yīng)的request類,類名稱和接口名稱對應(yīng),當(dāng)前調(diào)用接口名稱:alipay.trade.app.pay
AlipayTradeAppPayRequest request=new AlipayTradeAppPayRequest();
//SDK已經(jīng)封裝掉了公共參數(shù),這里只需要傳入業(yè)務(wù)參數(shù)。以下方法為sdk的model入?yún)⒎绞?model和biz_content同時(shí)存在的情況下取biz_content)。
AlipayTradeAppPayModel model=new AlipayTradeAppPayModel();
model.setBody(desc);
model.setSubject(title);
model.setOutTradeNo(orderno);
model.setTimeoutExpress("30m");
model.setTotalAmount(paymoney);
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
request.setNotifyUrl(notifyUrl);
try{
//這里和普通的接口調(diào)用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response=alipayClient.sdkExecute(request);
//System.out.println(response.getBody());//就是orderString可以直接給客戶端請求,無需再做處理。
return response.getBody();
}catch(AlipayApiExceptione){
e.printStackTrace();
System.out.print("生成簽名異常:"+e.getErrMsg());
}catch(Exceptionex){
ex.printStackTrace();
System.out.print("生成簽名異常01:"+ex.getMessage());
}
return"";
}
public boolean verifySign(MaprequestParams){
try{
//獲取支付寶POST過來反饋信息
Mapparams=new HashMap ();
//MaprequestParams=request.getParameterMap();
for(Iteratoriter=requestParams.keySet().iterator();iter.hasNext();){
Stringname=(String)iter.next();
String[]values=(String[])requestParams.get(name);
StringvalueStr="";
for(inti=0;ivalueStr=(i==values.length-1)?valueStr+values[i]
:valueStr+values[i]+",";
}
//亂碼解決,這段代碼在出現(xiàn)亂碼時(shí)使用。
//valueStr=newString(valueStr.getBytes("ISO-8859-1"),"utf-8");
params.put(name,valueStr);
}
//切記alipaypublickey是支付寶的公鑰,請去open.alipay.com對應(yīng)應(yīng)用下查看。
//booleanAlipaySignature.rsaCheckV1(Mapparams,StringpublicKey,Stringcharset,Stringsign_type)
booleanflag=AlipaySignature.rsaCheckV1(params,ALIPAY_PUBLIC_KEY,"UTF-8","RSA2");
returnflag;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
}
}
3.測試生成支付簽名,調(diào)用支付寶
AlipayUtils alipay=new AlipayUtils();
String payInfo=alipay.getOrderSignString("20191351245451","充值10元","錢包充值","10","http://localhost:8080/notify/alipay");
獲取的payInfo值可以app端調(diào)用支付寶進(jìn)行支付
4.支付寶回調(diào)驗(yàn)證
package com.demo.test.controller;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.demo.test.utils.AlipayUtils;
//支付寶回調(diào)
@RestController
@RequestMapping("/notify")
public class AlipayNotifyController{
private static final Logger log=LoggerFactory.getLogger(AlipayNotifyController.class);
@Resource
private HttpServletRequest request;
@ResponseBody
@RequestMapping(value="/alipay",method=RequestMethod.POST,produces=MediaType.TEXT_PLAIN_VALUE)
public String notify(@RequestBody String requestStr,@RequestParam Mapparams){
try{
log.info("支付寶回調(diào)請求內(nèi)容:"+requestStr);
AlipayUtils alipay=new AlipayUtils();
if(alipay.verifySign(request.getParameterMap())){
return"success";
}
return"fail";
}catch(Exceptione){
e.printStackTrace();
log.error("支付轉(zhuǎn)化失敗:{}",e);
return"fail";
}
}
}
(正文已結(jié)束)
推薦閱讀:人民信息網(wǎng)
免責(zé)聲明及提醒:此文內(nèi)容為本網(wǎng)所轉(zhuǎn)載企業(yè)宣傳資訊,該相關(guān)信息僅為宣傳及傳遞更多信息之目的,不代表本網(wǎng)站觀點(diǎn),文章真實(shí)性請瀏覽者慎重核實(shí)!任何投資加盟均有風(fēng)險(xiǎn),提醒廣大民眾投資需謹(jǐn)慎!