Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Android IAP in C# project to Godot 4.3 #1104

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mono/android_iap/Android in-app purchases with C#.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Godot.NET.Sdk/4.0.0-dev5">
<Project Sdk="Godot.NET.Sdk/4.3.0">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>AndroidIAP</RootNamespace>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions mono/android_iap/GodotGooglePlayBilling/BillingResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public BillingResult(Dictionary billingResult)
try
{
Status = (int)billingResult["status"];
ResponseCode = (billingResult.Contains("response_code") ? (BillingResponseCode?)billingResult["response_code"] : null);
DebugMessage = (billingResult.Contains("debug_message") ? (string)billingResult["debug_message"] : null);
ResponseCode = billingResult.ContainsKey("response_code") ? billingResult["response_code"].As<BillingResponseCode>() : null;
DebugMessage = billingResult.ContainsKey("debug_message") ? billingResult["debug_message"].AsString() : null;
}
catch (System.Exception ex)
{
Expand Down
48 changes: 24 additions & 24 deletions mono/android_iap/GodotGooglePlayBilling/GooglePlayBilling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ public enum PurchaseType

public partial class GooglePlayBilling : Node
{
[Signal] public delegate void Connected();
[Signal] public delegate void Disconnected();
[Signal] public delegate void ConnectError(int code, string message);
[Signal] public delegate void SkuDetailsQueryCompleted(Array skuDetails);
[Signal] public delegate void SkuDetailsQueryError(int code, string message, string[] querySkuDetails);
[Signal] public delegate void PurchasesUpdated(Array purchases);
[Signal] public delegate void PurchaseError(int code, string message);
[Signal] public delegate void PurchaseAcknowledged(string purchaseToken);
[Signal] public delegate void PurchaseAcknowledgementError(int code, string message);
[Signal] public delegate void PurchaseConsumed(string purchaseToken);
[Signal] public delegate void PurchaseConsumptionError(int code, string message, string purchaseToken);
[Signal] public delegate void ConnectedEventHandler();
[Signal] public delegate void DisconnectedEventHandler();
[Signal] public delegate void ConnectErrorEventHandler(int code, string message);
[Signal] public delegate void SkuDetailsQueryCompletedEventHandler(Array skuDetails);
[Signal] public delegate void SkuDetailsQueryErrorEventHandler(int code, string message, string[] querySkuDetails);
[Signal] public delegate void PurchasesUpdatedEventHandler(Array purchases);
[Signal] public delegate void PurchaseErrorEventHandler(int code, string message);
[Signal] public delegate void PurchaseAcknowledgedEventHandler(string purchaseToken);
[Signal] public delegate void PurchaseAcknowledgementErrorEventHandler(int code, string message);
[Signal] public delegate void PurchaseConsumedEventHandler(string purchaseToken);
[Signal] public delegate void PurchaseConsumptionErrorEventHandler(int code, string message, string purchaseToken);

[Export] public bool AutoReconnect { get; set; }
[Export] public bool AutoConnect { get; set; }

public bool IsAvailable { get; private set; }

private Object _payment;
private GodotObject _payment;

public override void _Ready()
{
Expand All @@ -38,17 +38,17 @@ public override void _Ready()
_payment = Engine.GetSingleton("GodotGooglePlayBilling");
// These are all signals supported by the API
// You can drop some of these based on your needs
_payment.Connect("connected", this, nameof(OnGodotGooglePlayBilling_connected)); // No params
_payment.Connect("disconnected", this, nameof(OnGodotGooglePlayBilling_disconnected)); // No params
_payment.Connect("connect_error", this, nameof(OnGodotGooglePlayBilling_connect_error)); // Response ID (int), Debug message (string)
_payment.Connect("sku_details_query_completed", this, nameof(OnGodotGooglePlayBilling_sku_details_query_completed)); // SKUs (Array of Dictionary)
_payment.Connect("sku_details_query_error", this, nameof(OnGodotGooglePlayBilling_sku_details_query_error)); // Response ID (int), Debug message (string), Queried SKUs (string[])
_payment.Connect("purchases_updated", this, nameof(OnGodotGooglePlayBilling_purchases_updated)); // Purchases (Array of Dictionary)
_payment.Connect("purchase_error", this, nameof(OnGodotGooglePlayBilling_purchase_error)); // Response ID (int), Debug message (string)
_payment.Connect("purchase_acknowledged", this, nameof(OnGodotGooglePlayBilling_purchase_acknowledged)); // Purchase token (string)
_payment.Connect("purchase_acknowledgement_error", this, nameof(OnGodotGooglePlayBilling_purchase_acknowledgement_error)); // Response ID (int), Debug message (string), Purchase token (string)
_payment.Connect("purchase_consumed", this, nameof(OnGodotGooglePlayBilling_purchase_consumed)); // Purchase token (string)
_payment.Connect("purchase_consumption_error", this, nameof(OnGodotGooglePlayBilling_purchase_consumption_error)); // Response ID (int), Debug message (string), Purchase token (string)
_payment.Connect(SignalName.Connected, Callable.From(OnGodotGooglePlayBilling_connected)); // No params
_payment.Connect(SignalName.Disconnected, Callable.From(OnGodotGooglePlayBilling_disconnected)); // No params
_payment.Connect(SignalName.ConnectError, Callable.From<int, string>(OnGodotGooglePlayBilling_connect_error)); // Response ID (int), Debug message (string)
_payment.Connect(SignalName.SkuDetailsQueryCompleted, Callable.From<Array>(OnGodotGooglePlayBilling_sku_details_query_completed)); // SKUs (Array of Dictionary)
_payment.Connect(SignalName.SkuDetailsQueryError, Callable.From<int,string,string[]>(OnGodotGooglePlayBilling_sku_details_query_error)); // Response ID (int), Debug message (string), Queried SKUs (string[])
_payment.Connect(SignalName.PurchasesUpdated, Callable.From<Array>(OnGodotGooglePlayBilling_purchases_updated)); // Purchases (Array of Dictionary)
_payment.Connect(SignalName.PurchaseError, Callable.From<int,string>(OnGodotGooglePlayBilling_purchase_error)); // Response ID (int), Debug message (string)
_payment.Connect(SignalName.PurchaseAcknowledged, Callable.From<string>(OnGodotGooglePlayBilling_purchase_acknowledged)); // Purchase token (string)
_payment.Connect(SignalName.PurchaseAcknowledgementError, Callable.From<int,string>(OnGodotGooglePlayBilling_purchase_acknowledgement_error)); // Response ID (int), Debug message (string), Purchase token (string)
_payment.Connect(SignalName.PurchaseConsumed, Callable.From<string>(OnGodotGooglePlayBilling_purchase_consumed)); // Purchase token (string)
_payment.Connect(SignalName.PurchaseConsumptionError, Callable.From<int,string,string>(OnGodotGooglePlayBilling_purchase_consumption_error)); // Response ID (int), Debug message (string), Purchase token (string)
}
else
{
Expand All @@ -64,7 +64,7 @@ public override void _Ready()

public void QuerySkuDetails(string[] querySkuDetails, PurchaseType type) => _payment?.Call("querySkuDetails", querySkuDetails, $"{type}".ToLower());

public bool IsReady() => (_payment?.Call("isReady") as bool?) ?? false;
public bool IsReady() => _payment?.Call("isReady").AsBool() ?? false;

public void AcknowledgePurchase(string purchaseToken) => _payment?.Call("acknowledgePurchase", purchaseToken);

Expand Down
4 changes: 2 additions & 2 deletions mono/android_iap/GodotGooglePlayBilling/Purchase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Purchase(Dictionary purchase)
{
try
{
switch (key)
switch (key.AsString())
{
case "order_id":
OrderId = (string)purchase[key];
Expand All @@ -31,7 +31,7 @@ public Purchase(Dictionary purchase)
PackageName = (string)purchase[key];
break;
case "purchase_state":
PurchaseState = (PurchaseState)purchase[key];
PurchaseState = purchase[key].As<PurchaseState>();
break;
case "purchase_time":
PurchaseTime = Convert.ToInt64(purchase[key]);
Expand Down
2 changes: 1 addition & 1 deletion mono/android_iap/GodotGooglePlayBilling/PurchasesResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public PurchasesResult(Dictionary purchasesResult)
{
try
{
Purchases = (purchasesResult.Contains("purchases") ? GooglePlayBillingUtils.ConvertPurchaseDictionaryArray((Array)purchasesResult["purchases"]) : null);
Purchases = (purchasesResult.ContainsKey("purchases") ? GooglePlayBillingUtils.ConvertPurchaseDictionaryArray(purchasesResult["purchases"].AsGodotArray()) : null);
}
catch (System.Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions mono/android_iap/GodotGooglePlayBilling/SkuDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public SkuDetails(Dictionary skuDetails)
{
try
{
switch (key)
switch (key.AsString())
{
case "sku":
Sku = (string)skuDetails[key];
Expand Down Expand Up @@ -62,7 +62,7 @@ public SkuDetails(Dictionary skuDetails)
SubscriptionPeriod = (string)skuDetails[key];
break;
case "type":
switch(skuDetails[key])
switch(skuDetails[key].AsString())
{
case "inapp":
Type = PurchaseType.InApp;
Expand Down
40 changes: 26 additions & 14 deletions mono/android_iap/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Godot;
using System.Linq;
using System;
using Array = Godot.Collections.Array;

namespace AndroidInAppPurchasesWithCSharp
{
Expand All @@ -27,27 +28,38 @@ public override void _Ready()
_label.Text += $"\n\n\nTest item SKU: {TestItemSku}";

// No params.
_payment.Connect(nameof(GooglePlayBilling.Connected), this, nameof(OnConnected));
_payment.Connect(GooglePlayBilling.SignalName.Connected,
Callable.From(OnConnected));
// No params.
_payment.Connect(nameof(GooglePlayBilling.Disconnected), this, nameof(OnDisconnected));
_payment.Connect(GooglePlayBilling.SignalName.Disconnected,
Callable.From(OnDisconnected));
// Response ID (int), Debug message (string).
_payment.Connect(nameof(GooglePlayBilling.ConnectError), this, nameof(OnConnectError));
_payment.Connect(GooglePlayBilling.SignalName.ConnectError,
Callable.From<int,string>(OnConnectError));
// Purchases (Dictionary[]).
_payment.Connect(nameof(GooglePlayBilling.PurchasesUpdated), this, nameof(OnPurchasesUpdated));
_payment.Connect(GooglePlayBilling.SignalName.PurchasesUpdated,
Callable.From<Array>(OnPurchasesUpdated));
// Response ID (int), Debug message (string).
_payment.Connect(nameof(GooglePlayBilling.PurchaseError), this, nameof(OnPurchaseError));
_payment.Connect(GooglePlayBilling.SignalName.PurchaseError,
Callable.From<int,string>(OnPurchaseError));
// SKUs (Dictionary[]).
_payment.Connect(nameof(GooglePlayBilling.SkuDetailsQueryCompleted), this, nameof(OnSkuDetailsQueryCompleted));
_payment.Connect(GooglePlayBilling.SignalName.SkuDetailsQueryCompleted,
Callable.From<Array>(OnSkuDetailsQueryCompleted));
// Response ID (int), Debug message (string), Queried SKUs (string[]).
_payment.Connect(nameof(GooglePlayBilling.SkuDetailsQueryError), this, nameof(OnSkuDetailsQueryError));
_payment.Connect(GooglePlayBilling.SignalName.SkuDetailsQueryError,
Callable.From<int,string, string[]>(OnSkuDetailsQueryError));
// Purchase token (string).
_payment.Connect(nameof(GooglePlayBilling.PurchaseAcknowledged), this, nameof(OnPurchaseAcknowledged));
// Response ID (int), Debug message (string), Purchase token (string).
_payment.Connect(nameof(GooglePlayBilling.PurchaseAcknowledgementError), this, nameof(OnPurchaseAcknowledgementError));
_payment.Connect(GooglePlayBilling.SignalName.PurchaseAcknowledged,
Callable.From<string>(OnPurchaseAcknowledged));
// Response ID (int), Debug message (string).
_payment.Connect(GooglePlayBilling.SignalName.PurchaseAcknowledgementError,
Callable.From<int,string>(OnPurchaseAcknowledgementError));
// Purchase token (string).
_payment.Connect(nameof(GooglePlayBilling.PurchaseConsumed), this, nameof(OnPurchaseConsumed));
_payment.Connect(GooglePlayBilling.SignalName.PurchaseConsumed,
Callable.From<string>(OnPurchaseConsumed));
// Response ID (int), Debug message (string), Purchase token (string).
_payment.Connect(nameof(GooglePlayBilling.PurchaseConsumptionError), this, nameof(OnPurchaseConsumptionError));
_payment.Connect(GooglePlayBilling.SignalName.PurchaseConsumptionError,
Callable.From<int,string,string>(OnPurchaseConsumptionError));
_payment.StartConnection();
}
else
Expand Down Expand Up @@ -100,7 +112,7 @@ private void OnConnectError(int code, string message)

private void OnPurchasesUpdated(Godot.Collections.Array arrPurchases)
{
GD.Print($"Purchases updated: {JSON.Print(arrPurchases)}");
GD.Print($"Purchases updated: {Json.Stringify(arrPurchases)}");

// See OnConnected
var purchases = GooglePlayBillingUtils.ConvertPurchaseDictionaryArray(arrPurchases);
Expand All @@ -127,7 +139,7 @@ private void OnPurchaseError(int code, string message)

private void OnSkuDetailsQueryCompleted(Godot.Collections.Array arrSkuDetails)
{
ShowAlert(JSON.Print(arrSkuDetails));
ShowAlert(Json.Stringify(arrSkuDetails));

var skuDetails = GooglePlayBillingUtils.ConvertSkuDetailsDictionaryArray(arrSkuDetails);
foreach (var skuDetail in skuDetails)
Expand Down
43 changes: 13 additions & 30 deletions mono/android_iap/main.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=3 format=3 uid="uid://ckuhu5rgquv8h"]

[ext_resource path="res://Main.cs" type="Script" id=1]
[ext_resource path="res://GodotGooglePlayBilling/GooglePlayBilling.cs" type="Script" id=2]
[ext_resource type="Script" path="res://Main.cs" id="1"]
[ext_resource type="Script" path="res://GodotGooglePlayBilling/GooglePlayBilling.cs" id="2"]

[node name="Main" type="Control"]
layout_mode = 3
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
Expand All @@ -12,30 +14,17 @@ offset_left = -512.711
offset_top = -300.0
offset_right = 511.289
offset_bottom = 300.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 2
size_flags_vertical = 2
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
script = ExtResource("1")

[node name="AlertDialog" type="AcceptDialog" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = 64.0
offset_top = 64.0
offset_right = -64.0
offset_bottom = -64.0
grow_horizontal = 2
grow_vertical = 2
rect_min_size = Vector2(400, 0)
size_flags_vertical = 4
popup_exclusive = true
dialog_autowrap = true

[node name="Label" type="Label" parent="."]
layout_mode = 0
offset_left = 300.0
offset_top = 40.0
offset_right = 996.0
Expand All @@ -51,37 +40,31 @@ text = "To test in-app purchase on android device,
4. Changes you make in the Play Console may take some time before taking effect"

[node name="QuerySkuDetailsButton" type="Button" parent="."]
layout_mode = 0
offset_left = 40.5697
offset_top = 39.9347
offset_right = 221.57
offset_bottom = 91.9347
text = "Query SKU details"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="PurchaseButton" type="Button" parent="."]
layout_mode = 0
offset_left = 40.5697
offset_top = 101.203
offset_right = 221.57
offset_bottom = 153.203
text = "Purchase"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="ConsumeButton" type="Button" parent="."]
layout_mode = 0
offset_left = 40.5697
offset_top = 162.142
offset_right = 221.57
offset_bottom = 214.142
text = "Consume"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="GooglePlayBilling" type="Node" parent="."]
script = ExtResource( 2 )
script = ExtResource("2")

[connection signal="pressed" from="QuerySkuDetailsButton" to="." method="OnQuerySkuDetailsButton_pressed"]
[connection signal="pressed" from="PurchaseButton" to="." method="OnPurchaseButton_pressed"]
Expand Down
6 changes: 5 additions & 1 deletion mono/android_iap/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ config/description="This demo shows how to make in-app payments in Android in C#
Note: Running the demo requires exporting and uploading the game to Google Play."
config/tags=PackedStringArray("demo", "mobile", "official", "porting")
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2")
config/features=PackedStringArray("4.3", "C#")
config/icon="res://icon.webp"

[display]

window/stretch/mode="canvas_items"
window/stretch/aspect="expand"

[dotnet]

project/assembly_name="Android in-app purchases with C#"

[rendering]

renderer/rendering_method="gl_compatibility"
Expand Down