I have alredy enable future payments permission in my app and using developer dashboard.but not working yet please find error
http://developer.paypal.com/ and log in
https://developer.paypal.com/developer/accountStatus there you can see what permits you have.
$data = array(
"intent" => "authorize",
"payer" => array(
"payment_method" => "paypal"
),
"transactions" => array(
array("amount" => array(
"currency" => "USD",
"total" => "1.88"
),
"description" => "future of sauces")
));
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
$headers = array(
'Content-Type: application/json',
'PayPal-Client-Metadata-Id: d6743cbb53ea4470a53bfe458f0cb885',
'Authorization: Bearer A103.B7d5318JDS6NA1zGh02avjCx16oxnBPadUat5z9SlGVGEiOhoAeMuqyz0anGSICS.FAkzECypTS1IXfemHcpVa5yyrGu',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
$information = curl_getinfo($ch);
curl_close($ch);
print_r($information);
die;
Out put here
{"name":"PERMISSION_DENIED","message":"No permission for the requested operation","information_link":"https://developer.paypal.com/docs/api/#PERMISSION_DENIED","debug_id":"5b39efd4cf370"}Array
(
[url] => https://api.sandbox.paypal.com/v1/payments/payment
[content_type] => application/json
[http_code] => 403
[header_size] => 592
I made a Paypal Module, hope this code would help you.
$result_json = json_decode($result);
/* Check if authentication is valid */
if (isset($result_json->access_token))
{
$enc_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/payments/payment');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, !in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $enc_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$result_json->access_token,
'PayPal-Client-Metadata-Id: ************'
));
$result = curl_exec($ch);
$json_result = json_decode($result);
curl_close($ch);
}
json_encode($data) there are additional information that may not be useful for the transaction you might trying to do but it is an example.
{
"intent": "sale",
"payer": {
"payment_method": "credit_card",
"payer_info": {
"email": "...",
"shipping_address": {
[...]
}
},
"funding_instruments": [
{
"credit_card": {
[...]
}
}
}
]
},
"transactions": [
{
"amount": {
"total": 32.91,
"currency": "USD"
},
"item_list": {
"items": [
{
"quantity": 1,
"name": "Product Name",
"price": 16.51,
"currency": "USD"
},
{
"quantity": 1,
"name": "Product Name 2",
"price": "16.40",
"currency": "USD"
},
{
"quantity": 1,
"name": "Shipping",
"price": 0,
"currency": "USD"
}
],
"shipping_address": {
[...]
}
}
}
]
}
As I didn't get any solution from anybody so I dug into my code step by step and found solution.
function paypalFuturePayment($userID,$amount)
{
$amount=number_format($amount,2);
/* paypal App truxx dev client and secret key*/
if($userID && $amount){
$userData = selectById('tbl_users','*',"id='".$userID."'");
$refresh_token = $userData['paypal_refresh_tokens'];
$Metadata_id = $userData['paypal_metadata_id'];
if($refresh_token && $Metadata_id){
if ($_SERVER['SERVER_NAME'] == 'syonserver.com') {
$clientId = "xxxxx";
$secret = "xxx";
$url1="https://api.sandbox.paypal.com/v1/oauth2/token";
$url2="https://api.sandbox.paypal.com/v1/payments/payment";
}else{
$account = 0; // 0 for sandbox ,1 for live
if ($account == 1) {
//client live
$clientId = "xxx";
$secret = xxx";
$url1 = "https://api.paypal.com/v1/oauth2/token";
$url2 = "https://api.paypal.com/v1/payments/payment";
} else {
//client sandbox
$clientId = "xxx";
$secret = "xxx";
$url1 = "https://api.sandbox.paypal.com/v1/oauth2/token";
$url2 = "https://api.sandbox.paypal.com/v1/payments/payment";
}
}
//print_r($refresh_token);die;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/x-www-form-urlencoded");
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=refresh_token&refresh_token=" . $refresh_token);
$result = curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
//11111111 for payment Authorize: For example, to first authorize the payment, use a request similar to this:
$access_token = $result->access_token;
$data = array(
"intent" => "authorize",
"payer" => array(
"payment_method" => "paypal"
),
"transactions" => array(
array("amount" => array(
"currency" => "USD",
"total" => $amount
),
"description" => "future of sauces")
));
$data_string = json_encode($data);
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url2);
$headers = array(
'Content-Type: application/json',
'PayPal-Client-Metadata-Id: '.$Metadata_id,
'Authorization: Bearer '.$access_token,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data_string);
$result1 = curl_exec($ch1);
curl_close($ch1);
$result1=json_decode($result1);
$message = $result1->message;
if($result1->state=='approved'){
$access_id= $result1->transactions[0]->related_resources[0]->authorization->id;
}else{
if(empty($message)){
$message ='Authorization error, Please try again.';
}
return array('response' => '', 'success' => '0','message'=>$message);
}
// print_r($result1);die;
//2222222 capture the payment:
$data = array("amount" => array(
"currency" => "USD",
"total" => $amount
),
"is_final_capture" => "true"
);
$data_string = json_encode($data);
$ch2 = curl_init();
if ($_SERVER['SERVER_NAME'] == 'syonserver.com') {
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/authorization/$access_id/capture");
}else {
$account = 0; // 0 for sandbox ,1 for live
if ($account == 1) {
//client live
curl_setopt($ch2, CURLOPT_URL, "https://api.paypal.com/v1/payments/authorization/$access_id/capture");
}else{
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/authorization/$access_id/capture");
}
}
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$access_token,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec($ch2);
curl_close($ch2);
$response_a = json_decode($response, true);
$state = $response_a['state'];
$message = $response_a['message'];
if(!empty($response_a)){
if($state=='completed') {
return array('response' => $response_a, 'success' => '1','message'=>'Data received');
}else{
if(empty($message)){
$message ='Payment authorization error, Please try again.';
}
return array('response' => '', 'success' => '0','message'=>$message);
}
}
else{
return array('response' => '','success'=>'0','message'=>'Response nil');
}
}
else
{
return array('response' => '', 'success' => '0','message'=>'Authorization code not available.');
}
}else{
return array('response' => '', 'success' => '0','message'=>'Unauthorize request.');
}
}
Related
I'm working with OpenSRS EMAIL API. I'm trying to create a new user email for a domain. In their first example, they are using JSON format :
{
"credentials": {
"user": "domain_admin#example.com",
"password": "sw0rdf1sh"
},
"user": "bhayden#example.com",
"attributes": {
"name": "Bob Hayden",
"password": "changeit",
"delivery_forward": true,
"forward_recipients": [
"bob.hayden#example.com
}
}
I'm sending this with cURL
$json = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
//
//
$payload = json_encode( $json );
//
$data = [
'Content-Type:application/json',
'X-Username:' . $connection_details['reseller_username'],
'X-Signature:' . md5(md5($payload . $connection_details['api_key']) . $connection_details['api_key']),
];
//
$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//
$response = curl_exec($ch);
//
//header('Content-type: application/json');
//
echo '<pre>';
echo $response;
echo '</pre>';
This print following error
0.9 400 0 Invalid Content-Type XCP
What am I doing wrong?
I fixed this issue by reformarting the entire request. This worked for me
$data = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
$postdata = json_encode($data);
//
$ch = curl_init($connection_details_email['api_host_port']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
I was getting a response from a REST flight api in JSON format which i was not able to convert into an array from JSON.
I have tried json encoding but it was only showing printing the json response but not converting it into array
Php Controller:
public function search_flites() {php controller
header('Content-type: application/json');
$this->load->library('curl');
$result = $this->curl->simple_get('http://13.235.39.41:8080/ettafly/api/session');
$Data = json_decode($result);
$session_id = $Data->SessionId;
$url = 'http://13.235.39.41:8080/ettafly/api/flightavaliblity';
$ch = curl_init($url);
$jsonData = array(
"user_id" => "Ettafly_APITest2019",
"user_password" => "Ettafly_TestPswd2019",
"access" => "Test",
"ip_address" => "13.235.39.41",
"session_id" => "$session_id",
"journey_type" => "OneWay",
"airport_from_code" => "DEL",
"airport_to_code" => "BOM",
"departure_date" => "2019-11-16",
"return_date" => "2019-11-18",
"adult_flight" => "1",
"child_flight" => "0",
"infant_flight" => "0",
"class_type" => "Economy",
"target" => "Test"
);
$ch = curl_init($url);
$jsonDataEncoded = json_encode($jsonData, JSON_PRETTY_PRINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
$result2 = curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$Data2 = json_decode($result2, true);
}
You didn't send that request.
Check this out: https://www.php.net/manual/en/curl.examples.php#88055
<?php
public function search_flites() {
header('Content-type: application/json');
$this->load->library('curl');
$result = $this->curl->simple_get('http://13.235.39.41:8080/ettafly/api/session');
$Data = json_decode($result);
$session_id = $Data->SessionId;
$url = 'http://13.235.39.41:8080/ettafly/api/flightavaliblity';
$ch = curl_init($url);
$jsonData = array(
"user_id" => "Ettafly_APITest2019",
"user_password" => "Ettafly_TestPswd2019",
"access" => "Test",
"ip_address" => "13.235.39.41",
"session_id" => "$session_id",
"journey_type" => "OneWay",
"airport_from_code" => "DEL",
"airport_to_code" => "BOM",
"departure_date" => "2019-11-16",
"return_date" => "2019-11-18",
"adult_flight" => "1",
"child_flight" => "0",
"infant_flight" => "0",
"class_type" => "Economy",
"target" => "Test"
);
$ch = curl_init($url);
$jsonDataEncoded = json_encode($jsonData, JSON_PRETTY_PRINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
$curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// You have to exec the curl request to get a response.
$result2 = curl_exec($ch);
$Data2 = json_decode($result2, true);
var_dump($Data2, JSON_PRETTY_PRINT);
}
by using below code
(
$payload = json_encode($jsonData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$jsonData2 = json_decode($result, true);)
instead of
(
$jsonDataEncoded = json_encode($jsonData, JSON_PRETTY_PRINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
$curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// You have to exec the curl request to get a response.
$result2 = curl_exec($ch);
$Data2 = json_decode($result2, true);
var_dump($Data2, JSON_PRETTY_PRINT);)
Now i was able to conver the result into a array
I am trying to send user-specific web notifications with onesignal and i solved everything except this variable null problem.
I use that variable multiple times and there is no problem except these lines:
<?php
if(isset($_POST["sub"]))
{
$my_variable= $_POST["t1"];
$SQL = "some sql here";
if (mysqli_query($db, $SQL)) {
echo $my_variable;
echo "<br>";
function sendMessage(){
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => "$my_variable")),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
The result is :
1 JSON sent: {"app_id":"5b0eacfc-3ac8-4dc6-891b-xxxxx","filters":[{"field":"tag","key":"key","relation":"=","value":"null"}],"data":{"foo":"bar"},"contents":{"en":"test message"}}
I tried so many variations with/without quotas , json_encode() function but couldn't pass that variable to that array.
Your variable is out of scope.
You define $my_variable outside of the function sendMessage(), but proceed to try and use it within the function, without passing it as a parameter.
This can be fixed with the following:
function sendMessage($filterValue)
{
$content = array(
"en" => 'test message'
);
$fields = array(
'app_id' => "5b0eacfc-3ac8-4dc6-891b-xxxxx",
'filters' => array(array("field" => "tag", "key" => "key", "relation" => "=", "value" => $filterValue)),
'data' => array("foo" => "bar"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage($my_variable);
$return["allresponses"] = $response;
$return = json_encode( $return);
I am trying to send a mail with attachments with mailgun.
The mail itself is fine, but it is missing the attachment.
Also in the mailgun log it shows up fine, but the attachment array is empty.
I replaced my credentials with example.com.
The file is placed in a subdirectory and is readable.
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example#mail.example.com>',
'to' => 'example#example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => '#' . $filename);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I don't get an error, this is the $response:
string(103) "{
"id": "<20170514122638.8820.55203.4543B111#mail.example.com>",
"message": "Queued. Thank you."
}"
Within the mailgun logs no attachments are listed:
"message": {
"headers": {
"to": "example#example.com",
"message-id": "20170514122638.8820.55203.4543B111#mail.example.com",
"from": "Example <example#mail.example.com>",
"subject": "Subject"
},
"attachments": [],
"size": 349
},
According to all documentation I found this would be the correct solution, but it is not working.
Thanks in advance for all replies.
Change your first code to:
$filename = 'directory/example.pdf';
$parameters = array('from' => 'Example <example#mail.example.com>',
'to' => 'example#example.com',
'subject' => 'Subject',
'text' => 'This is just a test.',
'attachment[1]' => curl_file_create($filename, 'application/pdf', 'example.pdf'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/mail.example.com/messages');
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-ThisIsJustARandomString');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
I changed '#'.$filename to curl_file_create($filename, 'application/pdf', 'example.pdf').
See documentation curl_file_create and check the notes for PHP < 5.5.
or use the api https://documentation.mailgun.com/en/latest/api-sending.html#examples
once the domains and parameters are set you can then just use $result = $mgClient->messages()->send( $domain, $params );
This worked for me.
<?php
$path = $filename;
define('MAILGUN_URL', 'https://api.mailgun.net/v3/domainname');
define('MAILGUN_KEY', 'private api key from mail gun');
**function sendmailbymailgun**($to,$toname,$mailfromname,$mailfrom,$subject,
$html,$text,$tag,$replyto, $path){
$array_data = array(
'from'=> $mailfromname .'<'.$mailfrom.'>',
'to'=>$toname.'<'.$to.'>',
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
'attachment[0]' => curl_file_create(__dir__."\\".$path, 'application/pdf', $path),
'attachment[1]' => curl_file_create(__dir__."\\".$path, 'application/pdf', "example.pdf")
);
$session = curl_init(MAILGUN_URL.'/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
$results = json_decode($response, true);
return $results;
}
//: call the function
$res = sendmailbymailgun("example#yahoo.com","Recipeint Name", "Sender Name", "sender#example.com","Email subject","Email body. find two attachment","","tags", "no-reply#example.com", $path);
echo "<pre>".print_r($res, true)."</pre>";
?>
$dest = "filepath/test.pdf";
$result = $mgClient->messages()->send($domain,
array('from' => 'Tester <test#test.com>',
'to' => 'test#test02.com',
'subject' => 'Testing',
'html' => '<h1>Normal Testing</h1>',
'attachment' => array(
array(
'filePath' => $dest,
'filename' => 'test.pdf'
)
)
));
This code is worked properly for me.
This works for me. I have passed the attachment file URL in the attachment array. now I can send multiple attachments in email.
$attachment = [ 0 =>
'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png',
1 => 'https://www.crm.truerater.com/public/assets/client_upload_images/1634327873.png'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/truerater.com/messages');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'from' => $mailfromname .'<'.$mailfrom.'>',
'to' => $toname.'<'.$to.'>',
'cc' => '',
'bcc' => '',
'subject' => $subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyto,
);
if(sizeOf($attachment) > 0){
$i=0;
foreach ($attachment as $attach){
$attachPath = substr($attach, strrpos($attach, '/public/') + 1);
$post['attachment['.$i.']'] = curl_file_create($attach, '', substr($attachPath, strrpos($attachPath, '/') + 1));
$i=$i+1;
}
}
$headers_arr = array("Content-Type:multipart/form-data");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'api' . ':' . $key);
curl_setopt($ch, CURLOPT_HEADER, $headers_arr);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
\Log::info($result);
}
else{
$result = json_decode($result,true);
}
curl_close($ch);
\Log::info($result);
return $result;
I'm having trouble just sending my first API call to PayPal.
I'm hoping its just simple syntax that I got wrong.
The error I'm getting:
"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
$sAccessToken = $json->access_token;
$sTokenType = $json->token_type;
$sAppID = $json->app_id;
print_r($json);
}
curl_close($ch);
$ch2 = curl_init();
//curl -v https://api.sandbox.paypal.com/v1/payments/payment
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
//-H "Content-Type:application/json" \
//-H "Authorization: Bearer <Access-Token>" \
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
"Content-Type:application/json",
"Authorization: Bearer ".$sAccessToken,
));
$payer = array(
'payment_method' => 'credit_card',
'funding_instruments' => array(
'credit_card' => array(
'number' => "4417119669820331",
"type" => "visa",
"expire_month" => "11",
"expire_year" => "2018",
"cvv2" => "874",
"first_name" => "Betsy",
"last_name" => "Buyer",
"billing_address" => array(
"line1" => "111 First Street",
"city" => "Saratoga",
"state" => "CA",
"postal_code" => "95070",
"country_code" => "US"
),
),
),
);
$params = array(
'intent' => 'sale',
'payer' => $payer,
'transactions' => array(array(
"amount" =>"7.47",
"currency" =>"USD",
"details" => array(
"subtotal"=>"7.41",
"tax"=>"0.03",
"shipping"=>"0.03"
),
"description" => "This is the payment transaction description."
)
));
curl_setopt($ch2, CURLOPT_POST, true );
curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode( $params ) );
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true );
$response2 = curl_exec( $ch2 );
$errrors2 = curl_error($ch2);
print_r($response2." / ");
print_r($errrors2);
curl_close($ch2);