function process_aim_payment($amount, $user, $subscription)
 {
     global $M_options;
     // A basic price or a single subscription
     $return = array();
     if ($amount >= 1) {
         // We have a charge to make
         $timestamp = time();
         if (get_option($this->gateway . "_mode", 'sandbox') == 'sandbox') {
             $endpoint = "https://test.authorize.net/gateway/transact.dll";
         } else {
             $endpoint = "https://secure.authorize.net/gateway/transact.dll";
         }
         $payment = new M_Gateway_Worker_AuthorizeNet_AIM($endpoint, get_option($this->gateway . "_delim_data", 'yes'), get_option($this->gateway . "_delim_char", ','), get_option($this->gateway . "_encap_char", ''), get_option($this->gateway . "_api_user", ''), get_option($this->gateway . "_api_key", ''), get_option($this->gateway . "_mode", 'sandbox') == 'sandbox');
         $payment->transaction($_POST['card_num']);
         $amount = number_format($amount, 2, '.', '');
         // Billing Info
         $payment->setParameter("x_card_code", $_POST['card_code']);
         $payment->setParameter("x_exp_date ", $_POST['exp_month'] . $_POST['exp_year']);
         $payment->setParameter("x_amount", $amount);
         // Payment billing information passed to authorize, thanks to Kevin L. for spotting this.
         $payment->setParameter("x_first_name", $_POST['first_name']);
         $payment->setParameter("x_last_name", $_POST['last_name']);
         $payment->setParameter("x_address", $_POST['address']);
         $payment->setParameter("x_zip", $_POST['zip']);
         $payment->setParameter("x_email", is_email($user->user_email) != false ? is_email($user->user_email) : '');
         // Order Info
         $payment->setParameter("x_description", $subscription->sub_name());
         $payment->setParameter("x_duplicate_window", 30);
         // E-mail
         $payment->setParameter("x_header_email_receipt", get_option($this->gateway . "_header_email_receipt", ''));
         $payment->setParameter("x_footer_email_receipt", get_option($this->gateway . "_footer_email_receipt", ''));
         $payment->setParameter("x_email_customer", strtoupper(get_option($this->gateway . "_email_customer", '')));
         $payment->setParameter("x_customer_ip", $_SERVER['REMOTE_ADDR']);
         $payment->process();
         if ($payment->isApproved()) {
             $return['status'] = 'success';
             $status = __('Processed', 'membership');
             $note = '';
             $this->record_transaction($user->ID, $subscription->sub_id(), $amount, $M_options['paymentcurrency'], time(), $payment->results[6] == 0 ? 'TESTMODE' : $payment->results[6], $status, $note);
         } else {
             $return['status'] = 'error';
             $return['errors'][] = __('Your payment was declined.  Please check all your details or use a different card.', 'membership');
         }
     } else {
         $return['status'] = 'error';
         $return['errors'][] = __('There was an issue determining the price.', 'membership');
     }
     return $return;
 }
 function process_payment_form()
 {
     global $M_options, $M_membership_url;
     $return = array();
     if (!is_ssl()) {
         wp_die(__('You must use HTTPS in order to do this', 'membership'));
         exit;
     }
     $popup = isset($M_options['formtype']) && $M_options['formtype'] == 'new' ? true : false;
     $coupon = membership_get_current_coupon();
     if (empty($M_options['paymentcurrency'])) {
         $M_options['paymentcurrency'] = 'USD';
     }
     $subscription = Membership_Plugin::factory()->get_subscription($_POST['subscription_id']);
     $pricing = $subscription->get_pricingarray();
     if (!empty($pricing) && !empty($coupon) && method_exists($coupon, 'valid_for_subscription') && $coupon->valid_for_subscription($subscription->id)) {
         $pricing = $coupon->apply_coupon_pricing($pricing);
     }
     $user_id = is_user_logged_in() ? get_current_user_id() : $_POST['user_id'];
     $user = get_userdata($user_id);
     $sub_id = $subscription->id;
     // A basic price or a single subscription
     if ($pricing) {
         $timestamp = time();
         if (get_option($this->gateway . "_mode", 'sandbox') == 'sandbox') {
             $endpoint = "https://test.authorize.net/gateway/transact.dll";
         } else {
             $endpoint = "https://secure.authorize.net/gateway/transact.dll";
         }
         $payment = new M_Gateway_Worker_AuthorizeNet_AIM($endpoint, get_option($this->gateway . "_delim_data", 'yes'), get_option($this->gateway . "_delim_char", ','), get_option($this->gateway . "_encap_char", ''), get_option($this->gateway . "_api_user", ''), get_option($this->gateway . "_api_key", ''), get_option($this->gateway . "_mode", 'sandbox') == 'sandbox');
         $payment->transaction($_POST['card_num']);
         $amount = number_format($pricing[0]['amount'], 2);
         // Billing Info
         $payment->setParameter("x_card_code", $_POST['card_code']);
         $payment->setParameter("x_exp_date ", $_POST['exp_month'] . $_POST['exp_year']);
         $payment->setParameter("x_amount", $amount);
         // Payment billing information passed to authorize, thanks to Kevin L. for spotting this.
         $payment->setParameter("x_first_name", $_POST['first_name']);
         $payment->setParameter("x_last_name", $_POST['last_name']);
         $payment->setParameter("x_address", $_POST['address']);
         $payment->setParameter("x_zip", $_POST['zip']);
         $payment->setParameter("x_email", is_email($user->user_email) != false ? is_email($user->user_email) : '');
         // Order Info
         $payment->setParameter("x_description", $subscription->sub_name());
         $payment->setParameter("x_duplicate_window", 30);
         // E-mail
         $payment->setParameter("x_header_email_receipt", get_option($this->gateway . "_header_email_receipt", ''));
         $payment->setParameter("x_footer_email_receipt", get_option($this->gateway . "_footer_email_receipt", ''));
         $payment->setParameter("x_email_customer", strtoupper(get_option($this->gateway . "_email_customer", '')));
         $payment->setParameter("x_customer_ip", $_SERVER['REMOTE_ADDR']);
         $payment->process();
         if ($payment->isApproved()) {
             $status = __('Processed', 'membership');
             $note = '';
             $member = Membership_Plugin::factory()->get_member($user_id);
             if ($member) {
                 if ($member->has_subscription() && $member->on_sub($sub_id)) {
                     remove_action('membership_expire_subscription', 'membership_record_user_expire', 10, 3);
                     remove_action('membership_add_subscription', 'membership_record_user_subscribe', 10, 4);
                     $member->expire_subscription($sub_id);
                     $member->create_subscription($sub_id, $this->gateway);
                 } else {
                     $member->create_subscription($sub_id, $this->gateway);
                 }
             }
             // TODO: create switch for handling different authorize aim respone codes
             $this->record_transaction($user_id, $sub_id, $amount, $M_options['paymentcurrency'], time(), $payment->results[6] == 0 ? 'TESTMODE' : $payment->results[6], $status, $note);
             do_action('membership_payment_subscr_signup', $user_id, $sub_id);
             $return['status'] = 'success';
             if ($popup && !empty($M_options['registrationcompleted_message'])) {
                 $return['redirect'] = 'no';
                 $registrationcompletedmessage = $this->get_completed_message($subscription);
                 $return['message'] = $registrationcompletedmessage;
             } else {
                 $return['redirect'] = !strpos(home_url(), 'https:') ? str_replace('https:', 'http:', M_get_registrationcompleted_permalink()) : M_get_registrationcompleted_permalink();
                 $return['message'] = '';
             }
         } else {
             $return['status'] = 'error';
             $return['errors'][] = __('Your payment was declined.  Please check all your details or use a different card.', 'membership');
         }
     } else {
         $return['status'] = 'error';
         $return['errors'][] = __('There was an issue determining the price.', 'membership');
     }
     echo json_encode($return);
     exit;
 }