Commit 1fe60edc authored by Michael Iseard's avatar Michael Iseard
Browse files

Further improvements to subscription cancellation

parent 49c30b65
......@@ -227,13 +227,12 @@ class PaymentService extends AbstractService {
/**
* Cancel the specified subscription
*
* @param string $subscription_id Mollie subscription id.
* @param null|string $customer_id Mollie customer id.
* @param string $subscription_id Id of subscription to cancel.
*
* @return bool
* @since 2.3.0
*/
public function cancel_subscription( string $subscription_id, $customer_id = null ): bool {
public function cancel_subscription( string $subscription_id ): bool {
$mapper = new MapperService( SubscriptionEntity::class );
......@@ -242,7 +241,7 @@ class PaymentService extends AbstractService {
if ( $subscription ) {
$result = $this->vendor->cancel_subscription( $subscription_id, $customer_id );
$result = $this->vendor->cancel_subscription( $subscription_id );
if($result) {
......@@ -258,7 +257,7 @@ class PaymentService extends AbstractService {
$this->logger->info(
'Subscription cancelled.',
[ 'customer_id' => $customer_id, 'subscription_id' => $subscription_id ]
[ 'subscription_id' => $subscription_id ]
);
return true;
......
......@@ -36,11 +36,10 @@ abstract class AbstractVendor extends AbstractService {
* Cancel the specified subscription
*
* @param string $subscription_id subscription id.
* @param null|string $customer_id customer id.
*
* @return bool
*/
abstract public function cancel_subscription( string $subscription_id, $customer_id = null ): bool;
abstract public function cancel_subscription( string $subscription_id ): bool;
/**
* Checks the provided api key by attempting to get associated payments
......
......@@ -95,29 +95,25 @@ class MollieVendor extends AbstractVendor {
* Cancel the specified subscription
*
* @param string $subscription_id Mollie subscription id.
* @param null|string $customer_id Mollie customer id.
*
* @return bool
* @since 2.0.0
*/
public function cancel_subscription( string $subscription_id, $customer_id = null ): bool {
// Get customer id from subscription if not provided
if ( ! $customer_id ) {
// Get the subscription from the database
$mapper = new MapperService( SubscriptionEntity::class );
/** @var SubscriptionEntity $subscription */
$subscription = $mapper->get_one_by( [ 'subscription_id' => $subscription_id ] );
$customer_id = $subscription->customer_id;
}
public function cancel_subscription( string $subscription_id ): bool {
// Get the subscription from the database
$mapper = new MapperService( SubscriptionEntity::class );
/** @var SubscriptionEntity $subscription */
$subscription = $mapper->get_one_by( [ 'subscription_id' => $subscription_id ] );
$customer_id = $subscription->customer_id;
$customer = $this->get_customer( $customer_id );
// Bail if no subscription found locally or if not active
if ( empty( $subscription ) || 'active' !== $subscription->status ) {
if ( empty( $subscription ) || 'active' !== $subscription->status || null === $customer ) {
return false;
}
$customer = $this->get_customer( $customer_id );
try {
$response = $customer->cancelSubscription( $subscription_id );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment