Facebook Credits callback in a mobile web application

442 Views Asked by At

I'm trying to create a Facebook Mobile Application using asp.net and MVC3 and integrate Facebook Credits as a payment method. First of all, taking the recent annoucements into consideration, is it now possible to have a mobile web application that accepts Facebook Credits?

If so, I've taken the example provided in the following post

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

And implemented the following Controller action:

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;
}

I've verified that the callback is being requested by facebook, and I've also captured the response being sent back, which appears to contain all of the required information to display the purchase dialog, but I'm still getting the following error message:

API Error Code: 1151 API Error Description: Sorry, but this app may not be eligible to accept Facebook Credits. If this app has accepted credits before, please try again. Error Message: Invalid Application

and when tested from a mobile browser:

Sorry, but we're having trouble processing your payment. You have not been charged for this transaction. Please try again.

I've also noticed that my callback is being requested twice which doesn't seem right either.

Any insight into how to get my integration up and running would be greatly appreciated. My Facebook AppId is 177876855621874

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Update: So I played around with the examples given and reverted back to webforms in order to test the example given at http://www.m-webs.com/blog_facebookcredits.html. In order to get this solution working in an asp.net MVC3 application I had to change the action type to HttpResponse instead of JsonResult which makes sense as the JsonResult leaves elements out that would normally be included in a HttpResponse.

So the Controller Action ended up looking like this:

[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;
}

I hope this helps out anyone doing an MVC3 implementation for Facebook Credits.