Java 支付宝之APP支付服务端 (二)
来源:互联网 发布:照片打印软件免费版 编辑:程序博客网 时间:2024/05/17 02:59
Java 支付宝之APP支付服务端 (二)
Entrance 支付宝的API文档: https://docs.open.alipay.com/api
在上一篇中,集成第三方支付的流程:下单,异步通知,查询,还涉及到提现,退款,关闭订单等,我们以后
在介绍。目前先介绍前三个。
一、微信统一下单请求交易
/** * 支付宝请求交易 * * @param bean * @return */@RequestMapping(value=Route.Payment.ALI_PAY,method=RequestMethod.POST)@ResponseBodypublic Response pay(@PathVariable("userId") int userId,@PathVariable("amount") String amount) {log.info("infoMsg:--- 支付宝请求交易开始");Response resp = this.getReponse();try {//----------------请求参数------------------//ThirdPayBean payBean = thirdPayService.findByPayId(18);Assert.notNull(payBean);String notify_url = payBean.getReturn_url();String APP_ID = payBean.getMer_no();//支付宝商戶号String mer_key = payBean.getMer_key();//支付宝公钥|商户私钥String public_key = mer_key.split("|")[0];String private_key = mer_key.split("|")[1];String ALIPAY_PUBLIC_KEY = MD5_UTIL.convertMD5(MD5_UTIL.convertMD5(public_key));String APP_PRIVATE_KEY = MD5_UTIL.convertMD5(MD5_UTIL.convertMD5(private_key));String passbackParams = userId + "|" + "18";//拓展参数//实例化客户端AlipayClient alipayClient = new DefaultAlipayClient(alipay_Url, APP_ID, APP_PRIVATE_KEY, "JSON", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2");//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.payAlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();model.setSubject("循心科技");model.setOutTradeNo(OrderGeneratedUtils.getOrderNo());model.setTimeoutExpress("30m");model.setTotalAmount(amount);model.setProductCode("QUICK_MSECURITY_PAY");model.setPassbackParams(passbackParams);request.setBizModel(model);request.setNotifyUrl(notify_url); //这里和普通的接口调用不同,使用的是sdkExecute AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);// System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。 //生成账单 boolean result = rechargeRecordService.generatedBills(response.getBody(),passbackParams); if(!result) { log.error("订单生成失败"); } log.info("infoMsg:--- 支付宝请求交易结束"); return resp.success(response.getBody()); } catch (AlipayApiException e) { log.error("errorMsg:--- 支付宝请求交易失败" + e.getMessage()); return resp.failure(e.getMessage());}}
二、支付宝支付通知地址
/** * 支付宝支付通知地址 * * @param request * @return */@RequestMapping(value=Route.Payment.ALI_PAY_NOTIFY,method=RequestMethod.POST)@ResponseBodypublic Response alipay_notify(HttpServletRequest request) {log.info("infoMsg:--- 支付宝验证异步通知信息开始");Response resp = this.getReponse();try {//----------------请求参数------------------//ThirdPayBean payBean = thirdPayService.findByPayId(18);Assert.notNull(payBean);String mer_key = payBean.getMer_key();//支付宝公钥|商户私钥String public_key = mer_key.split("|")[0];String ALIPAY_PUBLIC_KEY = MD5_UTIL.convertMD5(MD5_UTIL.convertMD5(public_key));//获取支付宝POST过来反馈信息Mapparams = new HashMap ();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);}//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。//boolean AlipaySignature.rsaCheckV1(Map params, String publicKey, String charset, String sign_type)boolean flag = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, "utf-8","RSA2");if(flag) {//TODO 账变,修改状态,到账提醒Double amount = Double.parseDouble(params.get("amount"));String passbackParams = params.get("passbackParams");String order_no = params.get("out_trade_no");boolean result = rechargeRecordService.updateBill(amount,passbackParams,order_no);if(result) {System.out.println("订单更新成功");}}log.info("infoMsg:--- 支付宝验证异步通知信息结束");return resp.success();} catch (AlipayApiException e) {log.info("infoMsg:--- 支付宝验证异步通知信息结束" + e.getMessage());return resp.failure(e.getMessage());}}
三、支付宝单笔提现
/** * 支付宝单笔提现 * * @param userId * @param amount * @return */@RequestMapping(value=Router.Payment.ALI_PAY_TRANSFER,method=RequestMethod.POST)@ResponseBodypublic Response alipay_transfer(@PathVariable("userId") int userId,@PathVariable("amount") String amount) {String message = "";log.info("infoMsg:--- 支付宝提现开始");Response resp = this.getReponse();try {//----------------请求参数------------------//ThirdPayBean payBean = thirdPayService.findByPayId(18);Assert.notNull(payBean);String APP_ID = payBean.getMer_no();//支付宝商戶号String mer_key = payBean.getMer_key();//支付宝公钥|商户私钥String public_key = mer_key.split("|")[0];String private_key = mer_key.split("|")[1];String ALIPAY_PUBLIC_KEY = MD5_UTIL.convertMD5(MD5_UTIL.convertMD5(public_key));String APP_PRIVATE_KEY = MD5_UTIL.convertMD5(MD5_UTIL.convertMD5(private_key));String passbackParams = userId + "|" + "18";//拓展参数String orderNo = OrderGeneratedUtils.getOrderNo();AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",APP_ID,APP_PRIVATE_KEY,"JSON","utf-8",ALIPAY_PUBLIC_KEY,"RSA2");AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();//转账参数拼装 TODOmodel.setSubject("循心科技");model.setOutTradeNo(orderNo);model.setTimeoutExpress("30m");model.setProductCode("QUICK_MSECURITY_PAY");request.setBizModel(model);AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);String body = response.getBody();//解析返回值message = rechargeRecordService.createTransfer(body,passbackParams);if(message.equalsIgnoreCase("Success")) {boolean result = transferRecordService.saveTransRecord(body,amount,passbackParams);if(result) {log.info("infoMsg:--- 支付宝提现成功");}}else {log.error("errorMsg:--- 支付宝提现失败" + message);}log.info("infoMsg:--- 支付宝提现结束");return resp.success();} catch (AlipayApiException e) {log.error("errorMsg:--- 支付宝提现失败" + e.getMessage());return resp.failure(message + e.getMessage());}}
四、支付宝订单查询
/** * 支付宝支付订单查询 * * @param request * @return */@RequestMapping(value=Route.Payment.ALI_PAY_QUERY,method=RequestMethod.POST)@ResponseBodypublic Response alipay_query(HttpServletRequest req) {log.info("infoMsg:--- 支付宝订单查询开始");Response resp = this.getReponse();try {AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();request.setBizContent("{" +"\"out_trade_no\":\"20150320010101001\"," +"\"trade_no\":\"2014112611001004680 073956707\"" +" }");AlipayTradeQueryResponse response;response = alipayClient.execute(request);if(response.isSuccess()){System.out.println("调用成功");} else {System.out.println("调用失败");}log.info("infoMsg:--- 支付宝订单查询结束");return resp.success();} catch (AlipayApiException e) {log.error("errorMsg:--- 支付宝订单查询失败");return resp.failure(e.getMessage());}}
ps: 总结,作为IT巨头,支付宝的文档总结得是很给力的。在开放平台下载相关jar宝 友情链
接:https://docs.open.alipay.com/54/103419/
About Me:
- Github地址:https://github.com/noseparte
- Email: noseparte@aliyun.com 有java与hadoop相关的技术问题,可以发私信与我交流。
- NPM地址: https://www.npmjs.com/~noseparte
- WebSite: http://www.noseparte.com/ Copyright © 2017 noseparte
阅读全文
0 0
- Java 支付宝之APP支付服务端 (二)
- 支付宝app支付java服务端
- 支付宝APP支付Java服务端
- 支付宝支付(网站支付,APP支付,扫码支付) Java服务端代码
- Java 微信支付之APP支付服务端 (一)
- APP服务端支付宝支付(PHP服务端)
- java服务端–支付宝APP支付接口
- java服务端–支付宝APP支付接口
- 支付宝APP支付集成,服务端(JAVA)
- java支付宝和微信app支付(服务端处理)
- java服务端–支付宝APP支付接口
- 服务端支付宝App支付对接笔记
- APP 支付宝支付,服务端处理
- php 支付宝App支付服务端操作
- 支付宝服务端-App支付-记录
- php 服务端集成支付宝APP支付
- java服务端和android支付宝支付
- java服务端,支付宝支付代码笔记
- el表达式设置option标签selected
- pycharm template
- 详解阿里海报设计AI“鲁班”,没错,人类设计师危险了
- vue.js 组件之间传递数据
- Log4j2的配置及使用方式
- Java 支付宝之APP支付服务端 (二)
- Unity 游戏框架搭建 (三) MonoBehaviour单例的模板
- html 限制输入
- Maven
- 在动态插入Easyui datebox控件时,使用$.parser.parse()手工渲染时,解决日期控件原有的值被清除问题
- 第1章 Git入门
- SpringMVC处理异步请求
- python3.4 字符串转16进制
- Spring Cache抽象详解