티스토리 뷰

class IOSInappPurchaseVerifier :
	url = "https://sandbox.itunes.apple.com/verifyReceipt"
	password = "PASSWORD"

	# receipt data should be encoded by base64
	def verify(self, receipt, signature) :
		req = { "receipt-data": receipt,"password": IOSInappPurchaseVerifier.password }
		headers = {'Content-Type': 'text/Json; charset=utf-8'}
		req_json = json.dumps(req)
		request = urllib2.Request(IOSInappPurchaseVerifier.url, req_json, headers)
		response = urllib2.urlopen(request)
		receipt_json = json.loads(response.read())
        
		print receipt_json
        
		result = {}
		result["transaction_id"] = ""
		result["result"] = False
		result["purchase_date"] = 0
		result["product_id"] = ""
        
		if 0 == receipt_json["status"] :
			result["transaction_id"] = receipt_json["receipt"]["transaction_id"]
			result["product_id"] = receipt_json["receipt"]["product_id"]
			result["purchase_date"] = int(receipt_json["receipt"]["purchase_date_ms"])/1000
			result["result"] = (0 == receipt_json["status"])
		return result
class GoogleInappPurchaseVerifier:
	public_key = "MIIBIjANBgkq.....pQ0YcQIDAQAB"
    
	def __init__(self) :
		self.pem = ""
		self.pem += "-----BEGIN PUBLIC KEY-----\n"
		for start in range(0, len(GoogleInappPurchaseVerifier.public_key), 64):
			self.pem += GoogleInappPurchaseVerifier.public_key[start:start+64]+"\n"
		self.pem += "-----END PUBLIC KEY-----"
		key = RSA.importKey(self.pem))
		self.verifier = PKCS1_v1_5.new(key)

	#receipt data should be encoded by base64
	def verify(self, receipt, signature):
		decoded_receipt = base64.b64decode(receipt)
		decoded_signature = base64.b64decode(signature)
		data = SHA.new(decoded_receipt)

		result = {}
		result["transaction_id"] = ""
		result["product_id"] = ""
		result["purchase_date"] = 0
		result["result"] = False

		if(1 == self.verifier.verify(data, decoded_signature)) :
			receipt_json = json.loads(decoded_receipt)
			result["transaction_id"] = receipt_json["orderId"]
			result["product_id"] = receipt_json["productId"]
			result["purchase_date"] = int(receipt_json["purchaseTime"])/1000
			result["result"] = True

		return result
댓글
댓글쓰기 폼