I want to process payments through my website and wanted to use sagepay for that purpose. I have gone through the guideline for sagepay go direct integration mentioned here:
http://www.sagepay.com/sites/default/files/pdf/user_guides/sagepaydirectprotocolandintegrationguidelines_0.pdf
Now,
I have created a script made necessary changes needed for the fields to process http request. here is the code:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp';
$txcode = 'prefix_' . time() . rand(0, 9999);
$fields = array(
'VPSProtocol'=>urlencode("2.23"),
'TxType'=>urlencode("PAYMENT"),
'Vendor'=>urlencode("myvendorname"),
'VendorTxCode'=>urlencode($txcode),
'Amount'=>urlencode("2.00"),
'Currency'=>urlencode("GBP"),
'Description'=>urlencode("payment for my site"),
'CardHolder'=>urlencode('DELTA'),
'CardNumber'=>urlencode(4929000000006),
'ExpiryDate'=>urlencode(1213),
'CV2'=>urlencode(123),
'CardType'=>urlencode('VISA'),
'BillingSurname'=>urlencode('surname'),
'BillingFirstnames'=>urlencode('name'),
'BillingAddress1'=>urlencode(' clifton'),
'BillingCity'=>urlencode('Bristol'),
'BillingPostCode'=>urlencode('BS82UE'),
'BillingCountry'=>urlencode('United Kingdom'),
'DeliverySurname'=>urlencode('surname'),
'DeliveryFirstnames'=>urlencode('name'),
'DeliveryAddress1'=>urlencode(' clifton'),
'DeliveryCity'=>urlencode('Bristol'),
'DeliveryPostCode'=>urlencode('bs82ue'),
'DeliveryCountry'=>urlencode('united kingdom'),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
print_r($result);
echo "end";
//close connection
curl_close($ch);
?>
But I am not getting any response back.. what could be the problem? Its in wamp server and non https url.
This code works for me. I added a few CURL functions and removed one and that did the trick.
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp';
$txcode = 'prefix_' . time() . rand(0, 9999);
$fields = array(
'VPSProtocol'=>urlencode("2.23"),
'TxType'=>urlencode("PAYMENT"),
'Vendor'=>urlencode("myvendorname"),
'VendorTxCode'=>urlencode($txcode),
'Amount'=>urlencode("2.00"),
'Currency'=>urlencode("GBP"),
'Description'=>urlencode("payment for my site"),
'CardHolder'=>urlencode('DELTA'),
'CardNumber'=>urlencode(4111111111111111),
'ExpiryDate'=>urlencode(1213),
'CV2'=>urlencode(123),
'CardType'=>urlencode('VISA'),
'BillingSurname'=>urlencode('surname'),
'BillingFirstnames'=>urlencode('name'),
'BillingAddress1'=>urlencode(' clifton'),
'BillingCity'=>urlencode('Bristol'),
'BillingPostCode'=>urlencode('BS82UE'),
'BillingCountry'=>urlencode('United Kingdom'),
'DeliverySurname'=>urlencode('surname'),
'DeliveryFirstnames'=>urlencode('name'),
'DeliveryAddress1'=>urlencode(' clifton'),
'DeliveryCity'=>urlencode('Bristol'),
'DeliveryPostCode'=>urlencode('bs82ue'),
'DeliveryCountry'=>urlencode('united kingdom'),
);
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute post
$result = curl_exec($ch);
print_r($result);
echo "end";
//close connection
curl_close($ch);
?>
FYI, using http_build_query() is great for building query_strings.
$fields_string = http_build_query($fields);
This code will work if you add field
Crypt='Your encryption password in your account setting in sagepay test';
But it returns null string if you use curl to post and it will be redirected to
https://test.sagepay.com/gateway/service/cardselection?vpstxid={BDBF098F-9BB5-3822-8CAE-AC0645A2CC57}
if you use Http POST.
Goodluck
Related
Not going to succeed when trying to send data using post method. Never used it before.
This is code of executing curl:
$url = LICENSE_URL."validate_system_key/validate_key/";
//url-ify the data for the POST
$data['system_key']='A8Z0-X1N7-S1V2-Y1I5';
$data['domain']='http://example.com';
$fields_string = '';
foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
//curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); # Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result=json_decode($result,TRUE);
And this is the Code On Client Url:
public function validate_key(){
$system_key=$_POST['system_key'];
$domain=$_POST['domain'];
$result['error']=3;
echo json_encode($result);
}
Note: When I am not using POST method (sending data through url) evrything is fine. Its all about POST method, not getting the mistake! Any kind of help will be appreciated! Thanks.
Use array instead of String in data passed to string like below:
foreach($data as $key=>$value) {
$data[$key] = $value;
}
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
I hope this will work.
my problem is that i'm working with salesforce in wordpress, i'm not using wordpress-to-lead plugin, I have a form in a template and that form sends data to salesforce via cURL and also is posting data in database cause I have to generate a password and then send it to the user but its not working, is working salesforce but not saving data in the database, here is my code to post data in database and generate the password
$keysString = implode(", ", array_keys($blank_section));
unset($_POST['userId']);
$user_id = mysql_query("SELECT MAX(id) AS id FROM int_form_data");
$user_id = $user_id+11;
$passwordSend = 'INTELIGOS'.rand(10000, 5000000);
$array_user_id = array('user_id' => $user_id, 'password' => $passwordSend);
$posted_data = array_merge($_POST,$array_user_id);
foreach($posted_data as $k=>$v) {
$itfdatainfo[$k] = $v;
}
$itfkeys = array_keys($itfdatainfo);
$itfvalues = array_values($itfdatainfo);
if(isset($_POST['submit'])) {
$sql = "INSERT INTO int_form_data (".implode(',',$itfkeys).") VALUES('".implode("','",$itfvalues)."')";
$result = mysql_query($sql);
}
And here I use cURL to send data to Salesforce:
//Initialize the $query_string variable for later use
$query_string = "";
$kv = array();
//If there are POST variables
if ($_POST) {
//Initialize the $kv array for later use
//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $llav => $value) {
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($llav)."=".stripslashes($value);
}
//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
//The original form action URL from Step 2
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
//Open cURL connection
$ch = curl_init();
//Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
//Set some settings that make it all work
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
//close cURL connection
curl_close($ch);
Anyone knows why is happening that? I have all the code in one template in wordpress
I'm looking at this INSERT INTO int_form_data (".implode(',',$itfkeys).") and thinking if some column name in $itfkeys needs to be enclosed in backticks then it would cause an sql error.
Heres my bit o' php, tryin to figure out why I'm not getting a $result going to the URL it produces will give me a valid JSON result based on a get or post. So I think my problem is how I am using cURL. So I need someone take on this.
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
if(!$result)
{
$error = curl_error($ch); echo $error; return false;
}
//close connection
curl_close($ch);
//return json_decode($result);
echo $result;
EDITED CODE ABOVE From original Post
$error does not report anything. I changed the return json... to echo to see if that was doing anything and it printed out 'Disallowed Key Characters.' on the screen.
EDIT 2
$url = http://domain.com/search
$params = array('q'=>'search,term')
$params is put through a foreach loop which builds the $fields_string
$fields_string = '?';
foreach($params as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = substr($fields_string,0,-1);
fields_string looks like ?ll=37.2790669,-121.874722&range=10 in the end (for what I am doing currently, I have anywhere from 1-12 optional parameters that can be passed which is why I am building it the way I am.
Try adding error checking to make sure curl_exec is executing successfully and to get a meaningful error message back on the server.
something like:
$ch = curl_init();
//Make string url safe with urlencode
curl_setopt($ch,CURLOPT_URL,urlencode($url));
curl_setopt($ch,CURLOPT_POST,count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(!$result) {
$error = curl_error();
//$error now contains the error thrown when curl_exec failed to execute
//echo this to terminal or to an error box in the browser?
}
curl_close($ch);
return json_decode($result);
Post your resulting error here if you still need help.
Also, here are the manual pages for the two functions I took advantage of:
http://www.php.net/manual/en/function.curl-error.php
http://www.php.net/manual/en/function.curl-exec.php
http://php.net/manual/en/function.urlencode.php
-Cheers
//set POST variables
$url = "http://example.com/edsym/registration.php"; // URL to calc.cgi
$fields = array(
'namee'=>urlencode($name),
'city'=>urlencode($city),
'phh'=>urlencode($ph),
'emaill'=>urlencode($email),
'msg1'=>urlencode("Message type")
);
$fields_string=" ";
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I am using curl to post like this and its working
Instead of printing the userinfo, the following code is printing the details of the access token granted by Google. The code is very simple yet I am not able to figure out the exact cause of this
<?php
$url = 'https://accounts.google.com/o/oauth2/token';
$fields = array(
'code'=>urlencode($_GET['code']),
'client_id'=>urlencode('###########.apps.googleusercontent.com'),
'client_secret'=>urlencode('###########'),
'redirect_uri'=>urlencode('http://######odie.co.in/googlogin'),
'grant_type'=>urlencode('authorization_code'),
);
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$data = json_decode($result);
print_r(file_get_contents(' https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$data['access_token']));
?>
You need to add CURLOPT_RETURNTRANSFER, true to your curl_setopt.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
refer this. its very simple.
Simple PHP Library for Google Authentication API
I'm trying to construct a PHP POST request that includes some binary data, following on from a previous StackOverflow question. The data is being submitted to this API call: http://www.cyclestreets.net/api/#addphoto
This is my PHP code:
$file = $_FILES['mediaupload'];
$file_field="#$file[tmp_name]";
$fields = array(
'mediaupload'=>$file_field,
'username'=>urlencode($_POST["username"]),
'password'=>urlencode($_POST["password"]),
'latitude'=>urlencode($_POST["latitude"]),
'longitude'=>urlencode($_POST["longitude"]),
'datetime'=>urlencode($_POST["datetime"]),
'category'=>urlencode($_POST["category"]),
'metacategory'=>urlencode($_POST["metacategory"]),
'caption'=>urlencode($_POST["description"])
);
$fields_string = http_build_query($fields);
echo 'FIELDS STRING: ' . $fields_string;
$url = 'https://www.cyclestreets.net/api/addphoto.json?key=$key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);
This is what my PHP file outputs:
FIELDS STRING: mediaupload=%40%2Fprivate%2Fvar%2Ftmp%2FphpHjfkRP&username=testing&password=testing&latitude=auto&longitude=auto&datetime=auto&category=cycleparking&metacategory=good&caption=
API RESPONSE: {"request":{"datetime":"1309886656"},"error":{"code":"unknown","message":"The photo was received successfully, but an error occurred while processing it."},"result":{}}
I believe this means that everything else about the request is OK, apart from the format of the binary data. Can anyone tell me what I am doing wrong?
CURL can accept a raw array of key=>value pairs for POST fields. There's no need to do all that urlencode() and http_build_query() stuff. Most likely the # in the array is being mangled into %40, so CURL doesn't see it as a file upload attempt.
$fields = array(
'mediaupload'=>$file_field,
'username'=> $_POST["username"),
etc...
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
The http_build_query function generates a URL encoded query string which means that the "#file.ext" is URL encoded in the output as a string and cURL doesn't know that you're trying to upload a file.
My advice would be not to include the file to upload in the http_build_query call and included manually in the CURLOPT_POSTFIELDS.
$file = $_FILES['mediaupload'];
$file_field="#$file[tmp_name]";
$fields = array(
'username'=>urlencode($_POST["username"]),
'password'=>urlencode($_POST["password"]),
'latitude'=>urlencode($_POST["latitude"]),
'longitude'=>urlencode($_POST["longitude"]),
'datetime'=>urlencode($_POST["datetime"]),
'category'=>urlencode($_POST["category"]),
'metacategory'=>urlencode($_POST["metacategory"]),
'caption'=>urlencode($_POST["description"])
);
$fields_string = http_build_query($fields);
echo 'FIELDS STRING: ' . $fields_string;
$url = 'https://www.cyclestreets.net/api/addphoto.json?key=$key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, 'mediaupload=' . $file_field . '&' . $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);