This is a simple PHP to validate Receipt for Apple and Google.
Import Prime31 plugins StoreKit for Apple Store and In-App-Billing for Google Play into Unity.
There are two steps for this.
- Setup Item`s Product Id
- Setup Payment Success Event to Get Receipt
Open a demo scene in folder
/Plugins/Prime31/Storekit/demo/StoreKitTestScene.
Edit StoreKitGUIManager.cs and modify Product Id.
/Plugins/Prime31/Storekit/demo/ StoreKitGUIManager.cs
// XXXX is the product Id, which must match what you have in iTunes.
var productIdentifiers = new string[] { "XXXX" };
StoreKitBinding.requestProductData( productIdentifiers );
The following function aims to capture the receipt from Apple when the payment becomes successful,
and send the receipt to PHP to validate the receipt`s correctness.
Edit /Plugins/Prime31/Storekit/demo/ StoreKitEventListener.cs
void purchaseSuccessfulEvent( StoreKitTransaction transaction )
{
Debug.Log( "purchaseSuccessfulEvent: " + transaction );
// Get iOS receipt
string receipt = transaction.base64EncodedTransactionReceipt;
// Build POST form
WWWForm form = new WWWForm ();
form.AddField ("key", "1234");
form.AddField ("receipt", receipt);
form.AddField ("en", "prod") // dev, prod
form.AddField ("os", "ios") // ios, android
// Server URL
string url = "http://your server IP/verifyPayment.php";
// Process respond
StartCoroutine(this.DoWWW(new WWW(url, form), (www) => {
Debug.Log("-------- Callback Success: " + www.text);
}));
}
private delegate void WWWCallback (WWW www);
private IEnumerator DoWWW (WWW www, WWWCallback callback) {
yield return www;
bool error = false;
if (!string.IsNullOrEmpty (www.error) || string.IsNullOrEmpty (www.text)) {
error = true;
}
if (!error && callback != null)
callback (www);
}
There are two steps for this.
- Setup App Public Key
- Setup Item`s Product Id
- Setup Payment Success Event to Get Receipt
Open a demo scene in folder
/Plugins/ InAppBillingAndroid /demo/IABTestScene.unity
Setup Public key
Edit /Plugins/ InAppBillingAndroid /demo/ IABUIManager.cs
// Setup Public key
var key = "Your Public Key on Google Play";
GoogleIAB.init( key );
Edit /Plugins/ InAppBillingAndroid /demo/ IABUIManager.cs
// Setup Product ID
private string[] skus = new string[]
{
"XXXXXX" // your Product Id here
};
GoogleIAB.queryInventory( skus );
Edit /Plugins/ InAppBillingAndroid /demo/ GoogleIABEventListener.cs
void purchaseCompleteAwaitingVerificationEvent( string purchaseData, string signature )
{
Debug.Log( "purchaseCompleteAwaitingVerificationEvent. purchaseData: " + purchaseData + ", signature: " + signature );
Prime31.Utils.logObject (purchaseData);
Prime31.Utils.logObject (signature);
// Goole receipt
string receipt = purchaseData;
// Initil POST form
WWWForm form = new WWWForm ();
form.AddField ("key", "1234");
form.AddField ("os", "android");
form.AddField ("en", "prod");
form.AddField ("receipt", receipt);
form.AddField ( "sing", signature);
// Server URL
string url = "http://your server ip/veryPayment.php";
// Process respond
StartCoroutine (this.DoWWW (new WWW (url, form), (www) =>
{
Debug.Log("-------- Callback Success: " + www.text);
}));
}
private delegate void WWWCallback (WWW www);
private IEnumerator DoWWW (WWW www, WWWCallback callback) {
yield return www;
bool error = false;
if (!string.IsNullOrEmpty (www.error) || string.IsNullOrEmpty (www.text)) {
error = true;
}
if (!error && callback != null)
callback (www);
}