Chris Drosos

Chris Drosos

  • NA
  • 39
  • 654

Converting InAppBilling.Plugin to Amazon

Jun 2 2020 8:51 AM
Hello Guys,
I have create a Android application in Xamarin and i would like to also have in on Amazon store.
It uses in App purchases
Currently im implementing those purchases with this code:
  1. public async Task<bool> WasItemPurchased(string productId)  
  2. {  
  3. var billing = CrossInAppBilling.Current;  
  4. var result = false;  
  5. try  
  6. {  
  7. var connected = await billing.ConnectAsync(ItemType.InAppPurchase);  
  8. if (!connected)  
  9. {  
  10. //Couldn't connect  
  11. return false;  
  12. }  
  13. //check purchases  
  14. var verify = new InAppBillingVerify();  
  15. var purchases = await billing.GetPurchasesAsync(ItemType.InAppPurchase, verify);  
  16. //check for null just incase  
  17. if (purchases?.Any(p => p.ProductId == productId) ?? false)  
  18. {  
  19. //Purchase restored  
  20. Preferences.Set(productId, true);  
  21. return true;  
  22. }  
  23. else  
  24. {  
  25. //no purchases found  
  26. //Toast.MakeText(this, GetString(Resource.String.Options_RestoreUnavailable), ToastLength.Long);  
  27. return false;  
  28. }  
  29. }  
  30. catch (InAppBillingPurchaseException purchaseEx)  
  31. {  
  32. var message = string.Empty;  
  33. switch (purchaseEx.PurchaseError)  
  34. {  
  35. case PurchaseError.AppStoreUnavailable:  
  36. RestorePurchasesStatusMessage = GetString(Resource.String.Options_PaidFunctions_ErrorAppStoreUnavailable);  
  37. break;  
  38. case PurchaseError.BillingUnavailable:  
  39. RestorePurchasesStatusMessage = GetString(Resource.String.Options_PaidFunctions_ErrorBillingUnavailable);  
  40. break;  
  41. case PurchaseError.PaymentInvalid:  
  42. RestorePurchasesStatusMessage = GetString(Resource.String.Options_PaidFunctions_ErrorPaymentInvalid);  
  43. break;  
  44. case PurchaseError.PaymentNotAllowed:  
  45. RestorePurchasesStatusMessage = GetString(Resource.String.Options_PaidFunctions_ErrorPaymentNotAllowed);  
  46. break;  
  47. case PurchaseError.AlreadyOwned:  
  48. RestorePurchasesStatusMessage = GetString(Resource.String.Options_PaidFunctions_ErrorAlreadyOwned);  
  49. Preferences.Set(productId, true);  
  50. result = true;  
  51. break;  
  52. }  
  53. //Decide if it is an error we care about  
  54. if (string.IsNullOrWhiteSpace(message))  
  55. return false;  
  56. //Display message to user  
  57. //Toast.MakeText(this, message, ToastLength.Long);  
  58. }  
  59. catch (Exception exception)  
  60. {  
  61. Crashes.TrackError(exception);  
  62. //Something has gone wrong  
  63. RestorePurchasesStatusMessage = GetString(Resource.String.Options_PaidFunctions_ErrorAppStoreUnavailable);  
  64. //Toast.MakeText(this, GetString(Resource.String.Options_PaidFunctions_ErrorAppStoreUnavailable), ToastLength.Long);  
  65. }  
  66. finally  
  67. {  
  68. await billing.DisconnectAsync();  
  69. }  
  70. return result;  
  71. }  
  72. public async Task<bool> PurchaseItem(string productId, string payload)  
  73. {  
  74. var billing = CrossInAppBilling.Current;  
  75. try  
  76. {  
  77. var connected = await billing.ConnectAsync(ItemType.InAppPurchase);  
  78. if (!connected)  
  79. {  
  80. //we are offline or can't connect, don't try to purchase  
  81. return false;  
  82. }  
  83. //check purchases  
  84. var verify = new InAppBillingVerify();  
  85. var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase, payload, verify);  
  86. //possibility that a null came through.  
  87. if (purchase == null)  
  88. {  
  89. //did not purchase  
  90. }  
  91. else if (purchase.State == PurchaseState.Purchased)  
  92. {  
  93. //purchased!  
  94. Preferences.Set(productId, true);  
  95. }  
  96. }  
  97. catch (InAppBillingPurchaseException purchaseEx)  
  98. {  
  99. var message = string.Empty;  
  100. switch (purchaseEx.PurchaseError)  
  101. {  
  102. case PurchaseError.AppStoreUnavailable:  
  103. message = GetString(Resource.String.Options_PaidFunctions_ErrorAppStoreUnavailable);  
  104. break;  
  105. case PurchaseError.BillingUnavailable:  
  106. message = GetString(Resource.String.Options_PaidFunctions_ErrorBillingUnavailable);  
  107. break;  
  108. case PurchaseError.PaymentInvalid:  
  109. message = GetString(Resource.String.Options_PaidFunctions_ErrorPaymentInvalid);  
  110. break;  
  111. case PurchaseError.PaymentNotAllowed:  
  112. message = GetString(Resource.String.Options_PaidFunctions_ErrorPaymentNotAllowed);  
  113. break;  
  114. case PurchaseError.AlreadyOwned:  
  115. message = GetString(Resource.String.Options_PaidFunctions_ErrorAlreadyOwned);  
  116. Preferences.Set(productId, true);  
  117. EnableOrDisableProFeatures();  
  118. break;  
  119. }  
  120. //Decide if it is an error we care about  
  121. if (string.IsNullOrWhiteSpace(message))  
  122. return false;  
  123. //Display message to user  
  124. DisplayToast(message, ToastLength.Long);  
  125. }  
  126. catch (Exception exception)  
  127. {  
  128. Crashes.TrackError(exception);  
  129. //Something else has gone wrong, log it  
  130. //Debug("Issue connecting: " + ex);  
  131. DisplayToast(GetString(Resource.String.Options_PaidFunctions_ErrorAppStoreUnavailable), ToastLength.Long);    
  132. }  
  133. finally  
  134. {  
  135. await billing.DisconnectAsync();  
  136. }  
  137. return false;  
  138. }  
  139. private async Task RestorePurchases()  
  140. {  
  141. var currentConnectivity = Connectivity.NetworkAccess;  
  142. var restorePurchasesResults = new List<bool>();  
  143. if (currentConnectivity == NetworkAccess.Internet)  
  144. {  
  145. restorePurchasesResults.Add(await WasItemPurchased(GetString(Resource.String.Options_PaidFunctions_PurchaseTemplatesAndMoreButtons)));  
  146. restorePurchasesResults.Add(await WasItemPurchased(GetString(Resource.String.Options_PaidFunctions_PurchaseCustomResources)));  
  147. restorePurchasesResults.Add(await WasItemPurchased(GetString(Resource.String.Options_PaidFunctions_PurchaseDisplayWebpageAfterVote)));  
  148. if (restorePurchasesResults.Contains(true))  
  149. {  
  150. DisplayToast(GetString(Resource.String.Options_PaidFunctions_PurchasesRestored), ToastLength.Long);  
  151. EnableOrDisableProFeatures();  
  152. }  
  153. else  
  154. {  
  155. if (string.IsNullOrEmpty(RestorePurchasesStatusMessage))  
  156. RestorePurchasesStatusMessage = GetString(Resource.String.Options_PaidFunctions_PurchasesNotFound);  
  157. DisplayToast(RestorePurchasesStatusMessage, ToastLength.Long);  
  158. }  
  159. }  
  160. else  
  161. {  
  162. DisplayToast(GetString(Resource.String.Options_InternetUnavailable), ToastLength.Long);  
  163. }  
  164. }  
  165. private async Task<string> GetProductPrice(string productId)  
  166. {  
  167. var billing = CrossInAppBilling.Current;  
  168. try  
  169. {  
  170. var connected = await billing.ConnectAsync(ItemType.InAppPurchase);  
  171. if (!connected)  
  172. {  
  173. //Couldn't connect  
  174. return string.Empty;  
  175. }  
  176. //check purchases  
  177. var item = await billing.GetProductInfoAsync(ItemType.InAppPurchase, productId);  
  178. if (item != null)  
  179. return item.ToList()[0].LocalizedPrice;  
  180. }  
  181. catch (InAppBillingPurchaseException purchaseEx)  
  182. {  
  183. var message = string.Empty;  
  184. switch (purchaseEx.PurchaseError)  
  185. {  
  186. case PurchaseError.AppStoreUnavailable:  
  187. message = GetString(Resource.String.Options_PaidFunctions_ErrorAppStoreUnavailable);  
  188. break;  
  189. case PurchaseError.BillingUnavailable:  
  190. message = GetString(Resource.String.Options_PaidFunctions_ErrorBillingUnavailable);  
  191. break;  
  192. case PurchaseError.PaymentInvalid:  
  193. message = GetString(Resource.String.Options_PaidFunctions_ErrorPaymentInvalid);  
  194. break;  
  195. case PurchaseError.PaymentNotAllowed:  
  196. message = GetString(Resource.String.Options_PaidFunctions_ErrorPaymentNotAllowed);  
  197. break;  
  198. }  
  199. //Decide if it is an error we care about  
  200. if (string.IsNullOrWhiteSpace(message))  
  201. return string.Empty;  
  202. //Display message to user  
  203. DisplayToast(message, ToastLength.Long);  
  204. }  
  205. catch (Exception exception)  
  206. {  
  207. Crashes.TrackError(exception);  
  208. //Something has gone wrong  
  209. }  
  210. finally  
  211. {  
  212. await billing.DisconnectAsync();  
  213. }  
  214. return string.Empty;  
  215. }  
And i have to do similar function for the Amazon store like the API details here:
https://developer.amazon.com/docs/cross-platform-plugins/cpp-use-the-iap-plugin-for-xamarin.html
However i dont even know where to start to convert those functions, Amazon API confuse me, can someone help me?

Answers (1)