Paypal Classic API, I get errors with authentication - paypal

This is my code, the credentials are correct, why do I get Authentication Failed? It is probably the headers but I can't figure out which.
<?php
$ch = curl_init();
$headers = array("X-PAYPAL-SECURITY-USERID" => "--",
"X-PAYPAL-SECURITY-PASSWORD" => "--,
"X-PAYPAL-SECURITY-SIGNATURE" => "--",
"X-PAYPAL-APPLICATION-ID" => "APP-80W284485P519543T",
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NVP",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "JSON",
"X-PAYPAL-SANDBOX-EMAIL-ADDRESS" => "--",
"Accept" => "application/json",
'Content-Type: application/x-www-form-urlencoded');
curl_setopt($ch, CURLOPT_URL, "https://svcs.sandbox.paypal.com/AdaptivePayments/ConvertCurrency");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "requestEnvelope.errorLanguage=en_US&
baseAmountList.currency(0).code=USD&
baseAmountList.currency(0).amount=1&
baseAmountList.currency(1).code=EUR&
baseAmountList.currency(1).amount=1&
baseAmountList.currency(2).code=SEK&
baseAmountList.currency(2).amount=1&
convertToCurrencyList.currencyCode=USD");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
echo $result;
}
curl_close($ch);
?>
In return I get this:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-EBAY-SOA-REQUEST-ID: 144ee8af-72d0-a486-d657-e4f2fffe5f49!AdaptivePayments!10.72.109.101![]
X-PAYPAL-SERVICE-VERSION: 1.0.0
X-PAYPAL-SERVICE-NAME: {http://svcs.paypal.com/types/ap}AdaptivePayments
X-EBAY-SOA-RESPONSE-DATA-FORMAT: XML
X-PAYPAL-OPERATION-NAME: ConvertCurrency
Cache-Control: no-cache
X-PAYPAL-ERROR-RESPONSE: TRUE
X-EBAY-SOA-MESSAGE-PROTOCOL: NONE
Content-Type: text/xml;charset=UTF-8
DC: origin1-svcs.sandbox.paypal.com
Date: Sun, 23 Mar 2014 10:44:48 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Connection: Transfer-Encoding
Set-Cookie: Apache=10.72.109.11.1395571488549095; path=/; expires=Tue, 15-Mar-44 10:44:48 GMT
Set-Cookie: DC=origin1-svcs.sandbox.paypal.com; secure
<?xml version='1.0' encoding='UTF-8'?><ns3:FaultMessage xmlns:ns3="http://svcs.paypal.com/types/common" xmlns:ns2="http://svcs.paypal.com/types/ap"><responseEnvelope><timestamp>2014-03-23T03:44:48.571-07:00</timestamp><ack>Failure</ack><correlationId>95445f07b579b</correlationId><build>10175386</build></responseEnvelope><error><errorId>520003</errorId><domain>PLATFORM</domain><subdomain>Application</subdomain><severity>Error</severity><category>Application</category><message>Authentication failed. API credentials are incorrect.</message></error></ns3:FaultMessage>

You're using the sandbox shared X-PAYPAL-APPLICATION-ID which is correct for sandbox, but will not work in live. There you'd need your own application's ID.
My example also includes the header "X-PAYPAL-SERVICE-VERSION": "1.0.0".

Related

GSO shipping service API(SOAP) says “looks like we got no XML document” but i am getting valid xml

I'm creating SOAP based api,in which i'm getting same error each time.
here is passed xml and also array of data
I have attached wsdl file path also here:
What is the Problem?
Thanks.!
wsdl file path - https://wsa.gso.com/GSOShipWS1.0/GSOShipWS.asmx?WSDL
$XMLRequest = <<<XMLSCR
<?xml version="1.0" encoding="UTF-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<AuthenticationHeader xmlns="http://gso.com/GsoShipWS">
<UserName>username</UserName>
<Password>password</Password>
</AuthenticationHeader>
</soap12:Header>
<soap12:Body>
<GetServiceTypes xmlns="http://gso.com/GsoShipWS">
<GetServiceTypesRequest>
<AccountNumber>12345</AccountNumber>
</GetServiceTypesRequest>
</GetServiceTypes>
</soap12:Body>
</soap12:Envelope>
XMLSCR;
I've also try with passing array
$xml_array = array(
'GetServiceTypesRequest' => array(
'AccountNumber' => 12345,
)
);
I've also tried same using curl also,but it shows blank result.
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<AuthenticationHeader xmlns="http://gso.com/GsoShipWS">
<UserName>username</UserName>
<Password>password</Password>
</AuthenticationHeader>
</soap12:Header>
<soap12:Body>
<GetServiceTypes xmlns="http://gso.com/GsoShipWS">
<GetServiceTypesRequest>
<AccountNumber>12345</AccountNumber>
</GetServiceTypesRequest>
</GetServiceTypes>
</soap12:Body>
</soap12:Envelope>';
// data from the form, e.g. some ID number
$headers = array(
"POST /GSOShipWS1.0/GSOShipWS.asmx HTTP/1.1",
"Host: wsa.gso.com",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, username.":".password); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch);

Magento2; update stock by REST API

I've set up a local Magento 2.1.2 environment with sample data. I'm trying to find the correct code for updating stocks through the REST api (catalogInventoryStockRegistryV1 - Put).
This is what i've got so far:
<?php
$adminUrl = 'http://www.localurl.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$token= json_decode($token);
$headers = array("Authorization: Bearer $token");
$requestUrl='http://www.localurl.pro/rest/V1/products/24-MB01/stockItems/1';
// Sample SKU
$sampleProductData = array(
"qty" => 100
);
$productData = json_encode(array('stockItem' => $sampleProductData));
// Prints: {"stockItem":{"qty":100}}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
This script gives the following error:
string(122) "{"message":"Server cannot understand Content-Type HTTP header media type application\/x-www-form-urlencoded","trace":null}"
I'm not sure what the message exactly means. Some help would be greatly appreciated.
Solution
Solution given by Mohan Gopal. 'Content-Type: application/json' was missing in the curl header.
<?php
$adminUrl = 'http://www.localhost.pro/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "66sRz97CZt7N[GdqFU");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$token= json_decode($token);
//Use above token into header
$headers = array("Authorization: Bearer $token","Content-Type: application/json");
$skus = array(
'24-MB04' => 10,
'24-MB03' => 5
);
foreach ($skus as $sku => $stock) {
$requestUrl='http://www.localurl.pro/rest/V1/products/' . $sku . '/stockItems/1';
$sampleProductData = array(
"qty" => $stock
);
$productData = json_encode(array('stockItem' => $sampleProductData));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
unset($productData);
unset($sampleProductData);
}
Add the content type to header and change the header from
$headers = array("Authorization: Bearer $token");
to
$headers = array("Authorization: Bearer $token","Content-Type:
application/json");
after getting the token id on line 53.

Uber Ride Reminders API does not responds always

We have properly implemented the Uber Ride Reminder API and we are getting the expected results when requested.
The only issue we are facing is, out of 100+ requests we get the response for any one of those request. Rest of the requests simply responds with empty response.
We are using Following Function to make Curl Post request:
function get_data_post($url, $post_data, $headers = false, $timeout = 3) {
$ch = curl_init();
$timeout = $timeout;
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
And here are the input parameters:
URL: https://api.uber.com/v1/reminders
Request Object:
{"reminder_time":1469880000,"phone_number":"+911231231234","event":{"time":1469894400,"name":"DIY Artistic Frames","location":"pH Designs","latitude":"23.03743","longitude":"72.5682099"}}
Headers:
Cache-Control: no-cache
Accept: application/json
Content-Type: application/json
Authorization: Token xyxxyxxyx-Aux

Paypal PDT - SSL connect error

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.

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?