1. 程式人生 > >支付寶SDK整合服務端(java)

支付寶SDK整合服務端(java)

支付寶SDK整合服務端

配置項(公共引數)

// 商戶appid
public static String APPID = "2017120800451448";
// 私鑰 pkcs8格式的
public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCgSrIsDE9qdOFoc3Ej0wxD815zDOZ...";
// 伺服器非同步通知頁面路徑 需http://或者https://格式的完整路徑,不能加?id=123這類自定義引數,必須外網可以正常訪問
public static String notify_url = "http://xxxxx/alipay/notify_url.do"
; // 頁面跳轉同步通知頁面路徑 需http://或者https://格式的完整路徑,不能加?id=123這類自定義引數,必須外網可以正常訪問 商戶可以自定義同步跳轉地址 public static String return_url = "xxxxx/alipay/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 = "D:/log"; // RSA2 public static String SIGNTYPE = "RSA2";

支付工具類

package com.app.utils.alipay;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import
com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.domain.AlipayTradeAppPayModel; import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.request.AlipayTradeAppPayRequest; import com.alipay.api.request.AlipayTradeRefundRequest; import com.alipay.api.response.AlipayTradeAppPayResponse; import com.alipay.api.response.AlipayTradeRefundResponse; import com.app.utils.DateUtil; public class AlipayUtils { public static AlipayClient getAlipayClient(){ return new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); //獲得初始化的AlipayClient } /** * 驗證簽名 * @param request * @return */ @SuppressWarnings("all") public static boolean rsaCheckV1(HttpServletRequest request){ boolean verify_result = false; try { Map<String,String> params = new HashMap<String,String>(); 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); } verify_result = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2"); } catch (Exception e) { e.printStackTrace(); } return verify_result; } /** * 支付介面 * @param out_trade_no 商戶訂單號 * @param total_amount 金額 * @param body 內容 * @param subject 主題 * @param request * @return */ public static String aliPrePay(String out_trade_no,String total_amount,String body,String subject,HttpServletRequest request){ String orderString = null; //例項化客戶端 AlipayClient alipayClient = AlipayUtils.getAlipayClient(); //例項化具體API對應的request類,類名稱和介面名稱對應,當前呼叫介面名稱:alipay.trade.app.pay AlipayTradeAppPayRequest payRequest = new AlipayTradeAppPayRequest(); //SDK已經封裝掉了公共引數,這裡只需要傳入業務引數。以下方法為sdk的model入參方式(model和biz_content同時存在的情況下取biz_content)。 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); model.setBody(body); //標題 model.setSubject(subject); //商戶訂單號 model.setOutTradeNo(out_trade_no); model.setTimeoutExpress("30m"); //交易金額 model.setTotalAmount(total_amount); //交易方式 model.setProductCode(AlipayConfig.PRODUCT_CODE); payRequest.setBizModel(model); String url = request.getRequestURL().toString(); String domain = url.substring(0, url.lastIndexOf("/")+1); //生產環境 String notify_url= domain+"aliNotify.do"; //測試環境 // String notify_url = "http://XXXX/XXXX/alipay/aliNotify.do"; payRequest.setNotifyUrl(notify_url); try { //這裡和普通的介面呼叫不同,使用的是sdkExecute AlipayTradeAppPayResponse payResponse = alipayClient.sdkExecute(payRequest); //就是orderString 可以直接給客戶端請求,無需再做處理。 orderString = payResponse.getBody(); } catch (AlipayApiException e) { e.printStackTrace(); } return orderString; } /** * 退款 * @param out_trade_no 商戶訂單號 * @param trade_no 支付寶訂單流水號 * @param total_amount 金額 * @param refund_reason 退款原因 * @return */ public static boolean refund(String out_trade_no,String trade_no,double total_amount,String refund_reason){ boolean flag = false; String str = null; try { AlipayClient alipayClient = getAlipayClient(); AlipayTradeRefundRequest apliRequest = new AlipayTradeRefundRequest(); apliRequest.setBizContent("{" + "\"out_trade_no\":\""+out_trade_no+"\"," + "\"trade_no\":\""+trade_no+"\"," + "\"refund_amount\":"+total_amount+"," + "\"refund_reason\":\""+refund_reason+"\"," + "\"out_request_no\":\"\"," + "\"operator_id\":\"\"," + "\"store_id\":\"\"," + "\"terminal_id\":\"\"" + " }"); AlipayTradeRefundResponse aliResponse = alipayClient.execute(apliRequest);//通過alipayClient呼叫API,獲得對應的response類 if(aliResponse.isSuccess()){ //呼叫成功 flag = true; } else { //呼叫失敗 flag = false; } } catch (Exception e) { e.printStackTrace(); } return flag; } }

第一次寫,有問題的話請指正