Esempio n. 1
0
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);

	char *id = NULL;
	char *sku = NULL;

	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();
	unsigned int request_id;
	chdir(launch_dir_ptr);
	int ret = paymentservice_purchase_request(params.has("product_sku") ? NULL : p_id.get_data(),
			params.has("product_sku") ? p_sku.get_data() : NULL,
			NULL, NULL, NULL, NULL, get_window_group_id(), &request_id);
	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;
};
Esempio n. 2
0
/**
 * A sample application that demonstrates the BlackBerry Native APIs for
 * making in-app purchases. The sample sets the connection mode to local,
 * allows the purchase of a digital good by using the swipe down gesture,
 * and displays any existing purchases.
 */
int
main(int argc, char *argv[])
{
    int exit_application = 0;

    /*
     * Before we can listen for events from the BlackBerry Tablet OS platform
     * services, we need to initialize the BPS infrastructure
     */
    bps_initialize();

    /*
     * Initialize the screen so that the window group Id is properly set, to allow
     * the Payment Service dialogs to be displayed.
     */
    if (setup_screen() != EXIT_SUCCESS) {
        fprintf(stderr, "Unable to initialize screen.");
        exit(0);
    }

    /*
     * Once the BPS infrastructure has been initialized we can register for
     * events from the various BlackBerry Tablet OS platform services. The
     * Navigator service manages and delivers application life cycle and
     * visibility events.
     * For this sample, we request Navigator events so that we can track when
     * the system is terminating the application (NAVIGATOR_EXIT event), and as a
     * convenient way to trigger a purchase request (NAVIGATOR_SWIPE_DOWN).
     * We request PaymentService events so we can be notified when the payment service
     * responds to our requests/queries.
     */
    navigator_request_events(0);
    paymentservice_request_events(0);

    /*
     * Set the Payment Service connection mode to local. This allows us to
     * test the API without the need to contact the AppWorld nor payment servers.
     */
    paymentservice_set_connection_mode(true);

    /*
     * Create a set of purchase parameters, which describe the digital good
     * to be purchased and the application the goods are associated with.
     */
    const char* digital_good_id = "Digital-Good-1-ID";
    const char* digital_good_name = "Sample Digital Good 1";
    const char* digital_good_sku = "SAMPLE_DIGITAL_GOOD_SKU_1";
    const char* metadata = "Sample purchase metadata";
    const char* purchase_app_icon = "http://www.rim.com/products/appworld_3col.jpg";
    const char* purchase_app_name = "Payment Service Sample App";

    /*
     * Define a request ID to hold the returned value from the purchase request.
     */
    unsigned request_id = 0;

    /*
     * initiate the application with a purchase of the sample digital good.
     */
    if (paymentservice_purchase_request(digital_good_id, digital_good_sku, digital_good_name,
            metadata, purchase_app_name, purchase_app_icon, get_window_group_id(), &request_id) != BPS_SUCCESS) {
        fprintf(stderr, "Error: purchase request failed.\n");
    }

    /*
     * Process Payment Service and Navigator events until we receive a NAVIGATOR_EXIT event.
     */
    while (!exit_application) {
        /*
         * Using a negative timeout (-1) in the call to bps_get_event(...)
         * ensures that we don't busy wait by blocking until an event is
         * available.
         */
        bps_event_t *event = NULL;
        bps_get_event(&event, -1);

        if (event) {
            /*
             * If it is a Payment Service event, determine the response code
             * and handle the event accordingly.
             */
            if (bps_event_get_domain(event) == paymentservice_get_domain()) {
                if (SUCCESS_RESPONSE == paymentservice_event_get_response_code(event)) {
                    if (PURCHASE_RESPONSE == bps_event_get_code(event)) {
                        onPurchaseSuccess(event);
                        unsigned request_id = 0;
                        if (paymentservice_get_existing_purchases_request(false, get_window_group_id(), &request_id) != BPS_SUCCESS) {
                            fprintf(stderr, "Error: get existing purchases failed.\n");
                        }
                    } else
                        onGetExistingPurchasesSuccess(event);
                } else {
                    failureCommon(event);
                }
            }

            /*
             * If it is a NAVIGATOR_EXIT event then set the exit_application
             * flag so the application will stop processing events, clean up
             * and exit.
             *
             * If it is a NAVIGATOR_SWIPE_DOWN event, initiate the purchase of
             * the sample digital good.
             */
            if (bps_event_get_domain(event) == navigator_get_domain()) {
                if (NAVIGATOR_EXIT == bps_event_get_code(event)) {
                    exit_application = 1;
                } else if (NAVIGATOR_SWIPE_DOWN == bps_event_get_code(event)) {
                    if (paymentservice_purchase_request(digital_good_id, digital_good_sku, digital_good_name,
                            metadata, purchase_app_name, purchase_app_icon, get_window_group_id(), &request_id) != BPS_SUCCESS) {
                        fprintf(stderr, "Error: purchase request failed.\n");
                    }
                }
            }
        }
    }

    /*
     * Clean up the BPS infrastructure and exit
     */
    bps_shutdown();
    screen_destroy_window(screen_win);
    screen_destroy_context(screen_ctx);
    return 0;
}