Visa Payment Gateway API in ColdFusion - coldfusion

I have a Visa Payment Gateway example in PHP:
$authString = $userId.":".$password;
$authStringBytes = utf8_encode($authString);
$authloginString = base64_encode($authStringBytes);
$authHeader = "Authorization:Basic ".$authloginString;
echo "<strong>URL:</strong><br>".$url. "<br><br>";
$header = (array("Accept: application/json", "Content-Type: application/json", $authHeader));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBodyString);
curl_setopt($ch, CURLOPT_SSLCERT, $certificatePath);
curl_setopt($ch, CURLOPT_SSLKEY, $privateKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
I am trying to convert the PHP code to ColdFusion. How can I pass the CURLOPT_SSLKEY option via cfhttpparam? This is my code so far:
<cfhttp method="get" url="https://sandbox.api.visa.com/vdp/helloworld"
clientcert="C:\ColdFusion10\cfusion\wwwroot\visa\myapp_keyAndCertBundle.p12"
clientcertpassword="theCertPassword"
result="res">
<cfhttpparam type="header" name="Accept" value="application/json">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="Authorization:Basic#authloginString#">
</cfhttp>
cfhttp result output:

Related

libcurl post username and password

I think I have read every libcurl post related question on stackoverflow, yet I still can not seem to get my login code to work. I have copied code from other sample post programs as well as code from https://www.hackthissite.org/articles/read/1078
and it still doesn't work. There are no errors in the code itself, however the final get keeps redirecting to https://www.masteringbiology.com/site/notloggedin.html.
I am trying to login and use the cookies to access a specific page on the site.
I am 100% self taught and would appreciate help.
#include <stdio.h>
#include <iostream>
#include <curl\curl.h>
#include<fstream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
int main(void)
{
CURLcode res;
// string URL= "https://session.masteringbiology.com/myct/assignmentPrintViewassignmentID=6607103";
CURL* curly= curl_easy_init();
curl_global_init(CURL_GLOBAL_ALL);
if(curly){
cout<<"go!!!"<<endl;
curl_easy_setopt(curly, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curly, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curly, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(curly, CURLOPT_AUTOREFERER, 1 );
curl_easy_setopt(curly, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(curly, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curly, CURLOPT_URL,
"https://www.masteringbiology.com/site/login.html");
curl_easy_perform( curly );
// Now, can actually login. First we forge the HTTP referer field
curl_easy_setopt(curly, CURLOPT_REFERER,
"https://www.masteringbiology.com/site/login.html");
// Next we tell LibCurl what HTTP POST data to submit
char *data="nme=xxx&pwd=yyy";
curl_easy_setopt(curly, CURLOPT_POSTFIELDS, data);
curl_easy_perform( curly );
//todoc
curl_easy_setopt(curly, CURLOPT_HTTPGET, 1L);
//curl_easy_setopt(curly, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curly, CURLOPT_URL,
"https://session.masteringbiology.com/myct/mastering#/");
FILE * filename1;
filename1=fopen("filetest.txt","w+");
if(filename1) {
// write the page body to this file handle
curl_easy_setopt(curly, CURLOPT_WRITEDATA, filename1);
// get it!
res=curl_easy_perform(curly);
// close the header file
fclose(filename1);
}
if (res==CURLE_OK){
cout<<"Yeah!!!";
}
else{cout<<"oh no!!! "<<res;}
}
else{
fprintf(stderr, "curl initialization failure");
return 0;
}
curl_easy_cleanup(curly);
return 0;
}
Here is the html for the login form
<!-- Mastering hidden form -->
<form name="hiddenForm" id="hiddenForm" class="hidden"
method="POST" action="https://session.masteringbiology.com/login"
target="_top">
<input type="hidden" id="authProvider" name="authProvider"
value="SMS" />
<input type="hidden" id="username" name="username" value=""
/>
<input type="hidden" id="password" name="password" value=""
/>
<input type="hidden" id="passwordEncType" name="passwordEncType" value="" />
</form>
<!-- end Mastering hidden form -->
<form class="form-stacked has-validation" name="loginForm"
id="loginForm" method="post" action="#" target="_top" autocomplete="off"
data-errorMsg="Please supply the information for all required fields marked
below.">
<label for="nme">Username</label>
<div class="group nowrap">
<input type="text" name="nme" id="nme" class="medium-
width required-field">
</div>
<label for="pwd">Password</label>
<div class="group nowrap">
<input type="password" name="pwd" id="pwd"
class="medium-width password required-field">
</div>
<button type="submit" class="button button-big-icon bg-
color-match uppercase mar-top-x1Half">
<span aria-hidden="true" data-icon="" class="left">
</span>Sign In</button>

CFHTTP & CloudFlare API: DELETE purge_everything not working

Has anyone else been able to DELETE purge_everything with CFHTTP?
I can't seem to get ColdFusion CFHTTP to successfully purge a CloudFlare zone's cache. But I am able to do other things like list zones, etc. So I know I can successfully CFHTTP to CloudFlare's API.
This is the curl command, which works:
curl -svX DELETE -H 'X-Auth-Email: a#b.c' -H 'X-Auth-Key: XYZ' https://api.cloudflare.com/client/v4/zones/xxxxxxx/purge_cache -H 'Content-Type: application/json' --data '{"purge_everything":true}'
The error returned is:
{"success":false,"errors":[{"code":1012,"message":"Request must
contain one of \"purge_everything\" or \"files\", or
\"tags"}],"messages":[],"result":null}
I've tried so many combinations of code... these are the different variables I've tried:
<cfset stFields = '{"purge_everything":true}'>
<cfset stFieldsJson = {"purge_everything":true}>
<cfset stFieldsJson2 = {
"fields" : {
"purge_everything" : true
}
}>
<cfset stFieldsJson3 = {
"purge_everything" : true,
"fields" : {
"purge_everything" : true
}
}>
<cfset tmp = {} />
<cfset tmp['purge_everything'] = true />
... and here are some different combinations of calls I've made...
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare" charset="utf-8">
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value="#serializeJson(stFieldsJson)#" encoded="false">
</cfhttp>
<cfdump var="#cFlare#"><Cfflush>
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare" charset="utf-8">
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value="#serializeJson(stFieldsJson2)#" encoded="false">
</cfhttp>
<cfdump var="#cFlare#"><Cfflush>
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare">
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value="#serializeJson(stFieldsJson3)#" encoded="false">
</cfhttp>
<cfdump var="#cFlare#"><Cfflush>
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare">
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value="#serializeJson(tmp)#" encoded="false">
</cfhttp>
<cfdump var="#cFlare#"><Cfflush>
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare" charset="utf-8">
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value='{"purge_everything":true}' encoded="false">
</cfhttp>
<cfdump var="#cFlare#"><Cfflush>
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare" charset="utf-8">
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value='"purge_everything":true' encoded="false">
</cfhttp>
<cfdump var="#cFlare#"><Cfflush>
<cfhttp url="https://api.cloudflare.com/client/v4/zones/4da78b2707f9753eb79a93d505b4d0d3/purge_cache" method="DELETE" result="cFlare" charset="utf-8">
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value='purge_everything' encoded="false">
</cfhttp>
<cfdump var="#cFlare#"><Cfflush>
I've also tried with and without the 'Encoded' Body attribute, with and without the 'Charset' attribute in all places.
Any help is appreciated.
Not sure which version of CF you are running. However, I suspect you are not doing anything wrong, but that <cfhttp> simply is not sending a body when the method="DELETE", which would make sense given the error message.
A simple way to test it is point your <cfhttp> call to a test page on your local CF server. On the test page dump GetHttpRequestData() so you can view the actual headers and content submitted. (Another option is to use the built in TCPMonitor on an open port, which provides more detail about both request and response. However, for this scenario, the first method is simplest.)
Test Page
<!--- echo request headers and content --->
<cfdump var="#getHTTPRequestData()#">
Request
<!--- simulate request --->
<cfset requestBody["purge_everything"] = true>
<cfhttp url="http://localhost/testPage.cfm" method="DELETE" result="cFlare" charset="utf-8" >
<cfhttpparam type="header" name="X-Auth-Email" value="a#b.c">
<cfhttpparam type="header" name="X-Auth-Key" value="XYZ">
<cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8">
<cfhttpparam type="header" name="accept" value="*/*">
<cfhttpparam type="body" value="#serializeJson(requestBody)#" encoded="false">
</cfhttp>
<!--- display request headers and content --->
<cfoutput>#cFlare.fileContent#</cfoutput>
Notice the content, or body, is empty when method="DELETE"? However, change it to method="POST" and the content magically appears.
Sending a body with a DELETE request should be valid, so it sounds like a bug. If so, you will need to find a different tool to make the http request, such as invoking curl.exe from cfexecute, or using a custom tag like cfx_http5, or use java classes like as URLConnection or Apache's HTTPClient.
After reading up on some docs, it appears that the CloudFlare API is mixing methods in a way that CFHTTP will not.
CloudFlare's API states to use the DELETE method and content type header of "application/json". Their exact example is:
$ curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
However when one uses --data, curl will POST with content-type application/x-www-form-urlencoded. https://curl.haxx.se/docs/manpage.html#-d
CFHTTP however is following specs.
POST is used to send data. DELETE is for deleting the URI.
POST: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5
DELETE: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7
When I CFHTTP with POST I get an error that only DELETE is accepted for the URI. When I CFHTTP with DELETE, no content is being POSTed.
My workaround, without going out of standard CFML, was to use their v1 API. It's working like a champ. https://www.cloudflare.com/docs/client-api.html

Convert php function curl_init () to coldfusion

I need to convert this code PHP to coldfusion:
There hum parameter xml What is Necessary Spending As the parameter is
<?php
$_string = '<SPCA-XML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.scpc.inf.br/spcn/spcaxml.xsd">
<VERSAO>20131120</VERSAO>
<SOLICITACAO>
<S-CODIGO>2122</S-CODIGO>
<S-SENHA>5454</S-SENHA>
<S-CONSULTA>4545</S-CONSULTA>
<S-SOLICITANTE>XXXXXX</S-SOLICITANTE>
<S-CPF>2222222</S-CPF>
<S-TIPO-CREDITO>CD</S-TIPO-CREDITO>
</SOLICITACAO>
</SPCA-XML>';
$_post = curl_init();
curl_setopt($_post, CURLOPT_URL, "https://www.scpc.inf.br/cgi-bin/spcaxml");
curl_setopt($_post, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($_post, CURLOPT_POSTFIELDS, $_string);
curl_setopt($_post, CURLOPT_RETURNTRANSFER, 1);
$_result = curl_exec($_post);
curl_close($_post);
header("Content-type: text/xml");
echo $_result;
?>
You can do it like this:
<cfsavecontent variable="xmlString">
<SPCA-XML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.scpc.inf.br/spcn/spcaxml.xsd">
<VERSAO>20131120</VERSAO>
<SOLICITACAO>
<S-CODIGO>2122</S-CODIGO>
<S-SENHA>5454</S-SENHA>
<S-CONSULTA>4545</S-CONSULTA>
<S-SOLICITANTE>XXXXXX</S-SOLICITANTE>
<S-CPF>2222222</S-CPF>
<S-TIPO-CREDITO>CD</S-TIPO-CREDITO>
</SOLICITACAO>
</SPCA-XML>'
</cfsavecontent>
<cfhttp url="https://www.scpc.inf.br/cgi-bin/spcaxml" method="post" result="result">
<cfhttpparam type="header" name="Content-Type" value="text/xml" />
<cfhttpparam type="xml" value="#xmlString#" />
</cfhttp>
<cfdump var="#result#">
Below is the cfdump of response:
For more info on the code please go through the link, #James A Mohler suggested.

Google Drive API with ColdFusion

I started to work on Google Drive API with ColdFusion and I am stuck to upload the file using ColdFusion. I have done with the registration of new project, getting client and client secret and I am successfully able to get the accessToken but somehow I am not able to upload the file on the google drive.
Here is the code to get the code and accesstoken
<cfoutput>
<cfset request.oauthSettings = {
scope = "https://www.googleapis.com/auth/drive", client_id = "clientid",
client_secret = "clientsecret",
redirect_uri = "link"}
/>
<!--- create login url --->
<cfset loginURL = "https://accounts.google.com/o/oauth2/auth?scope="
& request.oauthSettings["scope"]
& "&redirect_uri=" & request.oauthSettings["redirect_uri"]
& "&response_type=code&client_id=" & request.oauthSettings["client_id"]
& "&access_type=offline"
/>
Login with Google account that has access to analytics
<cfif isDefined("URL.code") AND URL.code NEQ "access_denied">
<cfhttp url="#arguments.gaOauthUrl#" method="post">
<cfhttpparam name="code" type="formField" value="#arguments.code#">
<cfhttpparam name="client_id" type="formField" value="clientid">
<cfhttpparam name="client_secret" type="formField" value="clientsecret">
<cfhttpparam name="redirect_uri" type="formField" value="link">
<cfhttpparam name="grant_type" type="formField" value="authorization_code">
</cfhttp>
</cfif>
</cfoutput>
I am using the following code to upload the file, I know I have to pass some more parameters to make it correct but I don't know what are that parameters.
<cfhttp url="https://www.googleapis.com/upload/drive/v2/files?uploadType=media" method="post">
<cfhttpparam name="Content-Type" type="formField" value="text/plain">
<cfhttpparam name="Authorization" type="formField" value="#session.ga_accessToken#">
</cfhttp>
I am trying to find out in the google docs but no luck; there is no documentation for ColdFusion. Please let me know the other parameters if someone has some clue about this area.
You aren't setting the Authorization header correctly. It should be
Authorization: Bearer ya29.AHES6ZRosLBEnyGGH9EysIrAB7Z

Google oauth token giving 405 error

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.