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();
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.