java後端處理Apple Pay流程
阿新 • • 發佈:2019-02-16
總結整理Apple Pay流程
客戶端發起支付---蘋果收到支付--通知客戶端--成功/失敗
客戶端成功後呼叫後端介面處理
主要方法:
/** * * @Title: doIosRequest * @Description:Ios客戶端 * @param request //蘋果客戶端傳上來的收據資訊 * @param response * @param receipt 設定檔案 * @return void 返回型別 * @throws */ @RequestMapping(value="/ios",method = RequestMethod.POST) public void doIosRequest(HttpServletRequest request, HttpServletResponse response, String receipt) { String verifyResult = = IosVerify.buyAppVerify(receipt); if (verifyResult == null) { // 蘋果伺服器沒有返回驗證結果 throw new Exception("無訂單資訊!");// } else { // 蘋果驗證有返回結果------------------ JSONObject job = JSONObject.parseObject(verifyResult); String states = job.getString("status"); if (states.equals("0")) // 驗證成功 { String r_receipt = job.getString("receipt"); JSONObject returnJson = JSONObject.parseObject(r_receipt); if(!returnJson.getString("bid").trim().equals("xxxx")){//商戶的id不匹配 throw new Exception("訂單無效!");// } // 產品ID String product_id = returnJson.getString("product_id"); // 訂單號 String transaction_id = returnJson.getString("transaction_id"); // 交易日期 String purchase_date = returnJson.getString("purchase_date"); // 儲存到資料庫 System.out.println("product_id:"+product_id+" transaction_id : "+transaction_id+" purchase_date: "+purchase_date+" uid"+user.getId()); /***************************+自己的業務邏輯*****************************************/// /*** 此處要判斷是否已經支付過,支付過之後不能重複發貨**/ //***************************************/ paymentService.doIosPayment(product_id, transaction_id); ///***************************自己的業務邏輯end************************************/// } else { throw new ZMServiceException("訂單無效!");// } } ResponseUtils.writeSuccessReponse(request, response, "success"); }
請求訂單資訊
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.List; import java.util.Locale; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; /** * @ClassName: IosVerify * @Description:Apple Pay */ public class IosVerify { private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } private static class TrustAnyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { return true; } } private static final String url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt"; private static final String url_verify = "https://buy.itunes.apple.com/verifyReceipt"; /** * 蘋果伺服器驗證 * * @param receipt * 賬單 * @url 要驗證的地址 * @return null 或返回結果 沙盒 https://sandbox.itunes.apple.com/verifyReceipt * */ public static String buyAppVerify(String receipt,int _o,int _v) { //環境判斷 線上/開發環境用不同的請求連結 String url = EnvUtils.isOnline() ?url_verify : url_sandbox; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); URL console = new URL(url); HttpsURLConnection conn = (HttpsURLConnection) console.openConnection(); conn.setSSLSocketFactory(sc.getSocketFactory()); conn.setHostnameVerifier(new TrustAnyHostnameVerifier()); conn.setRequestMethod("POST"); conn.setRequestProperty("content-type", "text/json"); conn.setRequestProperty("Proxy-Connection", "Keep-Alive"); conn.setDoInput(true); conn.setDoOutput(true); BufferedOutputStream hurlBufOus = new BufferedOutputStream(conn.getOutputStream()); String str = String.format(Locale.CHINA, "{\"receipt-data\":\"" + receipt + "\"}"); hurlBufOus.write(str.getBytes()); hurlBufOus.flush(); InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = null; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * 用BASE64加密 * * @param str * @return */ public static String getBASE64(String str) { byte[] b = str.getBytes(); String s = null; if (b != null) { s = new sun.misc.BASE64Encoder().encode(b); } return s; } }