dotpot / InAppPy

Python In-app purchase validator for Apple AppStore and GooglePlay.
MIT License
207 stars 50 forks source link

[BUG] It does not work #86

Open vporton opened 1 year ago

vporton commented 1 year ago

In my case the purchase is never validated: https://stackoverflow.com/q/75766712/856090

vporton commented 1 year ago
import json

from flask import request, jsonify
from inapppy import GooglePlayVerifier, errors

from common import app, config, OurDB, fund_account

try:
    with open("google_credentials.json") as f:
        google_credentials = json.load(f)
except FileNotFoundError:
    google_credentials = None

@app.route('/google_play_purchase', methods=['POST'])
def playmarket_purchase():
    """Send by the client to notify that a product was purchased."""
    if google_credentials is None:
        app.logger.error("No Google credentials file!")
        return "No Google credentials file!", 500

    purchase_token = request.form['purchaseToken']
    product_sku = request.form['productId']
    app_guid = request.form['appGuid']

    verifier = GooglePlayVerifier(
        config['android']['bundleId'],
        google_credentials,
    )
    try:
        result = verifier.verify(
            purchase_token,
            product_sku,
            is_subscription=False,
        )
    except errors.GoogleError as exc:
        app.logger.error('Purchase validation failed {}'.format(exc))
        return jsonify(success=False, reason='validationFailed'), 402  # payment required
    try:
        amount = config['products'][product_sku]['amount'] * result['quantity']
    except KeyError:
        app.logger.error("ERROR: Unknown product!!")
        return jsonify(success=False, reason='unknownProduct'), 400   # bad request
    with OurDB() as our_db:
        fund_account(our_db, app_guid, amount)
        # FIXME: https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.products/consume
    return jsonify(success=True)