Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use provided package name to ACK purchase #281

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading