Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 302 lines (262 sloc) 11.152 kB
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
1 /*
bca4b2a @jhoffmannrim update NDK-Samples next for NDK 2.0
jhoffmannrim authored
2 * Copyright (c) 2011-2012 Research In Motion Limited.
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <bps/bps.h>
18 #include <bps/paymentservice.h>
19 #include <bps/navigator.h>
20 #include <screen/screen.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 static screen_context_t screen_ctx;
24 static screen_window_t screen_win;
25
26 /**
27 * Use the PID to set the window group id.
28 */
693b288 @jhoffmannrim return a const char * from get_window_group_id()
jhoffmannrim authored
29 static const char *
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
30 get_window_group_id()
31 {
32 static char s_window_group_id[16] = "";
33
34 if (s_window_group_id[0] == '\0') {
35 snprintf(s_window_group_id, sizeof(s_window_group_id), "%d", getpid());
36 }
37
38 return s_window_group_id;
39 }
40
41 /**
42 * Set up a basic screen, so that the navigator will
43 * send window state events when the window state changes.
44 *
45 * @return @c EXIT_SUCCESS or @c EXIT_FAILURE
46 */
47 int
48 setup_screen()
49 {
50 if (screen_create_context(&screen_ctx, SCREEN_APPLICATION_CONTEXT) != 0) {
51 return EXIT_FAILURE;
52 }
53
54 if (screen_create_window(&screen_win, screen_ctx) != 0) {
55 screen_destroy_context(screen_ctx);
56 return EXIT_FAILURE;
57 }
58
c7f4f26 Update samples to enable use of dialog service
Anthony Hu authored
59 if (screen_create_window_group(screen_win, get_window_group_id()) != 0) goto fail;
60
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
61 int usage = SCREEN_USAGE_NATIVE;
62 if (screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_USAGE, &usage) != 0) goto fail;
63
64 if (screen_create_window_buffers(screen_win, 1) != 0) goto fail;
65
66 screen_buffer_t buff;
67 if (screen_get_window_property_pv(screen_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void*)&buff) != 0) goto fail;
68
69 int buffer_size[2];
70 if (screen_get_buffer_property_iv(buff, SCREEN_PROPERTY_BUFFER_SIZE, buffer_size) != 0) goto fail;
71
72 int attribs[1] = {SCREEN_BLIT_END};
73 if (screen_fill(screen_ctx, buff, attribs) != 0) goto fail;
74
75 int dirty_rects[4] = {0, 0, buffer_size[0], buffer_size[1]};
76 if (screen_post_window(screen_win, buff, 1, (const int*)dirty_rects, 0) != 0) goto fail;
77
78 return EXIT_SUCCESS;
79
80 fail:
81 screen_destroy_window(screen_win);
82 screen_destroy_context(screen_ctx);
83 return EXIT_FAILURE;
84 }
85
86
87 /**
88 * Handle the failure case for either event. Print the error information.
89 */
90 void failureCommon(bps_event_t *event)
91 {
92 if (event == NULL) {
da29162 Fix an issue in which the PaymentServiceSample was exiting upon recep…
Graham Pancio authored
93 fprintf(stderr, "Invalid event.\n");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
94 return;
95 }
96
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
97 unsigned request_id = paymentservice_event_get_request_id(event);
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
98 int error_id = paymentservice_event_get_error_id(event);
99 const char* error_text = paymentservice_event_get_error_text(event);
100
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
101 fprintf(stderr, "Payment System error. Request ID: %d Error ID: %d Text: %s\n",
102 request_id, error_id, error_text ? error_text : "N/A");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
103 }
104
105 /**
106 * Upon successful completion of a purchase, print a string containing
107 * information about the digital good that was purchased.
108 */
109 void onPurchaseSuccess(bps_event_t *event)
110 {
111 if (event == NULL) {
da29162 Fix an issue in which the PaymentServiceSample was exiting upon recep…
Graham Pancio authored
112 fprintf(stderr, "Invalid event.\n");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
113 return;
114 }
115
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
116 unsigned request_id = paymentservice_event_get_request_id(event);
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
117 const char* date = paymentservice_event_get_date(event, 0);
118 const char* digital_good = paymentservice_event_get_digital_good_id(event, 0);
119 const char* digital_sku = paymentservice_event_get_digital_good_sku(event, 0);
120 const char* license_key = paymentservice_event_get_license_key(event, 0);
121 const char* metadata = paymentservice_event_get_metadata(event, 0);
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
122 const char* purchase_id = paymentservice_event_get_purchase_id(event, 0);
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
123
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
124 fprintf(stderr, "Purchase success. Request Id: %d\n Date: %s\n DigitalGoodID: %s\n SKU: %s\n License: %s\n Metadata: %s\n PurchaseId: %s\n\n",
125 request_id,
126 date ? date : "N/A",
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
127 digital_good ? digital_good : "N/A",
128 digital_sku ? digital_sku : "N/A",
129 license_key ? license_key : "N/A",
130 metadata ? metadata : "N/A",
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
131 purchase_id ? purchase_id : "N/A");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
132 }
133
134 /**
135 * On successful completion of a get existing purchases request,
136 * print the existing purchases.
137 */
138 void onGetExistingPurchasesSuccess(bps_event_t *event)
139 {
140 if (event == NULL) {
da29162 Fix an issue in which the PaymentServiceSample was exiting upon recep…
Graham Pancio authored
141 fprintf(stderr, "Invalid event.\n");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
142 return;
143 }
144
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
145 unsigned request_id = paymentservice_event_get_request_id(event);
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
146 int purchases = paymentservice_event_get_number_purchases(event);
147
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
148 fprintf(stderr, "Get existing purchases success. Request ID: %d\n", request_id);
da29162 Fix an issue in which the PaymentServiceSample was exiting upon recep…
Graham Pancio authored
149 fprintf(stderr, "Number of existing purchases: %d\n", purchases);
150 fprintf(stderr, "Existing purchases:\n");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
151
152 int i = 0;
153 for (i = 0; i<purchases; i++) {
154 const char* date = paymentservice_event_get_date(event, i);
155 const char* digital_good = paymentservice_event_get_digital_good_id(event, i);
156 const char* digital_sku = paymentservice_event_get_digital_good_sku(event, i);
157 const char* license_key = paymentservice_event_get_license_key(event, i);
158 const char* metadata = paymentservice_event_get_metadata(event, i);
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
159 const char* purchase_id = paymentservice_event_get_purchase_id(event, i);
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
160
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
161 fprintf(stderr, " Date: %s PurchaseID: %s DigitalGoodID: %s SKU: %s License: %s Metadata: %s\n",
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
162 date ? date : "N/A",
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
163 purchase_id ? purchase_id : "N/A",
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
164 digital_good ? digital_good : "N/A",
165 digital_sku ? digital_sku : "N/A",
166 license_key ? license_key : "N/A",
167 metadata ? metadata : "N/A");
168 }
169 }
170
171 /**
c4dd34f @jhoffmannrim update NDK-Samples for BB 10.0.04
jhoffmannrim authored
172 * A sample application that demonstrates the BlackBerry(R) 10 Native SDK APIs
173 * for making in-app purchases. The sample sets the connection mode to local,
174 * allows the purchase of a digital good by using the swipe down gesture, and
175 * displays any existing purchases.
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
176 */
177 int
178 main(int argc, char *argv[])
179 {
180 int exit_application = 0;
181
182 /*
c4dd34f @jhoffmannrim update NDK-Samples for BB 10.0.04
jhoffmannrim authored
183 * Before we can listen for events from the BlackBerry(R) 10 OS platform
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
184 * services, we need to initialize the BPS infrastructure
185 */
186 bps_initialize();
187
188 /*
189 * Initialize the screen so that the window group Id is properly set, to allow
190 * the Payment Service dialogs to be displayed.
191 */
192 if (setup_screen() != EXIT_SUCCESS) {
da29162 Fix an issue in which the PaymentServiceSample was exiting upon recep…
Graham Pancio authored
193 fprintf(stderr, "Unable to initialize screen.");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
194 exit(0);
195 }
196
197 /*
198 * Once the BPS infrastructure has been initialized we can register for
c4dd34f @jhoffmannrim update NDK-Samples for BB 10.0.04
jhoffmannrim authored
199 * events from the various BlackBerry(R) 10 OS platform services. The
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
200 * Navigator service manages and delivers application life cycle and
201 * visibility events.
202 * For this sample, we request Navigator events so that we can track when
203 * the system is terminating the application (NAVIGATOR_EXIT event), and as a
204 * convenient way to trigger a purchase request (NAVIGATOR_SWIPE_DOWN).
205 * We request PaymentService events so we can be notified when the payment service
206 * responds to our requests/queries.
207 */
208 navigator_request_events(0);
209 paymentservice_request_events(0);
210
211 /*
212 * Set the Payment Service connection mode to local. This allows us to
213 * test the API without the need to contact the AppWorld nor payment servers.
214 */
215 paymentservice_set_connection_mode(true);
216
217 /*
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
218 * Create a set of purchase parameters, which describe the digital good
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
219 * to be purchased and the application the goods are associated with.
220 */
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
221 const char* digital_good_id = "Digital-Good-1-ID";
222 const char* digital_good_name = "Sample Digital Good 1";
223 const char* digital_good_sku = "SAMPLE_DIGITAL_GOOD_SKU_1";
224 const char* metadata = "Sample purchase metadata";
225 const char* purchase_app_icon = "http://www.rim.com/products/appworld_3col.jpg";
226 const char* purchase_app_name = "Payment Service Sample App";
227
228 /*
229 * Define a request ID to hold the returned value from the purchase request.
230 */
231 unsigned request_id = 0;
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
232
233 /*
bca4b2a @jhoffmannrim update NDK-Samples next for NDK 2.0
jhoffmannrim authored
234 * initiate the application with a purchase of the sample digital good.
235 */
236 if (paymentservice_purchase_request(digital_good_id, digital_good_sku, digital_good_name,
237 metadata, purchase_app_name, purchase_app_icon, get_window_group_id(), &request_id) != BPS_SUCCESS) {
238 fprintf(stderr, "Error: purchase request failed.\n");
239 }
240
241 /*
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
242 * Process Payment Service and Navigator events until we receive a NAVIGATOR_EXIT event.
243 */
244 while (!exit_application) {
245 /*
246 * Using a negative timeout (-1) in the call to bps_get_event(...)
247 * ensures that we don't busy wait by blocking until an event is
248 * available.
249 */
250 bps_event_t *event = NULL;
251 bps_get_event(&event, -1);
252
253 if (event) {
254 /*
255 * If it is a Payment Service event, determine the response code
256 * and handle the event accordingly.
257 */
258 if (bps_event_get_domain(event) == paymentservice_get_domain()) {
259 if (SUCCESS_RESPONSE == paymentservice_event_get_response_code(event)) {
260 if (PURCHASE_RESPONSE == bps_event_get_code(event)) {
261 onPurchaseSuccess(event);
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
262 unsigned request_id = 0;
263 if (paymentservice_get_existing_purchases_request(false, get_window_group_id(), &request_id) != BPS_SUCCESS) {
da29162 Fix an issue in which the PaymentServiceSample was exiting upon recep…
Graham Pancio authored
264 fprintf(stderr, "Error: get existing purchases failed.\n");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
265 }
266 } else
267 onGetExistingPurchasesSuccess(event);
268 } else {
269 failureCommon(event);
270 }
271 }
272
273 /*
274 * If it is a NAVIGATOR_EXIT event then set the exit_application
275 * flag so the application will stop processing events, clean up
276 * and exit.
277 *
278 * If it is a NAVIGATOR_SWIPE_DOWN event, initiate the purchase of
279 * the sample digital good.
280 */
281 if (bps_event_get_domain(event) == navigator_get_domain()) {
282 if (NAVIGATOR_EXIT == bps_event_get_code(event)) {
283 exit_application = 1;
284 } else if (NAVIGATOR_SWIPE_DOWN == bps_event_get_code(event)) {
4d4b393 @jhoffmannrim Update all NDK Samples for NDK 2.0 except BelligerentBlocks
jhoffmannrim authored
285 if (paymentservice_purchase_request(digital_good_id, digital_good_sku, digital_good_name,
286 metadata, purchase_app_name, purchase_app_icon, get_window_group_id(), &request_id) != BPS_SUCCESS) {
da29162 Fix an issue in which the PaymentServiceSample was exiting upon recep…
Graham Pancio authored
287 fprintf(stderr, "Error: purchase request failed.\n");
0518ec1 @jhoffmannrim NDK Samples for 1.0
jhoffmannrim authored
288 }
289 }
290 }
291 }
292 }
293
294 /*
295 * Clean up the BPS infrastructure and exit
296 */
297 bps_shutdown();
298 screen_destroy_window(screen_win);
299 screen_destroy_context(screen_ctx);
300 return 0;
301 }
Something went wrong with that request. Please try again.