<?php
if(file_exists('../../../wp-load.php')) { include_once('../../../wp-load.php');}
else { echo "Error - unable to locate 'wp-load.php'"; exit(); }

// --------------------------------//
// Get info from wp_options table //
// ------------------------------//
$mailto = get_bloginfo(admin_email);
$subm_mbrs = get_option('subm_members_option');
$skey = $subm_mbrs[skey];    
    
// ----------------------------------------------//
// include proof-of-purchase validation routine //
// --------------------------------------------//
include_once("subm_popvalidate.php");
$flag=cbValid($skey);

if($flag) { // valid transaction
  global $wpdb;   
  $sub="SubscriptionMate (CB: $ctransaction) IPN"; 
  // ------------------------------ //
  // check for RFND (refund) status //
  // ------------------------------ //
  if($ctransaction == "SALE" || $ctransaction == "BILL") {
    // user creation is done in subm_members, use IPN to just notify vendor //
    $sub = "New Member/Sale Notification";
  }
  // ------------------------------ //
  // check for RFND (refund) status //
  // ------------------------------ //
  if($ctransaction == "RFND" || $ctransaction == "CGBK" || $ctransaction == "TEST_RFND") { 
    $cbreceipt = $ctransreceipt; // from subm_popvalidate.php ($_REQUEST[ctransreceipt]
    $q="SELECT user_id, meta_value FROM wp_usermeta WHERE meta_value='$cbreceipt'";
    $results = $wpdb->get_row($wpdb->prepare($q));
    $mbr_id = $results->user_id;
    if($mbr_id !="") {
      $user = new WP_User($mbr_id);     
      $user->set_role('subscriber'); // reset member's to default subscriber     
      $msg =  "$q\nTransaction: $ctransaction\nMember $mbr_id has been de-activated";
      // notify vendor of refund:
      $sub="SubscriptionMate (CB: $ctransaction) IPN";
    } else { // $mbr_id not found, alert vendor
      $msgbody = "$q\nID: $mbr_id\nTransaction: $ctransaction\nUser ID not found\nRcpt:$cbreceipt";
      // DEBUG --> wp_mail($mailto,"CB Refund IPN failed","Reason: $msgbody");
    }
  }
  
  if($ctransaction == "CANCEL-REBILL" ) {
    // TBL //
  }
  
// *** else for invalid transaction ///
} else {
  $sub="Invalid SubscriptionMate (CB) IPN";
  $msg = $flag;
}
// -------------------------//
// notify Vendor via email //
// -----------------------//
// DEBUG --> wp_mail("$mailto", "$sub", "$msg");
?>