Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Created March 1, 2022 17:16
Show Gist options
  • Save scottopolis/f21bab810c628e0b5fa02cb230037cc6 to your computer and use it in GitHub Desktop.
Save scottopolis/f21bab810c628e0b5fa02cb230037cc6 to your computer and use it in GitHub Desktop.
AppPresser IAP Indeed Memberships
<?php
/*
Plugin Name: AppPresser In App Purchases for Indeed Memberships
Plugin URI: https://apppresser.com
Description: This plugin listens for in app purchases or cancellations and adds/removes members from a membership level.
Version: 1.0.0
Author: Scott Bolinger
Author URI: https://apppresser.com
License: GPLv2
*/
class AppPresser_IAP_Indeed {
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) {
$user_email = (isset($request['applicationUsername']) ? $request['applicationUsername'] : null);
AppPresser_Logger::log('New purchase from email: ', $user_email);
$user = get_user_by('email', $user_email);
if (!$user || is_wp_error($user)) {
return new WP_Error(
'rest_user_error',
__('Cannot get user for ' . $user_email, 'apppresser'),
array(
'status' => 404,
)
);
}
$user_id = $user->ID;
$product_id = $purchase['productId'];
$level_id = 3;
if (str_contains($product_id, 'monthly')) {
$level_id = 2;
}
if (function_exists('ihc_do_complete_level_assign_from_ap')) {
ihc_do_complete_level_assign_from_ap($user_id, $level_id, strtotime('now'), strtotime('+10 years'));
} else {
AppPresser_Logger::log('Function does not exist ihc_do_complete_level_assign_from_ap' . $user_id, 'Product id: ' . $purchase['productId']);
}
AppPresser_Logger::log('New in app purchase. User ' . $user_id . ' added to level ' . $level_id, "Product ID: " . $product_id);
}
/*
* 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_email = (isset($request['applicationUsername']) ? $request['applicationUsername'] : null);
$user = get_user_by('email', $user_email);
if (!$user || is_wp_error($user)) {
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;
AppPresser_Logger::log('Cancelling membership... ' . $user_id, 'Product id: ' . $purchase['productId']);
// this cancels all levels and changes role to suspended, we don't want that
// ihc_suspend_account($user_id);
// indeed-membership-pro/utilities.php line 6538
// remove the user from the membership level
if (function_exists('ihc_delete_user_level_relation')) {
ihc_delete_user_level_relation(self::$level_id, $user_id);
$cancel = new \Indeed\Ihc\CancelSubscription($user_id, self::$level_id);
$cancel->stopRedirectIfCase(true)->proceed();
} else {
AppPresser_Logger::log('Function does not exist ihc_delete_user_level_relation' . $user_id, 'Product id: ' . $purchase['productId']);
}
}
}
AppPresser_IAP_Indeed::get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment