class AppPresser_IAP_MemberPress {
// change this to subscription Level ID
public static $membership_level_id = 40865;
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 = 85.00;
$txn->total = 85.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("+1 years") );
$txn->store();
AppPresser_Logger::log( 'User added to MemberPress.', serialize( $txn ) );
$mbr_user->remove_role( 'email6' );
$mbr_user->remove_role( 'customer' );
$mbr_user->add_role( 'subscriber' );
}
/*
* 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 ) );
$mbr_user->remove_role( 'subscriber' );
$mbr_user->add_role( 'customer' );
}
}
}
AppPresser_IAP_MemberPress::get();