Class Purchase
Represents the status of in-app-purchase goods, this class provides information about products purchased by a user as well as the ability to purchase additional products. There are generally two types of payment systems: Manual and managed. In manual payments we pay a specific amount in a specific currency while with managed payment systems we work against a product catalog defined in the server.
In-app-purchase API's rely on managed server based products, other payment systems use the manual approach. An application dealing with virtual goods must support both since not all devices feature in-app-purchase API's. An application dealing with physical goods & services must use the latter according to the TOS of current in-app-purchase solutions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal DategetExpiryDate(String... skus) Gets the expiry date for a set of skus.getFirstReceiptExpiringAfter(Date dt, String... skus) Gets the first receipt that expires after the specified date for the provided skus.static PurchaseReturns the native OS purchase implementation if applicable, if unavailable this method will try to fallback to a custom purchase implementation and failing that will return nullstatic PurchasegetInAppPurchase(boolean d) Deprecated
Gets a list of purchases that haven't yet been sent to the server.Product[]getProducts(String[] skus) Returns the product list for the given SKU arrayGets all of the receipts for this app.final Receipt[]getReceipts(String... skus) Gets all of the receipts for the specified skus.protected final ReceiptStoreReturns the store code associated with this in-app purchase object.booleanIndicates whether the payment platform supports things such as "item listing" or requires that items be coded into the system.booleanIndicates whether the purchasing platform supports managed payments which work by picking products that are handled by the servers/OS of the platform vendor.booleanChecks to see if this platform supports the#manageSubscriptions(java.lang.String)method.booleanIndicates whether the purchasing platform supports manual payments which are just payments of a specific amount of money.booleanisRefundable(String sku) Indicates whether refunding is possible when the SKU is purchasedbooleanIndicates whether a purchase restore button is supported by the OSfinal booleanisSubscribed(String... skus) Checks to see if the user is currently subscribed to any of the given skus.booleanReturns true if the subscription API is supported in this platformbooleanSome platforms support subscribing but don't support unsubscribevoidOpen the platform's UI for managing subscriptions.Performs payment of a specific amount based on the manual payment API, notice that this doesn't use the in-app-purchase functionality of the device!Performs payment of a specific amount based on the manual payment API, notice that this doesn't use the in-app-purchase functionality of the device!static voidpostReceipt(String storeCode, String sku, String transactionId, long datePurchased, String orderData) Posts a receipt to be added to the receipt store.voidBegins the purchase process for the given SKUvoidpurchase(String sku, PromotionalOffer promotionalOffer) Begins the purchase process for the given SKU using a provided promotional offer.voidTries to refund the given SKU if applicable in the current market/productvoidrestore()Restores purchases if applicable, this will only work if isRestoreSupported() returns truefinal voidsetReceiptStore(ReceiptStore store) Installs a given receipt store to handle receipt managementvoidBegins subscribe process for the given subscription SKUvoidsubscribe(String sku, PromotionalOffer promotionalOffer) Begins subscribe process for the given subscription SKU using a provided promotional offer.final voidfinal voidsynchronizeReceipts(long ifOlderThanMs, SuccessCallback<Boolean> callback) Synchronize with receipt store.final booleansynchronizeReceiptsSync(long ifOlderThanMs) Synchronize receipts and wait for the sync to complete before proceeding.voidunsubscribe(String sku) Cancels the subscription to a given SKUbooleanwasPurchased(String sku) Returns true if the given SKU was purchased in the past, notice this method might not work as expected for Unmanaged/consumable products which can be purchased multiple times.
-
Constructor Details
-
Purchase
public Purchase()
-
-
Method Details
-
postReceipt
public static void postReceipt(String storeCode, String sku, String transactionId, long datePurchased, String orderData) Posts a receipt to be added to the receipt store.
Parameters
-
sku: The sku of the product -
transactionId: The transaction ID -
datePurchased: The date of the purchase.
Deprecated
For internal implementation use only.
-
-
getInAppPurchase
Returns the native OS purchase implementation if applicable, if unavailable this method will try to fallback to a custom purchase implementation and failing that will return null
Returns
instance of the purchase class or null
-
getInAppPurchase
Deprecated
use the version that takes no arguments
-
getReceiptStore
-
setReceiptStore
Installs a given receipt store to handle receipt management
Parameters
store
-
getReceipts
-
getReceipts
-
isManualPaymentSupported
public boolean isManualPaymentSupported()Indicates whether the purchasing platform supports manual payments which are just payments of a specific amount of money.
Returns
true if manual payments are supported
-
isManagedPaymentSupported
public boolean isManagedPaymentSupported()Indicates whether the purchasing platform supports managed payments which work by picking products that are handled by the servers/OS of the platform vendor.
Returns
true if managed payments are supported
-
pay
Performs payment of a specific amount based on the manual payment API, notice that this doesn't use the in-app-purchase functionality of the device!
Parameters
-
amount: the amount to pay -
currency: the three letter currency type
Returns
- Returns:
a token representing the pending transaction which will be matched when receiving a callback from the platform or a null if the payment has failed or was canceled
Throws
RuntimeException: @throws RuntimeException This method is a part of the manual payments API and will fail if isManualPaymentSupported() returns false
-
-
pay
Performs payment of a specific amount based on the manual payment API, notice that this doesn't use the in-app-purchase functionality of the device!
Parameters
-
amount: the amount to pay -
currency: the three letter currency type -
invoiceNumber: application specific invoice number
Returns
- Returns:
a token representing the pending transaction which will be matched when receiving a callback from the platform or a null if the payment has failed or was canceled
Throws
RuntimeException: @throws RuntimeException This method is a part of the manual payments API and will fail if isManualPaymentSupported() returns false
-
-
isItemListingSupported
public boolean isItemListingSupported()Indicates whether the payment platform supports things such as "item listing" or requires that items be coded into the system. iOS provides listing and pricing where Android expects developers to redirect into the Play application for application details.
Returns
true if the OS supports this behavior
-
getProducts
Returns the product list for the given SKU array
Parameters
skus: the ids for the specific products
Returns
the product instances
Throws
-
RuntimeException: @throws RuntimeException This method is a part of the managed payments API and will fail if isManagedPaymentSupported() returns false -
RuntimeException: This method works only if isItemListingSupported() returns true
-
wasPurchased
Returns true if the given SKU was purchased in the past, notice this method might not work as expected for Unmanaged/consumable products which can be purchased multiple times. In addition, this will only return true if the product was purchased (or has been restored) on the current device.
Parameters
sku: the id of the product
Returns
true if the product was purchased
Throws
RuntimeException: @throws RuntimeException This method is a part of the managed payments API and will fail if isManagedPaymentSupported() returns false
-
purchase
Begins the purchase process for the given SKU
On Android you must use
#subscribe(java.lang.String)for play store subscription products instead of this method. You cannot use#purchase(java.lang.String). On iOS there is no difference between#subscribe(java.lang.String)and#purchase(java.lang.String), so if you are simulating subscriptions on iOS using auto-renewables, you are better to use#subscribe(java.lang.String)as this will work correctly on both Android and iOS.Parameters
sku: the SKU with which to perform the purchase process
Throws
RuntimeException: @throws RuntimeException This method is a part of the managed payments API and will fail if isManagedPaymentSupported() returns false
-
purchase
Begins the purchase process for the given SKU using a provided promotional offer.
Promotional offers are currently only supported on iOS. See Apple's documentation
Parameters
-
sku: the SKU with which to perform the purchase process -
promotionalOffer: The promotional offer.
Throws
RuntimeException: @throws RuntimeException This method is a part of the managed payments API and will fail if isManagedPaymentSupported() returns false
See also
- ApplePromotionalOffer
-
-
subscribe
Begins subscribe process for the given subscription SKU
On Android you must use this method for play store subscription products. You cannot use
#purchase(java.lang.String). On iOS there is no difference between#subscribe(java.lang.String)and#purchase(java.lang.String), so if you are simulating subscriptions on iOS using auto-renewables, you are better to use#subscribe(java.lang.String)as this will work correctly on both Android and iOS.Parameters
sku: the SKU with which to perform the purchase process
Throws
RuntimeException: @throws RuntimeException This method is a part of the managed payments API and will fail if isManagedPaymentSupported() returns false
-
subscribe
Begins subscribe process for the given subscription SKU using a provided promotional offer.
Promotional offers are currently only supported on iOS. See Apple's documentation
Parameters
-
sku: the SKU with which to perform the purchase process -
promotionalOffer: The promotional offer.
Throws
RuntimeException: @throws RuntimeException This method is a part of the managed payments API and will fail if isManagedPaymentSupported() returns false
See also
- ApplePromotionalOffer
-
-
unsubscribe
Cancels the subscription to a given SKU
Parameters
sku: the SKU with which to perform the purchase process
Throws
RuntimeException: @throws RuntimeException This method is a part of the managed payments API and will fail if isManagedPaymentSupported() returns false
-
getPendingPurchases
Gets a list of purchases that haven't yet been sent to the server. You can use this for diagnostic and debugging purposes periodically in the app to make sure there aren't a queue of purchases that aren't getting submitted to the server.
Returns
List of receipts that haven't been sent to the server.
-
synchronizeReceipts
public final void synchronizeReceipts() -
synchronizeReceipts
Synchronize with receipt store. This will try to submit any pending purchases to the receipt store, and then reload receipts from the receipt store
Parameters
-
ifOlderThanMs: Only fetch receipts if they haven't been fetched inifOlderThanMsmilliseconds. -
callback: @param callback Callback called when sync is done. Will be passed true if all pending purchases were successfully submitted to the receipt store AND receipts were successfully loaded.
-
-
synchronizeReceiptsSync
public final boolean synchronizeReceiptsSync(long ifOlderThanMs) Synchronize receipts and wait for the sync to complete before proceeding.
Parameters
ifOlderThanMs: Only re-fetch if it hasn't been reloaded in this number of milliseconds.
Returns
True if the synchronization succeeds. False otherwise.
-
getExpiryDate
-
isSubscribed
Checks to see if the user is currently subscribed to any of the given skus. A user is deemed to be subscribed if
#getExpiryDate(java.lang.String...)returns a date later than now.Parameters
skus: Set of skus to check.
-
getFirstReceiptExpiringAfter
-
isRefundable
Indicates whether refunding is possible when the SKU is purchased
Parameters
sku: the sku
Returns
true if the SKU can be refunded
-
refund
Tries to refund the given SKU if applicable in the current market/product
Parameters
sku: the id for the product
-
isSubscriptionSupported
public boolean isSubscriptionSupported()Returns true if the subscription API is supported in this platform
Returns
true if the subscription API is supported in this platform
-
isUnsubscribeSupported
public boolean isUnsubscribeSupported()Some platforms support subscribing but don't support unsubscribe
Returns
true if the subscription API allows for unsubscribe
Deprecated
use
#isManageSubscriptionsSupported()instead -
isRestoreSupported
public boolean isRestoreSupported()Indicates whether a purchase restore button is supported by the OS
Returns
true if you can invoke the restore method
-
restore
public void restore()Restores purchases if applicable, this will only work if isRestoreSupported() returns true -
isManageSubscriptionsSupported
public boolean isManageSubscriptionsSupported()Checks to see if this platform supports the
#manageSubscriptions(java.lang.String)method.Returns
True if the platform supports the
#manageSubscriptions(java.lang.String)method.Since
6.0
-
manageSubscriptions
Open the platform's UI for managing subscriptions. Currently iOS and Android are the only platforms that support this. Other platforms will simply display a dialog stating that it doesn't support this feature. Use the
#isManageSubscriptionsSupported()method to check if the platform supports this feature.Parameters
sku: @param sku Optional sku of product whose subscription you wish to manage. If left null, then the general subscription management UI will be opened. iOS doesn't support "deep-linking" directly to the management for a particular sku, so this parameter is ignored there. If included on Android, howerver, it will open the UI for managing the specified sku.
Since
6.0
-
getStoreCode
Returns the store code associated with this in-app purchase object.
Returns
- Returns:
The store code. One of
Receipt#STORE_CODE_ITUNES,Receipt#STORE_CODE_PLAY,Receipt#STORE_CODE_SIMULATOR, orReceipt#STORE_CODE_WINDOWS.Since
8.0
-