Jump to content

Cancelling Subscriptions Created with PayPal


Nathum

Recommended Posts

Hi,

 

I thought I would share that I managed to do this, and should help out others.

 

The topic that did it for me.

http://thereforei.am/2012/07/03/cancelling-subscriptions-created-with-paypal-standard-via-the-express-checkout-api/

 

So in the module I created under the function terminate. I used the API to get the product details of the service. This gave me the subscription ID. I then run the PayPal function which terminated the subscription.

 

//// SUSPEND PAYPAL PAYMENTS ////
$command = "getclientsproducts";
$adminuser = "whmusername";
$values["clientid"] = $params['clientsdetails']['userid'];
$values["serviceid"] = $params["serviceid"];
$values["responsetype"] = "xml";
$PRODUCTARRAY = localAPI($command,$values,$adminuser);
$SUBSCRIPTIONID = $PRODUCTARRAY[products][product][0]['subscriptionid'];    
change_subscription_status( $SUBSCRIPTIONID, 'Cancel' );
//// SUSPEND PAYPAL PAYMENTS ////

 

 

PayPal Function

/**
* 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;
}

 

Hope this helps and saves plenty of time and $$$

 

Feel free to improve it. Like if subcription == ' '

 

Enjoy

Link to comment
Share on other sites

  • 7 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated