IOS 애플 내 결제 iap 자바 서버

5526 단어 자바
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/pay")
public class IAP {

	//         
	private static final String certificateUrl = "https://buy.itunes.apple.com/verifyReceipt";
	//            
	private static final String certificateUrlTest = "https://sandbox.itunes.apple.com/verifyReceipt";

	/**
	 *   X509TrustManager
	 */
	private static TrustManager myX509TrustManager = new X509TrustManager() {
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}
		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}
		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}
	};

	/**
	 *   iOS         
	 * @param url
	 * @param strings
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/iap")
	private void sendHttpsCoon(long uid, String receipt, boolean chooseEnv)
	{
		if( 0 == uid || null == receipt)
		{
			System.out.println("      ");
		}
		else
		{
			String url = chooseEnv == true ? certificateUrl : certificateUrlTest;
			try
			{
				//   SSLContext
				SSLContext ssl = SSLContext.getInstance("SSL");
				ssl.init(null, new TrustManager[] { myX509TrustManager }, null);
	
				//     
				HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
				//       
				conn.setSSLSocketFactory(ssl.getSocketFactory());
				//     
				conn.setRequestMethod("POST");
				conn.setDoOutput(true);
				conn.setRequestProperty("Content-type", "application/json");
				conn.setRequestProperty("Proxy-Connection", "Keep-Alive");	
				JSONObject obj = new JSONObject();
				obj.put("receipt-data", chooseEnv);
				//      
				BufferedOutputStream buffOutStr = new BufferedOutputStream(conn.getOutputStream());
				buffOutStr.write(obj.toString().getBytes());
				buffOutStr.flush();
				buffOutStr.close();
				//      
				BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	
				String line = null;
				StringBuffer sb = new StringBuffer();
				while ((line = reader.readLine()) != null)
				{
					sb.append(line);
				}
				String stringadsf = sb.toString();
				conn.getInputStream().close();
				System.err.println("         -->  " + stringadsf);
	
				JSONObject job = JSONObject.parseObject(stringadsf);// App Store    
				String money = "";
				String orderId = "";
				if(job.getString("status").equals("0"))//           --    
				{
					JSONObject code = JSONObject.parseObject(job.getString("receipt"));
					String creation_date_ms = code.getString("receipt_creation_date_ms"); // receipt    (ms)
					JSONArray jsonArray = (JSONArray) code.get("in_app");//     
					JSONObject targetOrder = null;
					if(1 == jsonArray.size())//       ,    
					{
						targetOrder = jsonArray.getJSONObject(0);
					}
					else//       ,                 
					{
						for (int i = 0; i < jsonArray.size(); i++)
						{
							JSONObject orderItem = jsonArray.getJSONObject(i);
							if(orderItem.getString("purchase_date_ms").equals(creation_date_ms))
							{
								targetOrder = orderItem;
							}
						}
					}
					if(null == targetOrder)
					{
						System.out.println("            ");
					}
					else
					{
						String product_id = targetOrder.getString("product_id");
						money = product_id.substring(4, product_id.length());
						orderId = uid + "_" + targetOrder.getString("transaction_id");// transaction_id   
					}					
					boolean contains = RedisUtil.getAllkeys().contains(orderId);
					if((contains && !RedisUtil.getByKey(orderId).equals("3")) || !contains)
					{
						RedisUtil.setValue(orderId, "2");//     ,    
						
						//  redis,          
						JSONArray json = JSONArray.parseArray(RedisUtil.getByKey("RechargeMeter"));//         JSONArray  
						int diamond = 0;
						if(json.size() > 0)
						{
							for(int i = 0; i < json.size(); i++)
							{
								JSONObject jsonob = json.getJSONObject(i);//   jsonarray  ,        json  
								if(jsonob.get("money").equals(money))
								{
									diamond = Integer.parseInt((String) jsonob.get("diamonds"));
								}
							}
						}
						System.out.println("add diamond : " + diamond);
						//     
						if (true)//    
						{
							System.out.println("      ");
						}
						else
						{
							RedisUtil.setValue(orderId, "3");//       
							System.out.println("      ");
						}
					}
				}
				else if(job.getString("status").equals("21007"))
				{
					System.out.println("21007:         (sandbox),             ");
				}				
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}

좋은 웹페이지 즐겨찾기