bool PaymentService::handle_event(bps_event_t *p_event) { 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"; const char *pid = paymentservice_event_get_digital_good_id(p_event, 0); dict["product_id"] = String(pid ? pid : ""); }; } else { const char *desc = paymentservice_event_get_error_text(p_event); 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; };
/** * 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; }