Reputation: 5302
I have this code:
$cancel_payment = UpdateRecurringPaymentsProfile( $paypal_profileid , 'Cancel' );
function UpdateRecurringPaymentsProfile( $user_id, $method )
{
//$user_id is the profile ID returned in the CreateRecurringPaymentsProfile response.
$nvpstr = "&profileid=".$user_id;
$nvpstr .= "&PROFILESTATUS=".$method;
$nvpstr .= "&ACTION=Cancel";
$resArray = hash_call( "UpdateRecurringPaymentsProfile" , $nvpstr );
return $resArray;
}
function hash_call($methodName,$nvpStr)
{
//declaring of global variables
global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;
global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
global $gv_ApiErrorURL;
global $sBNCode;
//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);
// var_dump($nvpreq);
//setting the nvpreq as POST FIELD to curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
//getting response from server
$response = curl_exec($ch);
//convrting NVPResponse to an Associative Array
$nvpResArray=deformatNVP($response);
$nvpReqArray=deformatNVP($nvpreq);
$_SESSION['nvpReqArray']=$nvpReqArray;
if (curl_errno($ch))
{
// moving to display page to display curl errors
$_SESSION['curl_error_no']=curl_errno($ch) ;
$_SESSION['curl_error_msg']=curl_error($ch);
//Execute the Error handling module to display errors.
}
else
{
//closing the curl
curl_close($ch);
}
return $nvpResArray;
}
I'm just showing you bits and pieces of my code..
This code does not return any error. The problem is that when I check on my "Preapproved Payments" on my paypal dashboard(I'm using sandbox), I'm not seeing the status as cancelled, it's still Active. So inshort, my code is wrong or there's something wrong.
Can anyone guide me how to implement a cancelation of recurring payment in paypal?
Upvotes: 2
Views: 4887
Reputation: 24
Here is my canceled PayPal Recurring payment Code.
$ch = curl_init();
$clientId ='Your Client ID';
$clientSecret= 'Your Secret ID';
//you get Clientid and clientSecret
https://developer.paypal.com/developer/applications
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.":".$clientSecret);
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);
}
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/billing/subscriptions/'.$value1->transaction_id.'/cancel');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"reason\": \"Not satisfied with the service\"\n}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$json->access_token.'';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$workerpayment= DB::table('subscriptions')->where('id','=',$value1->id)->update(['subscription_status' => 'cancelled']);
}
It's working fine for me.
Upvotes: 0
Reputation: 12524
To cancel a recurring payment you need to use the ManageRecurringPaymentsProfileStatus API operation and set the action to Cancel
Upvotes: 2