iapminimal.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "iapminimal.h"
  2. #include <QStringBuilder>
  3. #include <QTimer>
  4. #include <QDateTime>
  5. #define PRODUCTID "751224"
  6. IAPminimal::IAPminimal(QObject *parent) :
  7. QObject(parent),
  8. isbusy(false)
  9. {
  10. qRegisterMetaType<IAPClient::ProductDataHash>("IAPClient::ProductDataHash");
  11. startIAP();
  12. }
  13. IAPminimal::~IAPminimal()
  14. {
  15. stopIAP();
  16. }
  17. void IAPminimal::getProductData()
  18. {
  19. if(isbusy)
  20. return;
  21. startIAP();
  22. iap_client->getProductData(PRODUCTID);
  23. emit statusBusy();
  24. isbusy = true;
  25. }
  26. void IAPminimal::purchaseProduct()
  27. {
  28. if(isbusy)
  29. return;
  30. startIAP();
  31. iap_client->purchaseProduct(PRODUCTID, IAPClient::ForcedAutomaticRestoration);
  32. emit statusBusy();
  33. isbusy = true;
  34. }
  35. void IAPminimal::productDataReceived(int /*requestId*/, QString status, IAPClient::ProductDataHash productData)
  36. {
  37. isbusy = false;
  38. QString result = status;
  39. if(QString::compare(status, "OK", Qt::CaseInsensitive)==0){
  40. result = "id : " % productData.value("id").toString() % "\n" %
  41. "info : " % productData.value("info").toString() % "\n" %
  42. "shortdescription : " % productData.value("shortdescription").toString() % "\n" %
  43. "price : " % productData.value("price").toString() %
  44. "\n Clickme to buy this product";
  45. }
  46. emit productDataComplete(result);
  47. }
  48. void IAPminimal::closeIAP()
  49. {
  50. stopIAP();
  51. QTimer::singleShot(21000, this, SIGNAL(iapClosed()));
  52. emit statusBusy();
  53. isbusy = true;
  54. }
  55. void IAPminimal::stopIAP()
  56. {
  57. delete iap_client;
  58. iap_client = NULL;
  59. }
  60. void IAPminimal::startIAP()
  61. {
  62. if(iap_client)
  63. return;
  64. // NOTE: this is syncronous operation takes considerable amount of time
  65. iap_client = new IAPClient();
  66. connect(iap_client, SIGNAL(productDataReceived( int, QString, IAPClient::ProductDataHash)), this,
  67. SLOT(productDataReceived(int, QString, IAPClient::ProductDataHash)), Qt::QueuedConnection);
  68. connect(iap_client, SIGNAL(purchaseCompleted( int , QString, QString)), this,
  69. SLOT(purchaseCompleted( int , QString, QString)),Qt::QueuedConnection);
  70. connect(iap_client, SIGNAL(purchaseFlowFinished(int)), this, SLOT(purchaseFlowFinished(int)), Qt::QueuedConnection);
  71. }
  72. void IAPminimal::purchaseFlowFinished( int /*requestId*/ )
  73. {
  74. isbusy = false;
  75. }
  76. void IAPminimal::purchaseCompleted( int /*requestId*/, QString status, QString purchaseTicket )
  77. {
  78. QString result = "purchaseCompleted : " % status % "\nclickme to exit the app";
  79. emit productPurchaseComplete(result);
  80. }