def verify(self, purchase_token: str, product_sku: str, is_subscription: bool = False) -> dict: service = build("androidpublisher", "v3", http=self.http) if is_subscription: result = self.check_purchase_subscription(purchase_token, product_sku, service) cancel_reason = int(result.get("cancelReason", 0)) if cancel_reason != 0: raise GoogleError("Subscription is canceled", result) ms_timestamp = result.get("expiryTimeMillis", 0) if self._ms_timestamp_expired(ms_timestamp): raise GoogleError("Subscription expired", result) else: result = self.check_purchase_product(purchase_token, product_sku, service) purchase_state = int(result.get("purchaseState", 1)) if purchase_state != 0: raise GoogleError("Purchase cancelled", result) return result
def verify(self, purchase_token, product_sku, is_subscription=False): service = build("androidpublisher", "v3", http=self.http) if is_subscription: subscriptions = self.get_subscriptions(purchase_token, product_sku, service) cancel_reason = int(subscriptions.get('cancelReason', 0)) if cancel_reason != 0: raise GoogleError('Subscription is canceled', subscriptions) ms_timestamp = subscriptions.get('expiryTimeMillis', 0) if _ms_timestamp_expired(ms_timestamp): raise GoogleError('Subscription expired', subscriptions) else: raise NotImplementedError() return subscriptions
def test_google_error_message_and_raw_response_stringify( generic_error_message, generic_raw_response): error = GoogleError(generic_error_message, generic_raw_response) assert repr( error ) == f"GoogleError(message={generic_error_message!r}, raw_response={generic_raw_response!r})" assert str(error) == "GoogleError error message {'foo': 'bar'}"
def check_purchase_product(self, purchase_token: str, product_sku: str, service) -> dict: try: return (service.purchases().products().get( packageName=self.bundle_id, productId=product_sku, token=purchase_token).execute(http=self.http)) except HttpError as e: if e.resp.status == 400: raise GoogleError(e.resp.reason, repr(e)) else: raise e
def check_purchase_product(self, purchase_token, product_sku, service): try: purchases = service.purchases() products = purchases.products() products_get = products.get(packageName=self.bundle_id, productId=product_sku, token=purchase_token) result = products_get.execute(http=self.http) return result except HttpError as e: if e.resp.status == 400: raise GoogleError(e.resp.reason, repr(e)) else: raise e
def check_purchase_subscription(self, purchase_token: str, product_sku: str, service) -> dict: try: purchases = service.purchases() subscriptions = purchases.subscriptions() subscriptions_get = subscriptions.get(packageName=self.bundle_id, subscriptionId=product_sku, token=purchase_token) result = subscriptions_get.execute(http=self.http) return result except HttpError as e: if e.resp.status == 400: raise GoogleError(e.resp.reason, repr(e)) else: raise e
def test_google_error_message_stringify(generic_error_message): error = GoogleError(generic_error_message) assert repr( error ) == f"GoogleError(message={generic_error_message!r}, raw_response=None)" assert str(error) == f"GoogleError {generic_error_message} None"
def test_google_error_plain_stringify(): error = GoogleError() assert error assert repr(error) == "GoogleError(message=None, raw_response=None)" assert str(error) == "GoogleError None None"