Facebook Credits在移动Web应用程序中回调

时间:2011-10-17 02:37:20

标签: asp.net asp.net-mvc-3 facebook-credits

我正在尝试使用asp.net和MVC3创建Facebook移动应用程序,并整合Facebook Credits作为付款方式。首先,考虑到最近的公告,现在是否可以拥有一个接受Facebook Credits的移动网络应用程序?

如果是的话,我采取了以下帖子中提供的例子

http://www.m-webs.com/blog_facebookcredits.html

并实施了以下Controller操作:

public JsonResult CallBack()
{
    string fborder_info = Request.Form["order_info"];
    string fborder_id = Request.Form["order_id"];
    string fbmethod = Request.Form["method"];



    if (fbmethod == "payments_get_items")
    {

        fborder_info = fborder_info.Substring(1, (fborder_info.Length - 2)); // remove the quotes 

        ulong credscost = 2; // Price of purchase in facebook credits 

        var theItem = new FacebookBuyItem()
        {
            item_id = 123456789,
            description = "Own yours today!",
            price = credscost,
            title = "Digital Unicorn",
            product_url = "http://www.facebook.com/images/gifts/21.png",
            image_url = "http://www.facebook.com/images/gifts/21.png"
        };

        var res = new Dictionary<string, object>();
        res["method"] = fbmethod;
        res["order_id"] = fborder_id;
        res["content"] = new object[] { theItem };
        var jss = new JavaScriptSerializer();
        var ob = jss.Serialize(res);
        ob = ob.Replace("#$", @"\/".Replace("//", @"\/"));

        return Json(ob, JsonRequestBehavior.AllowGet);
    }

    return null;
}

我已经验证了Facebook正在请求回调,并且我还捕获了被发回的响应,这似乎包含显示购买对话框所需的所有信息,但我仍然得到了以下错误消息:

API错误代码:1151 API错误说明:抱歉,此应用可能无法接受Facebook Credits。如果此应用之前已接受赠送,请再试一次。 错误消息:无效的应用程序

并且从移动浏览器进行测试时:

抱歉,我们无法处理您的付款。您未收到此笔交易的费用。请再试一次。

我也注意到我的回调被要求两次,这似乎也不对。

非常感谢任何有关如何使我的集成​​运行的见解。我的Facebook AppId是177876855621874

感谢。

1 个答案:

答案 0 :(得分:0)

更新:所以我玩了一些给出的例子,然后又回到了webforms,以测试http://www.m-webs.com/blog_facebookcredits.html给出的例子。为了让这个解决方案在asp.net MVC3应用程序中运行,我不得不将操作类型更改为 HttpResponse 而不是 JsonResult ,因为JsonResult会将元素排除在外通常会包含在HttpResponse中。

所以Controller Action最终看起来像这样:

[HttpPost]
public HttpResponse CallBack()
{
    if (Request.Form["signed_request"] != null)
    {
        var decodeFbSignedRequest = FacebookSignedRequest.Parse(FacebookApplication.Current.AppSecret,
                                                            Request.Form["signed_request"]);

        LogHelper.MicroLogMsg("SIGNED REQUEST DECODE:: " + decodeFbSignedRequest.Data);
    }

    string fborder_id = Request.Form["order_id"];
    string fbmethod = Request.Form["method"];
    string fborder_info = Request.Form["order_info"];  // Use this to look up a product on the database..

    if (fbmethod == "payments_get_items")
    {
        int credscost = 2; // Price of purchase in facebook credits 

        var theItem = new FacebookBuyItem()
        {
            item_id = "123456AA",
            description = "[Test Mode] Own yours today!",
            price = credscost,
            title = "[Test Mode] Digital Unicorn",
            product_url = @"http:\/\/www.facebook.com\/images\/gifts\/21.png",
            image_url = @"http:\/\/www.facebook.com\/images\/gifts\/21.png"
        };

        // Return the initial response to FB 
        //------------------------------------------ 
        var res = new Dictionary<string, object>();
        res["method"] = fbmethod;
        res["content"] = new object[] { theItem };

        var jss = new JavaScriptSerializer();
        string ob = jss.Serialize(res);

        LogHelper.MicroLogMsg(ob);

        Response.ContentType = "application/json";
        Response.Write(ob);
        Response.End();
    }

    return null;
}

我希望这有助于任何为Facebook Credits进行MVC3实施的人。