Skip to content

feat(payments): google play billing 8.0 #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class DemoModel extends Observable {
console.log(`Order Id: ${event.payload.orderId}`);
console.log(`Order Date: ${event.payload.orderDate}`);
console.log(`Receipt Token: ${event.payload.receiptToken}`);
finalizeOrder(event.payload);
finalizeOrder(event.payload, true);
}
break;
case PaymentEvent.Context.FINALIZING_ORDER:
Expand Down
45 changes: 25 additions & 20 deletions packages/payments/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
# @nativescript/payments

<!-- TODO: Add subscriptions example -->

A plugin that allows your app to use in-app purchases and subscriptions using Apple AppStore and Google PlayStore purchasing systems.

This plugin uses a [RxJS Observable](https://rxjs.dev/guide/observable) to emit the events to handle the purchase flow. To avoid threading errors with the platform purchasing flow, you can use `toMainThread()` as a pipe on the Observable so that the purchase logic executes on the main thread. `paymentEvents.pipe(toMainThread()).subscribe((event: PaymentEvent.Type) => {...`

[In-App Purchase exmple](#in-app-purchase-example) should give a good starting point on how to use the Observable and setup the purchasing mechanism.

**Payments on iOS example**
| Example Item List | PurchasConfirmation | Purchase Done | Purchase Successful
| Example Item List | PurchasConfirmation | Purchase Done | Purchase Successful
|:------|:------|:-------|:-----
| ![Purchase Item List Example](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/ios-payments4.png) | ![Purchase Flow Confirmation](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/ios-payments5.png) | ![Purchase Flow Done](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/ios-payments6.png) | ![Purchase Flow Success](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/ios-payments7.png) |

**Payments on Android example**

| Example Item List | Purchas Confirmation | Purchase Successful
|:------|:------|:-------
| Example Item List | Purchas Confirmation | Purchase Successful |
| :------------------------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------- |
| ![Item List Example](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/android-payments3.png) | ![Purchase Flow Confirmation](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/android-payments4.png) | ![Purchase Flow Successful](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/android-payments5.png) |

## Contents

* [Installation](#installation)
* [Prerequisites](#prerequisites)
* [iOS prerequisites](#ios-prerequisites)
* [Android prerequisites](#android-prerequisites)
* [Important note about Google items](#important-note-about-google-items)
* [Use @nativescript/payments](#use-nativescriptpayments)
* [Standard usage flow](#standard-usage-flow)
* [In-App Purchase example](#in-app-purchase-example)
* [API](#api)
* [License](#license)
- [@nativescript/payments](#nativescriptpayments)
- [Contents](#contents)
- [Installation](#installation)
- [Prerequisites](#prerequisites)
- [iOS prerequisites](#ios-prerequisites)
- [Android prerequisites](#android-prerequisites)
- [Important note about Google items](#important-note-about-google-items)
- [Use @nativescript/payments](#use-nativescriptpayments)
- [Standard usage flow](#standard-usage-flow)
- [In-App Purchase example](#in-app-purchase-example)
- [API](#api)
- [License](#license)

## Installation

Expand Down Expand Up @@ -61,14 +65,14 @@ To test iOS purchases fully, you will need a real iOS device. You will also need
1. To offer in-app purchases for your Android app, you will need to upload at least ONE apk/aab to the [Google Play Console](https://play.google.com).

2. Create in-app products on the console.
![Create new in app products](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/android-payments1.png)
![Create new in app products](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/android-payments1.png)

On the form to create your product, the `Product ID` is the value you will use to fetch your products for the user to purchase.

#### Important note about Google items

- Google does not like numeric values in the ID field. It seems to ignore the Sku when querying for your items and only returns one item instead of multiple values if the IDs contain numeric values.
![Product ID Form](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/android-payments2.png)
![Product ID Form](https://raw.githubusercontent.com/NativeScript/payments/main/assets/payments/images/android-payments2.png)

- Google in app products will not work until Google has reviewed the app. They will appear in the list of products, but the API will error trying to purchase them. The title of the item when you call `fetchItems(['your.product.id']) should be suffixed with (in review) or something similar when returned at this point. You will not be able to finish the purchase flow until the review period has passed.

Expand Down Expand Up @@ -102,7 +106,9 @@ buyItem('item.id');

// finalizeOrder(payload) will complete the purchase flow.
// The payload argument here is provided in the PaymentEvent.Context.PROCESSING_ORDER - SUCCESS event (see below example for detailed usage).
finalizeOrder(payload)
// Pass true to consume the order, or false to leave it as is.
// Example: don't consume the order if you want to restore it later, like if the user buys a one time item to remove ads from the app permanently. Consuming allows him to buy it again
finalizeOrder(payload, true)

// at this point you would process the order with your backend given the receiptToken from the purchase flow
```
Expand Down Expand Up @@ -149,7 +155,7 @@ export class SomeViewModel {
console.log('🟢 Payment Success 🟢');
console.log(`Order Date: ${event.payload.orderDate}`);
console.log(`Receipt Token: ${event.payload.receiptToken}`);
finalizeOrder(event.payload);
finalizeOrder(event.payload, true); // Pass true to consume the order
}
break;
case PaymentEvent.Context.FINALIZING_ORDER:
Expand Down Expand Up @@ -196,8 +202,8 @@ export class SomeViewModel {

The following methods get called in response to the events emitted by `paymentEvents`.

| Method | Description
|:-------|:-----------
| Method | Description |
| :------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fetchItems(itemIds: Array<string>)` | Queries the store for the items requested. You should handle these items inside the `PaymentEvent.Context.RETRIEVING_ITEMS` event. |
| `buyItem(item: Item, options?: BuyItemOptions)` | Starts the purchase flow on Android & iOS and emits `PaymentEvent.Context.PROCESSING_ORDER` with `SUCCESS` or `FAILURE`. If SUCCESS then you can call the last method to the `finalizeOrder(payload)`. |
| `fetchSubscriptions(itemIds: Array<string>)` | Queries the store for the subscriptions offered by the app. You should handle these subscriptions inside the `PaymentEvent.Context.RETRIEVING_ITEMS` event. |
Expand All @@ -206,7 +212,6 @@ The following methods get called in response to the events emitted by `paymentEv
| `canMakePayments()` | Returns `true` or `false` indicating whether the billing service is available and is setup successfully. |
| `tearDown()` | Closes the connection to the billing service to free up resources. |


## License

Apache License Version 2.0
Loading