13 Commits
4.0.0 ... 5.1.0

Author SHA1 Message Date
Björn Ritzl
649a8a1ebf Merge pull request #58 from defold/Issue-47-index-out-of-bounds-exception
Added product and purchase null checks
2023-02-07 15:53:39 +01:00
Björn Ritzl
5c09447e37 Merge pull request #59 from defold/Issue-56-app-crash-on-slow-purchase
Check that the purchase listener is not null
2023-02-07 15:51:25 +01:00
Björn Ritzl
ad06de7b9c Check purchase listener 2023-02-06 23:11:24 +01:00
Björn Ritzl
d74c97d5c7 Added null checks 2023-02-06 15:56:03 +01:00
Björn Ritzl
f97a7ee6b6 Merge pull request #55 from defold/fix-json-migration-issue
Push nil as last argument if there is no error
2022-11-21 10:30:12 +01:00
Björn Ritzl
5f3f43fb2e Push nil as last argument if there is no error 2022-11-21 10:21:12 +01:00
Mathias Westerdahl
caff4397d8 Merge pull request #54 from defold/lua-to-json
Updated to use dmScript::LuaToJson()
2022-11-02 15:00:12 +01:00
JCash
501af9c90d Updated to use dmScript::LuaToJson() 2022-11-02 14:36:36 +01:00
Björn Ritzl
961a43f732 Update index.md 2022-09-27 14:46:24 +02:00
Björn Ritzl
47f03108ab Merge branch 'master' of https://github.com/defold/extension-iap 2022-09-23 16:21:36 +02:00
Björn Ritzl
c0e1a9aef1 Fixed YAML format errors 2022-09-23 16:21:27 +02:00
Alexey Gulev
f4294a9eb5 Merge pull request #53 from defold/AGulev-patch-1
Update index.md
2022-09-12 16:26:20 +02:00
Alexey Gulev
8799860198 Update index.md 2022-09-12 16:26:07 +02:00
6 changed files with 71 additions and 102 deletions

View File

@@ -8,7 +8,7 @@ brief: This manual covers how to setup and use Google Play Game Services in Defo
This extension provides a unified, simple to use interface to several different stores for in-app purchase: This extension provides a unified, simple to use interface to several different stores for in-app purchase:
* Apples iOS Appstore - StoreKit * Apples iOS Appstore - StoreKit
* Google Play Billing 3.0 * Google Play Billing 5.0
* Amazon 'in-app billing' 2.0.61 * Amazon 'in-app billing' 2.0.61
* Facebook Canvas 'game payments' * Facebook Canvas 'game payments'
@@ -31,7 +31,7 @@ Detailed documentation from Apple, Google, Amazon and Facebook can be found here
## Installation ## Installation
To use this library in your Defold project, add the following URL to your `game.project` dependencies: To use this library in your Defold project, add the following URL to your `game.project` dependencies:
https://github.com/defold/extension-iap/archive/master.zip [https://github.com/defold/extension-iap/archive/master.zip](https://github.com/defold/extension-iap/archive/master.zip)
We recommend using a link to a zip file of a [specific release](https://github.com/defold/extension-iap/releases). We recommend using a link to a zip file of a [specific release](https://github.com/defold/extension-iap/releases).

View File

@@ -21,11 +21,11 @@
members: members:
- name: request_id - name: request_id
type: string type: string
desc: Facebook only. [icon:facebook] Optional custom unique request id to desc: Facebook only [icon:facebook]. Optional custom unique request id to
set for this transaction. The id becomes attached to the payment within the Graph API. set for this transaction. The id becomes attached to the payment within the Graph API.
- name: token - name: token
type: string type: string
desc: [icon:googleplay] Which subscription offer to use when buying a subscription. The token can be retrieved from desc: Google Play only [icon:googleplay]. Which subscription offer to use when buying a subscription. The token can be retrieved from
the subscriptions table returned when calling iap.list() the subscriptions table returned when calling iap.list()
examples: examples:
@@ -132,22 +132,22 @@
- name: price - name: price
type: number type: number
desc: The price of the product. desc: The price of the product.
[icon:googleplay]: Used only for in-app products For Google Play [icon:googleplay] this is used only for in-app products
- name: price_string - name: price_string
type: string type: string
desc: The price of the product, as a formatted string (amount and currency symbol). desc: The price of the product, as a formatted string (amount and currency symbol).
[icon:googleplay]: Used only for in-app products For Google Play [icon:googleplay] this is used only for in-app products
- name: currency_code - name: currency_code
type: string type: string
desc: [icon:ios] [icon:googleplay] [icon:facebook] The currency code. desc: The currency code.
[icon:googleplay]: The merchant's locale, instead of the user's For Google Play [icon:googleplay] this is the merchant's locale, instead of the user's.
[icon:googleplay]: Used only for in-app products For Google Play [icon:googleplay] this is used only for in-app products
- name: subscriptions - name: subscriptions
type: table type: table
desc: [icon:googleplay] List of subscription offers. desc: Only available for Google Play [icon:googleplay]. List of subscription offers.
Each offer contains a token and a list of price and billing options. Each offer contains a token and a list of price and billing options.
See https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.PricingPhase See https://developer.android.com/reference/com/android/billingclient/api/ProductDetails.PricingPhase
members: members:
@@ -269,36 +269,36 @@
- name: original_trans - name: original_trans
type: string type: string
desc: Apple only[icon:apple]. The original transaction. This field is only set when `state` is `TRANS_STATE_RESTORED`. desc: Apple only [icon:apple]. The original transaction. This field is only set when `state` is `TRANS_STATE_RESTORED`.
- name: original_json - name: original_json
type: string type: string
desc: Android only[icon:android]. The purchase order details in JSON format. desc: Android only [icon:android]. The purchase order details in JSON format.
- name: signature - name: signature
type: string type: string
desc: Google Play only[icon:googleplay]. A string containing the signature of the purchase data that was signed with the private key of the developer. desc: Google Play only [icon:googleplay]. A string containing the signature of the purchase data that was signed with the private key of the developer.
- name: request_id - name: request_id
type: string type: string
desc: Facebook only[icon:facebook]. This field is set to the optional custom unique request id `request_id` if set in the `iap.buy()` call parameters. desc: Facebook only [icon:facebook]. This field is set to the optional custom unique request id `request_id` if set in the `iap.buy()` call parameters.
- name: user_id - name: user_id
type: string type: string
desc: Amazon Pay only[icon:amazon]. The user ID. desc: Amazon Pay only [icon:amazon]. The user ID.
- name: is_sandbox_mode - name: is_sandbox_mode
type: boolean type: boolean
desc: Amazon Pay only[icon:amazon]. If `true`, the SDK is running in Sandbox mode. desc: Amazon Pay only [icon:amazon]. If `true`, the SDK is running in Sandbox mode.
This only allows interactions with the Amazon AppTester. Use this mode only for testing locally. This only allows interactions with the Amazon AppTester. Use this mode only for testing locally.
- name: cancel_date - name: cancel_date
type: string type: string
desc: Amazon Pay only[icon:amazon]. The cancel date for the purchase. This field is only set if the purchase is canceled. desc: Amazon Pay only [icon:amazon]. The cancel date for the purchase. This field is only set if the purchase is canceled.
- name: canceled - name: canceled
type: string type: string
desc: Amazon Pay only[icon:amazon]. Is set to `true` if the receipt was canceled or has expired; otherwise `false`. desc: Amazon Pay only [icon:amazon]. Is set to `true` if the receipt was canceled or has expired; otherwise `false`.
- name: error - name: error
type: table type: table

View File

@@ -306,23 +306,9 @@ static void HandleProductResult(const IAPCommand* cmd)
} }
if (cmd->m_ResponseCode == BILLING_RESPONSE_RESULT_OK) { if (cmd->m_ResponseCode == BILLING_RESPONSE_RESULT_OK) {
dmJson::Document doc; const char* json = (const char*)cmd->m_Data;
dmJson::Result r = dmJson::Parse((const char*) cmd->m_Data, &doc); dmScript::JsonToLua(L, json, strlen(json)); // throws lua error if it fails
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0) { lua_pushnil(L);
char err_str[128];
if (dmScript::JsonToLua(L, &doc, 0, err_str, sizeof(err_str)) < 0) {
dmLogError("Failed converting product result JSON to Lua; %s", err_str);
lua_pushnil(L);
IAP_PushError(L, "failed to convert JSON to Lua for product response", REASON_UNSPECIFIED);
} else {
lua_pushnil(L);
}
} else {
dmLogError("Failed to parse product response (%d)", r);
lua_pushnil(L);
IAP_PushError(L, "failed to parse product response", REASON_UNSPECIFIED);
}
dmJson::Free(&doc);
} else { } else {
dmLogError("IAP error %d", cmd->m_ResponseCode); dmLogError("IAP error %d", cmd->m_ResponseCode);
lua_pushnil(L); lua_pushnil(L);
@@ -356,23 +342,9 @@ static void HandlePurchaseResult(const IAPCommand* cmd)
if (cmd->m_ResponseCode == BILLING_RESPONSE_RESULT_OK) { if (cmd->m_ResponseCode == BILLING_RESPONSE_RESULT_OK) {
if (cmd->m_Data != 0) { if (cmd->m_Data != 0) {
dmJson::Document doc; const char* json = (const char*)cmd->m_Data;
dmJson::Result r = dmJson::Parse((const char*) cmd->m_Data, &doc); dmScript::JsonToLua(L, json, strlen(json)); // throws lua error if it fails
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0) { lua_pushnil(L);
char err_str[128];
if (dmScript::JsonToLua(L, &doc, 0, err_str, sizeof(err_str)) < 0) {
dmLogError("Failed converting purchase JSON result to Lua; %s", err_str);
lua_pushnil(L);
IAP_PushError(L, "failed to convert purchase response JSON to Lua", REASON_UNSPECIFIED);
} else {
lua_pushnil(L);
}
} else {
dmLogError("Failed to parse purchase response (%d)", r);
lua_pushnil(L);
IAP_PushError(L, "failed to parse purchase response", REASON_UNSPECIFIED);
}
dmJson::Free(&doc);
} else { } else {
dmLogError("IAP error, purchase response was null"); dmLogError("IAP error, purchase response was null");
lua_pushnil(L); lua_pushnil(L);

View File

@@ -46,23 +46,8 @@ static void IAPList_Callback(void* luacallback, const char* result_json)
if(result_json != 0) if(result_json != 0)
{ {
dmJson::Document doc; dmScript::JsonToLua(L, result_json, strlen(result_json)); // throws lua error if it fails
dmJson::Result r = dmJson::Parse(result_json, &doc); lua_pushnil(L);
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0) {
char err_str[128];
if (dmScript::JsonToLua(L, &doc, 0, err_str, sizeof(err_str)) < 0) {
dmLogError("Failed converting list result JSON to Lua; %s", err_str);
lua_pushnil(L);
IAP_PushError(L, "Failed converting list result JSON to Lua", REASON_UNSPECIFIED);
} else {
lua_pushnil(L);
}
} else {
dmLogError("Failed to parse list result JSON (%d)", r);
lua_pushnil(L);
IAP_PushError(L, "Failed to parse list result JSON", REASON_UNSPECIFIED);
}
dmJson::Free(&doc);
} }
else else
{ {
@@ -112,23 +97,8 @@ static void IAPListener_Callback(void* luacallback, const char* result_json, int
} }
if (result_json) { if (result_json) {
dmJson::Document doc; dmScript::JsonToLua(L, result_json, strlen(result_json)); // throws lua error if it fails
dmJson::Result r = dmJson::Parse(result_json, &doc); lua_pushnil(L);
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0) {
char err_str[128];
if (dmScript::JsonToLua(L, &doc, 0, err_str, sizeof(err_str)) < 0) {
dmLogError("Failed converting purchase result JSON to Lua; %s", err_str);
lua_pushnil(L);
IAP_PushError(L, "failed converting purchase result JSON to Lua", REASON_UNSPECIFIED);
} else {
lua_pushnil(L);
}
} else {
dmLogError("Failed to parse purchase response (%d)", r);
lua_pushnil(L);
IAP_PushError(L, "failed to parse purchase response", REASON_UNSPECIFIED);
}
dmJson::Free(&doc);
} else { } else {
lua_pushnil(L); lua_pushnil(L);
switch(error_code) switch(error_code)

View File

@@ -243,6 +243,20 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
return billingResponseCodeToDefoldResponse(result.getResponseCode()); return billingResponseCodeToDefoldResponse(result.getResponseCode());
} }
private void invokeOnPurchaseResultListener(IPurchaseListener purchaseListener, int billingResultCode, String purchaseData) {
if (purchaseListener == null) {
Log.w(TAG, "Received billing result but no listener has been set");
return;
}
purchaseListener.onPurchaseResult(billingResultCode, purchaseData);
}
private void invokeOnPurchaseResultListener(IPurchaseListener purchaseListener, BillingResult billingResult, Purchase purchase) {
invokeOnPurchaseResultListener(purchaseListener, billingResultToDefoldResponse(billingResult), convertPurchase(purchase));
}
private void invokeOnPurchaseResultListener(IPurchaseListener purchaseListener, BillingResult billingResult) {
invokeOnPurchaseResultListener(purchaseListener, billingResultToDefoldResponse(billingResult), "");
}
/** /**
* This method is called either explicitly from Lua or from extension code * This method is called either explicitly from Lua or from extension code
* when "set_listener()" is called from Lua. * when "set_listener()" is called from Lua.
@@ -306,7 +320,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
// note: we only call the purchase listener if an error happens // note: we only call the purchase listener if an error happens
if (billingResult.getResponseCode() != BillingResponseCode.OK) { if (billingResult.getResponseCode() != BillingResponseCode.OK) {
Log.e(TAG, "Unable to consume purchase: " + billingResult.getDebugMessage()); Log.e(TAG, "Unable to consume purchase: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
}); });
@@ -329,7 +343,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
// note: we only call the purchase listener if an error happens // note: we only call the purchase listener if an error happens
if (billingResult.getResponseCode() != BillingResponseCode.OK) { if (billingResult.getResponseCode() != BillingResponseCode.OK) {
Log.e(TAG, "Unable to acknowledge purchase: " + billingResult.getDebugMessage()); Log.e(TAG, "Unable to acknowledge purchase: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
}); });
@@ -348,12 +362,12 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
@Override @Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) { public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
Log.d(TAG, "handlePurchase() response code " + billingResult.getResponseCode() + " purchaseToken: " + purchaseToken); Log.d(TAG, "handlePurchase() response code " + billingResult.getResponseCode() + " purchaseToken: " + purchaseToken);
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), convertPurchase(purchase)); invokeOnPurchaseResultListener(purchaseListener, billingResult, purchase);
} }
}); });
} }
else { else {
purchaseListener.onPurchaseResult(billingResponseCodeToDefoldResponse(BillingResponseCode.OK), convertPurchase(purchase)); invokeOnPurchaseResultListener(purchaseListener, billingResponseCodeToDefoldResponse(BillingResponseCode.OK), convertPurchase(purchase));
} }
} }
@@ -362,13 +376,17 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
*/ */
@Override @Override
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) { public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
if (billingResult.getResponseCode() == BillingResponseCode.OK && purchases != null) { if (billingResult.getResponseCode() == BillingResponseCode.OK) {
for (Purchase purchase : purchases) { if (purchases != null && !purchases.isEmpty()) {
handlePurchase(purchase, this.purchaseListener); for (Purchase purchase : purchases) {
if (purchase != null) {
handlePurchase(purchase, this.purchaseListener);
}
}
} }
} }
else { else {
this.purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(this.purchaseListener, billingResult);
} }
} }
@@ -392,7 +410,7 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
BillingResult billingResult = billingClient.launchBillingFlow(this.activity, billingFlowParams); BillingResult billingResult = billingClient.launchBillingFlow(this.activity, billingFlowParams);
if (billingResult.getResponseCode() != BillingResponseCode.OK) { if (billingResult.getResponseCode() != BillingResponseCode.OK) {
Log.e(TAG, "Purchase failed: " + billingResult.getDebugMessage()); Log.e(TAG, "Purchase failed: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
@@ -413,11 +431,16 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
@Override @Override
public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> productDetailsList) { public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> productDetailsList) {
if (billingResult.getResponseCode() == BillingResponseCode.OK && (productDetailsList != null) && !productDetailsList.isEmpty()) { if (billingResult.getResponseCode() == BillingResponseCode.OK && (productDetailsList != null) && !productDetailsList.isEmpty()) {
buyProduct(productDetailsList.get(0), token, purchaseListener); for (ProductDetails productDetails : productDetailsList) {
if (productDetails != null) {
buyProduct(productDetails, token, purchaseListener);
break;
}
}
} }
else { else {
Log.e(TAG, "Unable to get product details before buying: " + billingResult.getDebugMessage()); Log.e(TAG, "Unable to get product details before buying: " + billingResult.getDebugMessage());
purchaseListener.onPurchaseResult(billingResultToDefoldResponse(billingResult), ""); invokeOnPurchaseResultListener(purchaseListener, billingResult);
} }
} }
}); });
@@ -435,10 +458,12 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
@Override @Override
public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> productDetails) { public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> productDetails) {
if (productDetails != null) { if (productDetails != null && !productDetails.isEmpty()) {
// cache products (cache will be used to speed up buying) // cache products (cache will be used to speed up buying)
for (ProductDetails pd : productDetails) { for (ProductDetails pd : productDetails) {
IapGooglePlay.this.products.put(pd.getProductId(), pd); if (pd != null) {
IapGooglePlay.this.products.put(pd.getProductId(), pd);
}
} }
// add to list of all product details // add to list of all product details
allProductDetails.addAll(productDetails); allProductDetails.addAll(productDetails);
@@ -485,9 +510,11 @@ public class IapGooglePlay implements PurchasesUpdatedListener {
@Override @Override
public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> productDetails) { public void onProductDetailsResponse(BillingResult billingResult, List<ProductDetails> productDetails) {
JSONArray a = new JSONArray(); JSONArray a = new JSONArray();
if (billingResult.getResponseCode() == BillingResponseCode.OK) { if ((billingResult.getResponseCode() == BillingResponseCode.OK) && (productDetails != null) && !productDetails.isEmpty()) {
for (ProductDetails pd : productDetails) { for (ProductDetails pd : productDetails) {
a.put(convertProductDetails(pd)); if (pd != null) {
a.put(convertProductDetails(pd));
}
} }
} }
else { else {

View File

@@ -16,7 +16,7 @@ target_sdk_version = 29
[project] [project]
title = extension-iap title = extension-iap
dependencies = https://github.com/andsve/dirtylarry/archive/master.zip dependencies#0 = https://github.com/andsve/dirtylarry/archive/master.zip
[library] [library]
include_dirs = extension-iap include_dirs = extension-iap