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

Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
This commit is contained in:
JavierStark
2024-08-23 04:00:10 +02:00
committed by GitHub
parent 6c635fe75a
commit 13b2787173
9 changed files with 79 additions and 80 deletions

View File

@@ -1,6 +1,6 @@
<Project Sdk="Godot.NET.Sdk/4.0.0-dev5"> <Project Sdk="Godot.NET.Sdk/4.3.0">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net472</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<RootNamespace>AndroidIAP</RootNamespace> <RootNamespace>AndroidIAP</RootNamespace>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -51,8 +51,8 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
try try
{ {
Status = (int)billingResult["status"]; Status = (int)billingResult["status"];
ResponseCode = (billingResult.Contains("response_code") ? (BillingResponseCode?)billingResult["response_code"] : null); ResponseCode = billingResult.ContainsKey("response_code") ? billingResult["response_code"].As<BillingResponseCode>() : null;
DebugMessage = (billingResult.Contains("debug_message") ? (string)billingResult["debug_message"] : null); DebugMessage = billingResult.ContainsKey("debug_message") ? billingResult["debug_message"].AsString() : null;
} }
catch (System.Exception ex) catch (System.Exception ex)
{ {

View File

@@ -11,24 +11,24 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
public partial class GooglePlayBilling : Node public partial class GooglePlayBilling : Node
{ {
[Signal] public delegate void Connected(); [Signal] public delegate void ConnectedEventHandler();
[Signal] public delegate void Disconnected(); [Signal] public delegate void DisconnectedEventHandler();
[Signal] public delegate void ConnectError(int code, string message); [Signal] public delegate void ConnectErrorEventHandler(int code, string message);
[Signal] public delegate void SkuDetailsQueryCompleted(Array skuDetails); [Signal] public delegate void SkuDetailsQueryCompletedEventHandler(Array skuDetails);
[Signal] public delegate void SkuDetailsQueryError(int code, string message, string[] querySkuDetails); [Signal] public delegate void SkuDetailsQueryErrorEventHandler(int code, string message, string[] querySkuDetails);
[Signal] public delegate void PurchasesUpdated(Array purchases); [Signal] public delegate void PurchasesUpdatedEventHandler(Array purchases);
[Signal] public delegate void PurchaseError(int code, string message); [Signal] public delegate void PurchaseErrorEventHandler(int code, string message);
[Signal] public delegate void PurchaseAcknowledged(string purchaseToken); [Signal] public delegate void PurchaseAcknowledgedEventHandler(string purchaseToken);
[Signal] public delegate void PurchaseAcknowledgementError(int code, string message); [Signal] public delegate void PurchaseAcknowledgementErrorEventHandler(int code, string message);
[Signal] public delegate void PurchaseConsumed(string purchaseToken); [Signal] public delegate void PurchaseConsumedEventHandler(string purchaseToken);
[Signal] public delegate void PurchaseConsumptionError(int code, string message, string purchaseToken); [Signal] public delegate void PurchaseConsumptionErrorEventHandler(int code, string message, string purchaseToken);
[Export] public bool AutoReconnect { get; set; } [Export] public bool AutoReconnect { get; set; }
[Export] public bool AutoConnect { get; set; } [Export] public bool AutoConnect { get; set; }
public bool IsAvailable { get; private set; } public bool IsAvailable { get; private set; }
private Object _payment; private GodotObject _payment;
public override void _Ready() public override void _Ready()
{ {
@@ -38,17 +38,17 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
_payment = Engine.GetSingleton("GodotGooglePlayBilling"); _payment = Engine.GetSingleton("GodotGooglePlayBilling");
// These are all signals supported by the API // These are all signals supported by the API
// You can drop some of these based on your needs // You can drop some of these based on your needs
_payment.Connect("connected", this, nameof(OnGodotGooglePlayBilling_connected)); // No params _payment.Connect(SignalName.Connected, Callable.From(OnGodotGooglePlayBilling_connected)); // No params
_payment.Connect("disconnected", this, nameof(OnGodotGooglePlayBilling_disconnected)); // No params _payment.Connect(SignalName.Disconnected, Callable.From(OnGodotGooglePlayBilling_disconnected)); // No params
_payment.Connect("connect_error", this, nameof(OnGodotGooglePlayBilling_connect_error)); // Response ID (int), Debug message (string) _payment.Connect(SignalName.ConnectError, Callable.From<int, string>(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(SignalName.SkuDetailsQueryCompleted, Callable.From<Array>(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(SignalName.SkuDetailsQueryError, Callable.From<int,string,string[]>(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(SignalName.PurchasesUpdated, Callable.From<Array>(OnGodotGooglePlayBilling_purchases_updated)); // Purchases (Array of Dictionary)
_payment.Connect("purchase_error", this, nameof(OnGodotGooglePlayBilling_purchase_error)); // Response ID (int), Debug message (string) _payment.Connect(SignalName.PurchaseError, Callable.From<int,string>(OnGodotGooglePlayBilling_purchase_error)); // Response ID (int), Debug message (string)
_payment.Connect("purchase_acknowledged", this, nameof(OnGodotGooglePlayBilling_purchase_acknowledged)); // Purchase token (string) _payment.Connect(SignalName.PurchaseAcknowledged, Callable.From<string>(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(SignalName.PurchaseAcknowledgementError, Callable.From<int,string>(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(SignalName.PurchaseConsumed, Callable.From<string>(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.PurchaseConsumptionError, Callable.From<int,string,string>(OnGodotGooglePlayBilling_purchase_consumption_error)); // Response ID (int), Debug message (string), Purchase token (string)
} }
else else
{ {
@@ -64,7 +64,7 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
public void QuerySkuDetails(string[] querySkuDetails, PurchaseType type) => _payment?.Call("querySkuDetails", querySkuDetails, $"{type}".ToLower()); 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); public void AcknowledgePurchase(string purchaseToken) => _payment?.Call("acknowledgePurchase", purchaseToken);

View File

@@ -22,7 +22,7 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
{ {
try try
{ {
switch (key) switch (key.AsString())
{ {
case "order_id": case "order_id":
OrderId = (string)purchase[key]; OrderId = (string)purchase[key];
@@ -31,7 +31,7 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
PackageName = (string)purchase[key]; PackageName = (string)purchase[key];
break; break;
case "purchase_state": case "purchase_state":
PurchaseState = (PurchaseState)purchase[key]; PurchaseState = purchase[key].As<PurchaseState>();
break; break;
case "purchase_time": case "purchase_time":
PurchaseTime = Convert.ToInt64(purchase[key]); PurchaseTime = Convert.ToInt64(purchase[key]);

View File

@@ -11,7 +11,7 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
{ {
try 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) catch (System.Exception ex)
{ {

View File

@@ -14,7 +14,7 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
{ {
try try
{ {
switch (key) switch (key.AsString())
{ {
case "sku": case "sku":
Sku = (string)skuDetails[key]; Sku = (string)skuDetails[key];
@@ -62,7 +62,7 @@ namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
SubscriptionPeriod = (string)skuDetails[key]; SubscriptionPeriod = (string)skuDetails[key];
break; break;
case "type": case "type":
switch(skuDetails[key]) switch(skuDetails[key].AsString())
{ {
case "inapp": case "inapp":
Type = PurchaseType.InApp; Type = PurchaseType.InApp;

View File

@@ -2,6 +2,7 @@ using AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling;
using Godot; using Godot;
using System.Linq; using System.Linq;
using System; using System;
using Array = Godot.Collections.Array;
namespace AndroidInAppPurchasesWithCSharp namespace AndroidInAppPurchasesWithCSharp
{ {
@@ -27,27 +28,38 @@ namespace AndroidInAppPurchasesWithCSharp
_label.Text += $"\n\n\nTest item SKU: {TestItemSku}"; _label.Text += $"\n\n\nTest item SKU: {TestItemSku}";
// No params. // No params.
_payment.Connect(nameof(GooglePlayBilling.Connected), this, nameof(OnConnected)); _payment.Connect(GooglePlayBilling.SignalName.Connected,
Callable.From(OnConnected));
// No params. // No params.
_payment.Connect(nameof(GooglePlayBilling.Disconnected), this, nameof(OnDisconnected)); _payment.Connect(GooglePlayBilling.SignalName.Disconnected,
Callable.From(OnDisconnected));
// Response ID (int), Debug message (string). // 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[]). // 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). // 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[]). // 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[]). // 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). // Purchase token (string).
_payment.Connect(nameof(GooglePlayBilling.PurchaseAcknowledged), this, nameof(OnPurchaseAcknowledged)); _payment.Connect(GooglePlayBilling.SignalName.PurchaseAcknowledged,
// Response ID (int), Debug message (string), Purchase token (string). Callable.From<string>(OnPurchaseAcknowledged));
_payment.Connect(nameof(GooglePlayBilling.PurchaseAcknowledgementError), this, nameof(OnPurchaseAcknowledgementError)); // Response ID (int), Debug message (string).
_payment.Connect(GooglePlayBilling.SignalName.PurchaseAcknowledgementError,
Callable.From<int,string>(OnPurchaseAcknowledgementError));
// Purchase token (string). // 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). // 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(); _payment.StartConnection();
} }
else else
@@ -100,7 +112,7 @@ namespace AndroidInAppPurchasesWithCSharp
private void OnPurchasesUpdated(Godot.Collections.Array arrPurchases) private void OnPurchasesUpdated(Godot.Collections.Array arrPurchases)
{ {
GD.Print($"Purchases updated: {JSON.Print(arrPurchases)}"); GD.Print($"Purchases updated: {Json.Stringify(arrPurchases)}");
// See OnConnected // See OnConnected
var purchases = GooglePlayBillingUtils.ConvertPurchaseDictionaryArray(arrPurchases); var purchases = GooglePlayBillingUtils.ConvertPurchaseDictionaryArray(arrPurchases);
@@ -127,7 +139,7 @@ namespace AndroidInAppPurchasesWithCSharp
private void OnSkuDetailsQueryCompleted(Godot.Collections.Array arrSkuDetails) private void OnSkuDetailsQueryCompleted(Godot.Collections.Array arrSkuDetails)
{ {
ShowAlert(JSON.Print(arrSkuDetails)); ShowAlert(Json.Stringify(arrSkuDetails));
var skuDetails = GooglePlayBillingUtils.ConvertSkuDetailsDictionaryArray(arrSkuDetails); var skuDetails = GooglePlayBillingUtils.ConvertSkuDetailsDictionaryArray(arrSkuDetails);
foreach (var skuDetail in skuDetails) foreach (var skuDetail in skuDetails)

View File

@@ -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 type="Script" path="res://Main.cs" id="1"]
[ext_resource path="res://GodotGooglePlayBilling/GooglePlayBilling.cs" type="Script" id=2] [ext_resource type="Script" path="res://GodotGooglePlayBilling/GooglePlayBilling.cs" id="2"]
[node name="Main" type="Control"] [node name="Main" type="Control"]
layout_mode = 3
anchors_preset = 8
anchor_left = 0.5 anchor_left = 0.5
anchor_top = 0.5 anchor_top = 0.5
anchor_right = 0.5 anchor_right = 0.5
@@ -12,30 +14,17 @@ offset_left = -512.711
offset_top = -300.0 offset_top = -300.0
offset_right = 511.289 offset_right = 511.289
offset_bottom = 300.0 offset_bottom = 300.0
size_flags_horizontal = 2
size_flags_vertical = 2
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[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_horizontal = 2
grow_vertical = 2 grow_vertical = 2
rect_min_size = Vector2(400, 0) size_flags_horizontal = 2
size_flags_vertical = 4 size_flags_vertical = 2
popup_exclusive = true script = ExtResource("1")
[node name="AlertDialog" type="AcceptDialog" parent="."]
dialog_autowrap = true dialog_autowrap = true
[node name="Label" type="Label" parent="."] [node name="Label" type="Label" parent="."]
layout_mode = 0
offset_left = 300.0 offset_left = 300.0
offset_top = 40.0 offset_top = 40.0
offset_right = 996.0 offset_right = 996.0
@@ -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" 4. Changes you make in the Play Console may take some time before taking effect"
[node name="QuerySkuDetailsButton" type="Button" parent="."] [node name="QuerySkuDetailsButton" type="Button" parent="."]
layout_mode = 0
offset_left = 40.5697 offset_left = 40.5697
offset_top = 39.9347 offset_top = 39.9347
offset_right = 221.57 offset_right = 221.57
offset_bottom = 91.9347 offset_bottom = 91.9347
text = "Query SKU details" text = "Query SKU details"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="PurchaseButton" type="Button" parent="."] [node name="PurchaseButton" type="Button" parent="."]
layout_mode = 0
offset_left = 40.5697 offset_left = 40.5697
offset_top = 101.203 offset_top = 101.203
offset_right = 221.57 offset_right = 221.57
offset_bottom = 153.203 offset_bottom = 153.203
text = "Purchase" text = "Purchase"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ConsumeButton" type="Button" parent="."] [node name="ConsumeButton" type="Button" parent="."]
layout_mode = 0
offset_left = 40.5697 offset_left = 40.5697
offset_top = 162.142 offset_top = 162.142
offset_right = 221.57 offset_right = 221.57
offset_bottom = 214.142 offset_bottom = 214.142
text = "Consume" text = "Consume"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="GooglePlayBilling" type="Node" parent="."] [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="QuerySkuDetailsButton" to="." method="OnQuerySkuDetailsButton_pressed"]
[connection signal="pressed" from="PurchaseButton" to="." method="OnPurchaseButton_pressed"] [connection signal="pressed" from="PurchaseButton" to="." method="OnPurchaseButton_pressed"]

View File

@@ -16,7 +16,7 @@ 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." Note: Running the demo requires exporting and uploading the game to Google Play."
config/tags=PackedStringArray("demo", "mobile", "official", "porting") config/tags=PackedStringArray("demo", "mobile", "official", "porting")
run/main_scene="res://main.tscn" run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2") config/features=PackedStringArray("4.3", "C#")
config/icon="res://icon.webp" config/icon="res://icon.webp"
[display] [display]
@@ -24,6 +24,10 @@ config/icon="res://icon.webp"
window/stretch/mode="canvas_items" window/stretch/mode="canvas_items"
window/stretch/aspect="expand" window/stretch/aspect="expand"
[dotnet]
project/assembly_name="Android in-app purchases with C#"
[rendering] [rendering]
renderer/rendering_method="gl_compatibility" renderer/rendering_method="gl_compatibility"