Class GooglePlayBillingProvider
- Namespace
- VpnHood.AppLib.Stores.GooglePlay
- Assembly
- VpnHood.AppLib.Stores.GooglePlay.dll
Provides Google Play's billing, through the Play Billing Library.
public class GooglePlayBillingProvider : IBillingProvider, IDisposable
- Inheritance
-
GooglePlayBillingProvider
- Implements
- Inherited Members
Constructors
GooglePlayBillingProvider()
Initializes a new instance of the GooglePlayBillingProvider class.
public GooglePlayBillingProvider()
Remarks
It connects to Play's billing service at its first call that needs it.
Properties
IsSubscriptionManagementSupported
Whether this device can show the store's own manage-subscriptions surface (cancel, change plan, payment method). A runtime question, not a constant: a TV carries a store that cannot show that screen, and answering false there is what lets the UI say "manage it where you bought it" instead of offering a control that does nothing.
public bool IsSubscriptionManagementSupported { get; }
Property Value
Remarks
Play's subscriptions screen is its web deep link: it is true where the device has an external browser to open it.
ProviderId
Which store this is (see StoreIds), in the vocabulary the account backend speaks. Declared here because the provider is the one thing that cannot be wrong about it — so nothing that wires a store has to state it a second time and risk disagreeing.
public string ProviderId { get; }
Property Value
Remarks
It is GooglePlay.
PurchaseState
Gets where the store's payment flow stands.
public PurchaseState PurchaseState { get; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
FinishPurchase(PurchaseProof, CancellationToken)
Tells the store the purchase behind this proof is delivered: the account backend has redeemed it. Called only then, so a purchase whose redeeming failed stays open where the store keeps one open (StoreKit's unfinished transaction), and a later Restore presents it again. A store whose backend closes the purchase itself (Play's acknowledgement) has nothing to do here.
public Task FinishPurchase(PurchaseProof purchaseProof, CancellationToken cancellationToken)
Parameters
purchaseProofPurchaseProofThe proof the account backend has redeemed.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
It does nothing: the backend acknowledges a Play purchase itself, once it has provisioned it.
Exceptions
- BillingException
The store failed; its Code says how.
- ArgumentException
The proof is not one this provider answered, as the App Store's checks.
- OperationCanceledException
cancellationTokenwas canceled.
GetSubscriptionPlans(IReadOnlyList<string>, CancellationToken)
Prices the given products. The store is not asked WHICH products exist — it cannot answer that (neither StoreKit nor Play Billing can list an app's own catalog) and it is not the authority on it either: the account backend decides what may be sold, this prices and localizes it.
public Task<IReadOnlyList<SubscriptionPlan>> GetSubscriptionPlans(IReadOnlyList<string> productIds, CancellationToken cancellationToken)
Parameters
productIdsIReadOnlyList<string>The product IDs the account backend gives (GetProductIds(CancellationToken)).
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IReadOnlyList<SubscriptionPlan>>
A task whose result is the plans the store prices for them.
Remarks
A plan's token names the product and its offer; a device whose Play does not sell subscriptions throws a BillingException. It does not watch the cancellation token while it connects to Play and queries it.
Exceptions
- ApiException
For the web store, the account backend refused the request.
- BillingException
The store failed; its Code says how.
- OperationCanceledException
cancellationTokenwas canceled.
OpenSubscriptionManagement(IUiContext?, CancellationToken)
Shows that surface. The store does it its own way — an in-app sheet, its own app — so no URL crosses to the UI and no browser has to exist. Only ever called for a subscription THIS store billed, and only while IsSubscriptionManagementSupported: the app refuses the call otherwise, so an implementation need not re-check its own answer.
public Task OpenSubscriptionManagement(IUiContext? uiContext, CancellationToken cancellationToken)
Parameters
uiContextIUiContextThe window, or null where none is open.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that completes once the store's surface is shown.
Remarks
It opens Play's subscriptions screen by its deep link: at the app's own subscription where the store account owns one, and at the account's list otherwise or where that one cannot be named. It returns once the link is handed to Android; its query of the subscription does not watch the cancellation token.
Exceptions
- NotSupportedException
The store has no such surface here, as the web store's.
- UiContextNotAvailableException
uiContextis null, and the store needs a window.- OperationCanceledException
cancellationTokenwas canceled.
Purchase(IUiContext?, PurchaseParams, PurchaseAttribution, CancellationToken)
Runs the store's payment flow and answers its proof of purchase.
Two arguments because they have two owners: purchaseParams is the buyer's
choice, attribution is the app's answer to whose account is paying.
public Task<PurchaseProof> Purchase(IUiContext? uiContext, PurchaseParams purchaseParams, PurchaseAttribution attribution, CancellationToken cancellationToken)
Parameters
uiContextIUiContextThe window, or null where none is open.
purchaseParamsPurchaseParamsThe plan the buyer chose.
attributionPurchaseAttributionWhose account the purchase is for.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<PurchaseProof>
A task whose result is the store's proof of purchase.
Remarks
The account's ID goes to Play as the obfuscated account ID, and the proof is Play's purchase token. Play reports every purchase to one listener, so callers must not run two purchases at once; one that finds an unfinished flow throws InvalidOperationException. It watches the cancellation token only once the payment flow is open.
Exceptions
- BillingException
The store failed, or the person closed its payment flow (Cancelled); its Code says how.
- NotSupportedException
The store sells outside the app, as the web store, on its checkout page.
- InvalidOperationException
A purchase is under way already, or the account's ID does not fit the store, as the App Store's, which takes a UUID.
- ArgumentException
The plan names a product the store does not have.
- UiContextNotAvailableException
uiContextis null, and the store needs a window.- OperationCanceledException
cancellationTokenwas canceled.- JsonException
The plan token is malformed JSON, or cannot be read as a plan token.
- InvalidDataException
The plan token holds
null.
RestorePurchase(IUiContext?, CancellationToken)
The proof for a subscription this store account already owns, or null when it owns none. Silent: it reads what the device already knows and never prompts.
public Task<PurchaseProof?> RestorePurchase(IUiContext? uiContext, CancellationToken cancellationToken)
Parameters
uiContextIUiContextThe window, or null where none is open.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<PurchaseProof>
A task whose result is the proof, or null where the store account owns no subscription.
Remarks
It queries the store account's subscriptions, without a prompt, and presents the newest one Play has given an order ID, so not one still pending. It does not watch the cancellation token.
Exceptions
- BillingException
The store failed; its Code says how.
- OperationCanceledException
cancellationTokenwas canceled.