payment_service.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* payment_service.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifdef PAYMENT_SERVICE_ENABLED
  30. #include "payment_service.h"
  31. #include "bbutil.h"
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <unistd.h>
  35. extern char* launch_dir_ptr;
  36. void PaymentService::_bind_methods() {
  37. ObjectTypeDB::bind_method(_MD("request_product_info"),&PaymentService::request_product_info);
  38. ObjectTypeDB::bind_method(_MD("purchase"),&PaymentService::purchase);
  39. ObjectTypeDB::bind_method(_MD("get_pending_event_count"),&PaymentService::get_pending_event_count);
  40. ObjectTypeDB::bind_method(_MD("pop_pending_event"),&PaymentService::pop_pending_event);
  41. };
  42. Error PaymentService::request_product_info(Variant p_params) {
  43. return ERR_UNAVAILABLE;
  44. };
  45. Error PaymentService::purchase(Variant p_params) {
  46. Dictionary params = p_params;
  47. ERR_FAIL_COND_V((!params.has("product_id")) && (!params.has("product_sku")), ERR_INVALID_PARAMETER);
  48. char* id = NULL;
  49. char* sku = NULL;
  50. CharString p_id = params.has("product_id")?String(params["product_id"]).ascii():CharString();
  51. CharString p_sku = params.has("product_sku")?String(params["product_sku"]).ascii():CharString();
  52. unsigned int request_id;
  53. chdir(launch_dir_ptr);
  54. int ret = paymentservice_purchase_request(params.has("product_sku") ? NULL : p_id.get_data(),
  55. params.has("product_sku") ? p_sku.get_data() : NULL,
  56. NULL, NULL, NULL, NULL, get_window_group_id(), &request_id);
  57. chdir("app/native");
  58. if (ret != BPS_SUCCESS) {
  59. int eret = errno;
  60. printf("purchase error %i, %x, %i, %x\n", ret, ret, eret, eret);
  61. ERR_FAIL_V((Error)eret);
  62. return (Error)eret;
  63. };
  64. return OK;
  65. };
  66. bool PaymentService::handle_event(bps_event_t* p_event) {
  67. if (bps_event_get_domain(p_event) != paymentservice_get_domain()) {
  68. return false;
  69. };
  70. Dictionary dict;
  71. int res = paymentservice_event_get_response_code(p_event);
  72. if (res == SUCCESS_RESPONSE) {
  73. dict["result"] = "ok";
  74. res = bps_event_get_code(p_event);
  75. if (res == PURCHASE_RESPONSE) {
  76. dict["type"] = "purchase";
  77. const char* pid = paymentservice_event_get_digital_good_id(p_event, 0);
  78. dict["product_id"] = String(pid?pid:"");
  79. };
  80. } else {
  81. const char* desc = paymentservice_event_get_error_text(p_event);
  82. if (strcmp(desc, "alreadyPurchased") == 0) {
  83. dict["result"] = "ok";
  84. } else {
  85. dict["result"] = "error";
  86. dict["error_description"] = paymentservice_event_get_error_text(p_event);
  87. dict["error_code"] = paymentservice_event_get_error_id(p_event);
  88. printf("error code is %i\n", paymentservice_event_get_error_id(p_event));
  89. printf("error description is %s\n", paymentservice_event_get_error_text(p_event));
  90. };
  91. dict["product_id"] = "";
  92. };
  93. res = bps_event_get_code(p_event);
  94. if (res == PURCHASE_RESPONSE) {
  95. dict["type"] = "purchase";
  96. };
  97. printf("********** adding event with result %ls\n", String(dict["result"]).c_str());
  98. pending_events.push_back(dict);
  99. return true;
  100. };
  101. int PaymentService::get_pending_event_count() {
  102. return pending_events.size();
  103. };
  104. Variant PaymentService::pop_pending_event() {
  105. Variant front = pending_events.front()->get();
  106. pending_events.pop_front();
  107. return front;
  108. };
  109. PaymentService::PaymentService() {
  110. paymentservice_request_events(0);
  111. #ifdef DEBUG_ENABLED
  112. paymentservice_set_connection_mode(true);
  113. #else
  114. paymentservice_set_connection_mode(false);
  115. #endif
  116. };
  117. PaymentService::~PaymentService() {
  118. };
  119. #endif