When a user upgrades their subscription on my site, I get contacted by IPN, then I want to use php to contact paypal to cancel any existing subscription they may have. How do I do this? I can only find how to do it using buttons the user has to click.
Asked
Active
Viewed 555 times
2
-
Website Payments Standard or Pro? – Yaniro Apr 22 '12 at 10:15
-
1Well, then to the best of my knowledge, you can't do this automatically, you can only redirect the user to his/her paypal account and let him/her cancel the subscription manually (unfortunately this is how i do it now...) – Yaniro Apr 22 '12 at 14:40
1 Answers
1
This is the best solution I have found:
/**
* Performs an Express Checkout NVP API operation as passed in $action.
*
* Although the PayPal Standard API provides no facility for cancelling a subscription, the PayPal
* Express Checkout NVP API can be used.
*/
function change_subscription_status( $profile_id, $action ) {
$api_request = 'USER=' . urlencode( 'api_username' )
. '&PWD=' . urlencode( 'api_password' )
. '&SIGNATURE=' . urlencode( 'api_signature' )
. '&VERSION=76.0'
. '&METHOD=ManageRecurringPaymentsProfileStatus'
. '&PROFILEID=' . urlencode( $profile_id )
. '&ACTION=' . urlencode( $action )
. '&NOTE=' . urlencode( 'Profile cancelled at store' );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp' ); // For live transactions, change to 'https://api-3t.paypal.com/nvp'
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
// Uncomment these to turn off server and peer verification
// 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 );
// Set the API parameters for this transaction
curl_setopt( $ch, CURLOPT_POSTFIELDS, $api_request );
// Request response from PayPal
$response = curl_exec( $ch );
// If no response was received from PayPal there is no point parsing the response
if( ! $response )
die( 'Calling PayPal to change_subscription_status failed: ' . curl_error( $ch ) . '(' . curl_errno( $ch ) . ')' );
curl_close( $ch );
// An associative array is more usable than a parameter string
parse_str( $response, $parsed_response );
return $parsed_response;
}
Example:
change_subscription_status( 'I-NARPL1C00000', 'Cancel' );
I hope it helps
-
cool! I don't have an active project anymore to test this with but good to know a solution is out there now. Do you have do anything different use the NVP API or can you just use the standard API with the exception of this function (coming from someone with no experience with NVP) – Sabrina Leggett Dec 23 '14 at 18:07
-
1@SabrinaGelbart Sincerely, I haven't used this API for any more things. I was just looking for a solution and I luckily found that blog. Here you have the api reference with all NVP avalaible methods and their documentation: https://developer.paypal.com/webapps/developer/docs/classic/api/ Hope this help! – Dec 23 '14 at 21:11