Table of Contents
This project provides a library wrapper, that provides an abstraction of the native app store libraries, and makes the necessary functionalities available in Qt6 and QML. Compatible with Apple App Store and Google Play Store.
Here's why:
- In-App-Purchasing might be an important way of monetizing your Qt/QML mobile app.
- QtPurchasing didn't make it to Qt6.
To add In-App-Purchasing capabilities to your Qt6/QML project follow the steps below.
- Qt/QML 6
- Apple StoreKit
- Android Billing Client
- Clone this repo into a folder in your project.
git clone https://github.com/moritzstoetter/qt6purchasing.git
- Move 'android/GooglePlayBilling.java' to
QT_ANDROID_PACKAGE_SOURCE_DIR/src/com/COMPANY_NAME/APP_NAME/GooglePlayBilling.java
For more information on how to include custom Java-Code in your Android App see Deploying an Application on Android. - Add the qt6purchasing Library to your Project. In your project's
CMakeLists.txt
add the following:target_link_libraries(APP_NAME PRIVATE ... store )
- Expose the purchasing classes to QML. In your
main.cpp
include the following lines:
#if defined Q_OS_ANDROID
#include <QJniObject>
#include <QCoreApplication>
#include "backend/store/android/googleplaystorebackend.h"
#include "backend/store/android/googleplaystoreproduct.h"
#include "backend/store/android/googleplaystoretransaction.h"
#endif
#if defined Q_OS_DARWIN
#include "backend/store/apple/appleappstorebackend.h"
#include "backend/store/apple/appleappstoreproduct.h"
#include "backend/store/apple/appleappstoretransaction.h"
#endif
...
qmlRegisterUncreatableType<AbstractStoreBackend>("AbstractStoreBackend",1,0,"AbstractStoreBackend","AbstractProduct uncreatable");
qmlRegisterUncreatableType<AbstractProduct>("AbstractProduct",1,0,"AbstractProduct","AbstractProduct uncreatable");
qmlRegisterUncreatableType<AbstractTransaction>("AbstractTransaction",1,0,"AbstractTransaction","AbstractTransaction uncreatable");
#if defined Q_OS_ANDROID
qmlRegisterType<GooglePlayStoreBackend>("Store", 1, 0, "Store");
qmlRegisterType<GooglePlayStoreProduct>("Product", 1, 0, "Product");
#elif defined Q_OS_DARWIN
qmlRegisterType<AppleAppStoreBackend>("Store", 1, 0, "Store");
qmlRegisterType<AppleAppStoreProduct>("Product", 1, 0, "Product");
#endif
- In your QML file include the purchasing classes:
import Store
import Product
- Use it like this, for a product that is called "test_1" in the app store(s):
Store {
id: iapStore
Product {
id: testingProduct
identifier: "test_1"
type: Product.Consumable
}
}
StoreItem {
id: testingStoreItem
product: testingProduct
onIapCompleted: root.accepted()
}
StoreItem.qml
:
import QtQuick
import Product
Item {
id: root
required property Product product
signal iapCompleted
enum PurchasingStatus {
NoPurchase,
PurchaseProcessing,
PurchaseSuccess,
PurchaseFail
}
property int purchasingStatus: StoreItem.PurchasingStatus.NoPurchase
function purchase() {
purchasingStatus = StoreItem.PurchasingStatus.PurchaseProcessing
product.purchase()
}
function finalize(transaction) {
purchasingStatus = StoreItem.PurchasingStatus.PurchaseSuccess
transaction.finalize()
}
Connections {
target: product
function onPurchaseSucceeded(transaction) {
finalize(transaction)
}
function onPurchaseRestored(transaction) {
finalize(transaction)
}
function onPurchaseFailed(transaction) {
purchasingStatus = StoreItem.PurchasingStatus.PurchaseFail
}
function onPurchaseConsumed(transaction) {
root.iapCompleted()
}
}
}
Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Moritz Stötter - moritzstoetter.dev - [email protected]