Java后台获取支付宝支付唯一订单号
/*** 支付宝小程序支付*/@PostMapping(value = "/xcxPayZFBTHREE")@ResponseBodypublic Map<String,Object> xcxPayZFBTHREE(@RequestBody byte[] req) {HashMap<String, Object> objectObjectMap = new HashMap<>();JSONObject json = JSONObject.parseObject(new String(req, Charset.forName("UTF-8")));System.out.println("请求参数"+json);String ip = "127.0.0.1";String money = null;String remark = null;AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",小程序appid,应用私钥,"json","UTF-8",支付宝公钥,"RSA2");//获得初始化的AlipayClient//设置请求参数AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();JSONObject map=new JSONObject();//订单号map.put("out_trade_no",订单号);//金额 这里的金额是以元为单位的可以不转换但必须是字符串map.put("total_amount",金额);//描述map.put("subject",remark);//用户唯一标识id 这里必须使用buyer_id 参考文档map.put("buyer_open_id",支付宝小程序的openid);//对象转化为json字符串String jsonStr=map.toString();//商户通过该接口进行交易的创建下单request.setBizContent(jsonStr);//回调地址 是能够访问到的域名加上方法名request.setNotifyUrl(回调地址);String orderStr=null;try {//使用的是executeAlipayTradeCreateResponse response = alipayClient.execute(request);orderStr=response.getTradeNo();System.out.println("支付返回结果订单号"+response.getTradeNo());} catch (Exception e) {e.printStackTrace();}objectObjectMap.put("status","0000");objectObjectMap.put("orderStr",orderStr);return objectObjectMap;}
支付宝小程序支付成功回调地址
/*** 异步通知-----支付宝小程序* 这里面可以写判断支付成功之后的及具体业务需求*/@RequestMapping(value = "/payNotifyZFBSIX", method = {RequestMethod.POST, RequestMethod.GET})@ResponseBodypublic String payNotifyZFBSIX(HttpServletRequest request) {System.out.println("支付通知=1" + request);/*** 这种方式效率高一点*///签名方式String sign_type="RSA2";//对待签名字符串数据通过&进行拆分Map requestParams = request.getParameterMap();LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();//把拆分数据放在map集合内for (Object o : requestParams.keySet()) {String name = (String) o;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] + ",";}map.put(name, valueStr);}System.out.println("支付通知=1" + map);try {
// boolean b = AlipaySignature.rsaCertCheckV1(map, publicKey, "utf-8", sign_type);String out_trade_no = map.get("out_trade_no");System.out.println("支付订单号:"+out_trade_no);String trade_status = map.get("trade_status");System.out.println("支付状态:"+out_trade_no);if("TRADE_SUCCESS".equals(trade_status)){}System.out.println(map.toString());} catch (Exception e) {System.out.println("支付宝异步验签失败:{}"+e.getMessage());}return null;}
支付宝小程序前端唤起支付窗口
pay(e){var that=this;my.tradePay({// orderStr: e.orderStr ,//支付请求参数字符串tradeNO : e //支付请求参数字符串}, function(result) {if (result.resultCode === '9000') {console.log('支付成功');} else if (result.resultCode === '6001') {console.log('用户取消支付');} else {console.log('支付失败');}// that.subMess()}, function(error) {//支付失败回调函数uni.showToast({title: '支付失败'});});},