moritzstoetter / qt6purchasing

Qt6/QML In-App-Purchasing
MIT License
25 stars 5 forks source link

qt6purchasing


Logo

Qt6/QML In-App-Purchasing

Bringing In-App-Purchasing to Qt6

Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Contributing
  5. License
  6. Contact

About The Project

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:

(back to top)

Built With

Qt6
Cpp
Objective-C
Java
Cmake

(back to top)

Getting Started

To add In-App-Purchasing capabilities to your Qt6/QML project follow the steps below.

Prerequisites

Installation

  1. Clone this repo into a folder in your project.
    git clone https://github.com/moritzstoetter/qt6purchasing.git
  2. 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.
  3. Add the qt6purchasing Library to your Project. In your project's CMakeLists.txt add the following:
    target_link_libraries(APP_NAME
    PRIVATE
        ...
        store
    )
  4. 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

(back to top)

Usage

  1. In your QML file include the purchasing classes:
    import Store
    import Product  
  2. 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()
        }
    }
    }

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Moritz Stötter - moritzstoetter.dev - hi@moritzstoetter.dev

(back to top)