<?PHP
// load WP
require_once("../wp-load.php");

// config
define("ipn_membership_role_level","2");
// IPN processor
class DS_IPN_membership
{
	public function __construct()
	{
		$ipn_data = $_REQUEST;
		// create WP user
		$user_id = wp_insert_user([
				"user_pass" => $ipn_data["order_id"],
				"user_login" => $ipn_data["buyer_email"],
				"user_email" => $ipn_data["buyer_email"],
				"first_name" => $ipn_data["address_first_name"],
				"last_name" => $ipn_data["address_last_name"],
				"user_nicename" => $ipn_data["buyer_email"],
				"show_admin_bar_front" => "false"
				]);
		
		$user = new WP_User($user_id);
		$user->set_role('subscriber');
		$member_id = SwpmMemberUtils::create_swpm_member_entry_from_array_data([
			"user_name"=>$ipn_data["buyer_email"],
			"first_name"=>$ipn_data["address_first_name"],
			"last_name"=>$ipn_data["address_last_name"],
			"password"=>$user->user_pass,
			"account_state"=>"active",
			"subscription_starts"=>date("Y-m-d"),
			"email"=>$ipn_data["buyer_email"],
			"member_since"=>date("Y-m-d"),
			"membership_level"=>ipn_membership_role_level
		]);
	
		die("OK");
		exit();
	}
}

new DS_IPN_membership();
