Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active December 10, 2023 16:11
Show Gist options
  • Save scottopolis/15c7b38277ab1ce7e434c8bed70689ed to your computer and use it in GitHub Desktop.
Save scottopolis/15c7b38277ab1ce7e434c8bed70689ed to your computer and use it in GitHub Desktop.
AppPresser In App Purchase integration for Paid Memberships Pro
<?php
/*
Plugin Name: AppPresser In App Purchases for Paid Memberships Pro
Plugin URI: https://apppresser.com
Description: This plugin listens for in app purchases or cancellations and adds/removes members from a membership level.
Version: 2.0.0
Author: Scott Bolinger
Author URI: https://apppresser.com
License: GPLv2
*/
class AppPresser_IAP_PMP {
// change this to your membership level ID
public static $membership_level_id = 1;
public static $instance = null;
/**
* Creates or returns an instance of this class.
* @since 1.0.0
* @return AppPresser A single instance of this class.
*/
public static function get() {
if ( self::$instance === null )
self::$instance = new self();
return self::$instance;
}
/**
*
* @since 1.0.0
*/
function __construct() {
self::hooks();
}
public function hooks() {
add_action( 'iap_new_purchase_request', array( $this, 'new_purchase' ), 10, 3 );
add_action( 'iap_subscription_cancel_request', array( $this, 'cancel_membership' ), 10, 3 );
}
/*
* This function is called when a new purchase is created. Add the user to the appropriate membership level here.
*/
public function new_purchase( $user_email, $purchase, $request ) {
AppPresser_Logger::log( 'New purchase from email: ', $user_email );
$user = get_user_by( 'email', $user_email );
if( !$user || is_wp_error( $user ) ) {
AppPresser_Logger::log( 'Cannot add to membership level because user does not exist.', $user_email );
return new WP_Error( 'rest_user_error',
__( 'Cannot get user for ' . $user_email, 'apppresser' ),
array(
'status' => 404,
)
);
}
$user_id = $user->ID;
if( !function_exists('pmpro_getMembershipLevelForUser') || !function_exists('pmpro_changeMembershipLevel') ) {
AppPresser_Logger::log( 'Cannot add to membership level because PMPro plugin is not active.', $user_email );
return new WP_Error( 'rest_plugin_error',
__( 'PMPro plugin not active.', 'apppresser' ),
array(
'status' => 404,
)
);
}
// is user already subscribed and active? If so, skip
$user_membership_level = pmpro_getMembershipLevelForUser( $user_id );
if( is_object( $user_membership_level ) ) {
$user_membership_level = $user_membership_level->ID;
}
if( $user_membership_level == self::$membership_level_id ) {
$membership_success = self::$membership_level_id;
AppPresser_Logger::log( 'User ' . $user_id . ' purchased but is already active on membership level.', self::$membership_level_id );
} elseif( !$user_membership_level || $user_membership_level != self::$membership_level_id ) {
// add user to level
//figure out start date
$now = current_time('timestamp');
$startdate = date("Y-m-d", $now);
$custom_level = array(
'user_id' => $user_id,
'membership_id' => self::$membership_level_id,
'code_id' => '',
'initial_payment' => '1.00',
'billing_amount' => '',
'cycle_number' => '',
'cycle_period' => '',
'billing_limit' => '',
'trial_amount' => '',
'trial_limit' => '',
'startdate' => $startdate,
'enddate' => ''
);
$change = pmpro_changeMembershipLevel( $custom_level, $user_id );
if( $change ) {
AppPresser_Logger::log( 'User ' . $user_id . ' added to membership level.', self::$membership_level_id );
} else {
AppPresser_Logger::log( 'User ' . $user_id . ' not added to membership level, unknown error.', serialize( $change ) );
}
}
}
/*
* This function is called when a membership is cancelled.
* A user can always click "restore membership" if their membership is cancelled in error.
*/
public function cancel_membership( $user_email, $purchase, $request ) {
$user = get_user_by( 'email', $user_email );
if( !$user || is_wp_error( $user ) ) {
AppPresser_Logger::log( 'Cannot cancel membership because user does not exist.', $user_email );
return new WP_Error( 'rest_user_error',
__( 'Cannot cancel membership, because cannot get user for ' . $user_email, 'apppresser' ),
array(
'status' => 404,
)
);
}
$user_id = $user->ID;
// change membership level to 0 to cancel
$cancel = pmpro_changeMembershipLevel( 0, $user_id );
AppPresser_Logger::log( 'Cancelled membership for ', $user_id );
}
}
AppPresser_IAP_PMP::get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment