def test_appstore_validate_attach_raw_response_to_the_exception(appstore_validator: AppStoreValidator): raw_response = {"status": 21000, "foo": "bar"} with pytest.raises(InAppPyValidationError) as ex: with patch.object(AppStoreValidator, "post_json", return_value=raw_response) as mock_method: appstore_validator.validate(receipt="test-receipt", shared_secret="shared-secret") assert mock_method.call_count == 1 assert appstore_validator.url == "https://buy.itunes.apple.com/verifyReceipt" assert mock_method.call_args[0][0] == {"receipt-data": "test-receipt", "password": "******"} assert ex.raw_response is not None assert ex.raw_response == raw_response
def test_appstore_validate_sandbox(appstore_validator_sandbox: AppStoreValidator): with patch.object(AppStoreValidator, "post_json", return_value={"status": 0}) as mock_method: appstore_validator_sandbox.validate(receipt="test-receipt", shared_secret="shared-secret") assert mock_method.call_count == 1 assert appstore_validator_sandbox.url == "https://sandbox.itunes.apple.com/verifyReceipt" assert mock_method.call_args[0][0] == {"receipt-data": "test-receipt", "password": "******"} with patch.object(AppStoreValidator, "post_json", return_value={"status": 0}) as mock_method: appstore_validator_sandbox.validate(receipt="test-receipt") assert mock_method.call_count == 1 assert appstore_validator_sandbox.url == "https://sandbox.itunes.apple.com/verifyReceipt" assert mock_method.call_args[0][0] == {"receipt-data": "test-receipt"}
def test_appstore_validate_sandbox(): validator = AppStoreValidator(bundle_id="test-bundle-id", sandbox=True) assert validator is not None with patch.object(AppStoreValidator, "post_json", return_value={"status": 0}) as mock_method: validator.validate(receipt="test-receipt", shared_secret="shared-secret") assert mock_method.call_count == 1 assert validator.url == "https://sandbox.itunes.apple.com/verifyReceipt" assert mock_method.call_args[0][0] == {"receipt-data": "test-receipt", "password": "******"} with patch.object(AppStoreValidator, "post_json", return_value={"status": 0}) as mock_method: validator.validate(receipt="test-receipt") assert mock_method.call_count == 1 assert validator.url == "https://sandbox.itunes.apple.com/verifyReceipt" assert mock_method.call_args[0][0] == {"receipt-data": "test-receipt"}
def process_app_pay_receipt(receipt, shared_secret=None) -> Tuple[bool, dict]: bundle_id = os.environ['BUNDLE_ID'] # if True, automatically query sandbox endpoint # if validation fails on production endpoint if is_true(os.environ['AUTO_RETRY_WRONG_ENV_REQUEST']): auto_retry_wrong_env_request = True else: auto_retry_wrong_env_request = False validator = AppStoreValidator( bundle_id=bundle_id, sandbox=is_true(os.environ['RECEIPT_SANDBOX']), auto_retry_wrong_env_request=auto_retry_wrong_env_request, ) try: # if True, include only the latest renewal transaction exclude_old_transactions = False logging.debug("Validating AppStore Receipt:") validation_result: Dict[Any, Any] = validator.validate( receipt=receipt, shared_secret=shared_secret, exclude_old_transactions=exclude_old_transactions) logging.debug(f'Validation Result: {validation_result}') except InAppPyValidationError as ex: # handle validation error # contains actual response from AppStore service. response_from_apple = ex.raw_response logging.debug(f"validation failure: {response_from_apple}") return (False, response_from_apple) return (True, validation_result)
def process_app_pay_receipt(receipt, total_usd, shared_secret=None): #return True; bundle_id = 'com.yourcompany.yourapp' auto_retry_wrong_env_request = False # if True, automatically query sandbox endpoint if # validation fails on production endpoint validator = AppStoreValidator( bundle_id="com.yourcompany.yourapp", sandbox=False, auto_retry_wrong_env_request=auto_retry_wrong_env_request) try: exclude_old_transactions = False # if True, include only the latest renewal transaction print("Validating AppStore Receipt:") validation_result = validator.validate( receipt=receipt, shared_secret=shared_secret, exclude_old_transactions=exclude_old_transactions) except InAppPyValidationError as ex: # handle validation error response_from_apple = ex.raw_response # contains actual response from AppStore service. print("validation failure:") print(response_from_apple) return False return True
def test_appstore_validate_attach_raw_response_to_the_exception(): validator = AppStoreValidator(bundle_id='test-bundle-id') assert validator is not None raw_response = {'status': 21000, 'foo': 'bar'} with pytest.raises(InAppPyValidationError) as ex: with patch.object(AppStoreValidator, 'post_json', return_value=raw_response) as mock_method: validator.validate(receipt='test-receipt', shared_secret='shared-secret') assert mock_method.call_count == 1 assert validator.url == 'https://buy.itunes.apple.com/verifyReceipt' assert mock_method.call_args[0][0] == { 'receipt-data': 'test-receipt', 'password': '******' } assert ex.raw_response is not None assert ex.raw_response == raw_response
def decode_receipt(receipt): validator = AppStoreValidator(ios_bundle_id, sandbox=True, auto_retry_wrong_env_request=False) try: validation = validator.validate(receipt, shared_secret=shared_secret) result = Struct(validation) return result except InAppPyValidationError as ex: # handle validation error response = ex.raw_response # contains actual response from AppStore service. return Struct(response)
def test_appstore_validate_sandbox(): validator = AppStoreValidator(bundle_id='test-bundle-id', sandbox=True) assert validator is not None with patch.object(AppStoreValidator, 'post_json', return_value={'status': 0}) as mock_method: validator.validate(receipt='test-receipt', shared_secret='shared-secret') assert mock_method.call_count == 1 assert validator.url == 'https://sandbox.itunes.apple.com/verifyReceipt' assert mock_method.call_args[0][0] == { 'receipt-data': 'test-receipt', 'password': '******' } with patch.object(AppStoreValidator, 'post_json', return_value={'status': 0}) as mock_method: validator.validate(receipt='test-receipt') assert mock_method.call_count == 1 assert validator.url == 'https://sandbox.itunes.apple.com/verifyReceipt' assert mock_method.call_args[0][0] == {'receipt-data': 'test-receipt'}
def test_appstore_auto_retry_wrong_env_request(): validator = AppStoreValidator(bundle_id="test-bundle-id", sandbox=False, auto_retry_wrong_env_request=True) assert validator is not None assert not validator.sandbox assert validator.auto_retry_wrong_env_request raw_response = {"status": 21007, "foo": "bar"} with pytest.raises(InAppPyValidationError): with patch.object(AppStoreValidator, "post_json", return_value=raw_response) as mock_method: validator.validate(receipt="test-receipt", shared_secret="shared-secret") assert mock_method.call_count == 1 assert validator.url == "https://buy.itunes.apple.com/verifyReceipt" assert mock_method.call_args[0][0] == {"receipt-data": "test-receipt", "password": "******"} assert validator.sandbox is True raw_response = {"status": 21008, "foo": "bar"} with pytest.raises(InAppPyValidationError): with patch.object(AppStoreValidator, "post_json", return_value=raw_response) as mock_method: validator.validate(receipt="test-receipt", shared_secret="shared-secret") assert validator.sandbox is False assert mock_method.call_count == 1 assert validator.url == "https://buy.itunes.apple.com/verifyReceipt" assert mock_method.call_args[0][0] == {"receipt-data": "test-receipt", "password": "******"}
def test_appstore_auto_retry_wrong_env_request(): validator = AppStoreValidator(bundle_id='test-bundle-id', sandbox=False, auto_retry_wrong_env_request=True) assert validator is not None assert not validator.sandbox assert validator.auto_retry_wrong_env_request raw_response = {'status': 21007, 'foo': 'bar'} with pytest.raises(InAppPyValidationError): with patch.object(AppStoreValidator, 'post_json', return_value=raw_response) as mock_method: validator.validate(receipt='test-receipt', shared_secret='shared-secret') assert mock_method.call_count == 1 assert validator.url == 'https://buy.itunes.apple.com/verifyReceipt' assert mock_method.call_args[0][0] == { 'receipt-data': 'test-receipt', 'password': '******' } assert validator.sandbox is True raw_response = {'status': 21008, 'foo': 'bar'} with pytest.raises(InAppPyValidationError): with patch.object(AppStoreValidator, 'post_json', return_value=raw_response) as mock_method: validator.validate(receipt='test-receipt', shared_secret='shared-secret') assert validator.sandbox is False assert mock_method.call_count == 1 assert validator.url == 'https://buy.itunes.apple.com/verifyReceipt' assert mock_method.call_args[0][0] == { 'receipt-data': 'test-receipt', 'password': '******' }