支付寶參數配置:html
public class AlipayConfig { // 商戶appid public static String APPID = ""; // 私鑰 pkcs8格式的 public static String RSA_PRIVATE_KEY = ""; // 服務器異步通知頁面路徑 需http://或者https://格式的完整路徑,不能加?id=123這類自定義參數,必須外網能夠正常訪問 public static String notify_url = "http://java.app88.cn/hmzmysql/app/alipay/wappay/notify_url.do"; //支付寶回調方法 // 頁面跳轉同步通知頁面路徑 需http://或者https://格式的完整路徑,不能加?id=123這類自定義參數,必須外網能夠正常訪問 // 商戶能夠自定義同步跳轉地址(app支付不須要) public static String return_url = "http://java.app88.cn/hmzmysql/app/alipay/wappay/return_url.do"; // 請求網關地址 public static String URL = "https://openapi.alipaydev.com/gateway.do"; // 編碼 public static String CHARSET = "UTF-8"; // 返回格式 public static String FORMAT = "json"; // 支付寶公鑰 public static String ALIPAY_PUBLIC_KEY = ""; // 日誌記錄目錄 public static String log_path = "/log"; // RSA2 public static String SIGNTYPE = "RSA2"; }
支付寶接口:java
public class AlipayInterfaceInvokeUtil { /** * 單筆轉帳到支付寶帳戶 * * @param out_biz_no * 商戶轉帳惟一訂單號 * @param payee_type * 收款方帳戶類型。可取值:一、ALIPAY_USERID:支付寶帳號對應的支付寶惟一用戶號。以2088開頭的16位純數字組成。二、ALIPAY_LOGONID:支付寶登陸號,支持郵箱和手機號格式。 * @param payee_account * 收款方帳戶 * @param amount * 轉帳金額,單位:元。只支持2位小數,小數點前最大支持13位,金額必須大於等於0.1元。 * @param payer_show_name * 付款方顯示姓名 * @param payee_real_name * 收款方真實姓名 * @param remark * 轉帳備註 * @return 是否轉帳成功 * @throws AlipayApiException */ public static boolean AlipayFundTransToaccountTransfer(String out_biz_no, String payee_type, String payee_account, String amount, String payer_show_name, String payee_real_name, String remark) throws AlipayApiException { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest(); Map<String, String> content = new HashMap<String, String>(); content.put("out_biz_no", out_biz_no); content.put("payee_type", payee_type); content.put("payee_account", payee_account); content.put("amount", amount); content.put("payer_show_name", payer_show_name); content.put("payee_real_name", payee_real_name); content.put("remark", remark); request.setBizContent(BaseUtil.getJson(content)); AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request); if (response.isSuccess()) { System.out.println("調用成功"); } else { System.out.println("調用失敗"); } return response.isSuccess(); } /** * 生成APP支付訂單信息 * * @param body * 對一筆交易的具體描述信息。若是是多種商品,請將商品描述字符串累加傳給body。 * @param subject * 商品的標題/交易標題/訂單標題/訂單關鍵字等。 * @param outtradeno * 商戶網站惟一訂單號 * @param totalAmount * 訂單總金額,單位爲元,精確到小數點後兩位,取值範圍[0.01,100000000] * @return APP支付所須要參數 * @throws IOException */ public static String AlipayTradeAppPay(HttpServletResponse httpResponse, HttpSession session, String subject, String outtradeno, String totalAmount) throws IOException { // 實例化客戶端 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); // 實例化具體API對應的request類,類名稱和接口名稱對應,當前調用接口名稱:alipay.trade.app.pay AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); // SDK已經封裝掉了公共參數,這裏只須要傳入業務參數。如下方法爲sdk的model入參方式(model和biz_content同時存在的狀況下取biz_content)。 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); // model.setBody(body); model.setSubject(subject); model.setOutTradeNo(outtradeno); model.setTimeoutExpress("30m"); model.setTotalAmount(totalAmount); model.setProductCode("QUICK_MSECURITzhY_PAY"); request.setBizModel(model); request.setNotifyUrl(AlipayConfig.notify_url); try { // 這裏和普通的接口調用不一樣,使用的是sdkExecute AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request); System.out.println(response.getBody());// 就是orderString 能夠直接給客戶端請求,無需再作處理。 return response.getBody(); } catch (AlipayApiException e) { e.printStackTrace(); return null; } } /** * 返回電腦網站支付表單 * * @param out_trade_no * 商戶訂單號 * @param total_amount * 訂單總金額,單位爲元,精確到小數點後兩位,取值範圍[0.01,100000000] * @param subject * 訂單標題 * @param body * 訂單描述 * @return 電腦網頁支付表單 * @throws IOException */ public static void AlipayTradePagePay(HttpServletResponse httpResponse, HttpSession session, String out_trade_no, String total_amount, String subject) throws IOException { // 建立API對應的request AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();// 建立API對應的request alipayRequest.setReturnUrl(AlipayConfig.return_url); alipayRequest.setNotifyUrl(AlipayConfig.notify_url);// 在公共參數中設置回跳和通知地址 Map<String, String> content = new HashMap<String, String>(); content.put("out_trade_no", out_trade_no); content.put("product_code", "FAST_INSTANT_TRADE_PAY"); content.put("total_amount", total_amount); content.put("subject", subject); // content.put("body", body); String bizContent = BaseUtil.getJson(content); alipayRequest.setBizContent(bizContent); String form = ""; try { form = alipayClient.pageExecute(alipayRequest).getBody(); // 調用SDK生成表單 } catch (AlipayApiException e) { e.printStackTrace(); } httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET); httpResponse.getWriter().write(form);// 直接將完整的表單html輸出到頁面 httpResponse.getWriter().flush(); httpResponse.getWriter().close(); AlipayTradeWapPayResponse response; try { response = alipayClient.pageExecute(alipayRequest); if (response.isSuccess()) { System.out.println("調用成功"); } else { System.out.println("調用失敗"); } } catch (AlipayApiException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } /** * * @param out_trade_no * 商戶訂單號,商戶網站訂單系統中惟一訂單號,必填 * @param subject * 訂單名稱,必填 * @param total_amount * 訂單總金額,單位爲元,精確到小數點後兩位,取值範圍[0.01,100000000],必填 * @param body * 商品描述,可空 * @return 手機支付網頁表單 * @throws IOException */ public static void AlipayTradeWapPay(HttpServletResponse httpResponse, HttpSession session, String out_trade_no, String subject, String total_amount) throws IOException { // 超時時間 可空 String timeout_express = "2m"; // 銷售產品碼 必填 String product_code = "QUICK_WAP_PAY"; /**********************/ // SDK 公共請求類,包含公共請求參數,以及封裝了簽名與驗籤,開發者無需關注簽名與驗籤 // 調用RSA簽名方式 AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest(); // 封裝請求支付信息 AlipayTradeWapPayModel model = new AlipayTradeWapPayModel(); model.setOutTradeNo(out_trade_no); model.setSubject(subject); model.setTotalAmount(total_amount); // model.setBody(body); model.setTimeoutExpress(timeout_express); model.setProductCode(product_code); alipay_request.setBizModel(model); // 設置異步通知地址 alipay_request.setNotifyUrl(AlipayConfig.notify_url); // 設置同步地址 alipay_request.setReturnUrl(AlipayConfig.return_url); // form表單生產 String form = ""; try { // 調用SDK生成表單 form = client.pageExecute(alipay_request).getBody(); } catch (AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET); httpResponse.getWriter().write(form);// 直接將完整的表單html輸出到頁面 httpResponse.getWriter().flush(); httpResponse.getWriter().close(); AlipayTradeWapPayResponse response; try { response = client.pageExecute(alipay_request); if (response.isSuccess()) { System.out.println("調用成功"); } else { System.out.println("調用失敗"); } } catch (AlipayApiException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // return form; } /** * 統一收單線下交易查詢 * * @param out_trade_no * 商戶訂單號,商戶網站訂單系統中惟一訂單號,必填 * @param trade_no * 支付寶交易號 * @return * @throws AlipayApiException */ public static String AlipayTradeWapPay(String out_trade_no, String trade_no) throws AlipayApiException { /// SDK 公共請求類,包含公共請求參數,以及封裝了簽名與驗籤,開發者無需關注簽名與驗籤 AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayTradeQueryRequest alipay_request = new AlipayTradeQueryRequest(); AlipayTradeQueryModel model = new AlipayTradeQueryModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); alipay_request.setBizModel(model); AlipayTradeQueryResponse alipay_response = client.execute(alipay_request); return alipay_response.getBody(); } /** * 統一收單交易關閉接口 * * @param out_trade_no * 商戶訂單號,和支付寶交易號二選一(商戶訂單號和支付寶交易號不能同時爲空。 trade_no、 * out_trade_no若是同時存在優先取trade_no) * @param trade_no * 支付寶交易號,和商戶訂單號二選一 * @return * @throws AlipayApiException */ public static String AlipayTradeClose(String out_trade_no, String trade_no) throws AlipayApiException { /**********************/ // SDK 公共請求類,包含公共請求參數,以及封裝了簽名與驗籤,開發者無需關注簽名與驗籤 AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayTradeCloseRequest alipay_request = new AlipayTradeCloseRequest(); AlipayTradeCloseModel model = new AlipayTradeCloseModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); alipay_request.setBizModel(model); AlipayTradeCloseResponse alipay_response = client.execute(alipay_request); return alipay_response.getBody(); } /** * 統一收單交易退款接口 * * @param out_trade_no * 商戶訂單號,和支付寶交易號二選一(商戶訂單號和支付寶交易號不能同時爲空。 trade_no、 * out_trade_no若是同時存在優先取trade_no) * @param trade_no * 支付寶交易號,和商戶訂單號二選一 * @param refund_amount * 退款金額,不能大於訂單總金額 * @param refund_reason * 退款的緣由說明 * @param out_request_no * 標識一次退款請求,同一筆交易屢次退款須要保證惟一,如需部分退款,則此參數必傳。 * @return * @throws AlipayApiException */ public static String AlipayTradeRefund(String out_trade_no, String trade_no, String refund_amount, String refund_reason, String out_request_no) throws AlipayApiException { /**********************/ // SDK 公共請求類,包含公共請求參數,以及封裝了簽名與驗籤,開發者無需關注簽名與驗籤 AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayTradeRefundRequest alipay_request = new AlipayTradeRefundRequest(); AlipayTradeRefundModel model = new AlipayTradeRefundModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); model.setRefundAmount(refund_amount); model.setRefundReason(refund_reason); model.setOutRequestNo(out_request_no); alipay_request.setBizModel(model); AlipayTradeRefundResponse alipay_response = client.execute(alipay_request); return alipay_response.getBody(); } /** * 統一收單交易退款查詢 * * @param out_trade_no * 商戶訂單號,和支付寶交易號二選一(商戶訂單號和支付寶交易號不能同時爲空。 trade_no、 * out_trade_no若是同時存在優先取trade_no) * @param trade_no * 支付寶交易號,和商戶訂單號二選一 * @param out_request_no * 請求退款接口時,傳入的退款請求號,若是在退款請求時未傳入,則該值爲建立交易時的外部交易號 * @return * @throws AlipayApiException */ public static String AlipayTradeFastpayRefundQuery(String out_trade_no, String trade_no, String out_request_no) throws AlipayApiException { /**********************/ // SDK 公共請求類,包含公共請求參數,以及封裝了簽名與驗籤,開發者無需關注簽名與驗籤 AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayTradeFastpayRefundQueryRequest alipay_request = new AlipayTradeFastpayRefundQueryRequest(); AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); model.setOutRequestNo(out_request_no); alipay_request.setBizModel(model); AlipayTradeFastpayRefundQueryResponse alipay_response = client.execute(alipay_request); return alipay_response.getBody(); } /** * 查詢對帳單下載地址 * * @param bill_type * 帳單類型,商戶經過接口或商戶經開放平臺受權後其所屬服務商經過接口能夠獲取如下帳單類型:trade、signcustomer; * trade指商戶基於支付寶交易收單的業務帳單;signcustomer是指基於商戶支付寶餘額收入及支出等資金變更的賬務帳單; * @param bill_date * 帳單時間:日帳單格式爲yyyy-MM-dd,月帳單格式爲yyyy-MM。 * @return 帳單下載地址 * @throws AlipayApiException */ public static String AlipayDataDataserviceBillDownloadurlQuery(String bill_type, String bill_date) throws AlipayApiException { /**********************/ // SDK 公共請求類,包含公共請求參數,以及封裝了簽名與驗籤,開發者無需關注簽名與驗籤 AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); AlipayDataDataserviceBillDownloadurlQueryRequest alipay_request = new AlipayDataDataserviceBillDownloadurlQueryRequest(); AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel(); model.setBillType(bill_type); model.setBillDate(bill_date); alipay_request.setBizModel(model); AlipayDataDataserviceBillDownloadurlQueryResponse alipay_response = client.execute(alipay_request); return alipay_response.getBillDownloadUrl(); } }
支付寶回調接口:mysql
/** * 支付寶回調地址 * * @param request * @param httpResponse * @param session * @return * @throws Exception */ @RequestMapping(value = "/alipayReturnOrder") public void alipayReturnOrder(HttpServletRequest request, HttpServletResponse httpResponse, HttpSession session) throws Exception { // 獲取支付寶POST過來反饋信息 Map<String, String> params = new HashMap<String, String>(); Map requestParams = request.getParameterMap(); Iterator iter = requestParams.keySet().iterator(); while (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] + ","; } // 亂碼解決,這段代碼在出現亂碼時使用。若是mysign和sign不相等也可使用這段代碼轉化 // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk"); params.put(name, valueStr); } // 獲取支付寶的通知返回參數 // 商戶訂單號 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8"); // 支付寶交易號 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8"); // 交易狀態 String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8"); // 支付金額 String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8"); boolean verify_result = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE); // 查詢訂單信息 Orders orders = ******; if (verify_result) {// 驗證成功 if (trade_status.equals("TRADE_SUCCESS")) {// 支付成功 // 更新訂單狀態 } } else { // 驗證失敗 log.info("驗證失敗!"); } }
pom依賴:sql
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java --> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.0.52.ALL</version> </dependency>