Last active
September 16, 2022 13:35
-
-
Save girafffee/997e28f188bcb3455d608d0f0914e7df to your computer and use it in GitHub Desktop.
User case: change user role on PayPal webhooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Hands up! If you are using version 1.1.0 or higher. You won't need the code below. Just use new events | |
* @link https://user-images.githubusercontent.com/46720998/190651417-bba9c801-812c-4d8e-bbf4-d32075bc4fd7.png | |
* | |
* | |
* | |
* This gist is an extended version of this gist. | |
* @link https://gist.github.com/girafffee/040ff0ec2f148cd429d4a91cc7006c50#file-jfb-add-paypal-webhooks-php | |
* | |
* If your case involves a role change on confirmation/expiration of a subscription, | |
* then you only need to copy 2 files from this (extended) gist. | |
* | |
* You must define your roles in the JET_FB_SUBSCRIPTION_ROLE and JET_FB_BASE_ROLE constants | |
* | |
* This example works on version 1.0.1 | |
*/ | |
const JET_FB_SUBSCRIPTION_ROLE = 'subscription_buyer'; | |
const JET_FB_BASE_ROLE = 'subscriber'; | |
function jfb_add_paypal_webhooks( array $webhooks ) { | |
require_once 'jfb-paypal-listen-subscription.php'; | |
$webhooks[] = new JFB_Paypal_Cancel_Subscription_Webhook(); | |
$webhooks[] = new JFB_Paypal_Expired_Subscription_Webhook(); | |
$webhooks[] = new JFB_Paypal_Suspend_Subscription_Webhook(); | |
$webhooks[] = new JFB_Paypal_Reactive_Subscription_Webhook(); | |
return $webhooks; | |
} | |
add_filter( | |
'jet-form-builder/gateways/paypal/events', | |
'jfb_add_paypal_webhooks' | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Jet_FB_Paypal\Resources\Subscription; | |
class JFB_Paypal_Cancel_Subscription_Webhook extends \Jet_FB_Paypal\EventsHandlers\BillingSubscriptionCancelled { | |
use JFB_Update_Role_On_Disable_Subscription; | |
public function on_catch_event( $webhook_event ) { | |
// Subscription status changes here | |
$this->change_role( parent::on_catch_event( $webhook_event ) ); | |
} | |
} | |
class JFB_Paypal_Expired_Subscription_Webhook extends \Jet_FB_Paypal\EventsHandlers\BillingSubscriptionExpired { | |
use JFB_Update_Role_On_Disable_Subscription; | |
public function on_catch_event( $webhook_event ) { | |
// Subscription status changes here | |
$this->change_role( parent::on_catch_event( $webhook_event ) ); | |
} | |
} | |
class JFB_Paypal_Suspend_Subscription_Webhook extends \Jet_FB_Paypal\EventsHandlers\BillingSubscriptionSuspended { | |
use JFB_Update_Role_On_Disable_Subscription; | |
public function on_catch_event( $webhook_event ) { | |
// Subscription status changes here | |
$this->change_role( parent::on_catch_event( $webhook_event ) ); | |
} | |
} | |
class JFB_Paypal_Reactive_Subscription_Webhook extends \Jet_FB_Paypal\EventsHandlers\BillingSubscriptionActivated { | |
use JFB_Update_Role_On_Disable_Subscription; | |
public function on_catch_event( $webhook_event ) { | |
// Subscription status changes here | |
$this->change_role( parent::on_catch_event( $webhook_event ), JET_FB_SUBSCRIPTION_ROLE ); | |
} | |
} | |
trait JFB_Update_Role_On_Disable_Subscription { | |
public function change_role( array $pair, $role = JET_FB_BASE_ROLE ) { | |
/** @var Subscription $resource */ | |
list( $subscription, $resource ) = $pair; | |
/** @var \WP_User|null */ | |
$user = $resource->get_user(); | |
if ( ! $user ) { | |
return; | |
} | |
// Checking if a user has a role. | |
if ( in_array( $role, (array) $user->roles, true ) ) { | |
return; | |
} | |
$user->set_role( $role ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where to add this code. Will we add this code in fuction.php? Can you please give me a clear instruction?