/**
* Find all subscription with a given billing agreement ID and cancel them becasue that billing agreement has been
* cancelled at PayPal, and therefore, no future payments can be charged.
*
* @since 2.0
*/
protected function cancel_subscriptions($billing_agreement_id)
{
$subscription_ids = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_subscription', 'post_status' => 'any', 'fields' => 'ids', 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => array(array('key' => '_paypal_subscription_id', 'compare' => '=', 'value' => $billing_agreement_id))));
if (empty($subscription_ids)) {
return;
}
$note = esc_html__('Billing agreement cancelled at PayPal.', 'woocommerce-subscriptions');
foreach ($subscription_ids as $subscription_id) {
$subscription = wcs_get_subscription($subscription_id);
try {
if (false !== $subscription && !$subscription->has_status(wcs_get_subscription_ended_statuses())) {
$subscription->cancel_order($note);
WC_Gateway_Paypal::log(sprintf('Subscription %s Cancelled: %s', $subscription_id, $note));
}
} catch (Exception $e) {
WC_Gateway_Paypal::log(sprintf('Unable to cancel subscription %s: %s', $subscription_id, $e->getMessage()));
}
}
}