iap.monkey2 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. Namespace iap
  2. #If __MOBILE_TARGET__
  3. #Import "<std>"
  4. #Import "<mojo>"
  5. #If __TARGET__="android"
  6. #Import "iap_android"
  7. #Elseif __TARGET__="ios"
  8. #Import "iap_ios"
  9. #Endif
  10. Using std..
  11. Using mojo..
  12. Enum ProductType
  13. Consumable=1
  14. NonConsumable=2
  15. End
  16. Class IAPStore
  17. Field OpenStoreComplete:Void( result:Int,interrupted:Product[],owned:Product[] )
  18. Field BuyProductComplete:Void( result:Int,product:Product )
  19. Field GetOwnedProductsComplete:Void( result:Int,owned:Product[] )
  20. Method New()
  21. _iap=New IAPStoreRep
  22. End
  23. Property Open:Bool()
  24. Return _state>0
  25. End
  26. Property Busy:Bool()
  27. Return _state>1
  28. End
  29. Method OpenStore( products:Product[] )
  30. If _state<>0 Return
  31. _products=products
  32. _state=2
  33. App.Idle+=UpdateState
  34. If _iap.OpenStoreAsync( _products ) Return
  35. App.Idle-=UpdateState
  36. _state=0
  37. OpenStoreComplete( -1,Null,Null )
  38. End
  39. Method BuyProduct( product:Product )
  40. If _state<>1 Return
  41. Print "_state="+_state
  42. _buying=product
  43. _state=3
  44. App.Idle+=UpdateState
  45. If _iap.BuyProductAsync( _buying ) Return
  46. App.Idle-=UpdateState
  47. _buying=Null
  48. _state=1
  49. BuyProductComplete( -1,product )
  50. End
  51. Method GetOwnedProducts()
  52. If _state<>1 Return
  53. _state=4
  54. App.Idle+=UpdateState
  55. If _iap.GetOwnedProductsAsync() Return
  56. App.Idle-=UpdateState
  57. _state=1
  58. GetOwnedProductsComplete( -1,Null )
  59. End
  60. Method CloseStore()
  61. If _state<>1 Return
  62. _iap.CloseStore()
  63. _iap=Null
  64. _state=-1
  65. End
  66. Function CanMakePayments:Bool()
  67. Return IAPStoreRep.CanMakePayments()
  68. End
  69. Private
  70. Field _products:Product[]
  71. Field _iap:IAPStoreRep
  72. Field _state:=0
  73. Field _buying:Product
  74. Method UpdateState()
  75. If _iap.IsRunning()
  76. App.Idle+=UpdateState
  77. Return
  78. Endif
  79. Local result:=_iap.GetResult()
  80. Local state:=_state
  81. _state=1
  82. Select state
  83. Case 2 'openstore
  84. If result<0 _state=0
  85. Local interrupted:=New Stack<Product>
  86. Local owned:=New Stack<Product>
  87. For Local product:=Eachin _products
  88. If product.Interrupted interrupted.Push( product )
  89. If product.Owned owned.Push( product )
  90. Next
  91. OpenStoreComplete( result,interrupted.ToArray(),owned.ToArray() )
  92. Case 3 'buyproduct
  93. Local buying:=_buying
  94. _buying=Null
  95. BuyProductComplete( result,buying )
  96. Case 4 'GetOwnedProducts
  97. Local owned:=New Stack<Product>
  98. For Local product:=Eachin _products
  99. If product.Owned owned.Push( product )
  100. Next
  101. GetOwnedProductsComplete( result,owned.ToArray() )
  102. End
  103. End
  104. End
  105. #Endif