PURCHASE
Purchase flow
Purchasing flow is a multistage process, which involves payment collection, purchase validation and consumption.
Starting purchasing flow
Use one of the snippets below to launch a purchasing flow depending on whether you use Unity InApp Purchasing or not:
- C#
- C++
storeClient.PurchaseProduct(productId, (product, error) =>
{
if (error != null)
{
// handle error
return;
}
var receipt = product.ToReceipt();
// handle the purchased product here (e.g. validate or consume)..
});
client->PurchaseProduct(productId,
[](std::shared_ptr<CXsollaStoreClientPurchasedProduct> product, std::shared_ptr<CXsollaStoreClientError> error) {
if (error == nullptr) {
std::cout << "C++ Purchase successful for product: " << product->GetSku() << "\n";
auto receipt = CXsollaStoreClientHelpers::ConvertToReceipt(product)
// handle the purchased product here (e.g. validate or consume)..
}
else {
std::cout << "C++ Purchase failed with error code: " << error->GetMessage() << "\n";
}
}
);
Validating purchases (recommended)
Although validation is NOT a mandatory step, yet it's highly recommended to minimize the chance of fraud and any other malevolent intents.
For instructions on how to validate your purchases, see Purchase Validation guide.
Consuming purchased goods
The very final step in the chain of purchasing flow stages is to award the purchased goods to the user and mark these goods as "awarded". The process also known as purchase consumption.
Use the code below to do just that:
- C#
- C++
storeClient.ConsumeProduct(item.sku, item.quantity, item.transactionId, error => {
if (error != null)
{
return;
}
// handle purchased item
});
client->ConsumeProduct( item->GetSku(), item->GetQuantity(), item->GetTransactionId(),
[](std::shared_ptr<CXsollaStoreClientError> error) {
if (error == nullptr) {
std::cout << "C++ Consume successful.\n";
} else {
std::cout << "C++ Consume failed with error code: " << error->GetMessage() << "\n";
}
}
);
It's highly recommended to perform at least some form of validation before consuming the purchased goods.