Complete the implementation of the GodotPayment plugin.

Move the remaining plugin components within the plugin source code.

(cherry picked from commit 99173c5fc4)
This commit is contained in:
fhuya 2020-03-17 10:16:49 -07:00 committed by Rémi Verschelde
parent cd2b2bf8bf
commit 9a035efe62
11 changed files with 39 additions and 175 deletions

View File

@ -98,7 +98,6 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL10;
import org.godotengine.godot.input.GodotEditText; import org.godotengine.godot.input.GodotEditText;
import org.godotengine.godot.payments.PaymentsManager;
import org.godotengine.godot.plugin.GodotPlugin; import org.godotengine.godot.plugin.GodotPlugin;
import org.godotengine.godot.plugin.GodotPluginRegistry; import org.godotengine.godot.plugin.GodotPluginRegistry;
import org.godotengine.godot.utils.GodotNetUtils; import org.godotengine.godot.utils.GodotNetUtils;
@ -261,16 +260,12 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
} }
public ResultCallback result_callback; public ResultCallback result_callback;
private PaymentsManager mPaymentsManager = null;
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PaymentsManager.REQUEST_CODE_FOR_PURCHASE) { if (result_callback != null) {
mPaymentsManager.processPurchaseResponse(resultCode, data);
} else if (result_callback != null) {
result_callback.callback(requestCode, resultCode, data); result_callback.callback(requestCode, resultCode, data);
result_callback = null; result_callback = null;
}; }
for (int i = 0; i < singleton_count; i++) { for (int i = 0; i < singleton_count; i++) {
@ -279,7 +274,7 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
for (GodotPlugin plugin : pluginRegistry.getAllPlugins()) { for (GodotPlugin plugin : pluginRegistry.getAllPlugins()) {
plugin.onMainActivityResult(requestCode, resultCode, data); plugin.onMainActivityResult(requestCode, resultCode, data);
} }
}; }
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
@ -555,8 +550,6 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
result_callback = null; result_callback = null;
mPaymentsManager = PaymentsManager.createManager(this).initService();
godot_initialized = true; godot_initialized = true;
} }
@ -713,7 +706,6 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
@Override @Override
protected void onDestroy() { protected void onDestroy() {
if (mPaymentsManager != null) mPaymentsManager.destroy();
for (int i = 0; i < singleton_count; i++) { for (int i = 0; i < singleton_count; i++) {
singletons[i].onMainDestroy(); singletons[i].onMainDestroy();
} }
@ -1063,10 +1055,6 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
return true; return true;
} }
public PaymentsManager getPaymentsManager() {
return mPaymentsManager;
}
public boolean requestPermission(String p_name) { public boolean requestPermission(String p_name) {
return PermissionsUtil.requestPermission(p_name, this); return PermissionsUtil.requestPermission(p_name, this);
} }

View File

@ -1,97 +0,0 @@
/*************************************************************************/
/* GodotPaymentInterface.java */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
package org.godotengine.godot.payments;
public interface GodotPaymentInterface {
void purchase(String sku, String transactionId);
void consumeUnconsumedPurchases();
String getSignature();
void callbackSuccess(String ticket, String signature, String sku);
void callbackSuccessProductMassConsumed(String ticket, String signature, String sku);
void callbackSuccessNoUnconsumedPurchases();
void callbackFailConsume(String message);
void callbackFail(String message);
void callbackCancel();
void callbackAlreadyOwned(String sku);
int getPurchaseCallbackId();
void setPurchaseCallbackId(int purchaseCallbackId);
String getPurchaseValidationUrlPrefix();
void setPurchaseValidationUrlPrefix(String url);
String getAccessToken();
void setAccessToken(String accessToken);
void setTransactionId(String transactionId);
String getTransactionId();
// request purchased items are not consumed
void requestPurchased();
// callback for requestPurchased()
void callbackPurchased(String receipt, String signature, String sku);
void callbackDisconnected();
void callbackConnected();
// true if connected, false otherwise
boolean isConnected();
// consume item automatically after purchase. default is true.
void setAutoConsume(boolean autoConsume);
// consume a specific item
void consume(String sku);
// query in app item detail info
void querySkuDetails(String[] list);
void addSkuDetail(String itemJson);
void completeSkuDetail();
void errorSkuDetail(String errorMessage);
}

View File

@ -28,7 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package org.godotengine.godot.payments; package org.godotengine.godot.plugin.payment;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask; import android.os.AsyncTask;

View File

@ -30,6 +30,7 @@
package org.godotengine.godot.plugin.payment; package org.godotengine.godot.plugin.payment;
import android.content.Intent;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
@ -38,28 +39,40 @@ import java.util.List;
import org.godotengine.godot.Dictionary; import org.godotengine.godot.Dictionary;
import org.godotengine.godot.Godot; import org.godotengine.godot.Godot;
import org.godotengine.godot.GodotLib; import org.godotengine.godot.GodotLib;
import org.godotengine.godot.payments.GodotPaymentInterface;
import org.godotengine.godot.payments.PaymentsManager;
import org.godotengine.godot.plugin.GodotPlugin; import org.godotengine.godot.plugin.GodotPlugin;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
public class GodotPayment extends GodotPlugin implements GodotPaymentInterface { public class GodotPayment extends GodotPlugin {
private Integer purchaseCallbackId = 0; private Integer purchaseCallbackId = 0;
private String accessToken; private String accessToken;
private String purchaseValidationUrlPrefix; private String purchaseValidationUrlPrefix;
private String transactionId; private String transactionId;
private PaymentsManager mPaymentManager; private final PaymentsManager mPaymentManager;
private Dictionary mSkuDetails = new Dictionary(); private final Dictionary mSkuDetails = new Dictionary();
public GodotPayment(Godot godot) { public GodotPayment(Godot godot) {
super(godot); super(godot);
mPaymentManager = godot.getPaymentsManager(); mPaymentManager = new PaymentsManager(godot, this);
mPaymentManager.setBaseSingleton(this); mPaymentManager.initService();
} }
@Override @Override
public void onMainActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PaymentsManager.REQUEST_CODE_FOR_PURCHASE) {
mPaymentManager.processPurchaseResponse(resultCode, data);
}
}
@Override
public void onMainDestroy() {
super.onMainDestroy();
if (mPaymentManager != null) {
mPaymentManager.destroy();
}
}
public void purchase(final String sku, final String transactionId) { public void purchase(final String sku, final String transactionId) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@ -69,7 +82,6 @@ public class GodotPayment extends GodotPlugin implements GodotPaymentInterface {
}); });
} }
@Override
public void consumeUnconsumedPurchases() { public void consumeUnconsumedPurchases() {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@ -81,89 +93,72 @@ public class GodotPayment extends GodotPlugin implements GodotPaymentInterface {
private String signature; private String signature;
@Override
public String getSignature() { public String getSignature() {
return this.signature; return this.signature;
} }
@Override
public void callbackSuccess(String ticket, String signature, String sku) { public void callbackSuccess(String ticket, String signature, String sku) {
GodotLib.calldeferred(purchaseCallbackId, "purchase_success", new Object[] { ticket, signature, sku }); GodotLib.calldeferred(purchaseCallbackId, "purchase_success", new Object[] { ticket, signature, sku });
} }
@Override
public void callbackSuccessProductMassConsumed(String ticket, String signature, String sku) { public void callbackSuccessProductMassConsumed(String ticket, String signature, String sku) {
Log.d(this.getClass().getName(), "callbackSuccessProductMassConsumed > " + ticket + "," + signature + "," + sku); Log.d(this.getClass().getName(), "callbackSuccessProductMassConsumed > " + ticket + "," + signature + "," + sku);
GodotLib.calldeferred(purchaseCallbackId, "consume_success", new Object[] { ticket, signature, sku }); GodotLib.calldeferred(purchaseCallbackId, "consume_success", new Object[] { ticket, signature, sku });
} }
@Override
public void callbackSuccessNoUnconsumedPurchases() { public void callbackSuccessNoUnconsumedPurchases() {
GodotLib.calldeferred(purchaseCallbackId, "consume_not_required", new Object[] {}); GodotLib.calldeferred(purchaseCallbackId, "consume_not_required", new Object[] {});
} }
@Override
public void callbackFailConsume(String message) { public void callbackFailConsume(String message) {
GodotLib.calldeferred(purchaseCallbackId, "consume_fail", new Object[] { message }); GodotLib.calldeferred(purchaseCallbackId, "consume_fail", new Object[] { message });
} }
@Override
public void callbackFail(String message) { public void callbackFail(String message) {
GodotLib.calldeferred(purchaseCallbackId, "purchase_fail", new Object[] { message }); GodotLib.calldeferred(purchaseCallbackId, "purchase_fail", new Object[] { message });
} }
@Override
public void callbackCancel() { public void callbackCancel() {
GodotLib.calldeferred(purchaseCallbackId, "purchase_cancel", new Object[] {}); GodotLib.calldeferred(purchaseCallbackId, "purchase_cancel", new Object[] {});
} }
@Override
public void callbackAlreadyOwned(String sku) { public void callbackAlreadyOwned(String sku) {
GodotLib.calldeferred(purchaseCallbackId, "purchase_owned", new Object[] { sku }); GodotLib.calldeferred(purchaseCallbackId, "purchase_owned", new Object[] { sku });
} }
@Override
public int getPurchaseCallbackId() { public int getPurchaseCallbackId() {
return purchaseCallbackId; return purchaseCallbackId;
} }
@Override
public void setPurchaseCallbackId(int purchaseCallbackId) { public void setPurchaseCallbackId(int purchaseCallbackId) {
this.purchaseCallbackId = purchaseCallbackId; this.purchaseCallbackId = purchaseCallbackId;
} }
@Override
public String getPurchaseValidationUrlPrefix() { public String getPurchaseValidationUrlPrefix() {
return this.purchaseValidationUrlPrefix; return this.purchaseValidationUrlPrefix;
} }
@Override
public void setPurchaseValidationUrlPrefix(String url) { public void setPurchaseValidationUrlPrefix(String url) {
this.purchaseValidationUrlPrefix = url; this.purchaseValidationUrlPrefix = url;
} }
@Override
public String getAccessToken() { public String getAccessToken() {
return accessToken; return accessToken;
} }
@Override
public void setAccessToken(String accessToken) { public void setAccessToken(String accessToken) {
this.accessToken = accessToken; this.accessToken = accessToken;
} }
@Override
public void setTransactionId(String transactionId) { public void setTransactionId(String transactionId) {
this.transactionId = transactionId; this.transactionId = transactionId;
} }
@Override
public String getTransactionId() { public String getTransactionId() {
return this.transactionId; return this.transactionId;
} }
// request purchased items are not consumed // request purchased items are not consumed
@Override
public void requestPurchased() { public void requestPurchased() {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
@ -174,41 +169,34 @@ public class GodotPayment extends GodotPlugin implements GodotPaymentInterface {
} }
// callback for requestPurchased() // callback for requestPurchased()
@Override
public void callbackPurchased(String receipt, String signature, String sku) { public void callbackPurchased(String receipt, String signature, String sku) {
GodotLib.calldeferred(purchaseCallbackId, "has_purchased", new Object[] { receipt, signature, sku }); GodotLib.calldeferred(purchaseCallbackId, "has_purchased", new Object[] { receipt, signature, sku });
} }
@Override
public void callbackDisconnected() { public void callbackDisconnected() {
GodotLib.calldeferred(purchaseCallbackId, "iap_disconnected", new Object[] {}); GodotLib.calldeferred(purchaseCallbackId, "iap_disconnected", new Object[] {});
} }
@Override
public void callbackConnected() { public void callbackConnected() {
GodotLib.calldeferred(purchaseCallbackId, "iap_connected", new Object[] {}); GodotLib.calldeferred(purchaseCallbackId, "iap_connected", new Object[] {});
} }
// true if connected, false otherwise // true if connected, false otherwise
@Override
public boolean isConnected() { public boolean isConnected() {
return mPaymentManager.isConnected(); return mPaymentManager.isConnected();
} }
// consume item automatically after purchase. default is true. // consume item automatically after purchase. default is true.
@Override
public void setAutoConsume(boolean autoConsume) { public void setAutoConsume(boolean autoConsume) {
mPaymentManager.setAutoConsume(autoConsume); mPaymentManager.setAutoConsume(autoConsume);
} }
// consume a specific item // consume a specific item
@Override
public void consume(String sku) { public void consume(String sku) {
mPaymentManager.consume(sku); mPaymentManager.consume(sku);
} }
// query in app item detail info // query in app item detail info
@Override
public void querySkuDetails(String[] list) { public void querySkuDetails(String[] list) {
List<String> nKeys = Arrays.asList(list); List<String> nKeys = Arrays.asList(list);
List<String> cKeys = Arrays.asList(mSkuDetails.get_keys()); List<String> cKeys = Arrays.asList(mSkuDetails.get_keys());
@ -225,7 +213,6 @@ public class GodotPayment extends GodotPlugin implements GodotPaymentInterface {
} }
} }
@Override
public void addSkuDetail(String itemJson) { public void addSkuDetail(String itemJson) {
JSONObject o = null; JSONObject o = null;
try { try {
@ -244,12 +231,10 @@ public class GodotPayment extends GodotPlugin implements GodotPaymentInterface {
} }
} }
@Override
public void completeSkuDetail() { public void completeSkuDetail() {
GodotLib.calldeferred(purchaseCallbackId, "sku_details_complete", new Object[] { mSkuDetails }); GodotLib.calldeferred(purchaseCallbackId, "sku_details_complete", new Object[] { mSkuDetails });
} }
@Override
public void errorSkuDetail(String errorMessage) { public void errorSkuDetail(String errorMessage) {
GodotLib.calldeferred(purchaseCallbackId, "sku_details_error", new Object[] { errorMessage }); GodotLib.calldeferred(purchaseCallbackId, "sku_details_error", new Object[] { errorMessage });
} }
@ -263,6 +248,8 @@ public class GodotPayment extends GodotPlugin implements GodotPaymentInterface {
@NonNull @NonNull
@Override @Override
public List<String> getPluginMethods() { public List<String> getPluginMethods() {
return Arrays.asList("purchase", "setPurchaseCallbackId", "setPurchaseValidationUrlPrefix", "setTransactionId", "getSignature", "consumeUnconsumedPurchases", "requestPurchased", "setAutoConsume", "consume", "querySkuDetails", "isConnected"); return Arrays.asList("purchase", "setPurchaseCallbackId", "setPurchaseValidationUrlPrefix",
"setTransactionId", "getSignature", "consumeUnconsumedPurchases", "requestPurchased",
"setAutoConsume", "consume", "querySkuDetails", "isConnected");
} }
} }

View File

@ -28,7 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package org.godotengine.godot.payments; package org.godotengine.godot.plugin.payment;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;

View File

@ -28,11 +28,10 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package org.godotengine.godot.payments; package org.godotengine.godot.plugin.payment;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Log;
public class PaymentsCache { public class PaymentsCache {

View File

@ -28,7 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package org.godotengine.godot.payments; package org.godotengine.godot.plugin.payment;
import android.app.Activity; import android.app.Activity;
import android.content.ComponentName; import android.content.ComponentName;
@ -52,20 +52,13 @@ public class PaymentsManager {
public static final int REQUEST_CODE_FOR_PURCHASE = 0x1001; public static final int REQUEST_CODE_FOR_PURCHASE = 0x1001;
private static boolean auto_consume = true; private static boolean auto_consume = true;
private Activity activity; private final Activity activity;
private final GodotPayment godotPayment;
IInAppBillingService mService; IInAppBillingService mService;
public void setActivity(Activity activity) { PaymentsManager(Activity activity, GodotPayment godotPayment) {
this.activity = activity;
}
public static PaymentsManager createManager(Activity activity) {
PaymentsManager manager = new PaymentsManager(activity);
return manager;
}
private PaymentsManager(Activity activity) {
this.activity = activity; this.activity = activity;
this.godotPayment = godotPayment;
} }
public PaymentsManager initService() { public PaymentsManager initService() {
@ -409,10 +402,4 @@ public class PaymentsManager {
})) }))
.start(); .start();
} }
private GodotPaymentInterface godotPayment;
public void setBaseSingleton(GodotPaymentInterface godotPaymentInterface) {
this.godotPayment = godotPaymentInterface;
}
} }

View File

@ -28,7 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package org.godotengine.godot.payments; package org.godotengine.godot.plugin.payment;
import android.app.Activity; import android.app.Activity;
import android.app.PendingIntent; import android.app.PendingIntent;

View File

@ -28,7 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package org.godotengine.godot.payments; package org.godotengine.godot.plugin.payment;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask; import android.os.AsyncTask;

View File

@ -28,7 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package org.godotengine.godot.payments; package org.godotengine.godot.plugin.payment;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
@ -42,7 +42,7 @@ import org.json.JSONObject;
abstract public class ValidateTask { abstract public class ValidateTask {
private Activity context; private Activity context;
private GodotPaymentInterface godotPayments; private GodotPayment godotPayments;
private ProgressDialog dialog; private ProgressDialog dialog;
private String mSku; private String mSku;
@ -79,7 +79,7 @@ abstract public class ValidateTask {
} }
} }
public ValidateTask(Activity context, GodotPaymentInterface godotPayments) { public ValidateTask(Activity context, GodotPayment godotPayments) {
this.context = context; this.context = context;
this.godotPayments = godotPayments; this.godotPayments = godotPayments;
} }