I am trying to take an existing ColdFusion website that currently uses Authorize to process cards. I would like to switch over the payment gateway and use Square. Does anyone have any sample ColdFusion code on how to use the Square API as a payment gateway?
Here is a gist another developer has used
<cfset IDKey = CreateUUID()>
<cfset request.params.card_nonce = form.nonce>
<cfset request.params.amount_money.amount = 100>
<cfset request.params.amount_money.currency = 'USD'>
<cfset request.params.idempotency_key = IDKey>
<cfset jsonString = serializejson(request.params)>
<cfset requestPath = "https://connect.squareup.com/v2/locations/<replace_locationid>/transactions">
<cfhttp url="#requestPath#" method="post" result="response">
<cfhttpparam type="HEADER" name="Accept" value="application/json">
<cfhttpparam type="HEADER" name="Content-Type" value="application/json">
<cfhttpparam type="HEADER" name="Authorization" value="Bearer <replace_access_token>">
<cfhttpparam type="body" name="params" value="#jsonString#">
</cfhttp>
Related
We have a REST API that returns a stream of content-type application/pdf. I just want to save it to a file on the server.
<cfhttp url="#ApiPath#" method="post" result="res">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cffile action = "write" file = "#FileName#" output = "#res.fileContent#">
I've producing a blank PDF, any ideas? ( ive tried various combinations of cfdocument/cfpdf with no luck)
here's a dump of the REST response:
I think I've got it:
Solution 1:
<cfhttp url="#url#" method="post" result="res">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cfset bytes = res.FileContent.toByteArray()>
<cfscript>
fos = createObject("java", "java.io.FileOutputStream").init("myfile.pdf");
fos.write(bytes);
fos.flush();
fos.close();
</cfscript>
EDIT: Based on SOS's solution this also worked:
Solution 2:
<cfhttp url="#url#" method="post" result="res" getAsBinary="Auto">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#payload#" />
</cfhttp>
<cfset fileName = listlast(res["responseHeader"]["content-disposition"],";=")>
<cffile action="write" file="#path#\#fileName#" output="#res.FileContent#">
I have the following Updated code where I am trying to connect to the website using the username/password and siteid combination, i am passing all values using get method and trying to fetch cookie to authenticate and go ahead but somehow i am again getting redirected to the login screen
My Code
<cfhttp method="get" url="https://stats.ezhostingserver.com/Login.aspx" resolveurl="true" redirect="false">
<cfhttpparam type="URL" name="ctl00$MPH$txtUserName" value="********">
<cfhttpparam type="URL" name="ctl00$MPH$txtPassword" value="********">
<cfhttpparam type="URL" name="ctl00$MPH$txtSiteId" value="*****">
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfset stm_cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfset stm_temp = REReplace(stm_cookies, ";.*", "")>
<cfset stm_cookieName = listfirst(stm_temp,'=')>
<cfset stm_cookievalue = listlast(stm_temp,'=')>
<cfhttp method="get" url="https://stats.ezhostingserver.com/default.aspx" charset="utf-8" result="results" redirect="no">
<cfhttpparam type="cookie" name="#stm_cookieName#" value="#stm_cookievalue#">
</cfhttp>
<cfoutput>#results.filecontent#</cfoutput>
But it is saying me object moved error
I am trying to loop over a cookie coming from cfhttp but it is not displaying correct results.
Below is my code
<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false">
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfloop collection="#cookies#" item="k">
<cfset temp = REReplace(k, ";.*", "")>
<cfset cookieName = listfirst(temp,'=')>
<cfset cookievalue = listlast(temp,'=')>
<cfhttpparam type="cookie" name="#cookieName#" value="#cookievalue#">
</cfloop>
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
Second attempt:
From one call, I am getting cookies and I am putting them in a structure as follows:
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfset cookieStruct=StructNew()>
<cfloop collection="#cookies#" item="key">
<cfset cookieKeyAndValue = REReplace(key, ";.*", "")>
<cfset cookieKey = listfirst(cookieKeyAndValue,'=')>
<cfset cookieValue = listlast(cookieKeyAndValue,'=')>
<cfset StructInsert(cookieStruct,cookieKey,cookieValue)>
</cfloop>
<cfdump var="#cookieStruct#" abort>
<cfhttp url="#addr#" method="get" throwOnError="Yes" resolveurl="false" result="objAddress">
<cfloop collection="#cookieStruct#" item="key">
<cfhttpparam type="cookie" name="#key#" value="#cookieStruct[key]#">
</cfloop>
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
This is giving me an error:
Invalid collection ASPJGASGHSG=KBHFPN; path=/. Must be a valid structure or COM object. Loop error.
CFHTTP GET returns a result into the CFHTTP data structure. Having the loop inside the CFHTTP open/close tag results in trying to loop over something that does not exist yet.
<cfhttp url="#address#" method="get" throwOnError="Yes" resolveurl="false" >
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfset cookies = cfhttp.responseHeader['Set-Cookie'] />
<cfloop collection="#cookies#" item="k">
<cfset temp = REReplace(k, ";.*", "")>
<cfset cookieName = listfirst(temp,'=')>
<cfset cookievalue = listlast(temp,'=')>
</cfloop>
I'm not sure what you are attempting to do here but if there is more than one cookie you're going to need different code in the loop.
I going to implement the DucksBoard API. I made my custom counter on Ducksboard now I want to PUSH value. I read the tutorial I just found this line on DUCKSBOARD to push data
curl -v -u YOUR_API_KEY:unused -d '{"value": 10}' https://push.ducksboard.com/values/235
SO i try something like this in my Coldfusion code
<cfset var1 = '{"value":5}'>
<cfhttp url="https://push.ducksboard.com/values/xxxx" method="post" result="httpResp" timeout="60">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#serializeJSON(var1)#">
</cfhttp>
There is less information in the site of Duckboard.Can someone has implemented this.If yes then kindly tell me how to push value and where to use my API key?
I've not used Duckboard but it looks like what you have is correct. The CFHTTP tag takes 2 more parameters, username and password which you can fill in with your username and your API password like so:
<cfset var1 = '{"value":5}'>
<cfhttp url="https://push.ducksboard.com/values/xxxx" method="post"
result="httpResp" timeout="60" username="myusername" password="mypassword"
>
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#serializeJSON(var1)#">
</cfhttp>
I study the API and found the solution You can use this code and change with yours this will work.And don't use serializeJSON in body tag.
<cfset var1 = '{"value":5}'>
<cfhttp url="https://push.ducksboard.com/v/xxxx" method="post" username="API-key" password="x" result="httpResp" >
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value='#var1#'>
</cfhttp>
I am trying to post using below Code. I expect it to return token but its returning error 405 Method Not Allowed.
<cfhttp method="POST" url="http://accounts.google.com/o/oauth2/token" >
<cfhttpparam type="Formfield" name="code" value="#url.CODE#">
<cfhttpparam type="Formfield" name="client_id" value="458381219741.apps.googleusercontent.com">
<cfhttpparam type="Formfield" name="client_secret" value="XXXXXXX">
<cfhttpparam type="Formfield" name="redirect_uri" value="http://console.mbwebportal.com/oauth2callback">
<cfhttpparam type="Formfield" name="grant_type" value="authorization_code">
</cfhttp>
The above code is on http://console.mbwebportal.com/oauth2callback and it gets the Code in url after user allows access to the application.
Please help!!
I found the answer: key was to use cfhttpparam type 'body'.
As per livedocs "body: specifies the body of the HTTP request. ColdFusion does not automatically set a content-type header or URL encode the body contents. To specify the content-type, use a separate cfhttp tag with type=header. "
Below code is returning me access token now :)
<cfset client_id = "458381219741.apps.googleusercontent.com">
<cfset client_secret = "**********">
<cfset callback = "http://console.mbwebportal.com/oauth2callback">
<cfset postBody = "code=" & UrlEncodedFormat(url.code) & "&">
<cfset postBody = postBody & "client_id=" & UrlEncodedFormat(client_id) & "&">
<cfset postBody = postBody & "client_secret=" & UrlEncodedFormat(client_secret) & "&">
<cfset postBody = postBody & "redirect_uri=" & UrlEncodedFormat(callback) & "&">
<cfset postBody = postBody & "grant_type=authorization_code">
<cfhttp method="post" url="https://accounts.google.com/o/oauth2/token">
<cfhttpparam name="Content-Type" type="header" value="application/x-www-form-urlencoded">
<cfhttpparam type="body" value="#postBody#">
</cfhttp>
Found a similar post here Google OAuth 2 authorization - swapping code for token. The answer for them was to url encode the client secret key and redirect uri. In ColdFusion you can use the URLEncodedFormat() function to do that for you.
<cfhttp method="POST" url="http://accounts.google.com/o/oauth2/token" >
<cfhttpparam type="Formfield" name="code" value="#url.CODE#">
<cfhttpparam type="Formfield" name="client_id" value="458381219741.apps.googleusercontent.com">
<cfhttpparam type="Formfield" name="client_secret" value="#URLEncodedFormat(XXXXXXX)#">
<cfhttpparam type="Formfield" name="redirect_uri" value="#URLEncodedFormat("http://console.mbwebportal.com/oauth2callback")#">
<cfhttpparam type="Formfield" name="grant_type" value="authorization_code">
</cfhttp>
And please validate your url.CODE value before using it as anything can be passed in the URL.