Making Validation Requests
When installing CME CloudPurchase, you will find the file 'Assets/CME/Sample/CloudPurchaseSample.cs' in your Unity project containing examples of how to use 'CloudPurchaseClient'.
The payment verification process with CME CloudPurchase is pretty straightforward. Basically it is all about creating an instance CloudPurchaseClient
and using its Validate
method . See the examples of usage below.
method . See the examples of usage below.
Using CloudPurchaseClient
Example of using 'CloudPurchaseClient' to validate payments:
public async Task Validate()
{
using (var cloudPurchaseClient = new CloudPurchaseClient()) // (1)
{
var request = new ValidationRequest // (2)
{
Store = Store.GooglePlay,
PackageId = "com.my.package.name",
ProductId = "my.product1",
ReceiptData = "Receipt data from store",
UserId = "Optional User ID in game", // (3)
Price = 9.99,
Currency = "USD",
PriceUsdCents = 999 // (4)
};
var response = await cloudPurchaseClient.Validate(request);
if (response.Valid) // (5)
{
if (response.PurchaseType != PurchaseType.Paid)
{
// This is not real pay, Tester etc
}
}
else
{
// See details in response.ErrorCode and response.ErrorMessage
}
}
}
- Create 'CloudPurchaseClient' instance for validation.
- Create validation request directly.
- Set 'UserId' to study the logs and solve potential user problems. Warning: This is personal data, for more information see here.
- Set
PriceUsdCents
for analytics and revenue diagrams in the dashboard. - It means that the purchase has been completed successfully.
Unity IAP IStoreListener
If you want to use 'CloudPurchaseClient' along with Unity IAP, first find the file 'Assets/CME/Sample/Extensions.cs' in your project and uncomment it. It is required to use the appropriate extension methods.
For more details on working with Unity IAP, see the official userguide.
Here is an example of using 'CloudPurchaseClient' with Unity IAP to validate payments:
public class PurchaseProcessor : MonoBehaviour, IStoreListener
{
private IStoreController? _storeController;
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
_storeController = controller;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
// Process error
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
{
ValidatePurchase(purchaseEvent.purchasedProduct); // (6)
return PurchaseProcessingResult.Pending; // (1)
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
// Process error
}
private async void ValidatePurchase(Product purchasedProduct)
{
try
{
using (var iapValidator = new CloudPurchaseClient())
{
var request = CreateValidationRequest(purchasedProduct);
var response = await iapValidator.Validate(request);
if (response.Valid) // (2)
{
if (response.PurchaseType != PurchaseType.Paid)
{
// This is not real pay, Tester etc
}
}
else
{
// See details in response.ErrorCode and response.ErrorMessage
}
}
}
finally
{
_storeController?.ConfirmPendingPurchase(purchasedProduct); // (3)
}
}
private ValidationRequest CreateValidationRequest(Product purchasedProduct)
{
#if UNITY_ANDROID
var validator = new CrossPlatformValidator(
GooglePlayTangle.Data(),
AppleTangle.Data(),
UnityEngine.Application.identifier);
var result = validator.Validate(purchasedProduct.receipt);
foreach (var purchaseReceipt in result)
{
if (purchaseReceipt is GooglePlayReceipt googlePlayReceipt)
{
return googlePlayReceipt.AsValidationRequest(); // (4)
}
}
throw new Exception("Can't create validation request!");
#elif UNITY_IOS
var validator = new CrossPlatformValidator(
GooglePlayTangle.Data(),
AppleTangle.Data(),
UnityEngine.Application.identifier);
var result = validator.Validate(purchasedProduct.receipt);
return purchasedProduct.AsAppleValidationRequest(); // (5)
#endif
}
}
- Still waiting for the purchase validation. Confirmation will come after validation.
- It means that the purchase has been completed successfully.
- Finally confirm the purchase.
- Use extension method to create ValidationRequest from GooglePlayReceipt.
- Use extension method to create ValidationRequest from Product.
- No need to wait for this request to be completed because we bring back the 'Pending' status of the purchase below.