Skip to content

Commit

Permalink
Merge pull request #281 from planetarium/bugfix/google-ack-package
Browse files Browse the repository at this point in the history
Use provided package name to ACK purchase
  • Loading branch information
U-lis authored Jun 28, 2024
2 parents 8df929b + 3c99717 commit ee7c729
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion iap/api/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def request_product(receipt_data: ReceiptSchema,
# raise_error(sess, receipt, ValueError(
# f"Invalid Product ID: Given {product.google_sku} is not identical to found from receipt: {purchase.productId}"))
if success:
ack_google(product_id, token)
ack_google(x_iap_packagename, product_id, token)
## Apple
elif receipt_data.store in (Store.APPLE, Store.APPLE_TEST):
success, msg, purchase = validate_apple(receipt.package_name, order_id)
Expand Down
19 changes: 4 additions & 15 deletions iap/validator/google.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
from typing import Tuple, Optional

from common import logger
from common.enums import GooglePurchaseState
from common.enums import GooglePurchaseState, PackageName
from common.utils.google import get_google_client
from iap import settings
from iap.schemas.receipt import GooglePurchaseSchema


def ack_google(sku: str, token: str):
def ack_google(package_name: PackageName, sku: str, token: str):
client = get_google_client(settings.GOOGLE_CREDENTIAL)
try:
(client.purchases().products()
.acknowledge(packageName=settings.GOOGLE_PACKAGE_NAME, productId=sku, token=token)
.execute()
)
except Exception as e:
logger.error(e)

def consume_google(sku: str, token: str):
client = get_google_client(settings.GOOGLE_CREDENTIAL)
try:
(client.purchases().products()
.consume(packageName=settings.GOOGLE_PACKAGE_NAME, productId=sku, token=token)
.execute()
)
.acknowledge(packageName=package_name.value, productId=sku, token=token)
.execute())
except Exception as e:
logger.error(e)

Expand Down
54 changes: 34 additions & 20 deletions worker/worker/google_refund_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
from typing import Optional

from common import logger
from common.enums import PackageName
from common.utils.aws import fetch_parameter
from common.utils.google import get_google_client, Spreadsheet

GOOGLE_PACKAGE_NAME = os.environ.get("GOOGLE_PACKAGE_NAME")
GOOGLE_PACKAGE_DICT = {
PackageName.NINE_CHRONICLES_M: {
"app_name": "9c M",
"sheet_name": "Google",
},
PackageName.NINE_CHRONICLES_K: {
"app_name": "9c K",
"sheet_name": "Google_K"
},
}
GOOGLE_CREDENTIAL = fetch_parameter(
os.environ.get("REGION_NAME"),
f"{os.environ.get('STAGE')}_9c_IAP_GOOGLE_CREDENTIAL", True
Expand Down Expand Up @@ -56,25 +66,29 @@ def __post_init__(self):
def handle(event, context):
client = get_google_client(GOOGLE_CREDENTIAL)
sheet = Spreadsheet(GOOGLE_CREDENTIAL, SHEET_ID)
prev_data = sheet.get_values("Google!A2:B").get("values", [])
prev_order_id = set([x[1] for x in prev_data])
last_num = int(prev_data[-1][0]) + 1 if prev_data else 1
logger.info(f"{len(prev_data)} refunded data are present.")
voided_list = client.purchases().voidedpurchases().list(packageName=GOOGLE_PACKAGE_NAME).execute()
voided_list = sorted([RefundData(**x) for x in voided_list["voidedPurchases"]], key=lambda x: x.voidedTimeMillis)

new_data = []
index = last_num
for void in voided_list:
if void.orderId in prev_order_id:
continue
new_data.append(
[index, void.orderId, void.purchaseTime.isoformat(), void.voidedTime.isoformat(), void.voidedSource.name,
void.voidedReason.name])
index += 1

sheet.set_values(f"Google!A{last_num + 1}:F", new_data)
logger.info(f"{len(new_data)} Refunds are added")

for package_name, data in GOOGLE_PACKAGE_DICT.items():
prev_data = sheet.get_values(f"{data['sheet_name']}!A2:B").get("values", [])
prev_order_id = set([x[1] for x in prev_data])
last_num = int(prev_data[-1][0]) + 1 if prev_data else 1
logger.info(f"{len(prev_data)} refunded data are present in {data['app_name']}")
voided_list = client.purchases().voidedpurchases().list(packageName=package_name.value).execute()
voided_list = sorted([RefundData(**x) for x in voided_list["voidedPurchases"]],
key=lambda x: x.voidedTimeMillis)

new_data = []
index = last_num
for void in voided_list:
if void.orderId in prev_order_id:
continue
new_data.append(
[index, void.orderId, void.purchaseTime.isoformat(), void.voidedTime.isoformat(),
void.voidedSource.name,
void.voidedReason.name])
index += 1

sheet.set_values(f"{data['sheet_name']}!A{last_num + 1}:F", new_data)
logger.info(f"{len(new_data)} Refunds are added to {data['app_name']}")


if __name__ == "__main__":
Expand Down

0 comments on commit ee7c729

Please sign in to comment.