// Prompt the user to purchase a virtual item with the Facebook Pay Dialog
    // See: https://developers.facebook.com/docs/payments/reference/paydialog
    public static void BuyCoins(CoinPackage cPackage)
    {
        // Format payment URL
        string paymentURL = string.Format(PaymentObjectURL, PaymentObjects[cPackage]);

        // https://developers.facebook.com/docs/unity/reference/current/FB.Canvas.Pay
        FB.Canvas.Pay(paymentURL,
                      "purchaseitem",
                      1,
                      null, null, null, null, null,
                      (IPayResult result) =>
        {
            Debug.Log("PayCallback");
            if (result.Error != null)
            {
                Debug.LogError(result.Error);
                return;
            }
            Debug.Log(result.RawResult);

            object payIdObj;
            if (result.ResultDictionary.TryGetValue("payment_id", out payIdObj))
            {
                string payID = payIdObj.ToString();
                Debug.Log("Payment complete");
                Debug.Log("Payment id:" + payID);

                // Verify payment before awarding item
                if (VerifyPayment(payID))
                {
                    GameStateManager.CoinBalance += (int)cPackage;
                    GameStateManager.CallUIRedraw();
                    PopupScript.SetPopup("Purchase Complete", 2f);
                }
            }
            else
            {
                Debug.Log("Payment error");
            }
        });
    }
    private void OnBombBuy(BombPackage bPackage)
    {
        int price = BombPackageCost[bPackage];

        if (price <= GameStateManager.CoinBalance)
        {
            // execute transaction
            GameStateManager.CoinBalance -= price;
            GameStateManager.NumBombs    += (int)bPackage;

            // update UI
            GameStateManager.CallUIRedraw();
            PopupScript.SetPopup("Purchase Complete", 1f);

            // log App Event for spending credits
            FBAppEvents.SpentCoins(price, bPackage.ToString());
        }
        else
        {
            PopupScript.SetPopup("Not enough coins", 1f);
        }
    }
Esempio n. 3
0
 // The Graph API for Scores allows you to publish scores from your game to Facebook
 // This allows Friend Smash! to create a friends leaderboard keeping track of the top scores achieved by the player and their friends.
 // For more information on the Scores API see: https://developers.facebook.com/docs/games/scores
 //
 // When publishing a player's scores, these scores will be visible to their friends who also play your game.
 // As a result, the player needs to grant the app an extra permission, publish_actions, in order to publish scores.
 // This means we need to ask for the extra permission, as well as handling the
 // scenario where that permission wasn't previously granted.
 //
 public static void PostScore(int score, Action callback = null)
 {
     // Check for 'publish_actions' as the Scores API requires it for submitting scores
     if (FBLogin.HavePublishActions)
     {
         var query = new Dictionary <string, string>();
         query["score"] = score.ToString();
         FB.API(
             "/me/scores",
             HttpMethod.POST,
             delegate(IGraphResult result)
         {
             Debug.Log("PostScore Result: " + result.RawResult);
             // Fetch fresh scores to update UI
             FBGraph.GetScores();
         },
             query
             );
     }
     else
     {
         // Showing context before prompting for publish actions
         // See Facebook Login Best Practices: https://developers.facebook.com/docs/facebook-login/best-practices
         PopupScript.SetPopup("Prompting for Publish Permissions for Scores API", 4f, delegate
         {
             // Prompt for `publish actions` and if granted, post score
             FBLogin.PromptForPublish(delegate
             {
                 if (FBLogin.HavePublishActions)
                 {
                     PostScore(score);
                 }
             });
         });
     }
 }
Esempio n. 4
0
    // Callback for FB.AppRequest
    private static void AppRequestCallback(IAppRequestResult result)
    {
        // Error checking
        Debug.Log("AppRequestCallback");
        if (result.Error != null)
        {
            Debug.LogError(result.Error);
            return;
        }
        Debug.Log(result.RawResult);

        // Check response for success - show user a success popup if so
        object obj;

        if (result.ResultDictionary.TryGetValue("cancelled", out obj))
        {
            Debug.Log("Request cancelled");
        }
        else if (result.ResultDictionary.TryGetValue("request", out obj))
        {
            PopupScript.SetPopup("Request Sent", 3f);
            Debug.Log("Request sent");
        }
    }