1 /*************************************************************************/
2 /*  HandlePurchaseTask.java                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 package org.godotengine.godot.payments;
31 
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 
35 import org.godotengine.godot.GodotLib;
36 import org.godotengine.godot.utils.Crypt;
37 import com.android.vending.billing.IInAppBillingService;
38 
39 import android.app.Activity;
40 import android.app.PendingIntent;
41 import android.app.ProgressDialog;
42 import android.content.Context;
43 import android.content.Intent;
44 import android.content.IntentSender.SendIntentException;
45 import android.os.AsyncTask;
46 import android.os.Bundle;
47 import android.os.RemoteException;
48 import android.util.Log;
49 
50 abstract public class HandlePurchaseTask {
51 
52 	private Activity context;
53 
HandlePurchaseTask(Activity context)54 	public HandlePurchaseTask(Activity context) {
55 		this.context = context;
56 	}
57 
handlePurchaseRequest(int resultCode, Intent data)58 	public void handlePurchaseRequest(int resultCode, Intent data) {
59 		if (resultCode == Activity.RESULT_OK) {
60 			PaymentsCache pc = new PaymentsCache(context);
61 
62 			String purchaseData = data.getStringExtra(PaymentsManager.RESPONSE_INAPP_PURCHASE_DATA);
63 			String dataSignature = data.getStringExtra(PaymentsManager.RESPONSE_INAPP_SIGNATURE);
64 
65 			try {
66 				JSONObject jo = new JSONObject(purchaseData);
67 				String productId = jo.getString("productId");
68 				String developerPayload = jo.getString("developerPayload");
69 				String purchaseToken = jo.getString("purchaseToken");
70 
71 				if (!pc.getConsumableValue("validation_hash", productId).equals(developerPayload)) {
72 					error("Untrusted callback");
73 					return;
74 				}
75 
76 				pc.setConsumableValue("ticket_signature", productId, dataSignature);
77 				pc.setConsumableValue("ticket", productId, purchaseData);
78 				pc.setConsumableFlag("block", productId, true);
79 				pc.setConsumableValue("token", productId, purchaseToken);
80 
81 				success(productId, dataSignature, purchaseData);
82 				return;
83 			} catch (JSONException e) {
84 				error(e.getMessage());
85 			}
86 		} else if (resultCode == Activity.RESULT_CANCELED) {
87 			canceled();
88 		}
89 	}
90 
success(String sku, String signature, String ticket)91 	abstract protected void success(String sku, String signature, String ticket);
error(String message)92 	abstract protected void error(String message);
canceled()93 	abstract protected void canceled();
94 }
95