/**
 * Save member object after Multiple Membership Upgrade
 * replace upgrade mgm_member with old mgm_member
 * @param array $data
 * @param int $trans_id
 */
function mgm_multiple_upgrade_save_memberobject($data, $trans_id)
{
    if (empty($data['multiple_upgrade_prev_packid'])) {
        return;
    }
    if (isset($data['user_id'])) {
        $member = mgm_get_member($data['user_id']);
        if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
            // loop
            $prev_index = null;
            $new_index = null;
            foreach ($member->other_membership_types as $index => $member) {
                $member = mgm_convert_array_to_memberobj($member, $data['user_id']);
                //skip default values:
                //if(strtolower($memtypes->membership_type) == 'guest' || $memtypes->status == MGM_STATUS_NULL )	continue;
                if (strtolower($member->membership_type) == 'guest') {
                    continue;
                }
                // find previous member object index to be replaced
                if ($data['multiple_upgrade_prev_packid'] == $member->pack_id) {
                    $prev_index = $index;
                }
                // find newsly inserted member object index
                // consider only for success state
                if ($member->status == MGM_STATUS_ACTIVE && $data['pack_id'] == $member->pack_id) {
                    $new_index = $index;
                }
            }
            if (is_numeric($prev_index) && is_numeric($new_index)) {
                // swap old with new
                $member->other_membership_types[$prev_index] = $member->other_membership_types[$new_index];
                // remove old member object as it is an upgrade
                unset($member->other_membership_types[$new_index]);
                // save all
                $member->save();
                // update transacton - reset multiple_upgrade_prev_packid
                // to not consider next time
                // get
                $trans = mgm_get_transaction($trans_id);
                // reset
                $trans['data']['multiple_upgrade_prev_packid'] = '';
                // update
                mgm_update_transaction(array('data' => json_encode($trans['data'])), $trans_id);
            }
        }
    }
}
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     $member = mgm_get_member($user_id);
     // return url
     $return_url = add_query_arg(array('extra' => $tran_id), $this->setting['return_url']);
     // set data
     $data = array('processor_id' => $tran_id, 'Email' => $user->user_email);
     // address fields
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) { // old style
         // username/password
         if ($this->setting['send_userpass'] == 'yes') {
             $data['Username'] = $user->user_login;
             $data['Password'] = mgm_decrypt_password($member->user_password, $user_id);
         }
     }
     // urls
     $data['return_url'] = $return_url;
     $data['return_url_approve'] = add_query_arg(array('status' => 'success'), $return_url);
     $data['return_url_decline'] = add_query_arg(array('status' => 'error'), $return_url);
     // error url
     $data['return_url_error'] = add_query_arg(array('status' => 'error'), $return_url);
     // passthrough
     $data['extra'] = $tran_id;
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // data
     return $data;
 }
<?php

/** 
 * Notify user transaction merge/update
 */
global $wpdb;
//sql
$sql = "SELECT `id`,`data` FROM `" . TBL_MGM_TRANSACTION . "` WHERE `payment_type`='subscription_purchase' AND `status`='Active'";
//result
$results = $wpdb->get_results($sql);
//fetch
if ($results) {
    // loop
    foreach ($results as $row) {
        //check
        if (is_object($row)) {
            // decode
            $data = json_decode($row->data, true);
            //check
            if (isset($data['notify_user']) && $data['notify_user'] == true) {
                $data['notify_user'] = false;
                mgm_update_transaction(array('data' => json_encode($data)), $row->id);
            }
        }
    }
}
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // set data
     $data = array('instId' => $this->setting['inst_id'], 'cartId' => $tran_id, 'currency' => $pack['currency'], 'lang' => $this->setting['lang'], 'email' => $user->user_email, 'desc' => $item['name'], 'hideContact' => 0);
     // hideCurrency = 0
     // address fields
     $this->_set_address_fields($user, $data);
     // unit types
     $unit_types = array('d' => 1, 'w' => 2, 'm' => 3, 'y' => 4);
     // unit types
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // worldpay does not allow 1 time subscription @see #1278 issue notes
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) {// old style
         // type
         $data['M_payment_type'] = 'subscription';
         $data['option'] = 0;
         // check http://www.rbsworldpay.com/support/kb/bg/recurringpayments/rpfp.html -> Regular Agrrements -> Options
         $data['futurePayType'] = 'regular';
         // $data['startDate']   = date('Y-m-d', strtotime('+1 DAY'));@deprecated in favor of trial delay
         $data['startDelayMult'] = 1;
         // 1
         $data['startDelayUnit'] = 1;
         // day	To start the agreement 1 day after the agreement is setup
         // interval
         $data['intervalMult'] = $pack['duration'];
         $data['intervalUnit'] = $unit_types[$pack['duration_type']];
         // 1|3|4 = day|month|year
         // regular charge
         $data['normalAmount'] = $pack['cost'];
         // no of payments, limited
         if ((int) $pack['num_cycles'] > 0) {
             $data['noOfPayments'] = $pack['num_cycles'];
         }
         // trial
         if ($pack['trial_on']) {
             // set initialAmount as trial and normalAmount as recurring
             // #1223
             if ((double) $pack['trial_cost'] > 0.0) {
                 $data['amount'] = $pack['trial_cost'];
                 // instant payment
             }
             /*else{
             			$data['initialAmount'] = $pack['trial_cost'];// first payment
             		}*/
             $data['startDelayMult'] = $pack['trial_duration'];
             // to start the actual after trial settings
             $data['startDelayUnit'] = $unit_types[$pack['trial_duration_type']];
         }
         // signature field
         $amount_field = 'normalAmount';
     } else {
         // type of payment
         $data['M_payment_type'] = 'one-time';
         // signature field
         $amount_field = 'amount';
         // set amount
         $data['amount'] = $pack['cost'];
     }
     // signature
     if ($this->setting['md5_sig']) {
         $data['signatureFields'] = implode(':', array($amount_field, 'currency'));
         $data['signature'] = md5(implode(':', array($this->setting['md5_sig'], $pack['cost'], $pack['currency'])));
     }
     // custom
     $data['M_custom'] = $tran_id;
     // custom parameters:
     $trans_url = mgm_get_custom_url('transactions');
     // args
     $qry_args = array('module' => $this->code, 'method' => 'payment_return', 'transtatus' => 'success', 'transid' => mgm_encode_id($tran_id));
     // include success url to be parsed on Worldpay Gateway resultY.html(success) page
     // success thank you page
     $data['MC_success'] = add_query_arg($qry_args, $trans_url);
     //error page
     $qry_args['transtatus'] = 'error';
     // include error url to be parsed on Worldpay Gateway resultC.html(error/cancel) page
     $data['MC_error'] = add_query_arg($qry_args, $trans_url);
     $data['MC_redirectin'] = 7;
     //redirection in seconds
     $data['MC_sitename'] = get_option('blogname');
     //site name
     // test mode
     if ($this->status == 'test') {
         $data['testMode'] = 100;
     }
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // data
     return $data;
 }
 /**
  * set request params
  *  
  *
  * @param array $pack
  * @param int $tran_id
  * @return array
  */
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // secure
     $secure = array('USER' => $this->setting['username'], 'PWD' => $this->setting['password'], 'SIGNATURE' => $this->setting['signature'], 'VERSION' => '108.0');
     // cost
     $cost = $pack['cost'];
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // init
     $data = array('PAYMENTREQUEST_0_DESC' => substr($item['description'], 0, 127), 'PAYMENTREQUEST_0_AMT' => $cost, 'PAYMENTREQUEST_0_CURRENCYCODE' => $pack['currency'], 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Authorize', 'NOSHIPPING' => '1', 'METHOD' => 'SetExpressCheckout', 'INVNUM' => $tran_id, 'PAYMENTREQUEST_0_CUSTOM' => $tran_id, 'PAYMENTREQUEST_0_INVNUM' => $tran_id, 'ALLOWNOTE' => 0, 'ADDROVERRIDE' => 0, 'LOCALECODE' => $this->setting['locale'], 'EMAIL' => $user->user_email, 'SOLUTIONTYPE' => 'Sole', 'RETURNURL' => $this->_get_endpoint('return'), 'CANCELURL' => $this->setting['cancel_url'], 'PAYMENTREQUEST_0_NOTIFYURL' => $this->setting['notify_url']);
     // current date, takes 24 hours to activate
     $data['PROFILESTARTDATE'] = date('c');
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         $data['L_BILLINGTYPE0'] = 'RecurringPayments';
         $data['L_BILLINGAGREEMENTDESCRIPTION0'] = $item['name'];
         // required in recurrinhg for regular payment
         $data['MAXAMT'] = $cost;
         // recurring payments only
         // trial
         if ($pack['trial_on']) {
             // exprs
             $duration_exprs = mgm_get_class('subscription_packs')->get_duration_exprs();
             //array('d'=>'DAY','w' => 'WEEK', 'm'=>'MONTH', 'y'=>'YEAR' );
             $start_date = date('c', strtotime('+' . 1 * (int) $pack['trial_duration'] . ' ' . $duration_exprs[$pack['trial_duration_type']]));
             // shift start data
             $data['PROFILESTARTDATE'] = $start_date;
             // $data['PROFILESTARTDATE']     = date('c', (time() + $pack['trial_duration'] * 24 * 3600));
             // set trial data
             $data['TRIALBILLINGPERIOD'] = strtoupper($pack['trial_duration_type']);
             // D/M/Y
             $data['TRIALBILLINGFREQUENCY'] = $pack['trial_duration'];
             $data['TRIALAMT'] = $pack['trial_cost'];
             $data['TRIALTOTALBILLINGCYCLES'] = (int) $pack['trial_num_cycles'];
             // cost
             //if trail cost > pack cost not taking trail cost its taking pack cost so commented  condition
             //if( $pack['trial_cost'] <  $data['PAYMENTREQUEST_0_AMT'] )
             if (isset($pack['trial_cost'])) {
                 $data['PAYMENTREQUEST_0_AMT'] = $pack['trial_cost'];
                 // as per july 17, 2013 NV API Doc, page 71
             }
         }
     }
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // merge
     $data = array_merge($data, $secure);
     // return
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('VPSProtocol' => '2.23', 'TxType' => 'PAYMENT', 'Vendor' => $this->setting['vendor'], 'VendorTxCode' => $tran_id, 'Currency' => $pack['currency'], 'Description' => $item['name'], 'Apply3DSecure' => 0, 'ApplyAVSCV2' => 0, 'AllowGiftAid' => 0, 'FailureURL' => $this->setting['return_url'], 'CustomerEMail' => $user->user_email, 'Amount' => number_format($pack['cost'], 2, '.', ''));
     // additional fields
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) { // old style
         // nothing for sagepay
     }
     // custom/passthrough
     $custom = $tran_id;
     // set in success
     $data['SuccessURL'] = add_query_arg(array('custom' => $custom), $this->setting['return_url']);
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // actual form data
     $form_data = array_slice($data, 0, 3);
     // pluck first 3, VPSProtocol,TxType and Vendor
     // generate crypt
     $form_data['Crypt'] = $this->_crypt($data);
     // with data create, with no data parse
     // return data, crypted
     return $form_data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // user
     $user_id = $pack['user_id'];
     // add temp user meta
     update_user_option($user_id, 'mgm_1shoppingcart_tran_id', $tran_id, true);
     // add cookie for return tracking
     mgm_set_cookie_var('CK_USER_LAST_TRANSACTION_ID', $tran_id, '3 DAY');
     // set data
     $data = array('checkout_url' => $pack['product']['1shoppingcart_checkout_url']);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('sid' => $this->setting['sid'], 'product_name' => $item['name'], 'tco_currency' => $pack['currency'], 'lang' => $this->setting['lang'], 'pay_method' => 'CC', 'fixed' => 'Y', 'merchant_order_id' => $tran_id, 'return_url' => $this->setting['return_url'], 'email' => $user->user_email);
     // address fields, see parent for all fields, only different given here
     $this->_set_address_fields($user, $data);
     // product based
     if (isset($pack['product']['2checkout_product_id'])) {
         $data['product_id'] = $pack['product']['2checkout_product_id'];
         $data['quantity'] = 1;
     } else {
         // use total
         $data['total'] = $pack['cost'];
     }
     // test flag
     if ($this->status == 'test') {
         $data['demo'] = 'Y';
     }
     // custom passthrough
     $data['custom'] = $tran_id;
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // item
     $item = $this->get_pack_item($pack);
     // setup data array
     $data = array('business' => $this->setting['business_email'], 'invoice' => $tran_id, 'item_name' => $item['name'], 'no_shipping' => 1, 'no_note' => 1, 'currency_code' => $pack['currency'], 'lc' => $this->setting['locale'], 'notify_url' => $this->setting['notify_url'], 'cancel_return' => $this->setting['cancel_url'], 'rm' => (int) $this->setting['return_method'], 'cbt' => sprintf('Return to %s', get_option('blogname')), 'email' => $user->user_email);
     // address fields
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) { // old style
         // command
         $data['cmd'] = '_xclick-subscriptions';
         // subs
         $data['a3'] = $pack['cost'];
         $data['p3'] = $pack['duration'];
         $data['t3'] = strtoupper($pack['duration_type']);
         $data['src'] = 1;
         $data['sra'] = 1;
         // greater than 0
         if ($pack['num_cycles']) {
             $data['srt'] = $pack['num_cycles'];
         }
         // trial
         if ($pack['trial_on']) {
             $data['a1'] = $pack['trial_cost'];
             $data['p1'] = $pack['trial_duration'];
             $data['t1'] = strtoupper($pack['trial_duration_type']);
         }
     } else {
         // post purchase or one time billiing
         $data['cmd'] = '_xclick';
         $data['bn'] = 'PP-BuyNowBF';
         $data['amount'] = $pack['cost'];
         // Purchase Post Specifix
         if (isset($pack['buypost']) && $pack['buypost'] == 1) {
             $data['src'] = 0;
             $data['sra'] = 0;
         } else {
             // One time payment
             $data['src'] = 1;
             $data['sra'] = 1;
         }
         // apply addons
         $this->_apply_addons($pack, $data, array('amount' => 'amount', 'description' => 'item_name'));
         // known field => module $data field
     }
     // custom passthrough
     $data['custom'] = $tran_id;
     // set custom on request so that it can be tracked for post purchase
     $data['return'] = add_query_arg(array('custom' => $data['custom']), $this->setting['return_url']);
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     // set num_cycles from pack obejct if $pack doesn't contain num_cycles value
     if (!isset($pack['num_cycles']) && isset($pack['pack_id'])) {
         $subs_pack = mgm_get_class('subscription_packs')->get_pack($pack['pack_id']);
         $pack['num_cycles'] = $subs_pack['num_cycles'];
     }
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('ap_merchant' => $this->setting['ap_merchant'], 'ap_itemcode' => $tran_id, 'ap_itemname' => $item['name'], 'ap_description' => $item['name'], 'ap_currency' => $pack['currency'], 'ap_cancelurl' => $this->setting['cancel_url'], 'ap_amount' => $pack['cost'], 'ap_contactemail' => $user->user_email);
     // address fields,see parent for all fields, only different given here
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) { // old style
         // type
         $data['ap_purchasetype'] = 'subscription';
         // unit types
         $unit_types = array('d' => 'Day', 'w' => 'Week', 'm' => 'Month', 'y' => 'Year');
         // interval
         $data['ap_timeunit'] = $unit_types[$pack['duration_type']];
         // day|month|year
         $data['ap_periodlength'] = $pack['duration'];
         // greater than 0, limited
         if ((int) $pack['num_cycles'] > 0) {
             $data['ap_periodcount'] = $pack['num_cycles'];
         }
         // trial
         if ($pack['trial_on']) {
             $data['ap_trialamount'] = $pack['trial_cost'];
             $data['ap_trialperiodlength'] = $pack['trial_duration'];
             $data['ap_trialtimeunit'] = $unit_types[$pack['trial_duration_type']];
         }
     } else {
         // post purchase/one time billing
         $data['ap_purchasetype'] = 'item';
         // apply addons
         $this->_apply_addons($pack, $data, array('amount' => 'ap_amount', 'description' => 'ap_description'));
     }
     // custom passthrough
     $data['apc_1'] = $tran_id;
     // set apc on request so that it can be tracked for post purchase
     $data['ap_returnurl'] = add_query_arg(array('apc_1' => $data['apc_1']), $this->setting['return_url']);
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     // user email
     if (isset($pack['user_id'])) {
         $user_id = $pack['user_id'];
         $user = get_userdata($user_id);
         $user_email = $user->user_email;
     }
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('PARTNER' => $this->setting['partner'], 'VENDOR' => $this->setting['vendor'], 'USER' => $this->setting['username'], 'PWD' => $this->setting['password'], 'CURRENCY' => $pack['currency'], 'TRXTYPE' => 'S', 'INVNUM' => $tran_id, 'AMT' => $pack['cost'], 'CREATESECURETOKEN' => 'Y', 'VERBOSITY' => 'HIGH', 'SECURETOKENID' => mgm_create_token('unique', 36), 'COMMENT1' => $this->_remove_special_chars($item['name']));
     // additional fields
     if (isset($user)) {
         // email
         if (isset($user_email) && !empty($user_email)) {
             $data['EMAIL'] = $user_email;
         }
         $this->_set_address_fields($user, $data);
     }
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type'])) {
         // standard check
         //$data['BILLINGTYPE'] = 'RecurringBilling';// MerchantInitiatedBilling for BAID error as suggested by PayPal
         $data['L_BILLINGTYPE0'] = 'MerchantInitiatedBilling';
         $data['_TRAILER_PASSTHROUGH'] = 'Y';
     }
     // update currency - issue #1602
     /*		
     		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // strip
     $data = mgm_stripslashes_deep($data);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     // set data
     $data = array('co_code' => $this->setting['co_code'], 'pi_code' => isset($pack['product']['epoch_product_code']) ? $pack['product']['epoch_product_code'] : '', 'reseller' => $this->setting['reseller'], 'product_description' => $item['name'], 'response_post' => 'Y', 'pi_returnurl' => $this->setting['return_url'], 'email' => $user->user_email);
     // address fields
     $this->_set_address_fields($user, $data);
     // username/password
     if ($this->setting['send_userpass'] == 'yes') {
         $data['username'] = $user->user_login;
         $data['password'] = mgm_decrypt_password(mgm_get_member($user_id)->user_password, $user_id);
     } else {
         $data['no_userpass'] = 1;
         $data['no_userpassverify'] = 1;
     }
     // custom passthrough
     $data['x_custom'] = $tran_id;
     // passthrough field prefixed with x_
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _buy_membership($transid = null)
 {
     // packs
     $s_packs = mgm_get_class('subscription_packs');
     // get details
     //if transaction id is available:
     if (is_numeric($transid)) {
         $custom = $this->_get_transaction_passthrough($transid);
         extract($custom);
     } else {
         list($user_id, $duration, $duration_type, $pack_id) = explode('_', $_REQUEST['custom']);
         //mgm_log($_REQUEST['custom']);
     }
     // get pack
     $pack = $s_packs->get_pack($pack_id);
     // membership_type
     $membership_type = $pack['membership_type'];
     // check
     if ($user = get_userdata($user_id)) {
         // get member
         $member = mgm_get_member($user_id);
         // check
         if (!$member->duration) {
             $member->duration = $duration ? $duration : 1;
             // one year
             $member->duration_type = $duration_type ? $duration_type : 'y';
             $member->amount = 0.0;
             $member->currency = isset($pack['currency']) && !empty($pack['currency']) ? $pack['currency'] : 'USD';
             $member->membership_type = $membership_type;
         }
         // set pack
         $member->pack_id = $pack_id;
         $member->active_num_cycles = isset($num_cycles) && !empty($num_cycles) ? $num_cycles : $subs_pack['num_cycles'];
         // set status
         $member->status = MGM_STATUS_ACTIVE;
         $member->account_desc = __('Trial Account', 'mgm');
         $member->last_pay_date = '';
         // join date
         $time = time();
         // type expanded
         $duration_exprs = $s_packs->get_duration_exprs();
         // if not lifetime/date range
         if (in_array($member->duration_type, array_keys($duration_exprs))) {
             // take only date exprs
             // time
             $time = strtotime("+{$member->duration} {$duration_exprs[$member->duration_type]}", $time);
             // formatted
             $time_str = date('Y-m-d', $time);
             // date extended
             if (!$member->expire_date || strtotime($time_str) > strtotime($member->expire_date)) {
                 $member->expire_date = $time_str;
             }
         } else {
             //if lifetime:
             if ($member->duration_type == 'l') {
                 // el = lifetime
                 $member->expire_date = '';
             }
             //issue #1096
             if ($member->duration_type == 'dr') {
                 // el = /date range
                 $member->expire_date = $duration_range_end_dt;
             }
         }
         // whether to subscriber the user to Autoresponder - This should happen only once
         $acknowledge_ar = mgm_subscribe_to_autoresponder($member, $transid);
         // update
         $member->save();
         // on status - issue #1468
         switch ($member->status) {
             case MGM_STATUS_ACTIVE:
                 //sending notification email to user
                 if ($notify_user && $is_registration == 'Y') {
                     $user_pass = mgm_decrypt_password($member->user_password, $user_id);
                     do_action('mgm_register_user_notification', $user_id, $user_pass);
                 }
                 break;
         }
         // role
         if (isset($role)) {
             $obj_role = new mgm_roles();
             $obj_role->add_user_role($user_id, $role);
         }
         // update pack/transaction
         if (is_numeric($transid)) {
             mgm_update_transaction(array('module' => $this->module, 'status_text' => __('Success', 'mgm')), $transid);
         }
         // return action
         do_action('mgm_return_' . $this->module, array('user_id' => $user_id));
         //issue#: 343
         do_action('mgm_unpaid_autoresponder', array('user_id' => $user_id));
         // autoresponder
         do_action('mgm_return_subscription_payment_' . $this->module, array('user_id' => $user_id));
         //
         //issue#: 666
         do_action('mgm_return_subscription_payment', array('user_id' => $user_id, 'acknowledge_ar' => $acknowledge_ar, 'mgm_member' => $member));
         // new, global: pass mgm_member object to consider multiple level purchases as well.
     }
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system
     $system_obj = mgm_get_class('system');
     $s_packs = mgm_get_class('subscription_packs');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     // set data
     $data = array('TrxnNumber' => $tran_id, 'CustomerInvoiceDescription' => $item['name'], 'CustomerEmail' => $user->user_email);
     // additional fields
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && isset($pack['duration_type'])) { // old style
         $data['RecurringBilling'] = 'TRUE';
         //get current datetime for eWay
         $data['EwayTime'] = $eway_time = $this->_datetime_to_eway_serverdatetime(date('Y-m-d H:i:s'));
         // cust id
         $data['CustomerRef'] = $user_id;
         // interval types
         $interval_types = array('d' => '1', 'w' => '2', 'm' => '3', 'y' => '4');
         // exprs
         $data['DurationExprs'] = $duration_exprs = $s_packs->get_duration_exprs();
         //array('d'=>'DAY','w' => 'WEEK', 'm'=>'MONTH', 'y'=>'YEAR' );
         // first payment amount
         $data['RebillInitAmt'] = 0;
         // first payment date
         $data['RebillInitDate'] = date('d/m/Y', strtotime($eway_time));
         // today
         // normal recur amount
         $data['RebillRecurAmt'] = number_format($pack['cost'] * 100, 0, '.', '');
         //send price in cents: $1 = 100 cents
         $data['RebillStartDate'] = date('d/m/Y', strtotime($eway_time));
         // today
         $data['RebillInterval'] = $pack['duration'];
         $data['RebillIntervalType'] = $interval_types[$pack['duration_type']];
         // days|months|years
         // recurring ongoing =0 must be set as 9999, or integer 1-99
         // occurrences: greater than 0
         if ($pack['num_cycles']) {
             // limited
             $end_ts = '+' . (int) $pack['num_cycles'] * (int) $pack['duration'] . ' ' . $duration_exprs[$pack['duration_type']];
         } else {
             // ongoing
             // $end_ts = '+99 ' . $intv_types_ts[$pack['duration_type']];
             $end_ts = '+10 YEAR';
             // ongoing, 10 years ahead as per iss#645
         }
         // recurring end date
         $data['RebillEndDate'] = date('d/m/Y', strtotime($end_ts, strtotime($eway_time)));
         // temp var
         $data['TrialOn'] = $pack['trial_on'];
         // trial on
         if ($pack['trial_on']) {
             // init amount
             $data['RebillInitAmt'] = number_format($pack['trial_cost'] * 100, 0, '.', '');
             // 0 or greater
             // update RebillStartDate, it will be later than Trial init date
             $trial_end_ts = '+' . (int) $pack['trial_num_cycles'] * (int) $pack['trial_duration'] . ' ' . $duration_exprs[$pack['trial_duration_type']];
             // update
             $data['RebillStartDate'] = date('d/m/Y', strtotime($trial_end_ts, strtotime($eway_time)));
         } else {
             $data['RebillStartDate'] = date('d/m/Y', strtotime('+' . 1 * (int) $pack['duration'] . ' ' . $duration_exprs[$pack['duration_type']]));
         }
     } else {
         // post purchase
         $data['RecurringBilling'] = 'FALSE';
         // set price for addons
         $data['TotalAmount'] = $pack['cost'];
         // apply addons
         $this->_apply_addons($pack, $data, array('amount' => 'TotalAmount', 'description' => 'CustomerInvoiceDescription'));
         // set
         $data['TotalAmount'] = number_format($data['TotalAmount'] * 100, 0, '.', '');
         // total amount
     }
     // custom
     $data['Option1'] = $tran_id;
     // strip
     $data = mgm_stripslashes_deep($data);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _buy_membership()
 {
     // system
     $system_obj = mgm_get_class('system');
     $s_packs = mgm_get_class('subscription_packs');
     $dge = bool_from_yn($system_obj->get_setting('disable_gateway_emails'));
     $dpne = bool_from_yn($system_obj->get_setting('disable_payment_notify_emails'));
     // get passthrough, stop further process if fails to parse
     $custom = $this->_get_transaction_passthrough($_POST['custom']);
     // local var
     extract($custom);
     // currency
     if (!$currency) {
         $currency = $system_obj->get_setting('currency');
     }
     // find user
     $user = get_userdata($user_id);
     //another_subscription modification
     if (isset($custom['is_another_membership_purchase']) && bool_from_yn($custom['is_another_membership_purchase'])) {
         $member = mgm_get_member_another_purchase($user_id, $custom['membership_type']);
     } else {
         $member = mgm_get_member($user_id);
     }
     // Get the current AC join date
     if (!($join_date = $member->join_date)) {
         $member->join_date = time();
     }
     // Set current AC join date
     // if there is no duration set in the user object then run the following code
     if (empty($duration_type)) {
         //if there is no duration type then use Months
         $duration_type = 'm';
     }
     // membership type default
     if (empty($membership_type)) {
         //if there is no account type in the custom string then use the existing type
         $membership_type = md5($member->membership_type);
     }
     // validate parent method
     $membership_type_verified = $this->_validate_membership_type($membership_type, 'md5|plain');
     // verified
     if (!$membership_type_verified) {
         if (strtolower($member->membership_type) != 'free') {
             // notify admin, only if gateway emails on
             if (!$dge) {
                 mgm_notify_admin_membership_verification_failed($this->name);
             }
             // abort
             return;
         } else {
             $membership_type_verified = $member->membership_type;
         }
     }
     // set
     $membership_type = $membership_type_verified;
     // sub pack
     $subs_pack = $s_packs->get_pack($pack_id);
     // if trial on
     if ($subs_pack['trial_on']) {
         $member->trial_on = $subs_pack['trial_on'];
         $member->trial_cost = $subs_pack['trial_cost'];
         $member->trial_duration = $subs_pack['trial_duration'];
         $member->trial_duration_type = $subs_pack['trial_duration_type'];
         $member->trial_num_cycles = $subs_pack['trial_num_cycles'];
     }
     //pack currency over rides genral setting currency - issue #1602
     if (isset($subs_pack['currency']) && $subs_pack['currency'] != $currency) {
         $currency = $subs_pack['currency'];
     }
     // duration
     $member->duration = $duration;
     $member->duration_type = strtolower($duration_type);
     $member->amount = $amount;
     $member->currency = $currency;
     $member->membership_type = $membership_type;
     $member->pack_id = $pack_id;
     // $member->payment_type = 'subscription';
     $member->active_num_cycles = isset($num_cycles) && !empty($num_cycles) ? $num_cycles : $subs_pack['num_cycles'];
     $member->payment_type = (int) $member->active_num_cycles == 1 ? 'one-time' : 'subscription';
     // payment info for unsubscribe
     $member->payment_info = new stdClass();
     $member->payment_info->module = $this->code;
     // transaction type
     $member->payment_info->txn_type = 'subscription';
     // subscriber id
     $member->payment_info->subscr_id = '';
     // refer id
     $member->payment_info->txn_id = '';
     // mgm transaction id
     $member->transaction_id = $_POST['custom'];
     // process response
     $new_status = $update_role = false;
     // process
     $process = 'Success';
     // status
     switch ($process) {
         case 'Success':
             // set pending status
             $new_status = MGM_STATUS_PENDING;
             // starus string
             $member->status_str = __('Last payment is pending', 'mgm');
             $time = time();
             $last_pay_date = isset($member->last_pay_date) ? $member->last_pay_date : null;
             $member->last_pay_date = date('Y-m-d', $time);
             // for upgrading
             if ($member->status == MGM_STATUS_ACTIVE) {
                 // mark for later status reset
                 $member->status_reset_on = date('Y-m-d', strtotime($member->expire_date));
             }
             // check subscription_option
             if (isset($subscription_option)) {
                 // on option
                 switch ($subscription_option) {
                     // @ToDo, apply expire date login
                     case 'create':
                         // expire date will be based on current time
                     // expire date will be based on current time
                     case 'upgrade':
                         // expire date will be based on current time
                         // already on top
                         break;
                     case 'downgrade':
                         // expire date will be based on expire_date if exists, current time other wise
                     // expire date will be based on expire_date if exists, current time other wise
                     case 'extend':
                         // expire date will be based on expire_date if exists, current time other wise
                         // extend/expire date
                         // calc expiry	- issue #1226
                         // membership extend functionality broken if we try to extend the same day so removed && $last_pay_date != date('Y-m-d', $time) check
                         if (!empty($member->expire_date)) {
                             $expiry = strtotime($member->expire_date);
                             if ($expiry > 0 && $expiry > $time) {
                                 $time = $expiry;
                             }
                         }
                         break;
                 }
             }
             // type expanded
             $duration_exprs = $s_packs->get_duration_exprs();
             // if not lifetime/date range
             if (in_array($member->duration_type, array_keys($duration_exprs))) {
                 // take only date exprs
                 //consider trial duration if trial period is applicable
                 if (isset($trial_on) && $trial_on == 1) {
                     //Do it only once
                     if (!isset($member->rebilled) && isset($member->active_num_cycles) && $member->active_num_cycles != 1) {
                         $time = strtotime("+{$trial_duration} {$duration_exprs[$trial_duration_type]}", $time);
                     }
                 } else {
                     // time - issue #1068
                     $time = strtotime("+{$member->duration} {$duration_exprs[$member->duration_type]}", $time);
                 }
                 // formatted
                 $time_str = date('Y-m-d', $time);
                 // date extended
                 if (!$member->expire_date || strtotime($time_str) > strtotime($member->expire_date)) {
                     $member->expire_date = $time_str;
                 }
             } else {
                 //if lifetime:
                 if ($member->duration_type == 'l') {
                     // el = lifetime
                     $member->expire_date = '';
                 }
                 //issue #1096
                 if ($member->duration_type == 'dr') {
                     // el = /date range
                     $member->expire_date = $duration_range_end_dt;
                 }
             }
             //update rebill: issue #: 489
             if ($member->active_num_cycles != 1 && (int) $member->rebilled < (int) $member->active_num_cycles) {
                 // rebill
                 $member->rebilled = !$member->rebilled ? 1 : (int) $member->rebilled + 1;
             }
             // role update
             if ($role) {
                 $update_role = true;
             }
             // transaction_id
             $transaction_id = $this->_get_transaction_id();
             // hook args
             $args = array('user_id' => $user_id, 'transaction_id' => $transaction_id);
             // another membership
             if (isset($custom['is_another_membership_purchase']) && bool_from_yn($custom['is_another_membership_purchase'])) {
                 $args['another_membership'] = $custom['membership_type'];
             }
             // after succesful payment hook
             do_action('mgm_membership_transaction_success', $args);
             // backward compatibility
             do_action('mgm_subscription_purchase_payment_success', $args);
             // new organized name
             do_action('mgm_return_subscription_payment_' . $this->module, $args);
             // autoresponder
             break;
         case 'Denied':
             $new_status = MGM_STATUS_NULL;
             $member->status_str = __('Last payment was refunded or denied', 'mgm');
             break;
         case 'Pending':
             $new_status = MGM_STATUS_PENDING;
             $reason = 'Unknown';
             $member->status_str = sprintf(__('Last payment is pending. Reason: %s', 'mgm'), $reason);
             break;
         default:
             $new_status = MGM_STATUS_ERROR;
             $member->status_str = sprintf(__('Last payment status: %s', 'mgm'), $_POST['payment_status']);
             break;
     }
     // old status
     $old_status = $member->status;
     // set new status
     $member->status = $new_status;
     // whether to acknowledge the user - This should happen only once
     $acknowledge_user = $this->send_payment_email($_POST['custom']);
     // whether to subscriber the user to Autoresponder - This should happen only once
     $acknowledge_ar = mgm_subscribe_to_autoresponder($member, $_POST['custom']);
     //another_subscription modification
     if (isset($custom['is_another_membership_purchase']) && bool_from_yn($custom['is_another_membership_purchase'])) {
         //issue #1227
         if ($subs_pack['hide_old_content']) {
             $member->hide_old_content = $subs_pack['hide_old_content'];
         }
         mgm_save_another_membership_fields($member, $user_id);
         // Multiple membership upgrade: first time
         if (isset($member->transaction_id) && isset($custom['multiple_upgrade_prev_packid']) && is_numeric($custom['multiple_upgrade_prev_packid'])) {
             mgm_multiple_upgrade_save_memberobject($custom, $member->transaction_id);
         }
     } else {
         $member->save();
     }
     // status change event
     do_action('mgm_user_status_change', $user_id, $new_status, $old_status, 'module_' . $this->module, $member->pack_id);
     //update coupon usage
     do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
     // role
     if ($update_role) {
         $obj_role = new mgm_roles();
         $obj_role->add_user_role($user_id, $role);
     }
     // update transaction
     if (is_numeric($member->transaction_id)) {
         mgm_update_transaction(array('module' => $this->module, 'status' => $member->status, 'status_text' => $member->status_str), $member->transaction_id);
     }
     // return action
     do_action('mgm_return_' . $this->module, array('user_id' => $user_id));
     // backward compatibility
     do_action('mgm_return_subscription_payment_' . $this->module, array('user_id' => $user_id));
     // new , individual
     do_action('mgm_return_subscription_payment', array('user_id' => $user_id, 'acknowledge_ar' => $acknowledge_ar, 'mgm_member' => $member));
     // new, global
     // read member again for internal updates if any
     // another_subscription modification
     if (isset($custom['is_another_membership_purchase']) && bool_from_yn($custom['is_another_membership_purchase'])) {
         $member = mgm_get_member_another_purchase($user_id, $custom['membership_type']);
     } else {
         $member = mgm_get_member($user_id);
     }
     // send email notification to client
     $blogname = get_option('blogname');
     // notify
     if ($acknowledge_user) {
         // notify user, only if gateway emails on
         if (!$dpne) {
             // notify
             if (mgm_notify_user_membership_purchase($blogname, $user, $member, $custom, $subs_pack, $s_packs, $system_obj)) {
                 // update as email sent
                 $this->update_paymentemail_sent($alt_tran_id);
             }
         }
         // notify admin, only if gateway emails on
         if (!$dge) {
             // pack duration
             $pack_duration = $s_packs->get_pack_duration($subs_pack);
             // notify admin,
             mgm_notify_admin_membership_purchase($blogname, $user, $member, $pack_duration);
         }
     }
 }
/**
 * Send Email Notification to User on Membership Purchase
 *
 * @uses mgm_notify_user()
 * @param string $blogname
 * @param object $user
 * @param object $member
 * @param array $custom
 * @param array $subs_pack
 * @param object $s_packs
 * @param object $system_obj
 * @return bool $send
 */
function mgm_notify_user_membership_purchase($blogname, $user, $member, $custom, $subs_pack, $s_packs, $system_obj)
{
    // local var
    extract($custom);
    // on status
    switch ($member->status) {
        case MGM_STATUS_ACTIVE:
            //Sending notification email to user - issue #1468
            if (isset($notify_user) && isset($is_registration) && bool_from_yn($is_registration)) {
                // get pass
                $user_pass = mgm_decrypt_password($member->user_password, $user->ID);
                // action
                // send notification only once - issue #1601
                if ($system_obj->setting['enable_new_user_email_notifiction_after_user_active'] == 'Y' && $notify_user) {
                    //check - issue #1794
                    if (isset($member->transaction_id) && $member->transaction_id > 0) {
                        $trans = mgm_get_transaction($member->transaction_id);
                        $trans['data']['notify_user'] = false;
                        mgm_update_transaction(array('data' => json_encode($trans['data'])), $member->transaction_id);
                    }
                    //notify
                    do_action('mgm_register_user_notification', $user->ID, $user_pass);
                }
            }
            //sending upgrade notifaction email to admin
            if (isset($subscription_option) && $subscription_option == 'upgrade') {
                do_action('mgm_user_upgrade_notification', $user_id);
            }
            // init
            $subscription = '';
            // add trial
            if (isset($subs_pack['trial_on']) && (int) $subs_pack['trial_on'] == 1) {
                // trial
                $subscription = sprintf('%1$s %2$s for the first %3$s %4$s,<br> then ', $member->trial_cost, $member->currency, $member->trial_duration * $member->trial_num_cycles, $s_packs->get_pack_duration($subs_pack, true));
            }
            // on type
            if ($member->payment_type == 'subscription') {
                $payment_type = 'recurring subscription';
                $subscription .= sprintf('%1$s %2$s for each %3$s %4$s, %5$s', $member->amount, $member->currency, $member->duration, $s_packs->get_pack_duration($subs_pack), (int) $member->active_num_cycles > 0 ? sprintf('for %d installments', (int) $member->active_num_cycles) : 'until cancelled');
            } else {
                $payment_type = 'one-time payment';
                $subscription .= sprintf('%1$s %2$s for %3$s %4$s', $member->amount, $member->currency, $member->duration, $s_packs->get_pack_duration($subs_pack));
            }
            // subject
            $subject = $system_obj->get_template('payment_success_email_template_subject', array('blogname' => $blogname), true);
            // data
            $data = array('blogname' => $blogname, 'name' => $user->display_name, 'email' => $user->user_email, 'payment_type' => $payment_type, 'subscription' => $subscription, 'admin_email' => $system_obj->get_setting('admin_email'));
            // message
            $message = $system_obj->get_template('payment_success_subscription_email_template_body', $data, true);
            break;
        case MGM_STATUS_NULL:
            // subject
            $subject = $system_obj->get_template('payment_failed_email_template_subject', array('blogname' => $blogname), true);
            // data
            $data = array('blogname' => $blogname, 'name' => $user->display_name, 'email' => $user->user_email, 'payment_type' => 'subscription payment', 'reason' => $member->status_str, 'admin_email' => $system_obj->get_setting('admin_email'));
            // message
            $message = $system_obj->get_template('payment_failed_email_template_body', $data, true);
            break;
        case MGM_STATUS_PENDING:
            // subject
            $subject = $system_obj->get_template('payment_pending_email_template_subject', array('blogname' => $blogname), true);
            // data
            $data = array('blogname' => $blogname, 'name' => $user->display_name, 'email' => $user->user_email, 'reason' => $member->status_str, 'admin_email' => $system_obj->get_setting('admin_email'));
            // body
            $message = $system_obj->get_template('payment_pending_email_template_body', $data, true);
            break;
        case MGM_STATUS_ERROR:
            // subject
            $subject = $system_obj->get_template('payment_error_email_template_subject', array('blogname' => $blogname), true);
            // data
            $data = array('blogname' => $blogname, 'name' => $user->display_name, 'email' => $user->user_email, 'reason' => $member->status_str, 'admin_email' => $system_obj->get_setting('admin_email'));
            // body
            $message = $system_obj->get_template('payment_error_email_template_body', $data, true);
            break;
    }
    // replace tags
    $subject = mgm_replace_email_tags($subject, $user->ID);
    $message = mgm_replace_email_tags($message, $user->ID);
    // return
    return @mgm_notify_user($user->user_email, $subject, $message);
}
 /**
  *  get button data
  * 
  ** @see https://pagseguro.uol.com.br/v2/guia-de-integracao/pagamento-via-html.html/
  */
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('receiverEmail' => $this->setting['receiver_email'], 'reference' => $tran_id, 'itemId1' => $item['id'], 'itemDescription1' => substr($item['name'], 0, 100), 'itemAmount1' => number_format($pack['cost'], 2, '.', ''), 'itemQuantity1' => 1, 'currency' => $pack['currency'], 'senderEmail' => $user->user_email, 'encoding' => 'UTF-8');
     // address fields
     $this->_set_address_fields($user, $data);
     // custom passthrough
     $data['custom'] = $tran_id;
     // old v1
     // set custom on request so that it can be tracked for post purchase
     // $data['notify_url']    = $this->setting['notify_url'];
     // $data['return']        = add_query_arg(array('custom'=>$data['custom']), $this->setting['return_url']);
     // $data['cancel_return'] = $this->setting['cancel_url'];
     // new v2
     $data['notificationURL'] = $this->setting['notify_url'];
     $data['redirectURL'] = add_query_arg(array('custom' => $data['custom']), $this->setting['return_url']);
     $data['cancelURL'] = $this->setting['cancel_url'];
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     $member = mgm_get_member($user_id);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('clientAccnum' => $this->setting['client_acccnum'], 'clientSubacc' => $this->setting['client_subacc']);
     // upgrade
     if ($this->setting['upgrade_api'] == 'upgrade' && ($pack['subscription_option'] == 'upgrade' || $pack['subscription_option'] == 'purchase_another')) {
         // encode
         $data['enc'] = $this->_create_upgrade_enc($pack, $data, $tran_id);
     } else {
         // merge
         // formname
         $formname = isset($pack['product']['ccbill_formname']) && !empty($pack['product']['ccbill_formname']) ? $pack['product']['ccbill_formname'] : $this->setting['formname'];
         // data
         $data = array_merge($data, array('formName' => $formname, 'language' => 'English', 'email' => $user->user_email));
         // address fields
         $this->_set_address_fields($user, $data);
         // subscription purchase with ongoing/limited
         if (!isset($pack['buypost']) && isset($pack['duration_type'])) {
             // supports one-time recurring
             // if ($pack['num_cycles'] != 1 && isset($pack['duration_type']) ) { // old style
             // old functionality: using subscription id
             if (isset($pack['product']['ccbill_substype_id']) && !empty($pack['product']['ccbill_substype_id'])) {
                 // format type id
                 $typeid_fmt = str_pad(trim($pack['product']['ccbill_substype_id']), 10, '0', STR_PAD_LEFT);
                 // iso currency
                 $currency_iso = mgm_get_currency_iso4217($pack['currency']);
                 // set
                 $data['subscriptionTypeId'] = implode(':', array($typeid_fmt, $currency_iso));
                 $data['allowedTypes'] = $data['subscriptionTypeId'];
             } elseif (isset($this->setting['dynamic_pricing']) && $this->setting['dynamic_pricing'] == 'enabled') {
                 // use dynamic pricing
                 // vaidate
                 $this->_valididate_dynamic_pricing(false);
                 // types
                 $intv_types = array('d' => 1, 'w' => 7, 'm' => 30, 'y' => 365);
                 // currency
                 $data['currencyCode'] = mgm_get_currency_iso4217($pack['currency']);
                 // #1086 iso integer code code for currency
                 // price
                 $data['formPrice'] = number_format($pack['cost'], 2, '.', '');
                 //issue #1666
                 if (isset($pack['num_cycles']) && (int) $pack['num_cycles'] > 0) {
                     $data['formPeriod'] = $intv_types[strtolower($pack['duration_type'])] * $pack['duration'] * $pack['num_cycles'];
                     // 2;// in days
                 } else {
                     $data['formPeriod'] = $intv_types[strtolower($pack['duration_type'])] * $pack['duration'];
                 }
                 // trial
                 if ($pack['trial_on']) {
                     $data['formPrice'] = number_format($pack['trial_cost'], 2, '.', '');
                     $data['formPeriod'] = $intv_types[$pack['trial_duration_type']] * $pack['trial_duration'] * $pack['trial_num_cycles'];
                     //in days
                 }
                 //issue #1739
                 if ($pack['num_cycles'] != 1) {
                     // cost
                     $data['formRecurringPrice'] = $data['formPrice'];
                     $data['formRecurringPeriod'] = $intv_types[$pack['duration_type']] * $pack['duration'];
                     //number of days
                     $data['formRebills'] = isset($pack['num_cycles']) && (int) $pack['num_cycles'] > 0 ? (int) $pack['num_cycles'] : 99;
                     // iteration(99=infinite)
                 }
                 $data['formDigest'] = md5($data['formPrice'] . $data['formPeriod'] . $data['formRecurringPrice'] . $data['formRecurringPeriod'] . $data['formRebills'] . $data['currencyCode'] . $this->setting['md5_hashsalt']);
             }
             // send mgm created username/password
             if ($this->setting['send_userpass'] == 'yes') {
                 // username
                 $data['username'] = $user->user_login;
                 $data['password'] = mgm_decrypt_password($member->user_password, $user_id);
             }
         } else {
             // post purchase
             if (isset($this->setting['dynamic_pricing']) && $this->setting['dynamic_pricing'] == 'enabled') {
                 // vaidate
                 $this->_valididate_dynamic_pricing(false);
                 // set price for addons
                 $data['formPrice'] = $pack['cost'];
                 // apply addons
                 $this->_apply_addons($pack, $data, array('amount' => 'formPrice'));
                 // price
                 $data['formPrice'] = number_format($data['formPrice'], 2, '.', '');
                 $data['formPeriod'] = 2;
                 // in days @todo manage via settings
                 $data['currencyCode'] = mgm_get_currency_iso4217($pack['currency']);
                 // #1086 iso integer code code for currency
                 $data['formDigest'] = md5($data['formPrice'] . $data['formPeriod'] . $data['currencyCode'] . $this->setting['md5_hashsalt']);
             }
         }
         // custom passthrough
         $data['custom'] = $tran_id;
     }
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('invnum' => $tran_id, 'description' => $item['name'], 'currencycode' => $pack['currency'], 'notifyurl' => $this->setting['notify_url'], 'email' => $user->user_email, 'amount' => $pack['cost']);
     // additional fields
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) { // old style
         // recurring
         $data['recurring_billing'] = 'TRUE';
         // types
         $bp_types = array('d' => 'Day', 'm' => 'Month', 'y' => 'Year');
         // start date
         $data['start_date'] = date(DATE_ATOM);
         // Mon, 15 Aug 2005 15:12:46 UTC
         // billing period
         $data['billing_period'] = $bp_types[$pack['duration_type']];
         // Day|Month|Year
         $data['billing_frequency'] = $pack['duration'];
         // 3|12 etc.
         // greater than 0, limited
         if ((int) $pack['num_cycles'] > 0) {
             $data['total_billing_cycles'] = $pack['num_cycles'];
         }
         // trial
         if ($pack['trial_on']) {
             $data['trial_billing_period'] = $bp_types[$pack['trial_duration_type']];
             $data['trial_billing_frequency'] = $pack['trial_duration'];
             // greated than 0, limited cycle
             if ((int) $pack['trial_num_cycles'] > 0) {
                 // need this checked
                 $data['trial_total_billing_cycles'] = $pack['trial_num_cycles'];
             }
             // cost
             $data['trial_amt'] = $pack['trial_cost'];
         }
     } else {
         // post purchase/one time billing
         $data['recurring_billing'] = 'FALSE';
     }
     // custom passthrough
     $data['custom'] = $tran_id;
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // strip
     $data = mgm_stripslashes_deep($data);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     // set data
     $data = array('invoice_num' => $tran_id, 'description' => $item['name'], 'email' => $user->user_email, 'currency' => strtolower($this->setting['currency']));
     // additional fields,see parent for all fields, only different given here
     $this->_set_address_fields($user, $data);
     // product based
     if (isset($pack['product']['stripe_plan_id'])) {
         $data['plan'] = $pack['product']['stripe_plan_id'];
         $data['quantity'] = 1;
     } else {
         // use total
         $data['amount'] = mgm_convert_to_cents($pack['cost']);
     }
     // custom passthrough
     $data['custom'] = $tran_id;
     // update currency
     if ($pack['currency'] != $this->setting['currency']) {
         $pack['currency'] = $this->setting['currency'];
     }
     // strip
     $data = mgm_stripslashes_deep($data);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _buy_membership($transid = null)
 {
     // system
     $system_obj = mgm_get_class('system');
     $s_packs = mgm_get_class('subscription_packs');
     // get details
     //if transaction id is available:
     if (is_numeric($transid)) {
         $custom = $this->_get_transaction_passthrough($transid);
         extract($custom);
         // mgm_log($custom);
     } else {
         //Purchase Another Membership Level problem : issue #: 752
         $is_another_membership_purchase = 'N';
         $parts = explode('_', $_REQUEST['custom']);
         $params = array('user_id' => 0, 'duration' => '', 'duration_type' => '', 'pack_id' => 0, 'is_another_membership_purchase' => 'N', 'membership_type' => '');
         $i = 0;
         foreach ($params as $param => $default) {
             if (isset($parts[$i])) {
                 ${$param} = $parts[$i];
             } else {
                 ${$param} = $default;
             }
             $i++;
         }
     }
     // get pack
     $pack = $s_packs->get_pack($pack_id);
     // membership_type -issue #1005
     if (empty($membership_type)) {
         $membership_type = $pack['membership_type'];
     }
     // user
     if ($user = get_userdata($user_id)) {
         //Purchase Another Membership Level problem : issue #: 752
         if ($is_another_membership_purchase == 'Y') {
             // another_subscription modification
             //issue #1073
             $member = mgm_get_member_another_purchase($user_id, $membership_type);
         } else {
             // get member
             $member = mgm_get_member($user_id);
         }
         //pack currency over rides genral setting currency - issue #1602
         if (isset($pack['currency']) && !empty($pack['currency'])) {
             $currency = $pack['currency'];
         } else {
             $currency = $system_obj->setting['currency'];
         }
         // check
         //uncommented the below line as it is not updating in upgrade subscription
         //if (!$member->duration) {
         $member->duration = $duration ? $duration : 1;
         // one year
         $member->duration_type = $duration_type ? $duration_type : 'y';
         $member->amount = 0.0;
         //$member->currency        = 'USD';//not sure y hardcoded
         $member->currency = $currency;
         $member->membership_type = $membership_type;
         //}
         // set pack
         $member->pack_id = $pack_id;
         $member->active_num_cycles = isset($num_cycles) && !empty($num_cycles) ? $num_cycles : $pack['num_cycles'];
         // set status
         $member->status = MGM_STATUS_ACTIVE;
         $member->account_desc = __('Free Account', 'mgm');
         $member->last_pay_date = '';
         //reset payment_info if already set:
         if (isset($member->payment_info)) {
             unset($member->payment_info);
         }
         //unset rebill:
         if (isset($member->rebilled)) {
             unset($member->rebilled);
         }
         //unset transaction_id:
         if (isset($member->transaction_id)) {
             unset($member->transaction_id);
         }
         // join date
         $time = time();
         // set
         if (!isset($member->join_date) || isset($member->join_date) && empty($member->join_date)) {
             $member->join_date = $time;
         }
         // old content hide - issue #1227
         if (isset($hide_old_content)) {
             $member->hide_old_content = $hide_old_content;
         }
         // type expanded
         $duration_exprs = $s_packs->get_duration_exprs();
         // if not lifetime/date range
         if (in_array($member->duration_type, array_keys($duration_exprs))) {
             // take only date exprs
             // @TODO, time should be last expire date #773, 3 use cases must be tracked
             // expect new param in tran subscription_type: new, upgrade, downgrade, extend
             $expire_date_ts = !$member->expire_date ? $time : strtotime($member->expire_date);
             // time
             $expire_date_ts = strtotime("+{$member->duration} {$duration_exprs[$member->duration_type]}", $expire_date_ts);
             // formatted
             $expire_date = date('Y-m-d', $expire_date_ts);
             // date extended
             if (!$member->expire_date || $expire_date_ts > strtotime($member->expire_date)) {
                 $member->expire_date = $expire_date;
             }
         } else {
             //if lifetime:
             if ($member->duration_type == 'l') {
                 // el = lifetime
                 $member->expire_date = '';
             }
             //issue #1096
             if ($member->duration_type == 'dr') {
                 // el = /date range
                 $member->expire_date = $duration_range_end_dt;
             }
         }
         //Purchase Another Membership Level problem : issue #: 752
         if (bool_from_yn($is_another_membership_purchase)) {
             $custom = array('is_another_membership_purchase' => 'Y');
         }
         // old status
         $old_status = $member->status;
         // set new status
         $member->status = $new_status = MGM_STATUS_ACTIVE;
         // whether to subscriber the user to Autoresponder - This should happen only once
         //issue #1073
         if (!empty($transid) && $transid != null) {
             $acknowledge_ar = mgm_subscribe_to_autoresponder($member, $transid);
         } else {
             $acknowledge_ar = mgm_subscribe_to_autoresponder($member, null);
         }
         // update
         if (isset($custom['is_another_membership_purchase']) && bool_from_yn($custom['is_another_membership_purchase'])) {
             // get object - issue #1227
             $obj_sp = mgm_get_class('subscription_packs')->get($member->pack_id);
             if ($obj_sp['hide_old_content']) {
                 $member->hide_old_content = $obj_sp['hide_old_content'];
             }
             mgm_save_another_membership_fields($member, $user_id);
             // Multiple membership upgrade: first time
             if ($transid && isset($custom['multiple_upgrade_prev_packid']) && is_numeric($custom['multiple_upgrade_prev_packid'])) {
                 mgm_multiple_upgrade_save_memberobject($custom, $transid);
             }
         } else {
             // update
             $member->save();
         }
         // on status - issue #1468
         switch ($member->status) {
             case MGM_STATUS_ACTIVE:
                 //sending notification email to user
                 if ($notify_user && $is_registration == 'Y') {
                     $user_pass = mgm_decrypt_password($member->user_password, $user_id);
                     do_action('mgm_register_user_notification', $user_id, $user_pass);
                 }
                 break;
         }
         // status change event
         do_action('mgm_user_status_change', $user_id, $new_status, $old_status, 'module_' . $this->module, $member->pack_id);
         //update coupon usage
         do_action('mgm_update_coupon_usage', array('user_id' => $user_id));
         // role
         if (isset($role)) {
             $obj_role = new mgm_roles();
             $obj_role->add_user_role($user_id, $role);
         }
         // update pack/transaction
         if (is_numeric($transid)) {
             mgm_update_transaction(array('module' => $this->module, 'status_text' => __('Success', 'mgm')), $transid);
         }
         // return action
         do_action('mgm_return_' . $this->module, array('user_id' => $user_id));
         // backward compatibility
         do_action('mgm_return_subscription_payment_' . $this->module, array('user_id' => $user_id));
         // new , individual
         do_action('mgm_return_subscription_payment', array('user_id' => $user_id, 'acknowledge_ar' => $acknowledge_ar, 'mgm_member' => $member));
         // new, global: pass mgm_member object to consider multiple level purchases as well.
     }
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // set data
     $data = array('email' => $user->user_email);
     // address fields
     $this->_set_address_fields($user, $data);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // data
     return $data;
 }
 /**
  * Update payment notification email sent count  
  *
  * @param int $tran_id
  */
 function update_paymentemail_sent($tran_id)
 {
     if (!is_numeric($tran_id)) {
         return;
     }
     $trans = mgm_get_transaction($tran_id);
     if (!isset($trans['data']['payment_email'])) {
         $trans['data']['payment_email'] = 0;
     }
     $trans['data']['payment_email'] = (int) $trans['data']['payment_email'] + 1;
     // update
     mgm_update_transaction(array('data' => json_encode($trans['data']), 'module' => $this->module), $tran_id);
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system
     $system_obj = mgm_get_class('system');
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // type
     $membership_type = md5($pack['membership_type']);
     $user_id = mgm_get_user_id();
     $user = get_userdata($user_id);
     $member = mgm_get_member($user_id);
     $amount = mgm_convert_to_cents($pack['cost']);
     $payment_type = (int) $pack['num_cycles'] == 1 ? 'one-time' : 'subscription';
     $s_valid_until = date('Y-m-d\\TG:i:s\\Z', strtotime('+1 hour'));
     $description = substr($item['name'], 0, 32);
     // Setup hash string
     $hash_string = $this->setting['secret_key'] . $this->setting['merchant_id'] . $this->setting['sub_id'] . $amount . $tran_id . $payment_type . $s_valid_until . '1' . $description . '1' . $amount;
     // Remove HTML Entities
     $hash_string = html_entity_decode($hash_string);
     // Remove space characters: "\t", "\n", "\r" and " "
     $hash_string = str_replace(array("\t", "\n", "\r", " "), '', $hash_string);
     // Generate hash
     $hash_string = sha1($hash_string);
     $enc_trans_id = $this->_encode_id($tran_id);
     $cancel_url = add_query_arg(array('status' => 'cancel', 'extra' => $enc_trans_id), $this->setting['cancel_url']);
     $success_url = add_query_arg(array('status' => 'success', 'extra' => $enc_trans_id), $this->setting['return_url']);
     $error_url = add_query_arg(array('status' => 'error', 'extra' => $enc_trans_id), $this->setting['return_url']);
     // set data
     $data = array('merchantID' => $this->setting['merchant_id'], 'subID' => $this->setting['sub_id'], 'language' => $this->setting['language'], 'currency' => $pack['currency'], 'amount' => $amount, 'purchaseID' => $tran_id, 'description' => $description, 'hash' => $hash_string, 'paymentType' => $payment_type, 'validUntil' => $s_valid_until, 'itemNumber1' => '1', 'itemDescription1' => $description, 'itemQuantity1' => '1', 'itemPrice1' => $amount, 'urlCancel' => $cancel_url, 'urlSuccess' => $success_url, 'urlError' => $error_url);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // rabo changes
     if ($this->setting['aquirer'] == 'rabo_omnikassa') {
         // convert for rabobank
         $data = $this->_get_rabobank_data($data, $tran_id);
     } elseif ($this->setting['aquirer'] == 'sisow') {
         // convert for rabobank
         $data = $this->_get_sisow_data($data, $tran_id);
     }
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('PSPID' => $this->setting['pspid'], 'ORDERID' => $tran_id, 'AMOUNT' => mgm_convert_to_cents($pack['cost']), 'CURRENCY' => $pack['currency'], 'LANGUAGE' => $this->setting['language'], 'COM' => $item['name'], 'OPERATION' => 'SAL', 'EMAIL' => $user->user_email, 'CANCELURL' => $this->setting['cancel_url'], 'BACKURL' => $this->setting['cancel_url'], 'ACCEPTURL' => $this->setting['return_url'], 'DECLINEURL' => $this->setting['return_url']);
     // address fields
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) { // old style
         $data['ALIAS'] = 'user-' . $user_id;
         $data['ALIASUSAGE'] = $system_obj->get_subscription_name($pack);
         $data['ALIASOPERATION'] = 'BYMERCHANT';
         // 'BYPSP';
     } else {
         // post purchase
         // apply addons
         $this->_apply_addons($pack, $data, array('amount' => 'AMOUNT', 'description' => 'COM'));
     }
     // custom/passthrough
     $data['COMPLUS'] = $tran_id;
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // SHASIGN
     if (!empty($this->setting['sha1_passphrase'])) {
         $data['SHASIGN'] = $this->_generate_shasign($data);
     }
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     // set data
     $data = array('x_invoice_num' => $tran_id, 'x_description' => $item['name'], 'x_email' => $user->user_email, 'x_amount' => $pack['cost']);
     // additional fields,see parent for all fields, only different given here
     $this->_set_address_fields($user, $data);
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ( isset($pack['num_cycles']) && $pack['num_cycles'] != 1 && isset($pack['duration_type'])) { // old style
         // recurring
         $data['x_recurring_billing'] = 'TRUE';
         // cust id
         $data['x_cust_id'] = $user_id;
         // types
         $unit_types = array('d' => 'days', 'w' => 'days', 'm' => 'months', 'y' => 'months');
         // treat year a 12 x months	also  treat 7 x weeks
         // interval
         $data['x_interval_unit'] = $unit_types[strtolower($pack['duration_type'])];
         // days|months
         //$data['x_interval_length'] = ($pack['duration_type']=='y') ? ((int)$pack['duration'] * 12) : $pack['duration']; // 3|12|365 etc.
         //issue #1768
         if (strtolower($pack['duration_type']) == 'y') {
             $data['x_interval_length'] = (int) $pack['duration'] * 12;
         } elseif (strtolower($pack['duration_type']) == 'w') {
             $data['x_interval_length'] = (int) $pack['duration'] * 7;
         } else {
             $data['x_interval_length'] = $pack['duration'];
         }
         // start date
         // $data['x_start_date'] = date('Y-m-d') ;
         // issue#: 581 changes: calculate billing date for 2nd cycle as first cycle will be billed through AIM
         $add_by = str_replace('s', '', $data['x_interval_unit']);
         // DAY|MONTH
         $data['x_start_date'] = date('Y-m-d', strtotime("+{$data['x_interval_length']} {$add_by}", strtotime(date('Y-m-d'))));
         // trial
         if (isset($pack['trial_on']) && (bool) $pack['trial_on'] == true) {
             $data['x_trial_occurrences'] = $pack['trial_num_cycles'];
             $data['x_trial_amount'] = $pack['trial_cost'];
             //rewrite start date:
             $trial_interval_unit = $unit_types[$pack['trial_duration_type']];
             // days|months
             //$trial_interval_length 		 = ($pack['trial_duration_type']=='y') ? ((int)$pack['trial_duration'] * 12) : $pack['trial_duration']; // 3|12|365 etc.
             //issue #1768
             if (strtolower($pack['trial_duration_type']) == 'y') {
                 $trial_interval_length = (int) $pack['trial_duration'] * 12;
             } elseif (strtolower($pack['trial_duration_type']) == 'w') {
                 $trial_interval_length = (int) $pack['trial_duration'] * 7;
             } else {
                 $trial_interval_length = $pack['trial_duration'];
             }
             $trial_add_by = str_replace('s', '', $trial_interval_unit);
             $data['x_start_date'] = date('Y-m-d', strtotime("+{$trial_interval_length} {$trial_add_by}", strtotime(date('Y-m-d'))));
         }
         // arb ongoing =0 must be set as 9999, or integer 1-99
         // occurrences: greater than 0
         if ((int) $pack['num_cycles'] > 0) {
             // $data['x_total_occurrences'] = $pack['num_cycles'];
             // issue#: 581 changes: reduce total cycles by one as first cycle will be billed through AIM
             $data['x_total_occurrences'] = $pack['num_cycles'];
         } else {
             $data['x_total_occurrences'] = 9999;
             // ongoing for ARB
         }
     } else {
         // post purchase/ one time billing
         // recurring
         $data['x_recurring_billing'] = 'FALSE';
         // apply addons
         $this->_apply_addons($pack, $data, array('amount' => 'x_amount', 'description' => 'x_description'));
     }
     // custom passthrough
     $data['x_custom'] = $tran_id;
     // strip
     $data = mgm_stripslashes_deep($data);
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }
 function _get_button_data($pack, $tran_id = NULL)
 {
     // system setting
     $system_obj = mgm_get_class('system');
     $user_id = $pack['user_id'];
     $user = get_userdata($user_id);
     // item
     $item = $this->get_pack_item($pack);
     //pack currency over rides genral setting currency - issue #1602
     if (!isset($pack['currency']) || empty($pack['currency'])) {
         $pack['currency'] = $this->setting['currency'];
     }
     // setup data array
     $data = array('pay_to_email' => $this->setting['pay_to_email'], 'detail1_description' => $item['name'], 'detail1_text' => $item['name'], 'recipient_description' => $this->setting['recipient_desc'], 'confirmation_note' => $this->setting['confirmation_note'], 'transaction_id' => $tran_id, 'language' => $this->setting['language'], 'currency' => $pack['currency'], 'status_url' => $this->setting['notify_url'], 'cancel_url' => $this->setting['cancel_url'], 'return_url_text' => 'Return to ' . get_option('blogname'), 'pay_from_email' => $user->user_email, 'merchant_fields' => 'custom');
     // address fields
     $this->_set_address_fields($user, $data);
     // 'modify'=>1, // 1 new, 2 upgrade
     // subscription purchase with ongoing/limited
     if (!isset($pack['buypost']) && isset($pack['duration_type']) && $pack['num_cycles'] != 1) {
         // does not support one-time recurring
         // if ($pack['num_cycles'] != 1 && $pack['duration_type']) { // old style
         // types
         $rc_types = array('d' => 'day', 'm' => 'month', 'y' => 'year');
         // set
         $data['rec_amount'] = $pack['cost'];
         $data['rec_start_date'] = date('d/m/Y');
         $data['rec_period'] = $pack['duration'];
         $data['rec_cycle'] = $rc_types[$pack['duration_type']];
         $data['rec_grace_period'] = 7;
         // 7 days additional period ot pursue the payment if failed on inital attempt
         $data['rec_status_url'] = $this->setting['notify_url'];
         // cancel notification
         // greater than 0
         if ($pack['num_cycles']) {
             $data['rec_end_date'] = date('d/m/Y', strtotime("+{$pack['duration']} {$rc_types[$pack['duration_type']]}"));
             // expiration date
         }
         // trial
         if ($pack['trial_on']) {
             // set amount as trial and rec_amount as recurring ???? need test
             $data['amount'] = $pack['trial_cost'];
         }
     } else {
         // post purchase
         $data['amount'] = $pack['cost'];
     }
     // custom
     $data['custom'] = $tran_id;
     // set custom on request so that it can be tracked for post purchase
     $data['return_url'] = add_query_arg(array('custom' => $data['custom']), $this->setting['return_url']);
     // update currency - issue #1602
     /*		if($pack['currency'] != $this->setting['currency']){
     			$pack['currency'] = $this->setting['currency'];
     		}*/
     // add filter @todo test
     $data = apply_filters('mgm_payment_button_data', $data, $tran_id, $this->module, $pack);
     // update pack/transaction
     mgm_update_transaction(array('data' => json_encode($pack), 'module' => $this->module), $tran_id);
     // return data
     return $data;
 }