Skip to content

Commit

Permalink
updated to Android Billing 7 using https://github.com/code-with-max/g…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdugne committed Jul 14, 2024
1 parent 56894b8 commit ccc2b3e
Showing 1 changed file with 78 additions and 64 deletions.
142 changes: 78 additions & 64 deletions fox/stores/playstore.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ extends Node
# ------------------------------------------------------------------------------
# Android - Google Play Store
# ------------------------------------------------------------------------------
# reference: https://docs.godotengine.org/en/stable/tutorials/platform/android/android_in_app_purchases.html
# plugin: https://github.com/godotengine/godot-google-play-billing
# demo: https://github.com/godotengine/godot-demo-projects/blob/master/mobile/android_iap/iap_demo.gd
# plugin: https://github.com/code-with-max/godot-google-play-iapp
# response codes: https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponseCode
# ------------------------------------------------------------------------------

var playStore
Expand All @@ -21,76 +20,84 @@ signal skuDetailsReceived
# ------------------------------------------------------------------------------

func _ready():
if Engine.has_singleton('GodotGooglePlayBilling'):
if Engine.has_singleton('AndroidIAPP'):
G.log('-------------------------------')
G.log('✅ GodotGooglePlayBilling was found, connecting PlayStore.')
playStore = Engine.get_singleton('GodotGooglePlayBilling')
G.log('✅ AndroidIAPP was found, connecting PlayStore.')
playStore = Engine.get_singleton('AndroidIAPP')
connectPlayStore()

# ------------------------------------------------------------------------------

func connectPlayStore():
playStore.connect('connected', playStoreConnected) # No params
# playStore.connect('disconnected', _on_disconnected) # No params
playStore.connect('connect_error', playStoreErrorOnConnection) # Response ID (int), Debug message (string)
playStore.connect('query_purchases_response', receivedPurchases) # Purchases (Dictionary[])
playStore.connect('purchases_updated', onPurchasesUpdated) # Purchases (Dictionary[])
playStore.connect('purchase_error', onPurchaseError) # Response ID (int), Debug message (string)
playStore.connect('sku_details_query_completed', receivedSKUDetails) # SKUs (Dictionary[])
playStore.connect('sku_details_query_error', failedQueryingSKUDetails) # Response ID (int), Debug message (string), Queried SKUs (string[])
playStore.connect('purchase_acknowledged', onPurchaseAcknowledged) # Purchase token (string)
playStore.connect('purchase_acknowledgement_error', onPurchaseAcknowledgementError) # Response ID (int), Debug message (string), Purchase token (string)
playStore.connect('purchase_consumed', onPurchaseDone) # Purchase token (string)
playStore.connect('purchase_consumption_error', onPurchaseConsumptionError) # Response ID (int), Debug message (string), Purchase token (string)
# https://github.com/code-with-max/godot-google-play-iapp?tab=readme-ov-file#information-signals
playStore.connect('connected', playStoreConnected)

# https://github.com/code-with-max/godot-google-play-iapp?tab=readme-ov-file#billing-signals
playStore.connect('query_purchases', receivedPurchases)
playStore.connect('purchase_updated', onPurchasesUpdated)
playStore.connect('purchase_error', onPurchaseError)
playStore.connect('purchase_update_error', onPurchaseError)
playStore.connect('purchase_cancelled', onPurchaseCancelled)

playStore.connect('query_product_details', receivedProductDetails)
playStore.connect('query_product_details_error', failedQueryingProductDetails)
playStore.connect('purchase_acknowledged', onPurchaseAcknowledged)
playStore.connect('purchase_acknowledged_error', onPurchaseAcknowledgementError)
playStore.connect('purchase_consumed', onPurchaseDone)
playStore.connect('purchase_consumed_error', onPurchaseConsumptionError)

playStore.startConnection()

# ==============================================================================

func playStoreConnected():
queryingPurchasesAtStart = true

# Handling purchases made outside your app
# https://developer.android.com/google/play/billing/integrate#ooap
playStore.queryPurchases('inapp') # Use 'subs' for subscriptions.

# Show products available to buy
# https://developer.android.com/google/play/billing/integrate#show-products
for sku in G.STORE:
playStore.querySkuDetails([sku], 'inapp')

# ------------------------------------------------------------------------------

func playStoreErrorOnConnection(id, message):
G.log('🔴 PlayStore: Error on connection', id, message)
playStore.queryProductDetails([sku], 'inapp')

# ==============================================================================

# details: [{ "icon_url": "", "original_price": "0,99 €", "original_price_amount_micros": 990000, "introductory_price_period": "", "description": "blabla", "title": "blaplop", "type": "inapp", "price_amount_micros": 990000, "price_currency_code": "EUR", "introductory_price_cycles": 0, "introductory_price": "", "introductory_price_amount_micros": 0, "price": "0,99 €", "free_trial_period": "", "subscription_period": "", "sku": "xxx.xxx.xxx" }]
func receivedSKUDetails(details):
for receivedItem in details:
var item = G.STORE[receivedItem.sku]
item.price = receivedItem.price
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L262
func receivedProductDetails(response):
for receivedItem in response.product_details_list:
G.log('receivedItem', receivedItem);
var sku = receivedItem.product_id
var item = G.STORE[sku]
# https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/utils/IAPP_utils.kt#L93
item.price = receivedItem.one_time_purchase_offer_details.formatted_price

emit_signal('skuDetailsReceived')

# ------------------------------------------------------------------------------

func failedQueryingSKUDetails(response_id, error_message, products_queried):
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L268
func failedQueryingProductDetails(response):
G.log(
'🔴 PlayStore: error querying SKU details:', response_id,
' message: ', error_message,
" products: ", products_queried
'🔴 PlayStore: error querying product details:',
' message: ', response.debug_message,
)
G.log('Do you use the correct tester account?');

# ==============================================================================

func receivedPurchases(query_result):
G.log('✅ PlayStore: received ' + str(query_result.purchases.size()) + ' purchases')
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L211
func receivedPurchases(response):
G.log('✅ PlayStore: received ' + str(response.purchases_list.size()) + ' purchases')

if query_result.status == OK:
for _purchase in query_result.purchases:
if response.response_code == OK:
for _purchase in response.purchases_list:
# We must acknowledge all purchases.
# See https://developer.android.com/google/play/billing/integrate#process for more information

if not _purchase.is_acknowledged:
G.log('Purchase ' + str(_purchase.skus[0]) + ' has not been acknowledged. Acknowledging...')
G.log('Purchase ' + str(_purchase.products[0]) + ' has not been acknowledged. Acknowledging...')
playStore.acknowledgePurchase(_purchase.purchase_token)

# _purchase is_acknowledged but not consumed => either not consumed or not consumable
Expand All @@ -100,48 +107,51 @@ func receivedPurchases(query_result):

else:
G.log('🔴 PlayStore: queryPurchases failed, response code: ',
query_result.response_code,
response.response_code,
' debug message: ',
query_result.debug_message
__.Get('debug_message', response)
)

# ==============================================================================

func purchase(sku):
G.log('purchasing ', sku);
Router.showLoader()
var response = playStore.purchase(sku)
if response.status != OK:
G.log('🔴 Purchase could not be sent: error %s: %s' % [
response.response_code,
response.debug_message
])
playStore.purchase([sku], false)

# ------------------------------------------------------------------------------

# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L443
func onPurchaseCancelled(_response):
Router.hideLoader()

# ------------------------------------------------------------------------------

# codes: https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponseCode
func onPurchaseError(id, message):
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L337
func onPurchaseError(response):
Router.hideLoader()
match(id):
1:
G.log('> player cancelled')
return
var code = response.response_code
var id = response.product_id
var debug_message = response.debug_message
match(code):
3:
G.log('> paiement refused.')
return
7:
G.log('> player already owns this sku')
return
_:
G.log('🔴 Error during purchase', {id=id, message=message})
G.log('🔴 Error during purchase', {id=id, message=debug_message})

# ------------------------------------------------------------------------------

# https://developer.android.com/reference/com/android/billingclient/api/PurchasesUpdatedListener
# listener for purchases updates / initiated by a buy action from the game or the Play Store
# kind of the successfull callback for purchase()
func onPurchasesUpdated(purchases):
for _purchase in purchases:
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L438
func onPurchasesUpdated(response):
for _purchase in response.purchases_list:
var sku = JSON.parse_string(_purchase.original_json).productId
var purchaseToken = _purchase.purchase_token
storePurchaseToken(purchaseToken, sku)
Expand All @@ -151,17 +161,19 @@ func onPurchasesUpdated(purchases):

# ==============================================================================

func onPurchaseConsumptionError(id, message, purchaseToken):
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L540
func onPurchaseConsumptionError(response):
Router.hideLoader()
G.log('🔴 Error during consumption', {
id = id,
message = message,
purchaseToken = purchaseToken
message = response.debug_message,
purchaseToken = response.purchase_token
})

# ==============================================================================

func onPurchaseDone(purchaseToken):
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L535
func onPurchaseDone(response):
var purchaseToken = response.purchase_token
Router.hideLoader()

if(queryingPurchasesAtStart):
Expand Down Expand Up @@ -201,7 +213,9 @@ func foundPreviousPurchase(purchaseToken):

# ------------------------------------------------------------------------------

func onPurchaseAcknowledged(purchaseToken):
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L560
func onPurchaseAcknowledged(response):
var purchaseToken = response.purchase_token
var sku = _getSKUFromPurchaseToken(purchaseToken)
var storeItem = G.STORE[sku]

Expand All @@ -213,10 +227,10 @@ func onPurchaseAcknowledged(purchaseToken):

# ---------------

func onPurchaseAcknowledgementError(id, message, purchaseToken):
# response: https://github.com/code-with-max/godot-google-play-iapp/blob/be71531d191ad2a4989ba8658e40bee2f3367790/AndroidIAPP/src/main/java/one/allme/plugin/androidiapp/AndroidIAPP.kt#L565
func onPurchaseAcknowledgementError(response):
Router.hideLoader()
G.log('🔴 Purchase acknowledgement error:', {
id = id,
message = message,
purchaseToken = purchaseToken
message = response.debug_message,
purchaseToken = response.purchase_token
})

0 comments on commit ccc2b3e

Please sign in to comment.