Created
February 1, 2016 16:00
In app purchases in Godot Engine: Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Intent: interface to the GodotPayments module (remember to add the java path in the project settings android>modules ) | |
"Intent.gd" | |
extends Node | |
# Interface for the GodotPayments module | |
# To use extend this script and save the result on the Global singleton 'Data' | |
const STATIC_RESPONSE_NONE = 0 | |
const STATIC_RESPONSE_ALL = 1 | |
const STATIC_RESPONSE_PURCHASED = 2 | |
const STATIC_RESPONSE_CANCELED = 3 | |
const STATIC_RESPONSE_REFUNDED = 4 | |
const STATIC_RESPONSE_ITEM_UNAVAILABLE = 5 | |
var payments = null | |
var static_response = STATIC_RESPONSE_PURCHASED | |
# CALLBACKS: overwrite these | |
func do_intent(): | |
pass | |
func purchase_success(receipt, signature, sku): | |
pass | |
func purchase_fail(): | |
pass | |
func purchase_cancel(): | |
pass | |
func purchase_owned(sku): | |
pass | |
func consume_success(receipt, signature, sku): | |
pass | |
func has_purchased(receipt, signature, sku): | |
pass | |
# PUBLICS | |
func _ready(): | |
payments = Globals.get_singleton("GodotPayments") | |
func start_intent(): | |
if payments != null: | |
payments.setPurchaseCallbackId(get_instance_ID()) | |
do_intent() | |
func set_static_response(static_response_type): | |
static_response = static_response_type | |
func set_auto_consume(autoConsume): | |
if payments != null: | |
payments.setAutoConsume(autoConsume) | |
func request_purchased(): | |
if payments != null: | |
payments.requestPurchased() | |
func consume_unconsumed_purchases(): | |
if payments != null: | |
payments.consumeUnconsumedPurchases() | |
func purchase(item, payload = ""): | |
if payments != null: | |
payments.purchase(get_sku(item), payload) | |
func consume(item): | |
if payments != null: | |
payments.consume(item) | |
# PRIVATES: DO NOT OVERWRITE! | |
func get_sku(item): | |
if static_response == STATIC_RESPONSE_NONE: | |
return item | |
elif static_response == STATIC_RESPONSE_ALL: | |
var i = randi() % 4 | |
if i == 0: | |
return "android.test.purchased" | |
elif i == 1: | |
return "android.test.canceled" | |
elif i == 2: | |
return "android.test.refunded" | |
elif i == 3: | |
return "android.test.item_unavailable" | |
elif static_response == STATIC_RESPONSE_PURCHASED: | |
return "android.test.purchased" | |
elif static_response == STATIC_RESPONSE_CANCELED: | |
return "android.test.canceled" | |
elif static_response == STATIC_RESPONSE_REFUNDED: | |
return "android.test.refunded" | |
elif static_response == STATIC_RESPONSE_ITEM_UNAVAILABLE: | |
return "android.test.item_unavailable" | |
# EXAMPLE 1: An intent to consume previous purchases on startup: | |
extends "Intent.gd" | |
func _ready(): | |
start_intent() | |
func do_intent(): | |
consume_unconsumed_purchases() | |
func consume_success(receipt, signature, sku): | |
print("consuming previous purchased: ", sku, ", receipt: ", receipt, ", signature: ", signature) | |
if sku == "power" or sku == "android.test.purchased": | |
var c = Globals.get("Data").get("power") + 1 | |
Globals.get("Data").set("power", c) | |
Globals.get("Data").flush_database() | |
# EXAMPLE 2: Buy an item | |
func _on_buyButton_pressed(): | |
if OS.get_name().findn("android") >= 0: | |
get_node("buyPower").start_intent() | |
# On node 'buyPower' (extends Intent.gd): | |
func buyPower(): | |
purchase("power") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a cool script but I tried to install everything as you explained it in the file and line 45 at get_singleton("GodotPayments") I always get [Object:Null] as the value of payments...
Do you have any idea what I did wrong?