| | |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.crypto.BadPaddingException; |
| | |
| | | " }," + |
| | | " \"extend_params\":{" + |
| | | " \"royalty_freeze\":\"true\"" + |
| | | " }" + |
| | | "}"); |
| | | AlipayTradeSettleConfirmResponse response = null; |
| | | try { |
| | | response = alipayClient.execute(request); |
| | | } catch (AlipayApiException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(response.isSuccess()){ |
| | | System.out.println("调用成功"); |
| | | return ResultUtil.success(); |
| | | } else { |
| | | System.out.println("调用失败"); |
| | | return ResultUtil.error("出现问题啦"); |
| | | } |
| | | } |
| | | // 属于平台的运营商 因为无需分账不冻结资金 |
| | | public ResultUtil confirm1(String smid,String code, String outTradeNo, String amount) { |
| | | AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", |
| | | aliAppid, |
| | | appPrivateKey, |
| | | "json", |
| | | "GBK", |
| | | alipay_public_key, |
| | | "RSA2"); |
| | | AlipayTradeSettleConfirmRequest request = new AlipayTradeSettleConfirmRequest(); |
| | | request.setBizContent("{" + |
| | | " \"out_request_no\":\""+code+"\"," + |
| | | " \"trade_no\":\""+outTradeNo+"\"," + |
| | | " \"settle_info\":{" + |
| | | " \"settle_detail_infos\":[" + |
| | | " {" + |
| | | " \"trans_in_type\":\"defaultSettle\"," + |
| | | " \"settle_entity_id\":\""+smid+"\"," + |
| | | " \"settle_entity_type\":\"SecondMerchant\"," + |
| | | " \"amount\":"+amount+"," + |
| | | " }" + |
| | | " ]" + |
| | | " }," + |
| | | " \"extend_params\":{" + |
| | | " \"royalty_freeze\":\"false\"" + |
| | | " }" + |
| | | "}"); |
| | | AlipayTradeSettleConfirmResponse response = null; |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | JSONObject alipay_trade_precreate_response = JSON.parseObject(response.getBody()).getJSONObject("alipay_trade_precreate_response"); |
| | | |
| | | System.err.print(alipay_trade_precreate_response.getString("qr_code")); |
| | | return ResultUtil.success(alipay_trade_precreate_response.getString("qr_code")); |
| | | } |
| | |
| | | map.put("appid", appid); |
| | | map.put("mch_id", mchId); |
| | | map.put("nonce_str", nonce_str); |
| | | map.put("body", body); |
| | | String temp = ""; |
| | | if (body.split("-").length>1){ |
| | | temp = body.split("-")[1]; |
| | | map.put("body", body.split("-")[0]); |
| | | }else{ |
| | | map.put("body", body); |
| | | } |
| | | if (StringUtils.hasLength(temp) && temp.equals("1")){ |
| | | // 添加分账标识 |
| | | map.put("profit_sharing", "Y"); |
| | | } |
| | | map.put("attach", attach);//存储订单id |
| | | map.put("out_trade_no", out_trade_no);//存储的订单code |
| | | map.put("total_fee", i); |
| | |
| | | return ResultUtil.error(map1.get("return_msg"), new JSONObject()); |
| | | } |
| | | } |
| | | /** |
| | | * 发起分账 |
| | | * @param order 微信订单号 |
| | | * @return |
| | | */ |
| | | public ResultUtil fenzhang(String order,BigDecimal amount,String merchantNumber,String nonce_str) throws Exception { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("mch_id", mchId); |
| | | map.put("appid", appid); |
| | | map.put("nonce_str", nonce_str); |
| | | map.put("transaction_id", order); |
| | | // 将这个字符串使用json格式拼接起来 |
| | | Map<String, Object> body = new HashMap<>(); |
| | | body.put("type", "MERCHANT_ID"); |
| | | body.put("account", merchantNumber); |
| | | body.put("amount", amount); |
| | | body.put("description", "订单分账"); |
| | | JSONObject jsonObject = new JSONObject(body); |
| | | String jsonString = jsonObject.toString(); |
| | | map.put("receiver",jsonString); |
| | | String s = this.weixinSignature(map); |
| | | map.put("sign", s); |
| | | String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing"; |
| | | //设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_XML); |
| | | StringBuffer xmlString = new StringBuffer(); |
| | | Set<String> strings = map.keySet(); |
| | | String[] keys = {}; |
| | | keys = strings.toArray(keys); |
| | | Arrays.sort(keys); |
| | | xmlString.append("<xml>"); |
| | | for (int l = 0; l < keys.length; l++) { |
| | | xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">"); |
| | | } |
| | | xmlString.append("</xml>"); |
| | | |
| | | Map<String, String> map1 = null; |
| | | String body1 = null; |
| | | try { |
| | | body1 = HttpClientUtil.pushHttpsRequsetXml(url, xmlString.toString(), new HashMap<>(), mchId, certPath, "PKCS12"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.err.println("分账请求"+body1); |
| | | //将结果xml解析成map |
| | | body1 = body1.replaceAll("<!\\[CDATA\\[", ""); |
| | | body1 = body1.replaceAll("]]>", ""); |
| | | try { |
| | | map1 = this.xmlToMap(body1, "UTF-8"); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (DocumentException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | String return_code = map1.get("return_code"); |
| | | if ("SUCCESS".equals(return_code)) { |
| | | String result_code = map1.get("result_code"); |
| | | if ("SUCCESS".equals(result_code)) { |
| | | return ResultUtil.success(); |
| | | } else { |
| | | // System.err.println(map1.get("err_code_des")); |
| | | return ResultUtil.error(map1.get("err_code_des")); |
| | | } |
| | | } else { |
| | | // System.err.println(map1.get("return_msg") + appid + "----" + mchId); |
| | | return ResultUtil.error(map1.get("return_msg"), new JSONObject()); |
| | | } |
| | | } |
| | | /** |
| | | * 发起分账回退 |
| | | * @param order 微信订单号 |
| | | * @return |
| | | */ |
| | | public ResultUtil fenzhangRefund(String order,BigDecimal amount,String merchantNumber, |
| | | String nonce_str, |
| | | String fenzhangRefundNo) throws Exception { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("mch_id", mchId); |
| | | map.put("appid", appid); |
| | | map.put("nonce_str", nonce_str); |
| | | map.put("order_id", order); |
| | | map.put("out_return_no", fenzhangRefundNo); |
| | | map.put("return_account_type", "MERCHANT_ID"); |
| | | map.put("return_account", merchantNumber); |
| | | map.put("return_amount", amount); |
| | | map.put("description", "用户退款"); |
| | | String s = this.weixinSignature(map); |
| | | map.put("sign", s); |
| | | String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingreturn"; |
| | | //设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_XML); |
| | | StringBuffer xmlString = new StringBuffer(); |
| | | Set<String> strings = map.keySet(); |
| | | String[] keys = {}; |
| | | keys = strings.toArray(keys); |
| | | Arrays.sort(keys); |
| | | xmlString.append("<xml>"); |
| | | for (int l = 0; l < keys.length; l++) { |
| | | xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">"); |
| | | } |
| | | xmlString.append("</xml>"); |
| | | |
| | | Map<String, String> map1 = null; |
| | | String body1 = null; |
| | | try { |
| | | body1 = HttpClientUtil.pushHttpsRequsetXml(url, xmlString.toString(), new HashMap<>(), mchId, certPath, "PKCS12"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.err.println("分账金额回退"+body1); |
| | | //将结果xml解析成map |
| | | body1 = body1.replaceAll("<!\\[CDATA\\[", ""); |
| | | body1 = body1.replaceAll("]]>", ""); |
| | | try { |
| | | map1 = this.xmlToMap(body1, "UTF-8"); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (DocumentException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | String return_code = map1.get("return_code"); |
| | | if ("SUCCESS".equals(return_code)) { |
| | | String result_code = map1.get("result_code"); |
| | | if ("SUCCESS".equals(result_code)) { |
| | | return ResultUtil.success(map1.get("return_no")); |
| | | } else { |
| | | // System.err.println(map1.get("err_code_des")); |
| | | return ResultUtil.error(map1.get("err_code_des")); |
| | | } |
| | | } else { |
| | | // System.err.println(map1.get("return_msg") + appid + "----" + mchId); |
| | | return ResultUtil.error(map1.get("return_msg"), new JSONObject()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 微信支付成功后的回调处理 |