function premise_ipn_handler() { global $wpdb; if (!isset($_REQUEST['premiseipn']) || $_REQUEST['premiseipn'] != 'paypal') { return; } $gateway = new AccessPress_Paypal_Gateway(); if (!$gateway->validate_IPN()) { return; } if (!isset($_POST['txn_type']) || !isset($_POST['recurring_payment_id']) || $_POST['txn_type'] != 'recurring_payment') { return; } if (!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; } $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; } $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'); }
/** * */ 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 if ('cc' == $args['payment-method']) { $gateway = new AccessPress_AuthorizeNet_Gateway(); } else { $gateway = new AccessPress_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)); } } /** If CC payment method selected, but information not filled out */ if ('cc' == $args['payment-method']) { if (!$args['card-name'] || !$args['card-number'] || !$args['card-month'] || !$args['card-year'] || !$args['card-security'] || !$args['card-country'] || !$args['card-postal']) { return new WP_Error('credit_card_not_filled_out', 'The credit card info was not completed.'); } } /** 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); }
/** * 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; } if (isset($_REQUEST['test-paypal']) && 'true' == $_REQUEST['test-paypal']) { $gateway = new AccessPress_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>'; return; } /** test the gateway by requesting info on a non-existent customer */ if (isset($_REQUEST['test-cc']) && 'true' == $_REQUEST['test-cc']) { $gateway = new AccessPress_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; } return parent::notices(); }