Created
October 9, 2019 08:58
-
-
Save huiliu/a54c1b2319b0595ace7247ec347321f2 to your computer and use it in GitHub Desktop.
In-App Purchase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
from transactionTable import TransactionTable | |
class IAP(object): | |
@staticmethod | |
def url(debug=True): | |
if debug: | |
return "https://sandbox.itunes.apple.com/verifyReceipt" | |
else: | |
return "https://buy.itunes.apple.com/verifyReceipt" | |
@staticmethod | |
def validate(data): | |
pass | |
@staticmethod | |
def validateWithAppStore(data): | |
resp = IAP.sendReceiptToApppStore(data) | |
if not resp: | |
return None | |
# response body | |
# https://developer.apple.com/documentation/appstorereceipts/responsebody | |
if resp['status'] == 0: | |
pass | |
@staticmethod | |
def verifyReceipt(receipt): | |
""" | |
检验从app store 返回的receipt data | |
https://developer.apple.com/documentation/appstorereceipts/responsebody/receipt | |
https://developer.apple.com/documentation/appstorereceipts/responsebody/receipt/in_app | |
https://developer.apple.com/library/archive/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW10 | |
""" | |
ret = {'result': False} | |
try: | |
adamId = receipt["app_item_id"] | |
bundleId = receipt["bundle_id"] | |
product_id = receipt["in-app"]["product_id"] | |
transaction_id = receipt["in-app"]["transaction_id"] | |
# 验证Bundle Id是否正确 | |
if bundleId != 'com.YezhStudio.Hermes': | |
pass | |
return ret | |
# 验证production id是否正确 | |
products = [] | |
if product_id not in products: | |
pass | |
return ret | |
# 验证此订单是否重复处理 | |
table = TransactionTable() | |
e = table.exsit(transaction_id) | |
if not e: | |
table.insert(transaction_id, receipt) | |
else: | |
receipt = e[1] | |
# 票据验证成功 | |
# TODO: 返回结果 | |
except Exception as err: | |
ret['error'] = err | |
finally: | |
return ret | |
@staticmethod | |
def sendReceiptToApppStore(receipt, password=None, exclude=False): | |
# request body | |
# https://developer.apple.com/documentation/appstorereceipts/requestbody?language=objc | |
data = { | |
"receipt-data": receipt | |
} | |
if password: | |
data["password"] = password | |
if exclude: | |
data["exclude-old-transactions"] = True | |
resp = requests.post(IAP.url, json=json.dumps(data)) | |
return resp.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment