Difference between revisions of "StoreKit"

From GiderosMobile
 
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
 
 
<!-- GIDEROSOBJ:StoreKit -->
 
<!-- GIDEROSOBJ:StoreKit -->
'''<translate>Supported platforms</translate>:''' [[File:Platform ios.png]]<br/>
+
'''Supported platforms:''' [[File:Platform ios.png]]<br/>
'''<translate>Available since</translate>:''' Gideros 2012.2.2<br/>
+
'''Available since:''' Gideros 2012.2.2<br/>
=== <translate>Description</translate> ===
+
 
<translate><br />
+
=== Description ===
The [[Special:MyLanguage/StoreKit|StoreKit]] class provides the functionality that allow an application to request payment from a user. <br />
+
 
This class is only available to iOS platforms.<br />
+
'''THIS PLUGIN IS DEPRECATED!'''
<br />
+
 
The [[Special:MyLanguage/StoreKit|StoreKit]] class is defined in module &quot;storekit&quot;. Therefore, you need to call<br />
+
The StoreKit class provides the functionality that allow an application to request payment from a user. This class is only available to iOS platforms.
''require(&quot;storekit&quot;)'' before using it. Loading the &quot;storekit&quot; module <br />
+
 
also creates a global variable ''storekit'' of type [[Special:MyLanguage/StoreKit|StoreKit]] for direct use. <br />
+
The StoreKit class is defined in module "storekit". Therefore, you need to call
<br />
+
<syntaxhighlight lang="lua">
&lt;h3&gt;State of a Transaction&lt;/h3&gt;<br />
+
require("storekit")
<br />
+
</syntaxhighlight>
The state of a transaction is defined by 3 string constants:<br />
+
 
<br />
+
Loading the Storekit module also creates a global variable ''storekit'' of type StoreKit for direct use.
&lt;ul&gt;<br />
+
 
&lt;li&gt;**StoreKit.PURCHASED = &quot;purchased&quot;**: The App Store successfully processed payment. Your application should provide the content the user purchased.&lt;/li&gt;<br />
+
=== State of a Transaction ===
&lt;li&gt;**StoreKit.FAILED = &quot;failed&quot;**: The transaction failed. Check the ''event.errorCode'' and ''event.errorDescription'' fields to determine what happened.&lt;/li&gt;<br />
+
The state of a transaction is defined by 3 string constants:
&lt;li&gt;**StoreKit.RESTORED = &quot;restored&quot;**: This transaction restores content previously purchased by the user. Read the ''event.originalTransaction'' field to obtain information about the original purchase.&lt;/li&gt;<br />
+
*StoreKit.PURCHASED = "purchased": The App Store successfully processed payment. Your application should provide the content the user purchased
&lt;/ul&gt;<br />
+
*StoreKit.FAILED = "failed": The transaction failed. Check the ''event.errorCode'' and ''event.errorDescription'' fields to determine what happened
<br />
+
*StoreKit.RESTORED = "restored": This transaction restores content previously purchased by the user. Read the ''event.originalTransaction'' field to obtain information about the original purchase
&lt;h3&gt;StoreKit Events&lt;/h3&gt;<br />
+
 
<br />
+
=== StoreKit Events ===
The [[Special:MyLanguage/StoreKit|StoreKit]] class dispatches the events [[Special:MyLanguage/Event.REQUEST_PRODUCTS_COMPLETE|Event.REQUEST_PRODUCTS_COMPLETE]], [[Special:MyLanguage/Event.TRANSACTION|Event.TRANSACTION]] and [[Special:MyLanguage/Event.RESTORE_TRANSACTIONS_COMPLETE|Event.RESTORE_TRANSACTIONS_COMPLETE]].<br />
+
The StoreKit class dispatches the events Event.REQUEST_PRODUCTS_COMPLETE, Event.TRANSACTION and Event.RESTORE_TRANSACTIONS_COMPLETE.
<br />
+
 
&lt;h3&gt;# Event.REQUEST_PRODUCTS_COMPLETE&lt;/h3&gt;<br />
+
=== Event.REQUEST_PRODUCTS_COMPLETE ===
 
The function [[StoreKit:requestProducts]] is used to retrieve localized information about a list of products from the Apple App Store. <br />
 
The function [[StoreKit:requestProducts]] is used to retrieve localized information about a list of products from the Apple App Store. <br />
 
When the request completes, [[Special:MyLanguage/Event.REQUEST_PRODUCTS_COMPLETE|Event.REQUEST_PRODUCTS_COMPLETE]] event is dispatched. The resulting event table contains <br />
 
When the request completes, [[Special:MyLanguage/Event.REQUEST_PRODUCTS_COMPLETE|Event.REQUEST_PRODUCTS_COMPLETE]] event is dispatched. The resulting event table contains <br />
Line 38: Line 37:
 
&lt;li&gt;**event.invalidProductIdentifiers:** (table) array of product identifier strings that were not recognized by the Apple App Store&lt;/li&gt;<br />
 
&lt;li&gt;**event.invalidProductIdentifiers:** (table) array of product identifier strings that were not recognized by the Apple App Store&lt;/li&gt;<br />
 
&lt;/ul&gt;<br />
 
&lt;/ul&gt;<br />
<br />
+
 
 
Each table in ''event.products'' array contains these fields:<br />
 
Each table in ''event.products'' array contains these fields:<br />
<br />
 
 
&lt;ul&gt;<br />
 
&lt;ul&gt;<br />
 
&lt;li&gt;**title:** (number) localized name of the product&lt;/li&gt;<br />
 
&lt;li&gt;**title:** (number) localized name of the product&lt;/li&gt;<br />
Line 47: Line 45:
 
&lt;li&gt;**productIdentifier:** (string) string that identifies the product to the Apple App Store&lt;/li&gt;<br />
 
&lt;li&gt;**productIdentifier:** (string) string that identifies the product to the Apple App Store&lt;/li&gt;<br />
 
&lt;/ul&gt;<br />
 
&lt;/ul&gt;<br />
<br />
+
 
For example, this code can be used to print the retrieved product information:<br />
+
For example, this code can be used to print the retrieved product information
&lt;pre&gt;&lt;code&gt;<br />
+
<syntaxhighlight lang="lua">
<br />
+
require("storekit")
local function onRequestProductsComplete(event)<br />
+
local function onRequestProductsComplete(event)
if event.errorCode ~= nil then<br />
+
if event.errorCode ~= nil then
print(&quot;error&quot;, event.errorCode, event.errorDescription)<br />
+
print("error", event.errorCode, event.errorDescription)
return<br />
+
return
end<br />
+
end
<br />
+
 
print(&quot;products:&quot;)<br />
+
print("products:")
for i=1,#event.products do<br />
+
for i=1,#event.products do
print(&quot;title&quot;, event.products[i].title)<br />
+
print("title", event.products[i].title)
print(&quot;description&quot;, event.products[i].description)<br />
+
print("description", event.products[i].description)
print(&quot;price&quot;, event.products[i].price)<br />
+
print("price", event.products[i].price)
print(&quot;productIdentifier&quot;, event.products[i].productIdentifier)<br />
+
print("productIdentifier", event.products[i].productIdentifier)
end<br />
+
end
<br />
+
 
print(&quot;invalidProductIdentifiers:&quot;)<br />
+
print("invalidProductIdentifiers:")
for i=1,#event.invalidProductIdentifiers do<br />
+
for i=1,#event.invalidProductIdentifiers do
print(event.invalidProductIdentifiers[i])<br />
+
print(event.invalidProductIdentifiers[i])
end<br />
+
end
end<br />
+
end
<br />
+
</syntaxhighlight>
&lt;/code&gt;&lt;/pre&gt;<br />
+
 
 
&lt;h3&gt;# Event.TRANSACTION&lt;/h3&gt;<br />
 
&lt;h3&gt;# Event.TRANSACTION&lt;/h3&gt;<br />
<br />
 
 
This event is dispatched when a transaction is updated. The event listener should process all successful transactions, <br />
 
This event is dispatched when a transaction is updated. The event listener should process all successful transactions, <br />
 
unlock the functionality purchased by the user, and then finish the transaction by calling [[StoreKit:finishTransaction]] method. The resulting event table can <br />
 
unlock the functionality purchased by the user, and then finish the transaction by calling [[StoreKit:finishTransaction]] method. The resulting event table can <br />
Line 95: Line 92:
 
&lt;pre&gt;&lt;code&gt;<br />
 
&lt;pre&gt;&lt;code&gt;<br />
 
local function onTransaction(event)<br />
 
local function onTransaction(event)<br />
print(&quot;payment.productIdentifier&quot;, event.payment.productIdentifier)<br />
+
print("payment.productIdentifier", event.payment.productIdentifier)<br />
print(&quot;payment.quantity&quot;, event.payment.quantity)<br />
+
print("payment.quantity", event.payment.quantity)<br />
 
<br />
 
<br />
print(&quot;transaction.state&quot;, event.transaction.state)<br />
+
print("transaction.state", event.transaction.state)<br />
 
<br />
 
<br />
 
if event.transaction.state == StoreKit.FAILED then<br />
 
if event.transaction.state == StoreKit.FAILED then<br />
print(&quot;error&quot;, event.errorCode, event.errorDescription)<br />
+
print("error", event.errorCode, event.errorDescription)<br />
 
else <br />
 
else <br />
print(&quot;transaction.identifier&quot;, event.transaction.identifier)<br />
+
print("transaction.identifier", event.transaction.identifier)<br />
print(&quot;transaction.date&quot;, event.transaction.date)<br />
+
print("transaction.date", event.transaction.date)<br />
 
<br />
 
<br />
 
if event.transaction.state == StoreKit.PURCHASED then<br />
 
if event.transaction.state == StoreKit.PURCHASED then<br />
print(&quot;transaction.receipt&quot;, event.transaction.receipt)<br />
+
print("transaction.receipt", event.transaction.receipt)<br />
 
end<br />
 
end<br />
 
<br />
 
<br />
 
if event.transaction.state == StoreKit.RESTORED then<br />
 
if event.transaction.state == StoreKit.RESTORED then<br />
print(&quot;originalTransaction.identifier&quot;, event.originalTransaction.identifier)<br />
+
print("originalTransaction.identifier", event.originalTransaction.identifier)<br />
print(&quot;originalTransaction.date&quot;, event.originalTransaction.date)<br />
+
print("originalTransaction.date", event.originalTransaction.date)<br />
 
end<br />
 
end<br />
 
<br />
 
<br />
Line 131: Line 128:
 
&lt;/ul&gt;<br />
 
&lt;/ul&gt;<br />
 
<br />
 
<br />
If all transactions are delivered successfully, ''event.errorCode'' and ''event.errorDescription'' will be ''nil''.<br />
+
If all transactions are delivered successfully, ''event.errorCode'' and ''event.errorDescription'' will be ''nil''.
<br /></translate>
+
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
[[Special:MyLanguage/StoreKit:canMakePayments|StoreKit:canMakePayments]] ''<translate>returns whether the user is allowed to make payments</translate>''<br/><!-- GIDEROSMTD:StoreKit:canMakePayments() returns whether the user is allowed to make payments -->
+
[[StoreKit:canMakePayments]] ''returns whether the user is allowed to make payments''<br/><!-- GIDEROSMTD:StoreKit:canMakePayments() returns whether the user is allowed to make payments -->
[[Special:MyLanguage/StoreKit:finishTransaction|StoreKit:finishTransaction]] ''<translate>completes a pending transaction</translate>''<br/><!-- GIDEROSMTD:StoreKit:finishTransaction(transaction) completes a pending transaction -->
+
[[StoreKit:finishTransaction]] ''completes a pending transaction''<br/><!-- GIDEROSMTD:StoreKit:finishTransaction(transaction) completes a pending transaction -->
[[Special:MyLanguage/StoreKit:purchase|StoreKit:purchase]] ''<translate>process a payment request</translate>''<br/><!-- GIDEROSMTD:StoreKit:purchase(productIdentifier,quantity) process a payment request -->
+
[[StoreKit:purchase]] ''process a payment request''<br/><!-- GIDEROSMTD:StoreKit:purchase(productIdentifier,quantity) process a payment request -->
[[Special:MyLanguage/StoreKit:requestProducts|StoreKit:requestProducts]] ''<translate>retrieve localized information about a list of products</translate>''<br/><!-- GIDEROSMTD:StoreKit:requestProducts(productIdentifiers) retrieve localized information about a list of products -->
+
[[StoreKit:requestProducts]] ''retrieve localized information about a list of products''<br/><!-- GIDEROSMTD:StoreKit:requestProducts(productIdentifiers) retrieve localized information about a list of products -->
[[Special:MyLanguage/StoreKit:restoreCompletedTransactions|StoreKit:restoreCompletedTransactions]] ''<translate>restore previously completed purchases</translate>''<br/><!-- GIDEROSMTD:StoreKit:restoreCompletedTransactions() restore previously completed purchases -->
+
[[StoreKit:restoreCompletedTransactions]] ''restore previously completed purchases''<br/><!-- GIDEROSMTD:StoreKit:restoreCompletedTransactions() restore previously completed purchases -->
 +
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
[[Special:MyLanguage/Event.REQUEST_PRODUCTS_COMPLETE|Event.REQUEST_PRODUCTS_COMPLETE]]<br/><!-- GIDEROSEVT:Event.REQUEST_PRODUCTS_COMPLETE requestProductsComplete-->
+
[[Event.REQUEST_PRODUCTS_COMPLETE]]<br/><!-- GIDEROSEVT:Event.REQUEST_PRODUCTS_COMPLETE requestProductsComplete-->
[[Special:MyLanguage/Event.RESTORE_TRANSACTIONS_COMPLETE|Event.RESTORE_TRANSACTIONS_COMPLETE]]<br/><!-- GIDEROSEVT:Event.RESTORE_TRANSACTIONS_COMPLETE restoreTransactionsComplete-->
+
[[Event.RESTORE_TRANSACTIONS_COMPLETE]]<br/><!-- GIDEROSEVT:Event.RESTORE_TRANSACTIONS_COMPLETE restoreTransactionsComplete-->
[[Special:MyLanguage/Event.TRANSACTION|Event.TRANSACTION]]<br/><!-- GIDEROSEVT:Event.TRANSACTION transaction-->
+
[[Event.TRANSACTION]]<br/><!-- GIDEROSEVT:Event.TRANSACTION transaction-->
=== <translate>Constants</translate> ===
+
 
[[Special:MyLanguage/StoreKit.FAILED|StoreKit.FAILED]]<br/><!-- GIDEROSCST:StoreKit.FAILED failed-->
+
=== Constants ===
[[Special:MyLanguage/StoreKit.PURCHASED|StoreKit.PURCHASED]]<br/><!-- GIDEROSCST:StoreKit.PURCHASED purchased-->
+
[[StoreKit.FAILED]]<br/><!-- GIDEROSCST:StoreKit.FAILED failed-->
[[Special:MyLanguage/StoreKit.RESTORED|StoreKit.RESTORED]]<br/><!-- GIDEROSCST:StoreKit.RESTORED restored-->
+
[[StoreKit.PURCHASED]]<br/><!-- GIDEROSCST:StoreKit.PURCHASED purchased-->
 +
[[StoreKit.RESTORED]]<br/><!-- GIDEROSCST:StoreKit.RESTORED restored-->
 
|}
 
|}

Latest revision as of 21:29, 24 January 2026

Supported platforms: Platform ios.png
Available since: Gideros 2012.2.2

Description

THIS PLUGIN IS DEPRECATED!

The StoreKit class provides the functionality that allow an application to request payment from a user. This class is only available to iOS platforms.

The StoreKit class is defined in module "storekit". Therefore, you need to call

require("storekit")

Loading the Storekit module also creates a global variable storekit of type StoreKit for direct use.

State of a Transaction

The state of a transaction is defined by 3 string constants:

  • StoreKit.PURCHASED = "purchased": The App Store successfully processed payment. Your application should provide the content the user purchased
  • StoreKit.FAILED = "failed": The transaction failed. Check the event.errorCode and event.errorDescription fields to determine what happened
  • StoreKit.RESTORED = "restored": This transaction restores content previously purchased by the user. Read the event.originalTransaction field to obtain information about the original purchase

StoreKit Events

The StoreKit class dispatches the events Event.REQUEST_PRODUCTS_COMPLETE, Event.TRANSACTION and Event.RESTORE_TRANSACTIONS_COMPLETE.

Event.REQUEST_PRODUCTS_COMPLETE

The function StoreKit:requestProducts is used to retrieve localized information about a list of products from the Apple App Store.
When the request completes, Event.REQUEST_PRODUCTS_COMPLETE event is dispatched. The resulting event table contains
these additional fields about the products:

<ul>
<li>**event.error:** (number) error code if the request failed to execute</li>
<li>**event.errorDescription:** (string) error description if the request failed to execute</li>
<li>**event.products:** (table) array of products where each element is a table which contains the product information</li>
<li>**event.invalidProductIdentifiers:** (table) array of product identifier strings that were not recognized by the Apple App Store</li>
</ul>

Each table in event.products array contains these fields:
<ul>
<li>**title:** (number) localized name of the product</li>
<li>**description:** (string) localized description of the product</li>
<li>**price:** (number) cost of the product in the local currency</li>
<li>**productIdentifier:** (string) string that identifies the product to the Apple App Store</li>
</ul>

For example, this code can be used to print the retrieved product information

require("storekit")
local function onRequestProductsComplete(event)
	if event.errorCode ~= nil then
		print("error", event.errorCode, event.errorDescription)
		return
	end

	print("products:")
	for i=1,#event.products do
		print("title", event.products[i].title)
		print("description", event.products[i].description)
		print("price", event.products[i].price)
		print("productIdentifier", event.products[i].productIdentifier)
	end

	print("invalidProductIdentifiers:")
	for i=1,#event.invalidProductIdentifiers do
		print(event.invalidProductIdentifiers[i])
	end
end

<h3># Event.TRANSACTION</h3>
This event is dispatched when a transaction is updated. The event listener should process all successful transactions,
unlock the functionality purchased by the user, and then finish the transaction by calling StoreKit:finishTransaction method. The resulting event table can
contain these additional fields about the products:

<ul>
<li>**event.errorCode:** (number) error code if event.transaction.state is set to StoreKit.FAILED</li>
<li>**event.errorDescription:** (string) error description if event.transaction.state is set to StoreKit.FAILED</li>
<li>**event.payment.productIdentifier:** (string) product identifier of the transaction</li>
<li>**event.payment.quantity:** (number) number of items the user wants to purchase</li>
<li>**event.transaction.state:** (string) current state of the transaction</li>
<li>**event.transaction.identifier:** (string) string that uniquely identifies a successful payment transaction</li>
<li>**event.transaction.receipt:** (string) signed receipt that records all information about a successful payment transaction</li>
<li>**event.transaction.date:** (string) date the transaction was added to the App Store's payment queue</li>
<li>**event.originalTransaction.identifier:** (string) identifier of original transaction</li>
<li>**event.originalTransaction.date:** (string) date of the original transaction</li>
</ul>

This code can be used to print the transaction information and unlock the functionality purchased by the user:

<pre><code>
local function onTransaction(event)
print("payment.productIdentifier", event.payment.productIdentifier)
print("payment.quantity", event.payment.quantity)

print("transaction.state", event.transaction.state)

if event.transaction.state == StoreKit.FAILED then
print("error", event.errorCode, event.errorDescription)
else
print("transaction.identifier", event.transaction.identifier)
print("transaction.date", event.transaction.date)

if event.transaction.state == StoreKit.PURCHASED then
print("transaction.receipt", event.transaction.receipt)
end

if event.transaction.state == StoreKit.RESTORED then
print("originalTransaction.identifier", event.originalTransaction.identifier)
print("originalTransaction.date", event.originalTransaction.date)
end

-- unlock the functionality purchased by the user
end

storekit:finishTransaction(event.transaction)
end

</code></pre>
<h3># Event.RESTORE_TRANSACTIONS_COMPLETE</h3>

This event is dispatched after the transactions are delivered. The resulting event table can contain these additional fields:

<ul>
<li>**event.errorCode:** (number) error code if an error occurred while restoring transactions</li>
<li>**event.errorDescription:** (string) description of the error if an error occurred while restoring transactions</li>
</ul>

If all transactions are delivered successfully, event.errorCode and event.errorDescription will be nil.

Methods

StoreKit:canMakePayments returns whether the user is allowed to make payments
StoreKit:finishTransaction completes a pending transaction
StoreKit:purchase process a payment request
StoreKit:requestProducts retrieve localized information about a list of products
StoreKit:restoreCompletedTransactions restore previously completed purchases

Events

Event.REQUEST_PRODUCTS_COMPLETE
Event.RESTORE_TRANSACTIONS_COMPLETE
Event.TRANSACTION

Constants

StoreKit.FAILED
StoreKit.PURCHASED
StoreKit.RESTORED