第一时间获取精彩内容
官方文档
开放能力文档:
https://opendocs.alipay.com/open/203/107084
支付api
https://opendocs.alipay.com/apis/api_1/alipay.trade.wap.pay
开发准备工作创建应用
开发者使用支付宝账号登录开放平台(需实名认证的支付宝账号),根据实际需求创建应用(如“支付应用”)。
备注:创建应用时的应用状态为“开发中”,无法在线上正式调用接口。
填写应用基础信息添加应用功能配置应用环境应用申请上线,等待审核代码实现引入依赖<!–支付宝支,基础jdk–><dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.9.5.ALL</version></dependency>参数配置
application.yml
YmlParament
//支付宝相关@Value(“${ALIPAY.APP_ID}”)privateStringappId;@Value(“${ALIPAY.APP_PRIVATE_KEY}”)privateStringappPrivatekey;@Value(“${ALIPAY.APP_PUBLIC_KEY}”)privateStringappPublickey;@Value(“${ALIPAY.ALIPAY_PUBLIC_KEY}”)privateStringalipayPublickey;@Value(“${ALIPAY.SIGN_TYPE}”)privateStringsignType;@Value(“${ALIPAY.CHARSET}”)privateStringcharset;@Value(“${ALIPAY.FORMAT}”)privateStringformat;@Value(“${ALIPAY.SERVER_URL}”)privateStringserverUrl;@Value(“${ALIPAY.ALIPAY_NOTIFY_URL}”)privateStringalipayNotifyUrl;@Value(“${ALIPAY.RETURN_URL}”)privateStringreturnUrl;@Value(“${ALIPAY.API_VERSION}”)privateStringapiVersion;@Value(“${ALIPAY.PROD_CODE}”)privateStringprodCode;@Value(“${ALIPAY.TERMINAL_INFO}”)privateStringterminalInfo;@Value(“${ALIPAY.TERMINAL_TYPE}”)privateStringterminalType;初始化支付宝支付配置
AlipayConfig
@AutowiredprivateYmlParamentymlParament;/***设置支付宝客户端*@return*/@BeanpublicAlipayClientsetAlipayClient(){returnnewDefaultAlipayClient(ymlParament.getServerUrl(),ymlParament.getAppId(),ymlParament.getAppPrivatekey(),ymlParament.getFormat(),ymlParament.getCharset(),//这里不要搞错,这里是应用公钥而不是支付宝公钥ymlParament.getAppPublickey(),ymlParament.getSignType());}/***设置网页支付请求基础参数*@return*/@BeanpublicAlipayTradeWapPayRequestsetAlipayTradeWapPayRequest(){AlipayTradeWapPayRequestatwpr=newAlipayTradeWapPayRequest();atwpr.setApiVersion(ymlParament.getApiVersion());atwpr.setProdCode(ymlParament.getProdCode());//产品码atwpr.setTerminalInfo(ymlParament.getTerminalInfo());//终端信息atwpr.setTerminalType(ymlParament.getTerminalType());//终端信息类型returnatwpr;}手机网站支付
ZfbPay
/***手机网站支付**@paramac支付宝客户端,传入@AutowiredAlipayClient*@paramrquest基础参数,传入@AutowiredAlipayTradeWapPayRequest*@paramreturnUrl付款成功后跳转的页面*@paramnotifyUrl付款成功后通知的页面或方法*@parammode 实际参数,参考:https://opendocs.alipay.com/open/203/107090*/publicstaticAlipayTradeWapPayResponsewapPay(AlipayClientac,AlipayTradeWapPayRequestrquest,StringreturnUrl,StringnotifyUrl,AlipayTradeWapPayModelmode)throwsAlipayApiException{rquest.setBizModel(mode);rquest.setReturnUrl(returnUrl);rquest.setNotifyUrl(notifyUrl);returnac.pageExecute(rquest);}/***手机网站支付,处理业务参数*/publicstaticAlipayTradeWapPayResponsewapPay(AlipayClientac,AlipayTradeWapPayRequestrquest,StringreturnUrl,StringnotifyUrl,StringorderNo,Stringsubject,StringorderAmount)throwsAlipayApiException{AlipayTradeWapPayModelmode=newAlipayTradeWapPayModel();mode.setOutTradeNo(orderNo);mode.setSubject(subject);mode.setTotalAmount(orderAmount);returnwapPay(ac,rquest,returnUrl,notifyUrl,mode);}/***支付宝支付回调验证签名*验证签名;参考:https://opendocs.alipay.com/open/203/105286*@paramparams参数*@paramalipayPublickey传入支付宝公钥*@returntrue正确false失败*@throwsAlipayApiException*/publicstaticbooleanrsaCheckV1(Map<String,String>params,StringalipayPublickey)throwsAlipayApiException{returnAlipaySignature.rsaCheckV1(params,alipayPublickey,”utf-8″,”RSA2″);}
服务层(业务逻辑略)
@AutowiredprivateAlipayClientalipayClient;@AutowiredprivateAlipayTradeWapPayRequestalipayTradeWapPayRequest;@Transactional@OverridepublicAlipayTradeWapPayResponseinsertAlipayH5PayInfo(StringsendOrderNo,Stringsubject,StringorderAmount){try{AlipayTradeWapPayResponsealipayTradeWapPayResponse=ZfbPay.wapPay(alipayClient,alipayTradeWapPayRequest,ymlParament.getReturnUrl(),ymlParament.getAlipayNotifyUrl(),sendOrderNo,subject,orderAmount);/*获取页面成功*/if(!alipayTradeWapPayResponse.isSuccess()){thrownewRuntimeException(alipayTradeWapPayResponse.getMsg());}returnalipayTradeWapPayResponse;}catch(Exceptione){e.printStackTrace();}}
controller(略)
支付宝回调通知
官方文档:https://opendocs.alipay.com/open/203/105286
@ApiOperation(“支付宝回调”)@PostMapping(“callback”)publicStringcallback(HttpServletRequestrequest)throwsException{Objectobject=newObject();Map<String,String>params=convertRequestParamsToMap(request);log.info(“支付宝回调:已回调,参数为====” params);try{checkCallbackAlipay(params);/*处理一些业务*/}catch(Exceptione){e.printStackTrace();}returnZfbUtils.RETURN_SUCCESS;}}/***校验签名*验签文档:https://opendocs.alipay.com/open/203/105286*/privatevoidcheckCallbackAlipay(Map<String,String>params)throwsException{if(!”TRADE_SUCCESS”.equals(params.get(“trade_status”))){thrownewException(“支付宝支付失败!”);}//校验签名if(!ZfbPay.rsaCheckV1(params,ymlParament.getAlipayPublickey())){thrownewException(“支付宝回调签名认证失败”);}//校验一些业务逻辑//订单是否支付重复//判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额)//…..//校验通知中的seller_id(或者seller_email)是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email),//验证app_id是否为该商户本身。if(!params.get(“app_id”).equals(ymlParament.getAppId())){thrownewException(“APPID不对!”);}}/***获取支付宝回调参数*/privatestaticMap<String,String>convertRequestParamsToMap(HttpServletRequestrequest){Map<String,String>retMap=newHashMap<String,String>();Set<Map.Entry<String,String[]>>entrySet=request.getParameterMap().entrySet();for(Map.Entry<String,String[]>entry:entrySet){Stringname=entry.getKey();String[]values=entry.getValue();intvalLen=values.length;if(valLen==1){retMap.put(name,values[0]);}elseif(valLen>1){StringBuildersb=newStringBuilder();for(Stringval:values){sb.append(“,”).append(val);}retMap.put(name,sb.toString().substring(1));}else{retMap.put(name,””);}}returnretMap;}交易查询
若不想在支付回调时候去做签名校验,那我们可以主动去查询这笔订单是否支付成功