Ejemplo n.º 1
0
/**
 * 
 */
function accesspress_checkout($args = array())
{
    global $accesspress_checkout_member, $wpdb;
    $args = wp_parse_args($args, array('product_id' => '', 'renew' => '', 'member' => 0, 'member-key' => '', 'first-name' => '', 'last-name' => '', 'email' => '', 'username' => '', 'password' => '', 'password-repeat' => '', 'payment-method' => '', 'card-name' => '', 'card-number' => '', 'card-month' => '', 'card-year' => '', 'card-security' => '', 'card-country' => '', 'card-postal' => ''));
    /** Trim space from values */
    $args = array_map('trim', $args);
    // instantiate gateway
    $payment_method = empty($args['payment-method']) && isset($_REQUEST['payment-method']) ? $_REQUEST['payment-method'] : $args['payment-method'];
    $gateway = memberaccess_get_payment_gateway($payment_method);
    if (!$gateway) {
        $gateway = new MemberAccess_Paypal_Gateway();
    }
    // check for a completed transaction first
    $completed_transaction = $gateway->complete_sale($args);
    if (is_wp_error($completed_transaction)) {
        return $completed_transaction;
    }
    if ($completed_transaction) {
        $report_back = false;
        extract($completed_transaction);
    } else {
        // handle report back
        $report_back = $gateway->validate_reportback();
        if (is_wp_error($report_back)) {
            return $report_back;
        }
        if ($report_back) {
            // show confirmation form
            if (method_exists($gateway, 'confirmation_form')) {
                return $gateway->confirmation_form($report_back);
            }
            extract($report_back);
        }
    }
    // populate $args from posted form
    if (!$report_back && !$completed_transaction) {
        /** If order ID not set */
        if (!$args['product_id']) {
            return new WP_Error('product_id_not_set', 'The product ID was not set.');
        }
        /** check for resubmit where member was created */
        if ($args['member'] && $args['member-key'] && wp_verify_nonce($args['member-key'], 'checkout-member-' . $args['member'])) {
            $member = $args['member'];
        } elseif (!$args['first-name'] || !$args['last-name'] || !$args['email'] || !$args['username'] || !$args['password'] || !$args['password-repeat']) {
            return new WP_Error('account_info_not_filled_out', 'The account information was not filled out.');
        } elseif ($args['password'] !== $args['password-repeat']) {
            return new WP_Error('account_passwords_do_not_match', 'The passwords do not match.');
        }
        /** If no payment method selected */
        if (!$args['payment-method'] && accesspress_product_requires_payment($args['product_id'])) {
            return new WP_Error('payment_method_not_chosen', 'No payment method was chosen.');
        }
    }
    /** The order array, to be stored as an Order (CPT) */
    if (!isset($order_details)) {
        $duration = get_post_meta($args['product_id'], '_acp_product_duration', true);
        if ($duration && 'true' == $args['renew']) {
            $member_orders = get_user_option('acp_orders', (int) $member);
            if (!empty($member_orders)) {
                $order_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_acp_order_product_id' AND meta_value = %s AND post_id IN (" . implode(',', $member_orders) . ')', $args['product_id']));
                $order_time = get_post_meta($order_id, '_acp_order_time', true);
                $order_renewal_time = get_post_meta($order_id, '_acp_order_renewal_time', true);
                if ($order_time) {
                    $order_details = array('_acp_order_renewal_time' => ($order_renewal_time ? $order_renewal_time : $order_time) + $duration * 86400, '_acp_order_price' => get_post_meta($args['product_id'], '_acp_product_price', true), '_acp_order_id' => $order_id);
                }
            }
        }
        if (empty($order_details)) {
            $order_details = array('_acp_order_time' => time(), '_acp_order_status' => 'complete', '_acp_order_product_id' => $args['product_id'], '_acp_order_price' => get_post_meta($args['product_id'], '_acp_product_price', true));
        }
    }
    /** allow payment gateways to validate additional fields added to the checkout form */
    $form_validates = $gateway->validate_checkout_form($args);
    if (is_wp_error($form_validates)) {
        return $form_validates;
    }
    /** Create member before sending to gateway so we have a unique ID */
    if (!isset($member)) {
        $member = accesspress_create_member(array('first_name' => $args['first-name'], 'last_name' => $args['last-name'], 'user_email' => $args['email'], 'user_login' => $args['username'], 'user_pass' => $args['password']));
    }
    /** Bail, if there's a problem */
    if (is_wp_error($member)) {
        return $member;
    }
    /** Add member ID to order details */
    $order_details['_acp_order_member_id'] = $accesspress_checkout_member = $member;
    /** now to the gateway */
    if (!$report_back && !$completed_transaction) {
        $args['order_details'] = $order_details;
        $order_details = $gateway->process_order($args);
    }
    /** Bail, if the order is incomplete or there's an error on the gateway */
    if (empty($order_details) || is_wp_error($order_details)) {
        return $order_details;
    }
    return accesspress_create_order($member, $order_details);
}
Ejemplo n.º 2
0
function premise_ipn_handler()
{
    global $wpdb;
    if (!isset($_REQUEST['premiseipn']) || $_REQUEST['premiseipn'] != 'paypal') {
        return;
    }
    $gateway = new MemberAccess_Paypal_Gateway();
    if (!$gateway->validate_IPN()) {
        return;
    }
    $ipn_cancel = $_POST['txn_type'] == 'recurring_payment_profile_cancel';
    $ipn_payment = $_POST['txn_type'] == 'recurring_payment';
    if (!isset($_POST['txn_type']) || !isset($_POST['recurring_payment_id']) || !($ipn_cancel || $ipn_payment)) {
        return;
    }
    if ($ipn_payment && (!isset($_POST['txn_id']) || !isset($_POST['payment_status']) || strtolower($_POST['payment_status']) != 'completed')) {
        return;
    }
    $blog_prefix = $wpdb->get_blog_prefix();
    $user_meta = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->usermeta} WHERE meta_key LIKE %s AND meta_value = %s", $blog_prefix . 'memberaccess_paypal_profile%', $_POST['recurring_payment_id']));
    if (!$user_meta) {
        return;
    }
    // don't process a payment twice
    if ($ipn_payment) {
        $order_meta = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_acp_order_paypal_transaction_id' AND meta_value = %s", $_POST['txn_id']));
        if ($order_meta) {
            return;
        }
    }
    $user_orders = get_user_option('acp_orders', $user_meta->user_id);
    if (empty($user_orders)) {
        return;
    }
    $order_products = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id in (" . implode(',', $user_orders) . ") AND meta_key = '_acp_order_product_id'");
    if (empty($order_products)) {
        return;
    }
    $order_id = 0;
    $product_id = 0;
    foreach ($order_products as $product) {
        if ($user_meta->meta_key == $blog_prefix . 'memberaccess_paypal_profile_' . $product->meta_value) {
            $order_id = $product->post_id;
            $product_id = $product->meta_value;
            break;
        }
    }
    if (!$product_id || !$order_id) {
        return;
    }
    $duration = $gateway->get_subscription_duration($product_id);
    if (!$duration) {
        return;
    }
    if ($ipn_cancel) {
        update_post_meta($order_id, '_acp_order_status', __('cancel', 'premise '));
        do_action('premise_cancel_subscription', $order_id, $product_id, $user_meta->user_id, true);
        return;
    }
    $renewal_time = get_post_meta($order_id, '_acp_order_renewal_time', true);
    $now = time();
    // if expired more than a week renew from the current time
    if (empty($renewal_time) || $renewal_time + 7 * 86400 < $now) {
        $renewal_time = $now;
    }
    update_post_meta($order_id, '_acp_order_renewal_time', $renewal_time + $duration * 86400);
    update_post_meta($order_id, '_acp_order_paypal_transaction_id', $_POST['txn_id']);
    update_post_meta($order_id, '_acp_order_status', 'active');
}
Ejemplo n.º 3
0
 /**
  * Display notices on the save or reset of settings.
  *
  * @since 0.1.0
  *
  * @return type
  */
 public function notices()
 {
     if (!accesspress_is_menu_page($this->page_id)) {
         return;
     }
     $settings = get_option($this->settings_field);
     if (isset($settings['test-paypal'])) {
         unset($settings['test-paypal']);
         update_option($this->settings_field, $settings);
         $gateway = new MemberAccess_Paypal_Gateway();
         $gateway->test();
         if (is_wp_error($gateway->response)) {
             $message = $gateway->response->get_error_message();
         } else {
             $message = __('Paypal Gateway test passed.', 'premise');
         }
         echo '<div id="message" class="updated"><p><strong>' . $message . '</strong></p></div>';
     }
     /** test the gateway by requesting info on a non-existent customer */
     if (isset($settings['test-cc'])) {
         unset($settings['test-cc']);
         update_option($this->settings_field, $settings);
         $gateway = new MemberAccess_AuthorizeNet_Gateway();
         $result = $gateway->test();
         if (!$result && is_wp_error($gateway->response) && 'cc-error' == $gateway->response->get_error_code()) {
             $message = __('Authorize.Net Gateway test passed.', 'premise');
         } elseif (is_wp_error($gateway->response)) {
             $message = $gateway->response->get_error_message();
         } else {
             $message = __('Authorize.Net Gateway test failed.', 'premise');
         }
         echo '<div id="message" class="updated"><p><strong>' . $message . '</strong></p></div>';
     }
     return parent::notices();
 }