Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active Jun 16, 2020
Embed
What would you like to do?
WooCommerce Memberships In App Purchases for AppPresser
<?php
/*
Plugin Name: In App Purchases for WooCommerce 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: 2.0.0
Author: Scott Bolinger
Author URI: https://apppresser.com
License: GPLv2
*/
class AppPresser_IAP_Woo {
// change this to subscription Level ID
public static $membership_level_id = 1748;
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 . ' variation id: ', $variation_id );
$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;
// is user already subscribed and active? If so, skip
// https://docs.woocommerce.com/document/woocommerce-memberships-function-reference/
$memberships = wc_memberships_get_user_active_memberships( $user_id );
if( ! empty( $memberships ) ) {
AppPresser_Logger::log( 'New in app purchase but we did nothing because ' . $user_id . ' is already active.', $active );
return;
}
// create the membership
$args = array(
// Enter the ID (post ID) of the plan to grant at registration
'plan_id' => self::$membership_level_id,
'user_id' => $user_id,
);
wc_memberships_create_user_membership( $args );
AppPresser_Logger::log( 'New in app purchase added to WooCommerce.', $args );
}
/*
* 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'] );
$user_membership = wc_memberships_get_user_membership( $user_id, self::$membership_level_id );
if( $user_membership ) {
$user_membership->cancel_membership();
AppPresser_Logger::log( 'Cancelled user ' . $user_id, $user_membership );
}
}
}
AppPresser_IAP_Woo::get();
@joelrendall

This comment has been minimized.

Copy link

@joelrendall joelrendall commented Jun 14, 2020

I noticed that this snippet isn't listed in the documentation. Does it still with with AP4 and the new in-app purchase system with fovea receipt validation? Thanks in advance :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment