Add user to MemberPress after in app purchase, and cancel for AppPresser
This file contains 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 | |
/* | |
Plugin Name: In App Purchases for MemberPress | |
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_MemberPress { | |
// change this to subscription Level ID | |
public static $membership_level_id = 1997; | |
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 = 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; | |
// is user already subscribed and active? If so, skip | |
// https://github.com/wp-premium/memberpress-developer/blob/master/app/models/MeprUser.php | |
$mbr_user = new MeprUser( $user_id ); | |
$subscribed = $mbr_user->is_already_subscribed_to( self::$membership_level_id ); | |
if( $subscribed && $mbr_user->is_active() ) { | |
AppPresser_Logger::log( 'New in app purchase but we did nothing because ' . $user_id . ' is already active.', $active ); | |
return; | |
} | |
// create the purchase in MemberPress | |
// https://github.com/wp-premium/memberpress-developer/blob/master/app/models/MeprTransaction.php | |
$txn = new MeprTransaction(); | |
$txn->amount = 10.00; | |
$txn->total = 10.00; | |
$txn->user_id = $user_id; | |
$txn->product_id = self::$membership_level_id; | |
// $txn->trans_num = $order->order_key; | |
$txn->status = 'complete'; | |
$txn->txn_type = 'payment'; | |
$txn->gateway = 'manual'; | |
$txn->created_at = date('c'); | |
$txn->expires_at = date('c', strtotime("+5 years") ); | |
$txn->store(); | |
AppPresser_Logger::log( 'User added to MemberPress.', serialize( $txn ) ); | |
} | |
/* | |
* This function is called when a membership is cancelled. We get this from the iOS subscription API notification, or in Android from the app itself. | |
* 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 because we cannot get the user.', $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; | |
// https://github.com/wp-premium/memberpress-developer/blob/master/app/models/MeprUser.php | |
$mbr_user = new MeprUser( $user_id ); | |
$transactions = $mbr_user->transactions_for_product( self::$membership_level_id, false, true ); | |
if( $transactions[0] ) { | |
$txn = new MeprTransaction( $transactions[0]->id ); | |
$txn->expires_at = date('c', strtotime( "now" ) ); | |
$txn->store(); | |
AppPresser_Logger::log( 'Cancelled user ' . $user_id, serialize( $txn ) ); | |
} | |
} | |
} | |
AppPresser_IAP_MemberPress::get(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment