Cancel recurring contribution once completed.
This is a workaround for issue created on github https://github.com/mattwire/com.drastikbydesign.stripe/issues/19
Merge request reports
Activity
283 // Cancel subscription in stripe. 284 if ($dao->contribution_status_id == 1 && empty($getRecur['values'][0]['cancel_date'])) { 285 $paymentProcessor = civicrm_api3('PaymentProcessor', 'get', [ 286 'sequential' => 1, 287 'id' => $getRecur['values']['payment_processor_id'], 288 ]); 289 $mode = $getRecur['values'][0]['test'] ? 'live' : 'test'; 290 $paymentProcessor = $paymentProcessor['values'][0]; 291 $message = "Subscription cancelled successfully."; 292 $params['subscriptionId'] = $getRecur['values'][0]['processor_id']; 293 294 $cancelSub = new CRM_Core_Payment_Stripe($mode, $paymentProcessor); 295 $cancelSub->CancelSubscription($message, $params); 296 } 297 // Keep the status of recurring as completed in civicrm. 298 if ($dao->contribution_status_id == 3) { changed this line in version 2 of the diff
271 271 } 272 273 /* 274 * Implementation of hook_civicrm_postSave_[table_name]($dao). 275 * 276 */ 277 function stripe_civicrm_postSave_civicrm_contribution_recur($dao) { 278 $getRecur = civicrm_api3('ContributionRecur', 'get', [ 279 'sequential' => 1, 280 'return' => ["processor_id", "is_test", "payment_processor_id", "cancel_date"], 281 'id' => $dao->id, 282 ]); 283 // Cancel subscription in stripe. 284 if ($dao->contribution_status_id == 1 && empty($getRecur['values'][0]['cancel_date'])) { 285 $paymentProcessor = civicrm_api3('PaymentProcessor', 'get', [ 286 'sequential' => 1, changed this line in version 2 of the diff
907 * @param array $params 908 * 909 * @return bool|object 910 */ 911 public function cancelSubscription(&$message = '', $params = array()) { 912 // Include Stripe library then set plugin info and API credentials. 913 try { 914 require_once('vendor/stripe/stripe-php/init.php'); 915 \Stripe\Stripe::setAppInfo('CiviCRM', CRM_Utils_System::version(), CRM_Utils_System::baseURL()); 916 \Stripe\Stripe::setApiKey($this->_paymentProcessor['user_name']); 917 $subscription = \Stripe\Subscription::retrieve($params['subscriptionId']); 918 $subscription->cancel(); 919 } 920 catch (\Stripe\Error\InvalidRequest $e) { 921 // Invalid parameters were supplied to Stripe's API 922 CRM_Core_Error::debug_log_message($e->getMessage()); changed this line in version 2 of the diff
869 869 (subscription_id, customer_id, contribution_recur_id, processor_id, end_time, is_live) 870 870 VALUES (%1, %2, %3, %4, %5, '{$this->_islive}')", $query_params); 871 871 } 872 872 // update recur processor_id with subscriptionId 873 if ($subscription_id && $recuring_contribution_id) { 874 civicrm_api3('ContributionRecur', 'create', array( 875 'sequential' => 1, Thanks @madhavi for your contribution. Please could you have a look at my comments?
added 2 commits
@mattwire Thank you for the points. I have tried to fix them. Also I have updated the approach to not change the subscription status to completed when it is cancelled manually.
added 1 commit
- 98723c80 - Cleanup unnecessary code as CiviCRM core is handling recurring contribution status updates
changed milestone to %5.2
mentioned in issue #9 (closed)
added enhancement label
@madhavi Thankyou for your contribution. I've merged a modified version of this into master. 5.2 will support cancellation - please let me know if you are able to test?
@mattwire Thank you. Tested the latest version. Works absolutely fine with cancellation of recurrings through civicrm. I suppose the auto cancellation will be handled in next version.