Integrate Facebook Credits with AS3-SDK - actionscript-3

I’m trying to integrate Facebook Credits as a paying method using the as3-sdk. I managed to get “earn_credits” and “buy_credits” working. However the third and most important option, “buy_item”, doesn’t show up the pay dialog. Somehow the connection to the callback.php seems the reason for the issue. Note: I have typed in the callback URL in my apps settings, so I didn’t forget that. I use the example php file from the Facebook Developer docs.
This is my as3 code.
public static function buyItem ():void
{
var theAction:String = "buy_item";
var order_info:Object = { "item_id":"1a" };
var jOrder:String = JSON.encode(order_info);
var data:Object = {
action:theAction,
order_info:jOrder,
dev_purchase_params: {"oscif":true}
};
Facebook.ui("pay", data, purchaseCallback);
}
I think the json encoding might be the problem, but i'm not sure.
I use the example php file from the Facebook Developer docs (excerpt):
<?php
$app_secret = '***********************';
// Validate request is from Facebook and parse contents for use.
$request = parse_signed_request($_POST['signed_request'], $app_secret);
// Get request type.
// Two types:
// 1. payments_get_items.
// 2. payments_status_update.
$request_type = $_POST['method'];
// Setup response.
$response = '';
if ($request_type == 'payments_get_items') {
// Get order info from Pay Dialog's order_info.
// Assumes order_info is a JSON encoded string.
$order_info = json_decode($request['credits']['order_info'], true);
// Get item id.
$item_id = $order_info['item_id'];
// Simulutates item lookup based on Pay Dialog's order_info.
if ($item_id == '1a') {
$item = array(
'title' => '100 some game cash',
'description' => 'Spend cash in some game.',
// Price must be denominated in credits.
'price' => 1,
'image_url' => '**********************/banner1.jpg',
);
// Construct response.
$response = array(
'content' => array(
0 => $item,
),
'method' => $request_type,
);
// Response must be JSON encoded.
$response = json_encode($response);
}
Any help, is really appreciated.

Okay so I cannot confirm that this works but according to this forum, it does:
var title:String = "TITLE FOO";
var desc:String = "FOO";
var price:String = "200";
var img_url:String = [some image url];
var product_url:String = [some product url];
// create order info object
var order_info:Object = {
"title":title,
"description":desc,
"price":price,
"image_url":img_url,
"product_url":product_url
};
// calling the API ...
var obj:Object = {
method: 'pay.prompt',
order_info: order_info,
purchase_type: 'item',
credits_purchase: false
};
Facebook.ui('pay', obj, callbackFunction);
I see that this example differs from yours slightly on the AS3 side so hopefully this nfo will help you resolve your problem. I realize that this isn't the best way to go about answering questions but I can see after a couple of days on here, no one has a answered you so I figured anything could help at this point. :)

Thank you #Ascension Systems!
This worked out well and is much better than creating a pop-up via html, and using navigateToURL etc...
One caveat though, which caused your solution not to work for me initially:
If you are relying on the callback.php sample provided by Facebook ( at the end of this page: http://developers.facebook.com/docs/credits/callback/ ), then you need to add this tag to your order_info object:
var item_id:String = "1a";
var order_info:Object = {
"title":title,
"description":desc,
"price":price,
"image_url":img_url,
"product_url":product_url,
"item_id":item_id
};
Without item_id defined, the if statement in Facebook's callback.php ( if ($item_id == '1a') ... ) will fail, and you'll get an unpleasant window: "App Not Responding
The app you are using is not responding. Please try again later."

Related

Getting selected elements properties in forge viewer when more then one model loaded

I have have a viewer app with 8 models loaded
I have a plugin looking for the "AGGREGATE_SELECTION_CHANGED_EVENT" event
this.viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, this.onSelectionBinded);
I need to be able to get access to the selected elements properties
this.viewer.getProperties(_dbId, (result) => { })
but it seams the viewer is only looking at the last loaded model not all of them.
do i have to load/switch to the other models ? and if so how.
The viewer.model is always pointed to the first loaded model with my experience. If you want to access other loaded models, you can obtain them via calling viewer.impl.modelQueue().getModels(). Afterward, call Viewer properties APIs in this way:
var allModels = viewer.impl.modelQueue().getModels();
var model = allModels[1];
model.getProperties( dbId, onSuccessCallback, onErrorCallback );
Besides, you can obtain the model instance in the function argument event of your onSelectionBinded callback. So, your onSelectionBinded can be modified to this based on the above logic:
this.onSelectionBinded = function( event ) {
var selSet = event.selections;
var firstSel = selSet[0];
var model = firstSel.model;
var dbIds = firstSel.dbIdArray;
var firstDbId = dbIds[0];
model.getProperties( firstDbId, onSuccessCallback, onErrorCallback );
}
Hope it helps!
I know this is a little late...
Another way to get properties for multi-model, is to use the aggregated method.
var DBids = viewer.impl.selector.getAggregateSelection();
I have a blog post and sample website that goes through the details:
https://forge.autodesk.com/blog/highlighting-clashes-multi-model

LexResponse output does not understand HTML data

I'm having a problem trying to get my AWS Lambda function to successfully output a series of HTML links when its running a SQL Query.
private string GetEventSearchResults(ILambdaContext context, List<Event> events, string CustomerNumber)
{
var result = string.Empty;
var link = string.Empty;
if (events.Count > 0)
{
result = $"Events for {CustomerNumber}:";
foreach (var evt in events)
{
link = "http://localhost/event/" + $"{evt.ID}";
result += $"<br>Event: {evt.ID} - Status: {evt.Status}";
}
}
else
{
result = "No Data found matching your query";
}
return result;
}
When this method is called by my Lambda function as a LexResponse,
replyMessage = GetEventSearchResults(context, eventList, query.CustomerNumber);
return Close(
sessionAttributes,
"Fulfilled",
new LexResponse.LexMessage
{
ContentType = "PlainText",
Content = replyMessage
}
);
This response is then rendered in my HTML page by a Javascript function. Relevant portion of the Javascript that renders the response:
function showResponse(lexResponse) {
var conversationDiv = document.getElementById('conversation');
var responsePara = document.createElement("P");
responsePara.className = 'lexResponse';
if (lexResponse.message) {
responsePara.appendChild(document.createTextNode(lexResponse.message));
responsePara.appendChild(document.createElement('br'));
}
if (lexResponse.dialogState === 'ReadyForFulfillment') {
responsePara.appendChild(document.createTextNode(
'Ready for fulfillment'));
// TODO: show slot values
}
conversationDiv.appendChild(responsePara);
conversationDiv.scrollTop = conversationDiv.scrollHeight;
}
However, the output shown by the Lex bot is as shown below:
Lex Bot Output
Can anyone please help me understand what exactly is going on? Is the content type of the Lex Response responsible for this? (there's only plaintext and SSML available for Lex Response so I can't change that)
Also, if possible, can anyone please explain how to fix this if at all possible? Thanks!
Your code is correct and output is also correct.
However the console window is not able to render the HTML part of your result.
The client on which you will deploy the chatbot, is responsible for rendering the output. For example, if you respond with a ResponseCard, console or website will not be able to render it correctly but it will be displayed correctly on Facebook and Slack. So, if you integrate your chatbot on some website it will show the links in your output correctly as you desired.
You can try to integrate your chatbot with Slack or Facebook first, to see the rendering of output.
Hope it helps.
After further trial and error, I managed to get a solution that works for me.
function showResponse(lexResponse) {
var conversationDiv = document.getElementById('conversation');
var responsePara = document.createElement("P");
responsePara.className = 'lexResponse';
if (lexResponse.message) {
var message = lexResponse.message.replace(/"/g, '\'');
responsePara.innerHTML = message;
responsePara.appendChild(document.createElement('br'));
}
conversationDiv.appendChild(responsePara);
conversationDiv.scrollTop = conversationDiv.scrollHeight;
}
By making the LexResponse an Inner HTML, it fixed the markup of the text and thus the link can be seen everytime.

users ending up with the same id.... possible race condition?

I have inherited a piece of code from a Laravel 5 codebase that essentially takes an svg symbol, allows you to add effects and text, and saves it as an SVG that gets linked to a user's account. The system works, but every once and a while you will see that 2 customers will end up with the same "newAvatar->id" linked to their account. I have been scratching my brain as to why but can't seem to figure it out.
Would this be caused by some sort of race condition? Do I need to put a semaphore or mutex of some sort around this code?
public function createAvatar()
{
$status = 400;
$_data = array('error' => 'Unknown Error');
if (Input::has('avatar_svg'))
{
$svg = Input::get('avatar_svg');
$newAvatar = Avatar::create([
'svg_content' => $svg,
]);
if ($newAvatar) {
$status = 200;
$_data = array('avatarID' => $newAvatar->id);
}
}
//data is sent back as JSON to be processed by AngularJS
return Response::json([
'data' => $_data,
], $status, array(), JSON_PRETTY_PRINT);
}
Thanks to all for the help, but the answer was that users managed to find a path through the software that was never intended to be taken or possible.
It's amazing the things that people find when code goes live

Accessing DOM object properties from Chrome's content script

I ran into a strange problem with a content script. The content script is defined as "run_at" : "document_end" in the manifest. After a page is loaded the script inserts an object tag into the page (if the tag with predefined id does not exist yet), and sets some properties in it, such as type, width, height, innerHTML, and title. All works fine here.
function checkForObject()
{
var obj = document.getElementById("unique_id");
if(obj == null)
{
var d = document.createElement("object");
d.id = "unique_id";
d.width = "1";
d.height = "1";
d.type = "application/x-y-z";
d.title = "1000";
d.style.position = "absolute";
d.style.left = "0px";
d.style.top = "0px";
d.style.zIndex = "1";
document.getElementsByTagName("body")[0].appendChild(d);
}
}
checkForObject();
I see the new object in the page html-code with proper values in its properties.
Some time later I need to read the title property of the object in the same content script. The code is simple:
function ReadTitle()
{
var obj = document.getElementById("unique_id");
var value = obj.title; // breakpoint
console.log(value);
// TODO: want to use proper title value here
}
The function is called from background.html page:
chrome.tabs.onActivated.addListener(
function(info)
{
chrome.tabs.executeScript(info.tabId, {code: 'setTimeout(ReadTitle, 250);'});
});
Unfortunately, in ReadTitle I'm getting not what I expect. Instead of current value of the title I see the logged value is:
function title() { [native code] }
If I set a breakpoint at the line marked by // breakpoint comment, I see in the watcher that all object properties including the title are correct. Nevertheless, the variable value gets the abovementioned descriptive string.
Apparently, I have missed something simple, but I can't figure it out.
The answer. It was a bug in the npapi plugin, which hosts the object of used type. My apologies for all who have read the question with intention to help.
The NPAPI plugin used in the object erroneously reported title as supported method.

MediaWiki Extension question / suggestion

Complete beginner here. I want to create a new tab on each page that has a custom action. When clicked, it takes you to a new page which has custom HTML on it along with the text or the original article.
So far I could create a new Tab and could give a custom action mycustomaction to it. I am pasting what I did so far here. Please let me know if I am using the correct hooks etc. and what is a better way to achieve this basic functionality.
So far with their docs I have done this:
#Hook for Tab
$wgHooks['SkinTemplateContentActions'][] = 'myTab';
#Callback
function myTab( $content_actions) {
global $wgTitle;
$content_actions['0'] = array(
'text' => 'my custom label',
'href' => $wgTitle->getFullURL( 'action=mycustomaction' ),
);
return true;
}
#new action hook
$wgHooks['UnknownAction'][] = 'mycustomaction';
#callback
function mycustomaction($action, $article) {
echo $action;
return true;
}
This gives me error:
No such action
The action specified by the URL is invalid. You might have mistyped the URL, or followed an incorrect link. This might also indicate a bug in the software used by yourplugin
What I was doing wrong:
$content_actions[‘0’] should simply be $content_actions[] (minor nitpick)
$content_actions is passed-by-reference, it should be function myTab( &$content_actions ) {}
mycustomaction() should do something along the lines of
if ( $action == ‘mycustomaction’ ) {
do stuff; return false;
}
else {
return true;
}
It should use $wgOut->addHTML() instead of echo
Thanks a lot everyone for your help!