From b61f0018e7bc4bc33f3cd28a8b7dda5f8b8a0686 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 20 May 2015 11:32:00 +0200 Subject: [PATCH] Added activate account API to in app purchase --- .../purchase/InAppPurchaseHelper.java | 43 +++++++++++++++++-- .../purchase/InAppPurchaseListener.java | 8 +++- src/org/linphone/purchase/Purchasable.java | 15 +++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/org/linphone/purchase/InAppPurchaseHelper.java b/src/org/linphone/purchase/InAppPurchaseHelper.java index 70bae9101..44623040e 100644 --- a/src/org/linphone/purchase/InAppPurchaseHelper.java +++ b/src/org/linphone/purchase/InAppPurchaseHelper.java @@ -333,7 +333,7 @@ public class InAppPurchaseHelper { } } - public void recoverAccount(String productId, String sipUsername) { + public void recoverAccount(String sipUsername) { XMLRPCClient client = null; try { client = new XMLRPCClient(new URL(LinphonePreferences.instance().getInAppPurchaseValidatingServerUrl())); @@ -354,7 +354,7 @@ public class InAppPurchaseHelper { @Override public void onResponse(long id, Object result) { - Log.d("[In-app purchase] Server result is " + result); + Log.d("[In-app purchase] recoverAccount server result is " + result); mListener.onRecoverAccountSuccessful(result.equals("OK")); } @@ -368,6 +368,41 @@ public class InAppPurchaseHelper { } } + public void activateAccount(String sipUsername, String purchasedData, String signature) { + XMLRPCClient client = null; + try { + client = new XMLRPCClient(new URL(LinphonePreferences.instance().getInAppPurchaseValidatingServerUrl())); + } catch (MalformedURLException e) { + Log.e(e); + Log.e("[In-app purchase] Can't reach the server !"); + mListener.onError(CLIENT_ERROR_SERVER_NOT_REACHABLE); + } + + if (client != null) { + client.callAsync(new XMLRPCCallback() { + @Override + public void onServerError(long id, XMLRPCServerException error) { + Log.e(error); + Log.e("[In-app purchase] Server can't validate the payload and it's signature !"); + mListener.onError(SERVER_ERROR_SIGNATURE_VERIFICATION_FAILED); + } + + @Override + public void onResponse(long id, Object result) { + Log.d("[In-app purchase] activateAccount server result is " + result); + mListener.onActivateAccountSuccessful(result.equals("OK")); + } + + @Override + public void onError(long id, XMLRPCException error) { + Log.e(error); + Log.e("[In-app purchase] Server can't validate the payload and it's signature !"); + mListener.onError(SERVER_ERROR_SIGNATURE_VERIFICATION_FAILED); + } + }, "activate_account", mGmailAccount, sipUsername, purchasedData, signature, "google"); + } + } + public void destroy() { mContext.unbindService(mServiceConn); } @@ -416,6 +451,7 @@ public class InAppPurchaseHelper { String productId = json.getString(PURCHASE_DETAILS_PRODUCT_ID); Purchasable item = new Purchasable(productId); item.setExpire(longExpire); + item.setPayloadAndSignature(purchasedData, signature); //TODO parse JSON result to get the purchasable in it return item; } catch (XMLRPCException e) { @@ -428,7 +464,7 @@ public class InAppPurchaseHelper { return null; } - private void verifySignatureAndCreateAccountAsync(final VerifiedSignatureListener listener, final String purchasedData, String signature, String username) { + private void verifySignatureAndCreateAccountAsync(final VerifiedSignatureListener listener, final String purchasedData, final String signature, String username) { XMLRPCClient client = null; try { client = new XMLRPCClient(new URL(LinphonePreferences.instance().getInAppPurchaseValidatingServerUrl())); @@ -466,6 +502,7 @@ public class InAppPurchaseHelper { String productId = json.getString(PURCHASE_DETAILS_PRODUCT_ID); Purchasable item = new Purchasable(productId); item.setExpire(longExpire); + item.setPayloadAndSignature(purchasedData, signature); //TODO parse JSON result to get the purchasable in it listener.onParsedAndVerifiedSignatureQueryFinished(item); return; diff --git a/src/org/linphone/purchase/InAppPurchaseListener.java b/src/org/linphone/purchase/InAppPurchaseListener.java index a65bcbb2b..8f44ec751 100644 --- a/src/org/linphone/purchase/InAppPurchaseListener.java +++ b/src/org/linphone/purchase/InAppPurchaseListener.java @@ -49,10 +49,16 @@ public interface InAppPurchaseListener { /** * Callback called when the account has been recovered (or not) - * @param true if the recover has been successful, false otherwise + * @param success true if the recover has been successful, false otherwise */ void onRecoverAccountSuccessful(boolean success); + /** + * Callback called when the account has been activated (or not) + * @param success true if the activation has been successful, false otherwise + */ + void onActivateAccountSuccessful(boolean success); + /** * Callback called when an error occurred. * @param error the error that occurred diff --git a/src/org/linphone/purchase/Purchasable.java b/src/org/linphone/purchase/Purchasable.java index 3228757d0..30db76dae 100644 --- a/src/org/linphone/purchase/Purchasable.java +++ b/src/org/linphone/purchase/Purchasable.java @@ -29,6 +29,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. public class Purchasable { private String id, title, description, price; private long expire; + private String purchasePayload, purchasePayloadSignature; public Purchasable(String id) { this.id = id; @@ -79,4 +80,18 @@ public class Purchasable { this.expire = expire; return this; } + + public Purchasable setPayloadAndSignature(String payload, String signature) { + this.purchasePayload = payload; + this.purchasePayloadSignature = signature; + return this; + } + + public String getPayload() { + return this.purchasePayload; + } + + public String getPayloadSignature() { + return this.purchasePayloadSignature; + } }