2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* payment_service.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifdef PAYMENT_SERVICE_ENABLED
|
|
|
|
|
|
|
|
#include "payment_service.h"
|
|
|
|
|
|
|
|
#include "bbutil.h"
|
|
|
|
#include <errno.h>
|
2017-03-18 23:36:26 +00:00
|
|
|
#include <string.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
#include <unistd.h>
|
2017-03-18 23:36:26 +00:00
|
|
|
extern char *launch_dir_ptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void PaymentService::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("request_product_info"), &PaymentService::request_product_info);
|
|
|
|
ObjectTypeDB::bind_method(_MD("purchase"), &PaymentService::purchase);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_pending_event_count"), &PaymentService::get_pending_event_count);
|
|
|
|
ObjectTypeDB::bind_method(_MD("pop_pending_event"), &PaymentService::pop_pending_event);
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Error PaymentService::request_product_info(Variant p_params) {
|
|
|
|
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
};
|
|
|
|
|
|
|
|
Error PaymentService::purchase(Variant p_params) {
|
|
|
|
|
|
|
|
Dictionary params = p_params;
|
|
|
|
ERR_FAIL_COND_V((!params.has("product_id")) && (!params.has("product_sku")), ERR_INVALID_PARAMETER);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char *id = NULL;
|
|
|
|
char *sku = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
CharString p_id = params.has("product_id") ? String(params["product_id"]).ascii() : CharString();
|
|
|
|
CharString p_sku = params.has("product_sku") ? String(params["product_sku"]).ascii() : CharString();
|
2014-02-10 01:10:30 +00:00
|
|
|
unsigned int request_id;
|
|
|
|
chdir(launch_dir_ptr);
|
|
|
|
int ret = paymentservice_purchase_request(params.has("product_sku") ? NULL : p_id.get_data(),
|
2017-03-18 23:36:26 +00:00
|
|
|
params.has("product_sku") ? p_sku.get_data() : NULL,
|
|
|
|
NULL, NULL, NULL, NULL, get_window_group_id(), &request_id);
|
2014-02-10 01:10:30 +00:00
|
|
|
chdir("app/native");
|
|
|
|
|
|
|
|
if (ret != BPS_SUCCESS) {
|
|
|
|
int eret = errno;
|
|
|
|
printf("purchase error %i, %x, %i, %x\n", ret, ret, eret, eret);
|
|
|
|
ERR_FAIL_V((Error)eret);
|
|
|
|
return (Error)eret;
|
|
|
|
};
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool PaymentService::handle_event(bps_event_t *p_event) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (bps_event_get_domain(p_event) != paymentservice_get_domain()) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Dictionary dict;
|
|
|
|
int res = paymentservice_event_get_response_code(p_event);
|
|
|
|
if (res == SUCCESS_RESPONSE) {
|
|
|
|
dict["result"] = "ok";
|
|
|
|
|
|
|
|
res = bps_event_get_code(p_event);
|
|
|
|
if (res == PURCHASE_RESPONSE) {
|
|
|
|
dict["type"] = "purchase";
|
2017-03-18 23:36:26 +00:00
|
|
|
const char *pid = paymentservice_event_get_digital_good_id(p_event, 0);
|
|
|
|
dict["product_id"] = String(pid ? pid : "");
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
const char *desc = paymentservice_event_get_error_text(p_event);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (strcmp(desc, "alreadyPurchased") == 0) {
|
|
|
|
dict["result"] = "ok";
|
|
|
|
} else {
|
|
|
|
dict["result"] = "error";
|
|
|
|
dict["error_description"] = paymentservice_event_get_error_text(p_event);
|
|
|
|
dict["error_code"] = paymentservice_event_get_error_id(p_event);
|
|
|
|
printf("error code is %i\n", paymentservice_event_get_error_id(p_event));
|
|
|
|
printf("error description is %s\n", paymentservice_event_get_error_text(p_event));
|
|
|
|
};
|
|
|
|
dict["product_id"] = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
res = bps_event_get_code(p_event);
|
|
|
|
if (res == PURCHASE_RESPONSE) {
|
|
|
|
dict["type"] = "purchase";
|
|
|
|
};
|
|
|
|
|
|
|
|
printf("********** adding event with result %ls\n", String(dict["result"]).c_str());
|
|
|
|
pending_events.push_back(dict);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
int PaymentService::get_pending_event_count() {
|
|
|
|
return pending_events.size();
|
|
|
|
};
|
|
|
|
|
|
|
|
Variant PaymentService::pop_pending_event() {
|
|
|
|
|
|
|
|
Variant front = pending_events.front()->get();
|
|
|
|
pending_events.pop_front();
|
|
|
|
|
|
|
|
return front;
|
|
|
|
};
|
|
|
|
|
|
|
|
PaymentService::PaymentService() {
|
|
|
|
|
|
|
|
paymentservice_request_events(0);
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
paymentservice_set_connection_mode(true);
|
|
|
|
#else
|
|
|
|
paymentservice_set_connection_mode(false);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
PaymentService::~PaymentService(){
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|