現(xiàn)在越來越多的游戲需要全球化運(yùn)營.其中支付這塊,最重要的就是IOS和Google Play是必須要支持的.
一般要實(shí)現(xiàn)的需求如下:
- 支持Google Play和IOS道具支付
- 根據(jù)本地貨幣轉(zhuǎn)換匯率
- 支持游戲內(nèi)訂閱服務(wù)
- 內(nèi)購重置功能
下面我來用U3D內(nèi)置IAP逐條實(shí)現(xiàn)上述功能.
- 支持Google Play和IOS道具支付
基礎(chǔ)功能
/**
* Description: 根據(jù)ID購買商品
* productID: 商品ID
*/
public void BuyProductID(string productID)
{
if (m_PurchaseInProgress)
{
Debug.Log("Please wait, purchase in progress");
return;
}
if (m_Controller == null)
{
Debug.LogError("Purchasing is not initialized");
return;
}
if (m_Controller.products.WithID(productID) == null)
{
Debug.LogError("No product has id " + productID);
return;
}
// For platforms needing Login, games utilizing a connected backend
// game server may wish to login.
// Standalone games may not need to login.
if (m_IsLoggedIn == false)
{
Debug.LogWarning("Purchase notifications will not be forwarded server-to-server. Login incomplete.");
}
// Don't need to draw our UI whilst a purchase is in progress.
// This is not a requirement for IAP Applications but makes the demo
// scene tidier whilst the fake purchase dialog is showing.
m_PurchaseInProgress = true;
//Sample code how to add accountId in developerPayload to pass it to getBuyIntentExtraParams
//Dictionary<string, string> payload_dictionary = new Dictionary<string, string>();
//payload_dictionary["accountId"] = "Faked account id";
//payload_dictionary["developerPayload"] = "Faked developer payload";
//m_Controller.InitiatePurchase(m_Controller.products.WithID(productID), MiniJson.JsonEncode(payload_dictionary));
m_Controller.InitiatePurchase(m_Controller.products.WithID(productID), "developerPayload");
購買時(shí)直接調(diào)用即可
IAPManager.THIS.BuyProductID("test.pay4");
- 根據(jù)本地貨幣轉(zhuǎn)換匯率
/**
* Description: 獲取商品信息
* key:商品id
*/
public Product GetProduct(string key)
{
if (m_Controller == null)
{
Debug.LogError("Purchasing is not initialized");
return null;
}
if (productDic == null)
{
productDic = new Dictionary<string, Product>();
var products = m_Controller.products.all;
foreach (var product in products)
{
productDic[product.definition.id] = product;
}
}
return productDic[key];
}
其中Product.metadata.localizedPriceString 就是本地貨幣單位.
- 支持游戲內(nèi)訂閱服務(wù)
訂閱商品與一般商品購買上沒有區(qū)別,正常購買就好了咽袜。
注意要設(shè)置好商品類型,開發(fā)者賬號后臺(tái)建立訂閱型商品拒炎。
訂閱型商品分兩種长踊,自動(dòng)續(xù)訂和非自動(dòng)續(xù)訂的,一般都是用自動(dòng)續(xù)訂(You know why!)选侨。
促銷融蹂,如1個(gè)月的訂閱期,可以設(shè)置3天的免費(fèi)試用期七冲,用戶購買后痛倚,免費(fèi)期退訂,是不收費(fèi)的澜躺。但是如果過了試用期蝉稳,或者忘記退訂了,嘿嘿~
image.png
- 內(nèi)購重置功能
/**
* Description: 恢復(fù)購買
*/
public void RestoreButtonClick()
{
if (m_IsGooglePlayStoreSelected)
{
m_GooglePlayStoreExtensions.RestoreTransactions(OnTransactionsRestored);
}
else
{
m_AppleExtensions.RestoreTransactions(OnTransactionsRestored);
}
}
非消耗品的恢復(fù).