Skip to content

Commit

Permalink
Separate Expiration and Cancellation
Browse files Browse the repository at this point in the history
Expiration should cancel subscription and membership
Cancellation should cancel subscription
  • Loading branch information
misfist committed Jul 15, 2024
1 parent d15e2b4 commit da3689d
Showing 1 changed file with 105 additions and 24 deletions.
129 changes: 105 additions & 24 deletions webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,15 @@

break;

case 'Expiration':
case 'Cancellation':

$subscription_id = sanitize_text_field( $response['subscriptionId'] );

$morder = new MemberOrder();
$morder->getLastMemberOrderBySubscriptionTransactionID( $subscription_id );
$morder->getMembershipLevel();
$morder->getUser();

if(pmpro_ccbill_RecurringCancel($morder))
pmpro_ccbill_Exit();
break;
pmpro_ccbill_Cancellation( $response );
pmpro_ccbill_Exit();
break;

case 'Expiration':
pmpro_ccbill_Expiration( $response );
pmpro_ccbill_Exit();
break;

case 'RenewalSuccess':
$status = 'success';
Expand Down Expand Up @@ -226,7 +222,7 @@ function pmpro_ccbill_ChangeMembershipLevel( $response, $morder ) {
}

/**
* Add Renewal Order
* Handle Renewal
*
* @param array $response
* @param string $status
Expand Down Expand Up @@ -348,23 +344,108 @@ function pmpro_ccbill_AddRenewal( array $response, $status = 'success' ) : void

}

function pmpro_ccbill_RecurringCancel( $morder ) {
/**
* Handle Cancellation
*
* @see https://ccbill.com/doc/webhooks-user-guide#ftoc-heading-23
*
* @param array $response
* @param string $status
* @return boolean
*/
function pmpro_ccbill_Cancellation( array $response ) : void {
global $pmpro_error;

$subscription_id = sanitize_text_field( $response['subscriptionId'] );

$morder = new MemberOrder();
$morder->getLastMemberOrderBySubscriptionTransactionID( $subscription_id );

$update_status = $morder->updateStatus( 'cancelled' );

if( $update_status ) {
// $email = new PMProEmail();
// $email->sendCancelEmail( $morder->user, $morder->membership_level->id );

$email = new PMProEmail();
$email->sendCancelAdminEmail( $morder->user, $morder->membership_level->id );

pmpro_ccbill_webhook_log( sprintf( __( "Subscription Cancelled (%s)", 'pmpro-ccbill' ), $subscription_id ) );
} else {
pmpro_ccbill_webhook_log( sprintf( __( "Subscription Cancellation (%s) failed.", 'pmpro-ccbill' ), $subscription_id ) );
}
}

/**
* Handle Expiration
*
* @see https://ccbill.com/doc/webhooks-user-guide#ftoc-heading-24
*
* @param array $response
* @return void
*/
function pmpro_ccbill_Expiration( array $response ) {
$subscription_id = sanitize_text_field( $response['subscriptionId'] );
$morder = new MemberOrder();
$morder->getLastMemberOrderBySubscriptionTransactionID( $subscription_id );

$subscription_cancelled = pmpro_ccbill_SubscriptionCancel( $subscription_id );
$membership_cancelled = pmpro_ccbill_MembershipCancel( $morder );
}

/**
* Cancel Subscription
*
* @param int $subscription_id
* @return boolean
*/
function pmpro_ccbill_SubscriptionCancel( $subscription_id ) : bool {
global $pmpro_error;
$worked = pmpro_cancelMembershipLevel( $morder->membership_level->id, $morder->user_id, 'inactive' );

if ( $worked === true ) {
//send an email to the member
$myemail = new PMProEmail();
$myemail->sendCancelEmail();
//send an email to the admin
$myemail = new PMProEmail();
$myemail->sendCancelAdminEmail( $morder->user, $morder->membership_level->id );

$morder = new MemberOrder();
$morder->getLastMemberOrderBySubscriptionTransactionID( $subscription_id );

$updated = $morder->updateStatus( 'cancelled' );

if( $updated ) {
// $email = new PMProEmail();
// $email->sendCancelEmail( $morder->user, $morder->membership_level->id );

// $email = new PMProEmail();
// $email->sendCancelAdminEmail( $morder->user, $morder->membership_level->id );

pmpro_ccbill_webhook_log( sprintf( __( "Subscription Cancelled (%s)", 'pmpro-ccbill'), $morder->csubscription_transaction_id ) );
pmpro_ccbill_webhook_log( sprintf( __( "Subscription Cancelled (%s)", 'pmpro-ccbill' ), $subscription_id ) );

return true;
} else {
pmpro_ccbill_webhook_log( sprintf( __( "Subscription Cancellation (%s) failed.", 'pmpro-ccbill' ), $subscription_id ) );

return false;
}
}

/**
* Cancel Membership
*
* @param object MemberOrder $morder
* @return bool
*/
function pmpro_ccbill_MembershipCancel( $morder ) : bool {
global $pmpro_error;

$cancelled = pmpro_cancelMembershipLevel( $morder->membership_level->id, $morder->user_id, 'inactive' );

if( $cancelled ) {
$email = new PMProEmail();
$email->sendCancelEmail( $morder->user, $morder->membership_level->id );

pmpro_ccbill_webhook_log( sprintf( __( "Membership Cancelled (%s) for (%s)", 'pmpro-ccbill' ), $morder->membership_level->id, $morder->user_id ) );

return true;

} else {
pmpro_ccbill_webhook_log( sprintf( __( "Membership Cancellation (%s) failed for (%s)", 'pmpro-ccbill' ), $morder->membership_level->id, $morder->user_id ) );

return false;
}
}
Expand Down

1 comment on commit da3689d

@misfist
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses strangerstudios#49

Please sign in to comment.