Paypal PDT - SSL connect error - paypal

I use this code to process Paypal payment data transfer (PDT):
$url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$tx_token = $_GET['tx'];
$auth_token = "my_token";
$fields = array(
'cmd' => '_notify-synch',
'tx' => $tx_token,
'at' => $auth_token,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$res = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo 'Curl error: ' . curl_error($ch);
echo "status: " . $status;
curl_close($ch);
And I'm getting:
Curl error: SSL connect error
status: 0
I tried to make the API in Postman with the same URL: www.sandbox.paypal.com with the same parameters (cmd, at, tx) and I got success response. What I did wrong?

PayPal has upgraded to TLS 1.2 for its sandbox API endpoints. You can find out more about the upgrade and when it will affect production at the TLS 1.2 and HTTP/1.1 Upgrade Microsite.
You will need OpenSSL to be 1.0.1c or higher and a recent version of libcurl.
As far as your specific code, you probably want to do the following:
curl_setopt($ch, CURLOPT_SSLVERSION, 6); // CURL_SSLLVERSION_TLSv1_2 = 6
Also, you probably should leave the CURLOPT_SSL_VERIFYHOST to be 2 to verify the SSL certificate for the endpoint.
You can find out more at the PayPal TLS update repository for specific language environment requirements.

Related

Could not connect to digital ocean ip from json rpc

I am using this curl function to connect to my digitalocean ip from my website, but am getting
"Error:Failed to connect to myip port 8332: Connection refused"
This is the call function am using
<?php
function bitcoin($method="getblockchaininfo"){
$ch = curl_init();
$rpcuser='rpcusername';
$rpcpassword='rpcpassword';
$host='nodeIP'; //IP address of your node or localhost, if running locally
$port='port';
curl_setopt($ch, CURLOPT_URL, "http://$rpcuser:$rpcpassword#$host:$port/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"jsonrpc\":\"1.0\",\"id\":\"curltext\",\"method\":\"$method\",\"params\":[]}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
return($result);
}
?>
But if i visit my digital ocean ip directly and call a specific bitcoind jsonrpc method, everything works fine.

Magento 2 rest api Consumer is not authorized to access %resources<

I want to get the products from magento 2 that is in my localhost. but when i put /rest/V1/products? it gives me
<response>
<message>Consumer is not authorized to access %resources</message>
<parameters>
<resources>Magento_Catalog::products</resources>
</parameters>
</response>
i have made roles and also integration. i don't know how to access these resources. i have to get these products in my ionic app
That error is fairly self explanatory - you haven't authenticated.
Here's an example of how to authenticate with an admin user and fetch products using curl;
// Fetch the user auth token
$userData = array("username" => "USER", "password" => "PASS");
$ch = curl_init("http://DOMAINNAME.com/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
// Now use the token to get all products
$ch = curl_init("http://DOMAINNAME.com/index.php/rest/default/V1/products?searchCriteria");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
// Here are the results
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';
print_r($result);
echo '</pre>';

SetExpressCheckout is returning an empty array

Working in the Sandbox and I've been adjusting an existing application to handle multiple checkout items but now SetExpressCheckout is returning an empty array but I don't see anything wrong with my value: &Amt=60&PAYMENTACTION=Sale&RETURNURL=http://system/hg/shirts/PaypalInvoice.php&CANCELURL=http://system/hg/shirts/confirm.php&CURRENCYCODE=USD&SOLUTIONTYPE=Sole&LANDINGPAGE=Login&DESC=HG+Shirt+Order&L_NAME0=MenSmall&L_NUMBER0=001&L_DESC0=Mens+T-Shirt+Small&L_AMT0=20&L_QTY0=3&ITEMAMT=60&TAXAMNT=0
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//turning off the server and peer verification(TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
//if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
//Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
if($USE_PROXY)
curl_setopt ($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
//NVPRequest for submitting to server
$nvpreq="METHOD=" . urlencode($methodName) . "&VERSION=" . urlencode($version) . "&PWD=" . urlencode($API_Password) . "&USER=" . urlencode($API_UserName) . "&SIGNATURE=" . urlencode($API_Signature) . $nvpStr . "&BUTTONSOURCE=" . urlencode($sBNCode);
//setting the nvpreq as POST FIELD to curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
// these are supposed to fix Fatal
// Error curl_exec for SetExpressCheckout error:"error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure" - Code: 35
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'SSLv3');
//getting response from server
$response = curl_exec($ch);
if(!$response)
{
die('Fatal Error curl_exec for ' . $methodName . ' error:"' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
Well I found an answer. Not sure why I need to do this but it fixed the problem.
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);

Paypal PDT error

I use this code to process Paypal payment data transfer (PDT):
$pp_hostname = "www.sandbox.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "my_token";
$req .= "&tx=$tx_token&at=$auth_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
//if your server does not bundled with default verisign certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
$res = curl_exec($ch);
curl_close($ch);
if(!$res){
echo "HTTP ERROR";
}else{ ...
But it does not work, my code tells me that the request fails, and it outputs HTTP ERROR because $res is empty.
Why?

POST data to AtTask's API?

I'm using AtTask's API with PHP and cURL.
Is there a way to POST data instead of appending it to the end of the URL with a question mark?
I know that I can change the request name itself like CURLOPT_CUSTOMREQUEST => 'POST' and I tried adding CURLOPT_POST => true
However, the CURLOPT_POSTFIELDS => array('name' => 'Untitled Project') is still ignored.
Did anyone work with this?
Pretty sure you need to wrap your postfields array with http_build_query(). Here is an example for logging in that works for me (when I put in my username and password):
$URL = 'https://hub.attask.com/attask/api/login';
$username = 'admin';
$password = 'user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'username'=>$username,
'password'=>$password
)));
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
Hope that helps.
I just ran into the same problem you did. It's been a few months since you asked the question, but if you're still running into it (or for anyone else hitting this wall), the issue is that you need to set the proper CURL options for POST/GET.
For a GET you'll need to set CURLOPT_HTTPGET to "true". It just makes sure the headers are in the correct order for the AtTask API server.
I've just created a github repo for the changes I've made to their sample StreamClient class.
https://github.com/angrychimp/php-attask
Feel free to use that or just lift the code from it. For your login example, GreenJimmy's example is 100% accurate. For your search question, you'll need to do something like the following.
$URL = 'https://hub.attask.com/attask/api/v4.0/';
$username = 'admin';
$password = 'pass';
$URL .= "task/search/?sessionID=$sessionID&ID=$taskID";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
The StreamClient in my github repo allows for arrays to be used for search params and response fields. It does make things a lot easier. For example.
require_once('StreamClient.php');
$client = new StreamClient('https://hub.attask.com', '4.0');
$client->login('admin', 'pass');
$records = $client->search('task', array('ID' => $taskID), array('assignedToID:emailAddr','actualWork'));
var_dump($records);