Skip to content

Commit

Permalink
moved purchase tokens handling within fox.playstore
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdugne committed Apr 26, 2024
1 parent 8badab6 commit 3f6aaac
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
13 changes: 13 additions & 0 deletions docs/exporting/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ ios/plugins
├── inappstore.gdip
└── inappstore.release.xcframework
```

## install and run a debug build on a device

```sh
> xcrun devicectl list devices
> xcrun devicectl device install app --device XXXXXXX _build/iOS/battle-squares-debug.app
> xcrun devicectl device process launch --device XXXXXXX com.uralys.battlesquares
```

```sh
> brew install libimobiledevice
> idevicesyslog
```
83 changes: 57 additions & 26 deletions fox/stores/playstore.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extends Node

var playStore
var queryingPurchasesAtStart = false
var purchasesToConsume = {}

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

Expand All @@ -22,7 +23,7 @@ signal skuDetailsReceived
func _ready():
if Engine.has_singleton('GodotGooglePlayBilling'):
G.log('-------------------------------')
G.log('✅ PlayStore starting: GodotGooglePlayBilling was found')
G.log('✅ GodotGooglePlayBilling was found, connecting PlayStore.')
playStore = Engine.get_singleton('GodotGooglePlayBilling')
connectPlayStore()

Expand Down Expand Up @@ -94,7 +95,7 @@ func receivedPurchases(query_result):

# _purchase is_acknowledged but not consumed => either not consumed or not consumable
elif _purchase.purchase_state == 1:
Player.storePurchaseToken(_purchase.purchase_token, _purchase.sku)
storePurchaseToken(_purchase.purchase_token, _purchase.sku)
onPurchaseAcknowledged(_purchase.purchase_token)

else:
Expand All @@ -106,27 +107,6 @@ func receivedPurchases(query_result):

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

func onPurchaseAcknowledged(purchaseToken):
var sku = Player.getSKUFromPurchaseToken(purchaseToken)
var storeItem = G.STORE[sku]

if(storeItem.isConsumable):
playStore.consumePurchase(purchaseToken)
else:
onPurchaseDone(purchaseToken)

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

func onPurchaseAcknowledgementError(id, message, purchaseToken):
Router.hideLoader()
G.log('🔴 Purchase acknowledgement error:', {
id = id,
message = message,
purchaseToken = purchaseToken
})

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

func purchase(sku):
G.log('purchasing ', sku);
Router.showLoader()
Expand Down Expand Up @@ -164,7 +144,7 @@ func onPurchasesUpdated(purchases):
for _purchase in purchases:
var sku = JSON.parse_string(_purchase.original_json).productId
var purchaseToken = _purchase.purchase_token
Player.storePurchaseToken(purchaseToken, sku)
storePurchaseToken(purchaseToken, sku)

playStore.queryPurchases('inapp')

Expand All @@ -184,6 +164,57 @@ func onPurchaseDone(purchaseToken):
Router.hideLoader()

if(queryingPurchasesAtStart):
Player.foundPreviousPurchase(purchaseToken)
foundPreviousPurchase(purchaseToken)
else:
Player.onPurchaseCompleted(purchaseToken)
onPurchaseCompleted(purchaseToken)

# ==============================================================================
# android purchases backup waiting to be acknowleged
# ==============================================================================

func storePurchaseToken(purchaseToken, sku):
Player.state.purchasesToConsume[purchaseToken] = sku
Player.save()

func _getSKUFromPurchaseToken(purchaseToken):
var sku = Player.state.purchasesProcessing[purchaseToken]
return sku

func setPurchaseProcessed(purchaseToken):
Player.state.purchasesProcessing.erase(purchaseToken)
Player.save()

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

func onPurchaseCompleted(purchaseToken):
setPurchaseProcessed(purchaseToken)
var sku = _getSKUFromPurchaseToken(purchaseToken)
Player.bought(sku)

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

func foundPreviousPurchase(purchaseToken):
setPurchaseProcessed(purchaseToken)
var sku = _getSKUFromPurchaseToken(purchaseToken)
Player.previouslyBought(sku)

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

func onPurchaseAcknowledged(purchaseToken):
var sku = _getSKUFromPurchaseToken(purchaseToken)
var storeItem = G.STORE[sku]

if(storeItem.isConsumable):
playStore.consumePurchase(purchaseToken)
else:
onPurchaseDone(purchaseToken)

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

func onPurchaseAcknowledgementError(id, message, purchaseToken):
Router.hideLoader()
G.log('🔴 Purchase acknowledgement error:', {
id = id,
message = message,
purchaseToken = purchaseToken
})

1 comment on commit 3f6aaac

@chrisdugne
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#12

Please sign in to comment.