PHP cURL Cancel Subscription PayPal issues - php

I will admit, I have been on this for 3 days, back and forth through documentation, and even learned how to create my own products and plans through Postman.
So I have a complete working subscription button, now I learned I need to save the Subscription ID in order to cancel the subscription so now that is saving and loading perfectly.
To my sad surprise, I need to get an AuthKey every time someone wants to cancel, so I need to run 2 cURL commands, 1 GET Auth Key, and 1 POST cancel a subscription. How would I do this in PHP?
I don't get any errors when these commands run just no data.
Paypal cURL Example:
https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel
////////////////////////////// GET TEMP ACCESS TOKEN/////////////////////////////////
$ch = curl_init();
$clientId = "x";
$secret = "x";
$myIDKEY = "";
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);
print_r($json->access_token);
$myIDKEY = $json->access_token;
}
curl_close($ch);
/////////////////////////////// SEND CANCEL POST ////////////////////////////////
$ch = curl_init();
$headers = [
'Authorization: Bearer '.$myIDKEY,
'Content-Type: application/json'
];
$postData = [
'reason' => 'clicked cancel subscription button'
];
curl_setopt($ch, CURLOPT_URL,"https://api.sandbox.paypal.com/v1/billing/subscriptions/".$_SESSION['honeybeesubID']."/cancel");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $myIDKEY
Thank you so much!

As documented at https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel , a success response is an HTTP 204 with no data.
So, it will be normal to receive an empty response, along with that status code.

Related

PHP cURL how to send a JSON POST request and also include a URL querystring?

I'm trying to submit a POST request with JSON data to an api endpoint. The endpoint requires a querystring passing the api credentials, but also requires the JSON data to be POSTed.
When I try to do this with PHP cURL as shown below, the querystring is apparently removed - thus the api is rejecting the request due to missing api key.
I can do this easily with Postman when testing access to the api endpoint.
How can I make the cURL request include both the querystring AND the JSON POST body?
Example code:
// $data is previously defined as an array of parameters and values.
$url = "https://api.endpoint.url?api_key=1234567890";
$ch = curl_init();
$json = json_encode($data);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You are doing almost right.
Sometimes you need to relax SSL verification.
Otherwise, update php ca bundle:
https://docs.bolt.cm/3.7/howto/curl-ca-certificates
Add the following:
$headers = array(
"Content-type: application/json;charset=UTF-8",
"Accept-Encoding: gzip,deflate",
"Content-length: ".strlen($json),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, "identity, deflate, gzip");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Sometimes you need to change encoding too:
$result = utf8_decode($result);
And check the returning data.

What is process to integrate paypal payout rest API in php

I am working on PayPal Payout API.i am not getting what exactly process should be for a complete payout payment method.everything is working fine but still there are so many bugs. what I and mobile application developer is doing.
Mobile API calls to the server with an amount so that server side a PayPal SDK has been integrated where I make a call on PayPal and make a transaction.
The problem is that whether a payer email register or not in Paypal it returns status PENDING without any transaction id.
And I have set up an IPN listener on PayPal which makes a call whenever payout status update. but it returns transaction_id and I am not getting transaction id while payout made.
I don't know what I need to do with it. Please if you did not understand you can drop a comment.
I am using the simple code for Payouts.
$headers = array(
'Content-Type:application/json',
'Authorization:Bearer '.$accsessToken,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payouts?sync_mode=false");
// curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
$response = json_decode($result, 1);
$payoutBatchId = $response['batch_header']['payout_batch_id'];
$headers = array(
'Content-Type:application/json',
'Authorization:Bearer '.$accsessToken,
);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payouts/".$payoutBatchId);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$response = json_decode($result, 1);
dd($response);
I think you should use PHP Paypal SDK.
There is some easy examples.
Hope this help.

How to initiate refund using click bank API

I am trying to initiate a refund using click bank api with below source code.
$ch = curl_init();
$qry_str="?type=rfnd&comment=API refund check&reason=7&refundType=FULL";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.clickbank.com/rest/1.3/tickets/N5GNE72J'.$qry_str);
curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_GET, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization:DEV-xxxxxxxxx:API-yyyyyyyyyyyy"));
$result = curl_exec($ch);
curl_close($ch);
print $result;
I have used below two url for reference:
https://api.clickbank.com/api/api_13_examples/api_example.php
https://api.clickbank.com/rest/1.3/tickets
After executing above code it shows a blank screen nothing is displyed, My error flag is set to 1 still no error shown.
After a long struggle found the solution. Use below code it worked for me.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://api.clickbank.com/rest/1.3/tickets/627JE7CZ/?type=rfnd&comment=&reason=ticket.type.refund.7&refundType=FULL");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
/**
* ClickBank doesn't allow POST parameters to be sent in; however, for the CURL POST to work correctly, the
* CURL_POSTFIELDS option must be set, so we'll just set it to empty and put all our request parameters on the URL
*/
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/xml",
"Authorization:DEV-enter your dev key here:API-enter your clerk key here"
));
$result = curl_exec($ch);
curl_close($ch);
?>

CURL - get Retrieve all email from Google API

I'm using cURL to get all email from user via Google API. Following https://developers.google.com/admin-sdk/email-audit/#retrieving_all_email_monitors_of_a_source_user.
According this tutorial, the server return '201 Created' status code to successful. But, my result return '200 OK' code.
Here is code Authorization
$data = array(
'accountType' => 'HOSTED_OR_GOOGLE',
'Email' => 'myEmail',
'Passwd' => 'myPassword',
'source'=>'PHP-cUrl-Example',
'service'=>'apps');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
And here is code to Retrieving all email monitors of a source user
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
$header = array('Content-Type: application/atom+xml; charset=utf-8',
'Authorization: GoogleLogin auth='.trim($auth),
);
$url_email ="https://apps-apis.google.com/a/feeds/compliance/audit/mail/monitor/mydomain/username";
curl_setopt($ch, CURLOPT_URL, $url_email);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
$response = simplexml_load_string($response);
curl_close($ch);
print_r($response);
Help me pls ?
The API allows you to request the status of a single export request with a URL of:
https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/{domain name}/{source user name}/{mailbox requestId}
or of all requests across the domain with a request of:
https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/{domain name}?fromDate={fromDate}
there is no operation to retrieve the status of all requests for a given user like you are trying to do.
I suggest you confirm you've successfully created an audit request by using GAM to create the request. GAM will show you the request ID on success. Then you can try getting the results of the single request with your code.

PayPal PHP Sandbox Header Auth?

I am currently facing some problems during my paypal refund test.
I get the token of the account by making this:
public function __construct(){
$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, $this->clientID.":".$this->secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
$json = json_decode($result);
$this->auth = $json->access_token;
// echo "Auth?" . $this->auth;
}
And when i try refund a sale, it just returns 1 nomatter what. The transaction ID is correct, but it's like it doesn't set the right headers - can someone help me?
public function RefundSale($transaction_id){
$url = self::getPath() . "/payments/sale/".$transaction_id."/refund";
echo "URL:" . $url;
self::rest($url);
}
public function rest($url,$data=""){
$ch = curl_init();
if ( !empty($data)): $data=json_encode($data); endif; //Array to json?
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
if ( !empty($data)): curl_setopt($ch, CURLOPT_POSTFIELDS, $data); endif;
curl_setopt($ch, CURLOPT_USERPWD, $this->clientID . ":" . $this->secret);
$result = curl_exec($ch);
var_dump($result);
}
A couple of things
You must set curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) to retrieve curl response. Otherwise you get only a true/false success/error message.
After fixing this, the code snippet gives a 400 (bad request) http response code. This is rectified by calling curl_setopt($ch, CURLOPT_POSTFIELDS, $data) unconditionally. Even when you have no data to send, this ends up sending an empty payload.
The last issue is with the way you authenticate the call. Once you have obtained an auth token, you authenticated API calls by sending a 'Authentication: Bearer ' header.

Categories