| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Purchasing module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3-COMM$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include "qinapptransaction.h" |
| 30 | |
| 31 | QT_BEGIN_NAMESPACE |
| 32 | |
| 33 | class QInAppTransactionPrivate |
| 34 | { |
| 35 | public: |
| 36 | QInAppTransactionPrivate(QInAppTransaction::TransactionStatus s, |
| 37 | QInAppProduct *p) |
| 38 | : status(s) |
| 39 | , product(p) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | QInAppTransaction::TransactionStatus status; |
| 44 | QInAppProduct *product; |
| 45 | }; |
| 46 | |
| 47 | /*! |
| 48 | \qmltype Transaction |
| 49 | \inqmlmodule QtPurchasing |
| 50 | \since QtPurchasing 1.0 |
| 51 | \ingroup qtpurchasing |
| 52 | \brief Contains information about an in-app transaction. |
| 53 | |
| 54 | Transaction contains information about a transaction in the external app store and is |
| 55 | usually provided as a result of calling \l{QtPurchasing::Product::purchase()}{purchase()} |
| 56 | on a product. When the purchase flow has ended, whether it's successful or not, either the |
| 57 | product's \l{QtPurchasing::Product::onPurchaseSucceeded}{onPurchaseSucceeded} or |
| 58 | \l{QtPurchasing::Product::onPurchaseFailed}{onPurchaseFailed} handler will be called |
| 59 | with a transaction object as argument. |
| 60 | |
| 61 | Transaction cannot be created directly in QML, but is only provided as an argument to |
| 62 | the purchase handlers in the products. |
| 63 | */ |
| 64 | |
| 65 | |
| 66 | /*! |
| 67 | \class QInAppTransaction |
| 68 | \inmodule QtPurchasing |
| 69 | \brief Contains information about a transaction in the external app store. |
| 70 | |
| 71 | QInAppTransaction contains information about a transaction in the external app store and is |
| 72 | usually provided as a result of calling QInAppProduct::purchase(). When the purchase flow has |
| 73 | been completed by the user (confirming the purchase, for instance by entering their password), |
| 74 | the QInAppStore instance containing the product will emit a QInAppStore::transactionReady() |
| 75 | signal with data about the transaction. |
| 76 | |
| 77 | The status() provides information on whether the transaction was successful or not. If it was |
| 78 | successful, then the application should take appropriate action. When the necessary action has |
| 79 | been performed, finalize() should be called. The finalize() function should be called regardless |
| 80 | of the status of the transaction. |
| 81 | |
| 82 | It is important that the application stores the purchase information before calling finalize(). |
| 83 | If a transaction is not finalized (for example because the application was interrupted before |
| 84 | it had a chance to save the information), then the transaction will be emitted again the next |
| 85 | time the product is registered by QInAppStore::registerProduct(). |
| 86 | |
| 87 | Transactions can also be emitted after calling QInAppStore::restorePurchases(), at which point |
| 88 | a new transaction will be emitted for each previously purchased unlockable product with the |
| 89 | status of PurchaseRestored. |
| 90 | |
| 91 | \note Since transactions may under certain circumstances be emitted for the same transaction |
| 92 | several times, the application should always check if the transaction has been registered |
| 93 | before. Do not expect each transaction to be unique. |
| 94 | */ |
| 95 | |
| 96 | /*! |
| 97 | * \internal |
| 98 | */\ |
| 99 | QInAppTransaction::QInAppTransaction(TransactionStatus status, |
| 100 | QInAppProduct *product, |
| 101 | QObject *parent) |
| 102 | : QObject(parent) |
| 103 | { |
| 104 | d = QSharedPointer<QInAppTransactionPrivate>(new QInAppTransactionPrivate(status, product)); |
| 105 | } |
| 106 | |
| 107 | /*! |
| 108 | * \internal |
| 109 | */\ |
| 110 | QInAppTransaction::~QInAppTransaction() |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | /*! |
| 115 | * \qmlproperty object QtPurchasing::Transaction::product |
| 116 | * |
| 117 | * This property holds the product which is the object of this transaction. |
| 118 | */ |
| 119 | |
| 120 | /*! |
| 121 | * \property QInAppTransaction::product |
| 122 | * |
| 123 | * This property holds the product which is the object of this transaction. |
| 124 | */ |
| 125 | QInAppProduct *QInAppTransaction::product() const |
| 126 | { |
| 127 | return d->product; |
| 128 | } |
| 129 | |
| 130 | /*! |
| 131 | \enum QInAppTransaction::TransactionStatus |
| 132 | |
| 133 | This enum type is used to specify the status of the transaction. |
| 134 | |
| 135 | \value Unknown The transaction status has not been set. |
| 136 | \value PurchaseApproved The purchase was successfully completed. |
| 137 | \value PurchaseFailed The purchase was not completed for some reason. This could be because |
| 138 | the user canceled the transaction, but it could also for example be caused by a missing |
| 139 | network connection. |
| 140 | \value PurchaseRestored The product has previously been purchased and the purchase has |
| 141 | now been restored as a result of calling \l{QInAppStore::restorePurchases()}. |
| 142 | |
| 143 | */ |
| 144 | |
| 145 | /*! |
| 146 | \enum QInAppTransaction::FailureReason |
| 147 | |
| 148 | This enum type specifies the reason for failure if a transaction has the \l PurchaseFailed status. |
| 149 | |
| 150 | \value NoFailure The status of the transaction is not \l PurchaseFailed. |
| 151 | \value CanceledByUser The transaction was manually canceled by the user. |
| 152 | \value ErrorOccurred An error occurred, preventing the transaction from completing. See the \l errorString |
| 153 | property for more information on the precise error that occurred. |
| 154 | */ |
| 155 | |
| 156 | /*! |
| 157 | \qmlproperty enum QtPurchasing::Transaction::status |
| 158 | |
| 159 | This property holds the status of the transaction. |
| 160 | |
| 161 | \list |
| 162 | \li Transaction.PurchaseApproved The purchase was successfully completed. |
| 163 | \li Transaction.PurchaseFailed The purchase was not completed for some reason. This could be because |
| 164 | the user canceled the transaction, but it could also for example be caused by a missing |
| 165 | network connection. |
| 166 | \li PurchaseRestored The product has previously been purchased and the purchase has |
| 167 | now been restored as a result of calling \l{QtPurchasing::Store::restorePurchases()}{restorePurchases()}. |
| 168 | \endlist |
| 169 | */ |
| 170 | |
| 171 | /*! |
| 172 | * \property QInAppTransaction::status |
| 173 | * |
| 174 | * This property holds the status of the transaction. If the purchase was successfully |
| 175 | * completed, the status will be PurchaseApproved. If the purchase failed |
| 176 | * or was unsuccessful then the status will be PurchaseFailed. If the |
| 177 | * transaction was restored as a result of calling QInAppStore::restorePurchases() |
| 178 | * then the status will be PurchaseRestored. |
| 179 | */ |
| 180 | |
| 181 | QInAppTransaction::TransactionStatus QInAppTransaction::status() const |
| 182 | { |
| 183 | return d->status; |
| 184 | } |
| 185 | |
| 186 | /*! |
| 187 | * \qmlproperty enum QtPurchasing::Transaction::failureReason |
| 188 | * |
| 189 | * This property holds the reason for the failure if the transaction failed. |
| 190 | * |
| 191 | * \list |
| 192 | * \li Transaction.NoFailure The transaction did not fail. |
| 193 | * \li Transaction.CanceledByUser The transaction was canceled by the user. |
| 194 | * \li Transaction.ErrorOccurred The transaction failed due to an error. |
| 195 | * \endlist |
| 196 | * |
| 197 | * \sa errorString |
| 198 | */ |
| 199 | |
| 200 | /*! |
| 201 | * \property QInAppTransaction::failureReason |
| 202 | * |
| 203 | * This property holds the reason for the failure if the transaction's status is |
| 204 | * \l{PurchaseFailed}. If the purchase was canceled by the user, the failure |
| 205 | * reason will be \l{CanceledByUser}. If the purchase failed due to an error, it |
| 206 | * will be \l{ErrorOccurred}. If the purchase did not fail, the failure reason |
| 207 | * will be \l{NoFailure}. |
| 208 | * |
| 209 | * \sa errorString, status |
| 210 | */ |
| 211 | QInAppTransaction::FailureReason QInAppTransaction::failureReason() const |
| 212 | { |
| 213 | return NoFailure; |
| 214 | } |
| 215 | |
| 216 | /*! |
| 217 | * \qmlproperty string QtPurchasing::Transaction::errorString |
| 218 | * |
| 219 | * This property holds a string describing the error if the transaction failed |
| 220 | * due to an error. The contents of the error string is platform-specific. |
| 221 | * |
| 222 | * \sa failureReason, status |
| 223 | */ |
| 224 | |
| 225 | /*! |
| 226 | * \property QInAppTransaction::errorString |
| 227 | * |
| 228 | * This property holds a string describing the error if the transaction failed |
| 229 | * due to an error. The contents of the error string is platform-specific. |
| 230 | * |
| 231 | * \sa failureReason, status |
| 232 | */ |
| 233 | QString QInAppTransaction::errorString() const |
| 234 | { |
| 235 | return QString(); |
| 236 | } |
| 237 | |
| 238 | /*! |
| 239 | * \qmlproperty time QtPurchasing::Transaction::timestamp |
| 240 | * |
| 241 | * This property holds the timestamp of the transaction. The timestamp |
| 242 | * can be invalid if there is no valid transaction, for example if the user |
| 243 | * canceled the purchase. |
| 244 | * |
| 245 | * \sa orderId |
| 246 | */ |
| 247 | |
| 248 | /*! |
| 249 | * \property QInAppTransaction::timestamp |
| 250 | * |
| 251 | * This property holds the timestamp of the transaction. The timestamp |
| 252 | * can be invalid if there is no valid transaction, for example if the user |
| 253 | * canceled the purchase. |
| 254 | * |
| 255 | * \sa orderId |
| 256 | */ |
| 257 | QDateTime QInAppTransaction::timestamp() const |
| 258 | { |
| 259 | return QDateTime(); |
| 260 | } |
| 261 | |
| 262 | /*! |
| 263 | * \qmlproperty string QtPurchasing::Transaction::orderId |
| 264 | * |
| 265 | * This property holds a unique identifier for this transaction. This value may be an empty |
| 266 | * string if no transaction was registered (for example for canceled purchases). |
| 267 | */ |
| 268 | |
| 269 | /*! |
| 270 | * \property QInAppTransaction::orderId |
| 271 | * |
| 272 | * This property holds a unique identifier for this transaction. This value may be an empty |
| 273 | * string if no transaction was registered (for example for canceled purchases). |
| 274 | */ |
| 275 | QString QInAppTransaction::orderId() const |
| 276 | { |
| 277 | return QString(); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | |
| 282 | /*! |
| 283 | * Returns the platform-specific property given by \a propertyName. |
| 284 | * |
| 285 | * The following properties are available on Google Play: |
| 286 | * \list |
| 287 | * \li AndroidSignature: The signature of the transaction, as given by the |
| 288 | * private key for the application. |
| 289 | * \li AndroidPurchaseData: The purchase data returned by the Google Play store. |
| 290 | * \endlist |
| 291 | * These properties can be used to verify the purchase using the public key of |
| 292 | * your application. It is also possible to have the back-end verify the purchases |
| 293 | * by passing in the public key before registering products, using |
| 294 | * QInAppStore::setPlatformProperty(). |
| 295 | */ |
| 296 | QString QInAppTransaction::platformProperty(const QString &propertyName) const |
| 297 | { |
| 298 | Q_UNUSED(propertyName); |
| 299 | return QString(); |
| 300 | } |
| 301 | |
| 302 | /*! |
| 303 | * \fn void QInAppTransaction::finalize() |
| 304 | * |
| 305 | * Call this when the application has finished performing all necessary reactions |
| 306 | * to the purchase. If the status is PurchaseApproved, the application should |
| 307 | * store the information about the transaction in a safe way before finalizing it. |
| 308 | * All transactions should be finalized. |
| 309 | */ |
| 310 | |
| 311 | /*! |
| 312 | * \qmlmethod void QtPurchasing::Transaction::finalize() |
| 313 | * |
| 314 | * Call this when the application has finished performing all necessary reactions |
| 315 | * to the purchase. If the purchase succeeded, the application should store the |
| 316 | * information about the transaction in a safe way before finalizing it. |
| 317 | * All transactions should be finalized. |
| 318 | */ |
| 319 | |
| 320 | QT_END_NAMESPACE |
| 321 | |