PayPal Adaptive Payments Certificate Error - php

I am trying to implement PayPal Adaptive Payments, but as soon as I try to post with cURL, I get a NULL response and curl_error() gives the following error:
0NSS: client certificate not found (nickname not specified)
Here is the full PHP code, I am using sandbox. Some sensitive info is starred (*).
<?php
class PayPal {
public $API_USER = "*********.gmail.com";
public $API_PASSWORD = "********************";
public $API_SIGNATURE = "******************************";
public $API_APP_ID = "APP-80W284485P519543T";
public $apiUrl = "https://svcs.sandbox.paypal.com/AdaptivePayments/";
public $paypalUrl = "https://sandbox.paypal.com/webscr?cmd=ap-payment&paykey=";
public $headers = array();
function __construct() {
$this->headers = array(
"X-PAYPAL-SECURITY-USERID : " . $this->API_USER,
"X-PAYPAL-SECURITY-PASSWORD : " . $this->API_PASSWORD,
"X-PAYPAL-SECURITY-SIGNATURE : " . $this->API_SIGNATURE,
"X-PAYPAL-REQUEST-DATA-FORMAT : JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT : JSON",
"X-PAYPAL-APPLICATION-ID : " . $this->API_APP_ID
);
}
function paypalSend($data, $call) {
$ch = curl_init();
$curl_params = array(
CURLOPT_URL => $this->apiUrl . $call,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_HTTPHEADER => $this->headers,
CURLOPT_POSTFIELDS => json_encode($data)
);
curl_setopt_array($ch, $curl_params);
$result = curl_exec($ch);
echo(curl_errno($ch));
echo(curl_error($ch));
curl_close($ch);
return json_decode($result, TRUE);
}
function getPayKey() {
$createPacket = array(
"actionType" => "PAY",
"currencyCode" => "USD",
"receiverList" => array(
"receiver" => array(
array(
"amount" => "5.00",
"email" => "*********#gmail.com"
)
)
),
"returnUrl" => "http://example.com/success",
"cancelUrl" => "http://example.com/failure",
"requestEnvelope" => array(
"errorLanguage" => "en_US",
"detailLevel" => "ReturnAll"
)
);
// paypal Send
$response = $this->paypalSend($createPacket, "Pay");
var_dump($response);
}
}
$pp = new Paypal();
$pp->getPayKey();
?>

Related

Pritunl Rest Api using Php

Has anyone managed to access pritunl's api using php?
The guide provided from the source code https://github.com/pritunl/pritunl/blob/master/tools/test_pritunl.py has only python examples.
If I try do read data, it works fine. But when I try to update or create a new entity I receive HTTP request failed! HTTP/1.1 401 UNAUTHORIZED
My code is:
public static function auth_request($method, $path, $data="")
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
if ($data) {
array_push($auth_array, $data);
}
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
Solved removing the $data from $auth_array.
This is my new code:
public static function auth_request($method, $path, $data="") {
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
}

Requesting Api Call using cURL

Someone could help me ? I'm trying to do a request by the code bellow, but anything happen, any message appears. I believe my code it's right:
public function subscribe(){
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';
$json_string = json_encode(array(
"MerchantOrderId"=>"2014113245231706",
"Customer" => array(
"Name" => "Comprador rec programada"
),
"Payment" => array(
"Type" => "CreditCard",
"Amount" => 1500,
"Installments" => 1,
"SoftDescriptor" => "Assinatura Fraldas"
)
));
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array( $ch, $options );
$result = curl_exec($ch); // Getting jSON result string
print_r($result);
}
Find link with instructions of the site:
you will reiceive this:
[
{
"Code": 114,
"Message": "The provided MerchantId is not in correct format"
}
]
with this code:
function subscribe(){
$json_url = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';
$json_string = json_encode(
array(
"MerchantOrderId"=>"2014113245231706",
"Customer" => array(
"Name" => "Comprador rec programada"
),
"Payment" => array(
"Type" => "CreditCard",
"Amount" => 1500,
"Installments" => 1,
"SoftDescriptor" => "Assinatura Fraldas"
)
)
);
$headers = array(
'Content-Type: application/json',
'MerchantId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
'MerchantKey: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx',
'RequestId: xxxxxxxx-xxxxx-xxxxx-xxxxx-xxxxxxxxxxxx'
);
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $json_string
);
curl_setopt_array( $ch, $options ); $result = curl_exec($ch);
print_r($result);
}
subscribe()
It would be interesting what HTTP status code you get:
print_r(curl_getinfo($ch, CURLINFO_HTTP_CODE));

PayPal ipnNotificationUrl Turned off and not sending notifications to URL

I'm working with paypal adaptive payments API and all things were okay till last month.
The problem is ipnNotificationUrl is not sending any notifications to URL i provided in code.
Here is the code of PayPal adaptive payment,
<?php
class Paypal{
private $api_user;
private $api_pass;
private $api_sig;
private $app_id;
private $apiUrl = 'https://svcs.sandbox.paypal.com/AdaptivePayments/';
private $paypalUrl="https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=";
private $headers;
public function setDetails($api_u, $api_p, $api_s, $api_id){
$this->api_user = $api_u;
$this->api_pass = $api_p;
$this->api_sig = $api_s;
$this->app_id = $api_id;
$this->headers = array(
"X-PAYPAL-SECURITY-USERID: ".$this->api_user,
"X-PAYPAL-SECURITY-PASSWORD: ".$this->api_pass,
"X-PAYPAL-SECURITY-SIGNATURE: ".$this->api_sig,
"X-PAYPAL-REQUEST-DATA-FORMAT: JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT: JSON",
"X-PAYPAL-APPLICATION-ID: ".$this->app_id,
);
}
public function getPaymentOptions($paykey){
$pack = array(
"requestEnvelope" => array(
"errorLanguage" => "en_US",
"detailLevel" => "ReturnAll",
),
"payKey" => $paykey
);
return $this->_paypalSend($pack, "GetPaymentOptions");
}
public function setPaymentOptions(){
}
public function _paypalSend($data,$call){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiUrl.$call);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
$response = json_decode(curl_exec($ch),true);
return $response;
}
public function splitPay($currency, $r1_email, $r2_email, $r1_amount, $r2_amount, $returnUrl, $cancelUrl, $product_name, $indentifier){
$createPacket = array(
"actionType" =>"PAY",
"currencyCode" => $currency,
"receiverList" => array("receiver" =>
array(
array(
"amount"=> $r1_amount,
"email"=> $r1_email
),
array(
"amount"=> $r2_amount,
"email"=> $r2_email
)
)
),
"returnUrl" => $returnUrl,
"cancelUrl" => $cancelUrl,
"ipnNotificationUrl" => URL::to('/adaptive_payments'),
"requestEnvelope" => array(
"errorLanguage" => "en_US",
"detailLevel" => "ReturnAll",
),
);
$response = $this->_paypalSend($createPacket,"Pay");
$paykey = $response['payKey'];
$detailsPack = array(
"requestEnvelope" => array(
"errorLanguage" => "en_US",
"detailLevel" => "ReturnAll",
),
"payKey" => $paykey,
"receiverOptions" => array(
array(
"receiver" => array("email" => $r1_email),
"invoiceData" => array(
"item" => array(
array(
array(
"name" => $product_name,
"price" => $r1_amount,
"identifier" => $indentifier
)
)
)
)
),
array(
"receiver" => array("email" => $r2_email),
"invoiceData" => array(
"item" => array(
array(
array(
"name" => "product 2",
"price" => $r2_amount,
"identifier" => "p2"
)
)
)
)
)
)
);
$response = $this->_paypalSend($detailsPack, "SetPaymentOptions");
$dets = $this->getPaymentOptions($paykey);
return $this->paypalUrl.$paykey;
}
}
And here the response i get,
array(3) { ["responseEnvelope"]=> array(4) { ["timestamp"]=> string(29) "2015-06-04T14:04:07.395-07:00" ["ack"]=> string(7) "Success" ["correlationId"]=> string(13) "6c472863c4053" ["build"]=> string(8) "15743565" } ["payKey"]=> string(20) "AP-5LN44020A0587750B" ["paymentExecStatus"]=> string(7) "CREATED" }
Which is perfectly fine and i can use paykey, but ipnNotificationUrl not working as it should. it doesn't send anything at all at URL.
What t tried to solve the issue,
1) did change URL of ipnNotificationUrl even made hard coded
2) tested with IPN simulator (the listener), worked for test
3) changed return URL and cancel URL to test if others working, also made sure that i'm working with correct file and code
Please HELP!
The code is working fine , it wasn't because paypal had put IPN as queued.
More details here https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNOperations/
Hope this answer will help others.

PayPal Adaptive Payments PHP

I have been recently trying to make an API request using PHP to PayPal to pay multiple accounts but so far I have no luck.
`
class PayPal
{
public $_headers = array();
public $_config = array();
public $_data = array();
public function __construct()
{
if (!in_array('curl', get_loaded_extensions()))
{
trigger_error("CURL extension is not installed or enabled", E_USER_ERROR);
}
}
public function update_headers(array $credentails)
{
$this->_headers = $credentails;
return $this;
}
public function update_config(array $config)
{
$this->_config = $config;
return $this;
}
public function pay($data, $call)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->get_url());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_headers);
return curl_exec($ch);
}
public function split_pay()
{
// create the pay request
$this->_data = array(
"actionType" =>"PAY",
"currencyCode" => "USD",
"receiverList" => array(
"receiver" => array(
array(
"amount"=> "1.00",
"email"=>"******#gmail.com"
),
array(
"amount"=> "2.00",
"email"=>"*********.co.uk"
),
),
),
"returnUrl" => "http://test.local/payments/confirm",
"cancelUrl" => "http://test.local/payments/cancel",
"requestEnvelope" => array(
"errorLanguage" => "en_US",
"detailLevel" => "ReturnAll",
),
);
return $this->pay($this->_data, "PAY");
}
private function get_url()
{
if (empty($this->_config))
{
trigger_error("You have not setup the config", E_USER_ERROR);
}
return $this->_config['url'];
}
public function call_headers()
{
if (empty($this->_headers))
{
trigger_error("You have not set any headers", E_USER_ERROR);
}
print_r($this->_headers);
}
}
?>`
This is a class file which I have created, it is very rough but its not working. Here is some more code.
`load->library('paypal');
$pp = new PayPal();
//Bring me the money!
//Headers for paypal
$headers = array(
"X-PAYPAL-SECURITY-USERID: ********",
"X-PAYPAL-SECURITY-PASSWORD: *********",
"X-PAYPAL-SECURITY-SIGNATURE: **********",
"X-PAYPAL-REQUEST-DATA-FORMAT: NV",
"X-PAYPAL-RESPONSE-DATA-FORMAT: NV",
"X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T"
);
$config = array(
'url' => 'https://svcs.sandbox.paypal.com/AdaptivePayments/Pay',
'username' => '********',
'password' => '********',
'signature' => '********',
'app_id' => 'APP-80W284485P519543T',
);
$pp->update_headers($headers)->update_config($config);
print_r($pp->split_pay());
?>
`
I am being returned with this response.
responseEnvelope.timestamp=2014-04-22T06%3A15%3A49.821-07%3A00&responseEnvelope.ack=Failure&responseEnvelope.correlationId=b3d05fe547e22&responseEnvelope.build=10680030&error(0).errorId=580001&error(0).domain=PLATFORM&error(0).subdomain=Application&error(0).severity=Error&error(0).category=Application&error(0).message=Invalid+request%3A+%7B0%7D
Does anyone have any ideas why this might be

updating an issue in JIRA using rest api and PHP in API 5.1

I am using the following code to update an issue in JIRA but unable to diagnose the error. The error I am getting is as follows:
HTTP Status 415 - Unsupported Media Type
type Status report
message Unsupported Media Type
The code I have written is as follows:
$resource_array['api_name'] = 'issue/SPC-60';
$resource_array['fields'] = array (
'summary' => 'CLONE - Testing label stuff',
'assignee' =>
array (
'emailAddress' => 'avinashk.dubey#gmail.com',
),
'customfield_10649' =>
array (
'id' => '10668',
),
'customfield_10616' => 'This is observation'
);
$data = putJiraAPI($resource_array);
print_r($data);
////////////////////////////////////////////
function putJiraAPI($resource_array)
{
$api_name = $resource_array['api_name'];
unset($resource_array['api_name']);
$result = put_to($api_name, $resource_array);
if(is_array($result))
{
return $result;
}
else
{
return "error while getting data using ".BASE_URL.API_URL.$resource_string;
}
}
function put_to($api_name, $resource_array)
{
$jdata = json_encode($resource_array);
print_r($jdata);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_URL => BASE_URL . API_URL . $api_name,
CURLOPT_USERPWD => USER_NAME . ':' . PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHECURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_RETURNTRANSFER => true
));
echo BASE_URL . API_URL . $api_name;
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}
Actually, I did wrong here:
CURLOPT_HTTPHECURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
it should be:
CURLOPT_HTTPHEADER => array('Content-type: application/json'),

Categories