GooglePlayBilling.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Godot.Collections;
  2. using Godot;
  3. namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling
  4. {
  5. public enum PurchaseType
  6. {
  7. InApp,
  8. Subs,
  9. }
  10. public partial class GooglePlayBilling : Node
  11. {
  12. [Signal] public delegate void Connected();
  13. [Signal] public delegate void Disconnected();
  14. [Signal] public delegate void ConnectError(int code, string message);
  15. [Signal] public delegate void SkuDetailsQueryCompleted(Array skuDetails);
  16. [Signal] public delegate void SkuDetailsQueryError(int code, string message, string[] querySkuDetails);
  17. [Signal] public delegate void PurchasesUpdated(Array purchases);
  18. [Signal] public delegate void PurchaseError(int code, string message);
  19. [Signal] public delegate void PurchaseAcknowledged(string purchaseToken);
  20. [Signal] public delegate void PurchaseAcknowledgementError(int code, string message);
  21. [Signal] public delegate void PurchaseConsumed(string purchaseToken);
  22. [Signal] public delegate void PurchaseConsumptionError(int code, string message, string purchaseToken);
  23. [Export] public bool AutoReconnect { get; set; }
  24. [Export] public bool AutoConnect { get; set; }
  25. public bool IsAvailable { get; private set; }
  26. private Object _payment;
  27. public override void _Ready()
  28. {
  29. if (Engine.HasSingleton("GodotGooglePlayBilling"))
  30. {
  31. IsAvailable = true;
  32. _payment = Engine.GetSingleton("GodotGooglePlayBilling");
  33. // These are all signals supported by the API
  34. // You can drop some of these based on your needs
  35. _payment.Connect("connected", this, nameof(OnGodotGooglePlayBilling_connected)); // No params
  36. _payment.Connect("disconnected", this, nameof(OnGodotGooglePlayBilling_disconnected)); // No params
  37. _payment.Connect("connect_error", this, nameof(OnGodotGooglePlayBilling_connect_error)); // Response ID (int), Debug message (string)
  38. _payment.Connect("sku_details_query_completed", this, nameof(OnGodotGooglePlayBilling_sku_details_query_completed)); // SKUs (Array of Dictionary)
  39. _payment.Connect("sku_details_query_error", this, nameof(OnGodotGooglePlayBilling_sku_details_query_error)); // Response ID (int), Debug message (string), Queried SKUs (string[])
  40. _payment.Connect("purchases_updated", this, nameof(OnGodotGooglePlayBilling_purchases_updated)); // Purchases (Array of Dictionary)
  41. _payment.Connect("purchase_error", this, nameof(OnGodotGooglePlayBilling_purchase_error)); // Response ID (int), Debug message (string)
  42. _payment.Connect("purchase_acknowledged", this, nameof(OnGodotGooglePlayBilling_purchase_acknowledged)); // Purchase token (string)
  43. _payment.Connect("purchase_acknowledgement_error", this, nameof(OnGodotGooglePlayBilling_purchase_acknowledgement_error)); // Response ID (int), Debug message (string), Purchase token (string)
  44. _payment.Connect("purchase_consumed", this, nameof(OnGodotGooglePlayBilling_purchase_consumed)); // Purchase token (string)
  45. _payment.Connect("purchase_consumption_error", this, nameof(OnGodotGooglePlayBilling_purchase_consumption_error)); // Response ID (int), Debug message (string), Purchase token (string)
  46. }
  47. else
  48. {
  49. IsAvailable = false;
  50. }
  51. }
  52. #region GooglePlayBilling Methods
  53. public void StartConnection() => _payment?.Call("startConnection");
  54. public void EndConnection() => _payment?.Call("endConnection");
  55. public void QuerySkuDetails(string[] querySkuDetails, PurchaseType type) => _payment?.Call("querySkuDetails", querySkuDetails, $"{type}".ToLower());
  56. public bool IsReady() => (_payment?.Call("isReady") as bool?) ?? false;
  57. public void AcknowledgePurchase(string purchaseToken) => _payment?.Call("acknowledgePurchase", purchaseToken);
  58. public void ConsumePurchase(string purchaseToken) => _payment?.Call("consumePurchase", purchaseToken);
  59. public BillingResult Purchase(string sku)
  60. {
  61. if (_payment == null) return null;
  62. var result = (Dictionary)_payment.Call("purchase", sku);
  63. return new BillingResult(result);
  64. }
  65. public PurchasesResult QueryPurchases(PurchaseType purchaseType)
  66. {
  67. if (_payment == null) return null;
  68. var result = (Dictionary)_payment.Call("queryPurchases", $"{purchaseType}".ToLower());
  69. return new PurchasesResult(result);
  70. }
  71. #endregion
  72. #region GodotGooglePlayBilling Signals
  73. private void OnGodotGooglePlayBilling_connected() => EmitSignal(nameof(Connected));
  74. private void OnGodotGooglePlayBilling_disconnected() => EmitSignal(nameof(Disconnected));
  75. private void OnGodotGooglePlayBilling_connect_error(int code, string message) => EmitSignal(nameof(ConnectError), code, message);
  76. private void OnGodotGooglePlayBilling_sku_details_query_completed(Array skuDetails) => EmitSignal(nameof(SkuDetailsQueryCompleted), skuDetails);
  77. private void OnGodotGooglePlayBilling_sku_details_query_error(int code, string message, string[] querySkuDetails) => EmitSignal(nameof(SkuDetailsQueryError), code, message, querySkuDetails);
  78. private void OnGodotGooglePlayBilling_purchases_updated(Array purchases) => EmitSignal(nameof(PurchasesUpdated), purchases);
  79. private void OnGodotGooglePlayBilling_purchase_error(int code, string message) => EmitSignal(nameof(PurchaseError), code, message);
  80. private void OnGodotGooglePlayBilling_purchase_acknowledged(string purchaseToken) => EmitSignal(nameof(PurchaseAcknowledged), purchaseToken);
  81. private void OnGodotGooglePlayBilling_purchase_acknowledgement_error(int code, string message) => EmitSignal(nameof(PurchaseAcknowledgementError), code, message);
  82. private void OnGodotGooglePlayBilling_purchase_consumed(string purchaseToken) => EmitSignal(nameof(PurchaseConsumed), purchaseToken);
  83. private void OnGodotGooglePlayBilling_purchase_consumption_error(int code, string message, string purchaseToken) => EmitSignal(nameof(PurchaseConsumptionError), code, message, purchaseToken);
  84. #endregion
  85. }
  86. }