Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Trusted Contributor
Posts: 195
Registered: ‎03-12-2012
My Device: PlayBook
My Carrier: None

A barebone paymentservice sample for OpenGL ES1.1 template

For those who might be stuck using the paymentservice API with the OpenGL template, I'm including a small sample here to help those who are stuck as I have been.   The key to making paymentservice work is to specify the correct window group.  

 

In the OpenGL template, the application window is created in bbutil.c.  In the code below, I simply use the name "MAIN_WINDOW".  You can use anything you like.  For example, in the paymentservice sample by RIM, it uses the application PID.

 

The app draws an aqua background with an orange message saying "Tap screen to buy something expensive".  Tapping should bring up the paymentservice dialog in local mode.  Enjoy!

 

Here are the steps:

 

1. Create a new C/C++ Tablet OS project using the OpenGL ES 1.1 Application Template.

2. Add the following to bbutil.c (it's a file automatically created for you by Momentics) after the properties of screen_win have been specified

    rc = screen_create_window_group(screen_win, "MAIN_WINDOW");
    if (rc) {
        perror("screen_get_window_property_pv");
        bbutil_terminate();
        return EXIT_FAILURE;
    }

 

3. Replace main.c with the following:

#include <assert.h>
#include <screen/screen.h>
#include <bps/navigator.h>
#include <bps/paymentservice.h>
#include <bps/screen.h>
#include <bps/bps.h>
#include <bps/event.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <EGL/egl.h>
#include <GLES/gl.h>

#include "bbutil.h"

static font_t* font;

const char* digital_good_id = "1234557";
const char* digital_good_name = "Stuff to sell";
const char* digital_good_sku = "stuff_to_sell";
const char* metadata = "Some metadata";
const char* purchase_app_icon =
    "http://www.rim.com/products/appworld_3col.jpg";
const char* purchase_app_name = "OpenGLPaymentSample";

void handleScreenEvent(bps_event_t *event) {
    screen_event_t screen_event = screen_event_get_event(event);

    int screen_val;
    unsigned request_id = 0;

    screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_TYPE,
            &screen_val);

    switch (screen_val) {
    case SCREEN_EVENT_MTOUCH_TOUCH:
        if (paymentservice_purchase_request(digital_good_id, digital_good_sku, digital_good_name,
                metadata, purchase_app_name, purchase_app_icon, "MAIN_WINDOW", &request_id) != BPS_SUCCESS) {
            fprintf(stderr, "Error: purchase request failed.\n");
        }
        break;
    case SCREEN_EVENT_MTOUCH_MOVE:
    case SCREEN_EVENT_MTOUCH_RELEASE:
        break;
    }
}

int initialize() {
    //Query width and height of the window surface created by utility code
    EGLint surface_width, surface_height;

    eglQuerySurface(egl_disp, egl_surf, EGL_WIDTH, &surface_width);
    eglQuerySurface(egl_disp, egl_surf, EGL_HEIGHT, &surface_height);

    EGLint err = eglGetError();
    if (err != 0x3000) {
        fprintf(stderr, "Unable to query EGL surface dimensions\n");
        return EXIT_FAILURE;
    }

    glShadeModel(GL_SMOOTH);

    //set clear color to white
    glClearColor(0.0f, 0.5f, 0.4f, 1.0f);

    glViewport(0, 0, surface_width, surface_height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //glOrthof(0.0f, (float) (surface_width) / (float) (surface_height), 0.0f, 1.0f, -1.0f, 1.0f);

    glOrthof(0.0f, (float)(surface_width), 0.0f, (float)(surface_height),  -1.0f, 1.0f);


    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef((float) (surface_width) / (float) (surface_height) / 2, 0.5f,
            0.0f);

    return EXIT_SUCCESS;
}

void render() {
    //Typical render pass
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    bbutil_render_text(font, "Tap screen to buy something expensive", 10.0, 10.0, 1.0, 0.5, 0.0, 1.0);

    //Use utility code to update the screen
    bbutil_swap();
}

int main(int argc, char *argv[]) {
    int rc;
    int exit_application = 0;
    static screen_context_t screen_cxt;

    //Create a screen context that will be used to create an EGL surface to to receive libscreen events
    screen_create_context(&screen_cxt, 0);

    //Initialize BPS library
    bps_initialize();

    //Use utility code to initialize EGL for rendering with GL ES 1.1
    if (EXIT_SUCCESS != bbutil_init_egl(screen_cxt)) {
        fprintf(stderr, "bbutil_init_egl failed\n");
        bbutil_terminate();
        screen_destroy_context(screen_cxt);
        return 0;
    }

    //Initialize application logic
    if (EXIT_SUCCESS != initialize()) {
        fprintf(stderr, "initialize failed\n");
        bbutil_terminate();
        screen_destroy_context(screen_cxt);
        return 0;
    }

    //Signal BPS library that navigator and screen events will be requested
    if (BPS_SUCCESS != screen_request_events(screen_cxt)) {
        fprintf(stderr, "screen_request_events failed\n");
        bbutil_terminate();
        screen_destroy_context(screen_cxt);
        return 0;
    }

    if (BPS_SUCCESS != navigator_request_events(0)) {
        fprintf(stderr, "navigator_request_events failed\n");
        bbutil_terminate();
        screen_destroy_context(screen_cxt);
        return 0;
    }

    //Signal BPS library that navigator orientation is not to be locked
    if (BPS_SUCCESS != navigator_rotation_lock(false)) {
        fprintf(stderr, "navigator_rotation_lock failed\n");
        bbutil_terminate();
        screen_destroy_context(screen_cxt);
        return 0;
    }

    if (BPS_SUCCESS != paymentservice_request_events(0)) {
        fprintf(stderr, "paymentservice_request_events failed\n");
        bbutil_terminate();
        screen_destroy_context(screen_cxt);
        return 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);


	int dpi = bbutil_calculate_dpi(screen_cxt);

	int pointSize = 15;
	font = bbutil_load_font(
					"/usr/fonts/font_repository/monotype/arial.ttf", pointSize, dpi);
	if (!font) {
		fprintf(stderr, "load_font error\n");
		return EXIT_FAILURE;
	}

    while (!exit_application) {
        //Request and process all available BPS events
        bps_event_t *event = NULL;

        for(;;) {
            rc = bps_get_event(&event, 0);
            assert(rc == BPS_SUCCESS);

            if (event) {
                int domain = bps_event_get_domain(event);

                if (domain == screen_get_domain()) {
                    handleScreenEvent(event);
                } else if ((domain == navigator_get_domain())
                        && (NAVIGATOR_EXIT == bps_event_get_code(event))) {
                    exit_application = 1;
                } else 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)) {
                        	fprintf(stderr, "PURCHASE RESPONSE\n");
                        } else {
                            fprintf(stderr, "Some other payment service response");
                        }
                    } else {
                        fprintf(stderr, "paymentservice_event_get_response_code not SUCCESS_RESPONSE\n");
                    }
                }
            } else {
                break;
            }
        }
        render();
    }

    //Stop requesting events from libscreen
    screen_stop_events(screen_cxt);

    //Shut down BPS library for this process
    bps_shutdown();

    //Use utility code to terminate EGL setup
    bbutil_terminate();

    //Destroy libscreen context
    screen_destroy_context(screen_cxt);
    return 0;
}

 

Sucroid.com
Sweet Apps for the Fans
New Contributor
Posts: 3
Registered: ‎06-26-2012
My Device: Playbook
My Carrier: None

Re: A barebone paymentservice sample for OpenGL ES1.1 template

Hello! Thanks a TON, it made it perfectly clear for me, i already got it implemented in my APP, it was really helpful, works like a charm!

I couldnt find this info anywhere else!